three-base 2.0.130 → 2.0.134
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ApngTexture.d.ts +13 -13
- package/ApngTexture.js +61 -61
- package/ApngTextureManage.d.ts +18 -18
- package/ApngTextureManage.js +87 -87
- package/DirectionalLight.d.ts +6 -0
- package/DirectionalLight.js +68 -0
- package/EventMesh.d.ts +18 -18
- package/EventMesh.js +59 -59
- package/ExtrudeMesh.d.ts +112 -112
- package/ExtrudeMesh.js +366 -366
- package/FPScontrol.d.ts +71 -50
- package/FPScontrol.js +505 -232
- package/Fire/Fire.d.ts +28 -28
- package/Fire/Fire.js +79 -79
- package/Fire/FireGromtry.d.ts +9 -9
- package/Fire/FireGromtry.js +40 -40
- package/Fire/FireMaterial.d.ts +28 -28
- package/Fire/FireMaterial.js +32 -32
- package/Fire/index.d.ts +1 -1
- package/Fire/index.js +1 -1
- package/Grass/Grass.d.ts +8 -8
- package/Grass/Grass.js +38 -38
- package/Grass/index.d.ts +1 -1
- package/Grass/index.js +1 -1
- package/Ground1/Ground1.d.ts +11 -11
- package/Ground1/Ground1.js +54 -54
- package/Ground1/index.d.ts +1 -1
- package/Ground1/index.js +1 -1
- package/InstancedMeshBase.d.ts +39 -39
- package/InstancedMeshBase.js +293 -293
- package/KeyFrameAnimation.d.ts +25 -25
- package/KeyFrameAnimation.js +78 -78
- package/MeshBase.d.ts +35 -35
- package/MeshBase.js +223 -223
- package/Mirror.d.ts +25 -25
- package/Mirror.js +139 -139
- package/PipeMesh/PathGeometry.d.ts +44 -44
- package/PipeMesh/PathGeometry.js +300 -300
- package/PipeMesh/PathPoint.d.ts +17 -17
- package/PipeMesh/PathPoint.js +31 -31
- package/PipeMesh/PathPointList.d.ts +31 -31
- package/PipeMesh/PathPointList.js +208 -208
- package/PipeMesh/PathTubeGeometry.d.ts +25 -25
- package/PipeMesh/PathTubeGeometry.js +130 -130
- package/PipeMesh/PipeMesh.d.ts +25 -25
- package/PipeMesh/PipeMesh.js +63 -63
- package/PipeMesh/index.d.ts +1 -1
- package/PipeMesh/index.js +1 -1
- package/PlaneModal1/PlaneModal1.d.ts +31 -31
- package/PlaneModal1/PlaneModal1.js +105 -105
- package/PlaneModal1/index.d.ts +1 -1
- package/PlaneModal1/index.js +1 -1
- package/PlaneText.d.ts +39 -39
- package/PlaneText.js +30 -30
- package/PlaneWithHtml.d.ts +11 -11
- package/PlaneWithHtml.js +63 -68
- package/PointLight/PointLight.d.ts +14 -13
- package/PointLight/PointLight.js +70 -64
- package/PointLight/index.d.ts +1 -1
- package/PointLight/index.js +1 -1
- package/Sky/Sky.d.ts +9 -9
- package/Sky/Sky.js +22 -22
- package/Sky/index.d.ts +1 -1
- package/Sky/index.js +1 -1
- package/SpotLight.d.ts +6 -0
- package/SpotLight.js +72 -0
- package/SpriteModal1/SpriteModal1.d.ts +30 -30
- package/SpriteModal1/SpriteModal1.js +110 -110
- package/SpriteModal1/index.d.ts +1 -1
- package/SpriteModal1/index.js +1 -1
- package/SpriteText.d.ts +45 -45
- package/SpriteText.js +163 -163
- package/SpriteText2.d.ts +47 -47
- package/SpriteText2.js +198 -198
- package/SpriteWithHtml.d.ts +7 -7
- package/SpriteWithHtml.js +44 -49
- package/Text3D/Text3D.d.ts +20 -20
- package/Text3D/Text3D.js +62 -62
- package/Text3D/fontController.d.ts +59 -59
- package/Text3D/fontController.js +199 -199
- package/Text3D/index.d.ts +1 -1
- package/Text3D/index.js +1 -1
- package/ThreeBase.d.ts +97 -81
- package/ThreeBase.js +613 -560
- package/ThreeMouse.d.ts +40 -40
- package/ThreeMouse.js +407 -407
- package/Transition.d.ts +40 -40
- package/Transition.js +210 -210
- package/Water/Water.d.ts +5 -0
- package/Water/Water.js +26 -0
- package/Water/waternormals.jpg +0 -0
- package/declare.d.ts +167 -0
- package/index.d.ts +1 -1
- package/index.js +2 -2
- package/keyContorls.d.ts +25 -25
- package/keyContorls.js +98 -98
- package/package.json +1 -1
- package/pass/bloom_pass.d.ts +2 -2
- package/pass/bloom_pass.js +99 -99
- package/pass/cover_pass.d.ts +2 -2
- package/pass/cover_pass.js +95 -95
- package/pass/frosted_glass_pass.d.ts +2 -0
- package/pass/frosted_glass_pass.js +275 -0
- package/pass/fsQuad.d.ts +9 -9
- package/pass/fsQuad.js +28 -28
- package/pass/glow_pass.d.ts +2 -2
- package/pass/glow_pass.js +310 -254
- package/pass/ssao_pass.d.ts +11 -0
- package/pass/ssao_pass.js +294 -0
- package/pass/stroke_pass.d.ts +2 -2
- package/pass/stroke_pass.js +199 -199
- package/shaders/FXAAShaderMaterial.d.ts +3 -3
- package/shaders/FXAAShaderMaterial.js +21 -21
- package/shaders/baseTextureToRenderShader.d.ts +2 -2
- package/shaders/baseTextureToRenderShader.js +14 -14
- package/shaders/copyShader.d.ts +3 -3
- package/shaders/copyShader.js +14 -14
- package/shaders.d.ts +5 -5
- package/shaders.js +42 -42
- package/threeLoader.d.ts +34 -34
- package/threeLoader.js +527 -527
- package/threeUtils.d.ts +52 -52
- package/threeUtils.js +330 -330
package/ThreeBase.js
CHANGED
|
@@ -1,560 +1,613 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: xiaosihan
|
|
3
|
-
* @Date: 2021-12-20 13:38:22
|
|
4
|
-
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time: 2025-01-28 04:26:41
|
|
6
|
-
*/
|
|
7
|
-
import { round } from "lodash";
|
|
8
|
-
import { AxesHelper, Box3, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, MOUSE, NoBlending, NormalBlending, OrthographicCamera, PerspectiveCamera, RGBAFormat, ReinhardToneMapping, SRGBColorSpace, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer
|
|
9
|
-
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
10
|
-
import { degToRad, generateUUID } from "three/src/math/MathUtils";
|
|
11
|
-
import ThreeMouse from "./ThreeMouse";
|
|
12
|
-
import Transition from "./Transition";
|
|
13
|
-
import bloom_pass from "./pass/bloom_pass";
|
|
14
|
-
import cover_pass from "./pass/cover_pass";
|
|
15
|
-
import
|
|
16
|
-
import
|
|
17
|
-
import
|
|
18
|
-
import
|
|
19
|
-
import
|
|
20
|
-
import
|
|
21
|
-
import
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
baseDepthTexture
|
|
48
|
-
baseDepthTexture.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
renderer.
|
|
106
|
-
|
|
107
|
-
renderer.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
this.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
this.
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
//
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return
|
|
230
|
-
})();
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
this.
|
|
262
|
-
this.
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
this.
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
//
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
this.controls.
|
|
313
|
-
this.controls.
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
new
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
this.
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
//
|
|
476
|
-
this.
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
this.
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
this.renderer.
|
|
509
|
-
this.
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
this.
|
|
525
|
-
|
|
526
|
-
this.renderer.setRenderTarget(
|
|
527
|
-
this.renderer.clear();
|
|
528
|
-
this.
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2021-12-20 13:38:22
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2025-01-28 04:26:41
|
|
6
|
+
*/
|
|
7
|
+
import { round } from "lodash";
|
|
8
|
+
import { AxesHelper, Box3, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, MOUSE, NoBlending, NormalBlending, OrthographicCamera, PerspectiveCamera, RGBAFormat, ReinhardToneMapping, SRGBColorSpace, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
9
|
+
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
10
|
+
import { degToRad, generateUUID } from "three/src/math/MathUtils";
|
|
11
|
+
import ThreeMouse from "./ThreeMouse";
|
|
12
|
+
import Transition from "./Transition";
|
|
13
|
+
import bloom_pass from "./pass/bloom_pass";
|
|
14
|
+
import cover_pass from "./pass/cover_pass";
|
|
15
|
+
import frosted_glass_pass from "./pass/frosted_glass_pass";
|
|
16
|
+
import glow_pass from "./pass/glow_pass";
|
|
17
|
+
import stroke_pass from "./pass/stroke_pass";
|
|
18
|
+
import ssao_pass from "./pass/ssao_pass";
|
|
19
|
+
import { copyShader } from "./shaders";
|
|
20
|
+
import { baseTextureToRenderShader } from "./shaders/baseTextureToRenderShader";
|
|
21
|
+
import threeLoader from "./threeLoader";
|
|
22
|
+
import threeUtils from "./threeUtils";
|
|
23
|
+
import fsQuad from "./pass/fsQuad";
|
|
24
|
+
// three.js 的基础对象
|
|
25
|
+
export default class ThreeBase extends EventDispatcher {
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
// 资源加载进度有更新时, 需要渲染
|
|
29
|
+
threeLoader.addEventListener("loadding", () => {
|
|
30
|
+
this.dispatchEvent({ type: "loadding" });
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
this.needRender = true;
|
|
33
|
+
}, 100);
|
|
34
|
+
});
|
|
35
|
+
threeLoader.addEventListener("onLoad", () => {
|
|
36
|
+
this.dispatchEvent({ type: "onLoad" });
|
|
37
|
+
setTimeout(() => {
|
|
38
|
+
this.needRender = true;
|
|
39
|
+
}, 100);
|
|
40
|
+
});
|
|
41
|
+
this.init();
|
|
42
|
+
}
|
|
43
|
+
// 设置最小分辨率
|
|
44
|
+
minDevicePixelRatio = 1;
|
|
45
|
+
// 基础深度纹理
|
|
46
|
+
baseDepthTexture = (() => {
|
|
47
|
+
const baseDepthTexture = new DepthTexture(4096, 4096);
|
|
48
|
+
baseDepthTexture.format = DepthFormat;
|
|
49
|
+
baseDepthTexture.type = UnsignedShortType;
|
|
50
|
+
baseDepthTexture.anisotropy = 16;
|
|
51
|
+
return baseDepthTexture;
|
|
52
|
+
})();
|
|
53
|
+
// 基础纹理渲染对象(MSAA 抗锯齿,不挂 depthTexture 避免 resolve 深度跳变)
|
|
54
|
+
baseRenderTarget = (() => {
|
|
55
|
+
const baseRenderTarget = new WebGLRenderTarget(128, 128, {
|
|
56
|
+
minFilter: LinearFilter,
|
|
57
|
+
magFilter: LinearFilter,
|
|
58
|
+
format: RGBAFormat,
|
|
59
|
+
stencilBuffer: false,
|
|
60
|
+
anisotropy: 16,
|
|
61
|
+
colorSpace: SRGBColorSpace,
|
|
62
|
+
samples: 4,
|
|
63
|
+
});
|
|
64
|
+
return baseRenderTarget;
|
|
65
|
+
})();
|
|
66
|
+
// 深度专用渲染目标(无 MSAA,深度值稳定,供后处理深度比对使用)
|
|
67
|
+
depthRenderTarget = new WebGLRenderTarget(128, 128, {
|
|
68
|
+
minFilter: LinearFilter,
|
|
69
|
+
magFilter: LinearFilter,
|
|
70
|
+
format: RGBAFormat,
|
|
71
|
+
stencilBuffer: false,
|
|
72
|
+
depthTexture: this.baseDepthTexture,
|
|
73
|
+
});
|
|
74
|
+
// 公用渲染目标1
|
|
75
|
+
renderTarget = new WebGLRenderTarget(128, 128, {
|
|
76
|
+
minFilter: LinearFilter,
|
|
77
|
+
magFilter: LinearFilter,
|
|
78
|
+
format: RGBAFormat,
|
|
79
|
+
});
|
|
80
|
+
// 渲染目标2
|
|
81
|
+
renderTarget2 = new WebGLRenderTarget(128, 128, {
|
|
82
|
+
minFilter: LinearFilter,
|
|
83
|
+
magFilter: LinearFilter,
|
|
84
|
+
format: RGBAFormat,
|
|
85
|
+
});
|
|
86
|
+
// 公用二维向量
|
|
87
|
+
tempVector2 = new Vector2();
|
|
88
|
+
// 公用的三维向量
|
|
89
|
+
tempVector3 = new Vector3();
|
|
90
|
+
//渲染器的尺寸
|
|
91
|
+
size = new Vector2(0, 0);
|
|
92
|
+
//上下文丢失
|
|
93
|
+
rendererLost = false;
|
|
94
|
+
// 渲染器对象
|
|
95
|
+
renderer = (() => {
|
|
96
|
+
const renderer = new WebGLRenderer({
|
|
97
|
+
alpha: true, // 透明背景
|
|
98
|
+
antialias: true, // 抗锯齿
|
|
99
|
+
precision: "highp", //highp/mediump/lowp着色精度选择
|
|
100
|
+
logarithmicDepthBuffer: true, // 对数深度缓冲区 避免当2个面挨的很近时闪烁
|
|
101
|
+
preserveDrawingBuffer: true, // 是否保存绘图缓冲
|
|
102
|
+
stencil: false, // 模版
|
|
103
|
+
powerPreference: 'high-performance' // 高性能模式
|
|
104
|
+
});
|
|
105
|
+
renderer.domElement.id = generateUUID();
|
|
106
|
+
//监听上下文的销毁事件
|
|
107
|
+
renderer.domElement.addEventListener("webglcontextlost", (event) => {
|
|
108
|
+
this.rendererLost = true;
|
|
109
|
+
}, false);
|
|
110
|
+
// 监听上下文的恢复事件
|
|
111
|
+
renderer.domElement.addEventListener("webglcontextrestored", (event) => {
|
|
112
|
+
this.rendererLost = false;
|
|
113
|
+
}, false);
|
|
114
|
+
renderer.localClippingEnabled = true;
|
|
115
|
+
renderer.setPixelRatio(Math.max(window.devicePixelRatio, this.minDevicePixelRatio)),
|
|
116
|
+
renderer.autoClear = false;
|
|
117
|
+
renderer.clear();
|
|
118
|
+
renderer.outputColorSpace = SRGBColorSpace;
|
|
119
|
+
// 缓解过暗或者过亮
|
|
120
|
+
renderer.toneMapping = ReinhardToneMapping;
|
|
121
|
+
threeLoader.setAnisotropy(renderer.capabilities.getMaxAnisotropy());
|
|
122
|
+
return renderer;
|
|
123
|
+
})();
|
|
124
|
+
//透视相机
|
|
125
|
+
perspectiveCamera = new PerspectiveCamera(45, 1, 0.01, 20000);
|
|
126
|
+
//正交相机
|
|
127
|
+
orthographicCamera = new OrthographicCamera(-1, 1, 1, -1, 0.01, 20000);
|
|
128
|
+
//相机的类型 有透视相机和正交相机
|
|
129
|
+
cameraType = "perspectiveCamera";
|
|
130
|
+
setCameraType(cameraType) {
|
|
131
|
+
if (this.cameraType !== cameraType) {
|
|
132
|
+
this.cameraType = cameraType;
|
|
133
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
134
|
+
this.threeMouse._camera = this.perspectiveCamera;
|
|
135
|
+
this.controls.object = this.perspectiveCamera;
|
|
136
|
+
}
|
|
137
|
+
else if (this.cameraType === "orthographicCamera") {
|
|
138
|
+
this.threeMouse._camera = this.orthographicCamera;
|
|
139
|
+
this.controls.object = this.orthographicCamera;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//获取当前使用的的相机
|
|
144
|
+
getCamera() {
|
|
145
|
+
switch (this.cameraType) {
|
|
146
|
+
case "orthographicCamera":
|
|
147
|
+
return this.orthographicCamera;
|
|
148
|
+
case "perspectiveCamera":
|
|
149
|
+
default:
|
|
150
|
+
return this.perspectiveCamera;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// 场景对象
|
|
154
|
+
scene = new Scene();
|
|
155
|
+
// 这个方法等效 this.scene.add
|
|
156
|
+
// 想少写2个字母就用这个方法
|
|
157
|
+
add(...object) {
|
|
158
|
+
this.scene.add(...object);
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
///鼠标事件对象
|
|
162
|
+
threeMouse = new ThreeMouse(this.renderer, this.perspectiveCamera, this.scene);
|
|
163
|
+
// 轨道控制器
|
|
164
|
+
controls = (() => {
|
|
165
|
+
const controls = new OrbitControls(this.perspectiveCamera, this.renderer.domElement);
|
|
166
|
+
controls.addEventListener("change", () => {
|
|
167
|
+
this.needRender = true;
|
|
168
|
+
});
|
|
169
|
+
controls.rotateSpeed = 0.2;
|
|
170
|
+
controls.maxPolarAngle = Math.PI;
|
|
171
|
+
controls.enableDamping = true;
|
|
172
|
+
controls.dampingFactor = 0.1;
|
|
173
|
+
controls.screenSpacePanning = true;
|
|
174
|
+
return controls;
|
|
175
|
+
})();
|
|
176
|
+
// 设置 dom 容器
|
|
177
|
+
async setContainer(div) {
|
|
178
|
+
if (div) {
|
|
179
|
+
div.style.overflow = "hidden";
|
|
180
|
+
div.innerHTML = "";
|
|
181
|
+
div.append(this.renderer.domElement);
|
|
182
|
+
if (this.rendererLost) {
|
|
183
|
+
this.renderer.forceContextRestore();
|
|
184
|
+
this.needRender = true;
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
this.needRender = true;
|
|
187
|
+
}, 100);
|
|
188
|
+
setTimeout(() => {
|
|
189
|
+
this.renderer.domElement.style.opacity = "1";
|
|
190
|
+
}, 200);
|
|
191
|
+
}
|
|
192
|
+
this.setSize(div.clientWidth, div.clientHeight);
|
|
193
|
+
// 开启时时渲染
|
|
194
|
+
threeUtils.removeEventListener("requestAnimationFrame", this.renderBindThis);
|
|
195
|
+
threeUtils.addEventListener("requestAnimationFrame", this.renderBindThis);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
// 修改尺寸
|
|
199
|
+
setSize(width, height) {
|
|
200
|
+
//修改透视相机的比例
|
|
201
|
+
this.perspectiveCamera.aspect = width / height;
|
|
202
|
+
this.perspectiveCamera.updateProjectionMatrix();
|
|
203
|
+
//修改正交相机的比例
|
|
204
|
+
// this.orthographicCamera.top = height / width;
|
|
205
|
+
// this.orthographicCamera.bottom = -height / width;
|
|
206
|
+
this.orthographicCamera.left = -width / height;
|
|
207
|
+
this.orthographicCamera.right = width / height;
|
|
208
|
+
this.renderer.setPixelRatio(Math.max(window.devicePixelRatio, this.minDevicePixelRatio)),
|
|
209
|
+
this.renderer.setSize(width, height);
|
|
210
|
+
// 设置帧缓存区的尺寸
|
|
211
|
+
this.baseRenderTarget.setSize(width * Math.max(window.devicePixelRatio, this.minDevicePixelRatio), height * Math.max(window.devicePixelRatio, this.minDevicePixelRatio));
|
|
212
|
+
// 同步深度渲染目标尺寸
|
|
213
|
+
this.depthRenderTarget.setSize(width * Math.max(window.devicePixelRatio, this.minDevicePixelRatio), height * Math.max(window.devicePixelRatio, this.minDevicePixelRatio));
|
|
214
|
+
// 设置深度纹理的尺寸
|
|
215
|
+
this.baseDepthTexture.image.width = width * Math.max(window.devicePixelRatio, this.minDevicePixelRatio),
|
|
216
|
+
this.baseDepthTexture.image.height = height * Math.max(window.devicePixelRatio, this.minDevicePixelRatio),
|
|
217
|
+
this.size.set(width, height);
|
|
218
|
+
setTimeout(() => {
|
|
219
|
+
this.needRender = true;
|
|
220
|
+
}, 100);
|
|
221
|
+
// 渲染场景
|
|
222
|
+
// this.renderer.clear();
|
|
223
|
+
// this.renderer.render(this.scene, this.camera);
|
|
224
|
+
}
|
|
225
|
+
// 视角距离过度
|
|
226
|
+
controlsDistanceTransition = (() => {
|
|
227
|
+
const controlsDistanceTransition = new Transition({ distance: 0 });
|
|
228
|
+
controlsDistanceTransition.setDuration(1000);
|
|
229
|
+
return controlsDistanceTransition;
|
|
230
|
+
})();
|
|
231
|
+
//控制器的Y值过度计算
|
|
232
|
+
controlsYTransition = (() => {
|
|
233
|
+
const controlsYTransition = new Transition({ y: 0 });
|
|
234
|
+
controlsYTransition.setDuration(1000);
|
|
235
|
+
return controlsYTransition;
|
|
236
|
+
})();
|
|
237
|
+
//控制器的XZ值过度计算
|
|
238
|
+
controlsXZTransition = (() => {
|
|
239
|
+
const controlsXZTransition = new Transition({ x: 0, z: 0 });
|
|
240
|
+
controlsXZTransition.setDuration(1000);
|
|
241
|
+
return controlsXZTransition;
|
|
242
|
+
})();
|
|
243
|
+
// 控制器的 朝向带缓动的计算的顶点对象
|
|
244
|
+
controlsCenter = (() => {
|
|
245
|
+
const controlsCenter = new Transition({ x: 0, y: 0, z: 0 });
|
|
246
|
+
controlsCenter.setDuration(1000);
|
|
247
|
+
controlsCenter.onChange(({ x, y, z }) => {
|
|
248
|
+
this.controls.target.set(x, y, z);
|
|
249
|
+
this.perspectiveCamera.lookAt(this.controls.target);
|
|
250
|
+
this.orthographicCamera.lookAt(this.controls.target);
|
|
251
|
+
this.needRender = true;
|
|
252
|
+
});
|
|
253
|
+
return controlsCenter;
|
|
254
|
+
})();
|
|
255
|
+
// 控制器的位置缓动的计算的顶点对象
|
|
256
|
+
controlsPosition = (() => {
|
|
257
|
+
const controlsPosition = new Transition({ x: 100, y: 100, z: 100 });
|
|
258
|
+
controlsPosition.setDuration(1000);
|
|
259
|
+
controlsPosition.onChange(({ x, y, z }) => {
|
|
260
|
+
const distance = this.controlsDistanceTransition.get("distance");
|
|
261
|
+
this.perspectiveCamera.position.set(x + 0.0001, y, z + 0.0001);
|
|
262
|
+
// .sub(this.controls.target)
|
|
263
|
+
// .setLength(distance)
|
|
264
|
+
// .add(this.controls.target);
|
|
265
|
+
// // 创建初始方向向量
|
|
266
|
+
// const directionVecA = new Vector3(1, 0, 0); // 需要旋转的初始方向向量
|
|
267
|
+
// const referenceVecB = new Vector3(0, 1, 0); // 参考方向向量,用于确定旋转轴
|
|
268
|
+
// // 归一化参考方向向量
|
|
269
|
+
// referenceVecB.normalize();
|
|
270
|
+
// // 计算旋转轴
|
|
271
|
+
// const axis = new Vector3().crossVectors(directionVecA, referenceVecB).normalize();
|
|
272
|
+
// // 计算旋转角度(弧度)
|
|
273
|
+
// const angle = Math.PI / 36; // 旋转角度为5度,Math.PI / 36为弧度
|
|
274
|
+
// // 使用applyAxisAngle方法进行旋转
|
|
275
|
+
// directionVecA.applyAxisAngle(axis, angle);
|
|
276
|
+
// // 输出旋转后的方向向量
|
|
277
|
+
// console.log(directionVecA);
|
|
278
|
+
this.perspectiveCamera.lookAt(this.controls.target);
|
|
279
|
+
//计算正交相机的缓动
|
|
280
|
+
this.orthographicCamera.position
|
|
281
|
+
.set(x + 0.01, y, z + 0.01)
|
|
282
|
+
.sub(this.controls.target)
|
|
283
|
+
.setLength(5000)
|
|
284
|
+
.add(this.controls.target);
|
|
285
|
+
this.orthographicCamera.lookAt(this.controls.target);
|
|
286
|
+
this.needRender = true;
|
|
287
|
+
});
|
|
288
|
+
return controlsPosition;
|
|
289
|
+
})();
|
|
290
|
+
// 网格辅助线
|
|
291
|
+
gridHelper = (() => {
|
|
292
|
+
const gridHelper = new GridHelper(1000, 100, 0x303030, 0x303030);
|
|
293
|
+
if (threeUtils.isDev) {
|
|
294
|
+
this.scene.add(gridHelper);
|
|
295
|
+
}
|
|
296
|
+
return gridHelper;
|
|
297
|
+
})();
|
|
298
|
+
//坐标轴线
|
|
299
|
+
axesHelper = (() => {
|
|
300
|
+
const axesHelper = new AxesHelper(10000);
|
|
301
|
+
if (threeUtils.isDev) {
|
|
302
|
+
this.scene.add(axesHelper);
|
|
303
|
+
}
|
|
304
|
+
return axesHelper;
|
|
305
|
+
})();
|
|
306
|
+
//TODO 设置帧频率 默认50帧
|
|
307
|
+
frame = 30;
|
|
308
|
+
init() { }
|
|
309
|
+
// 重置控制器的位置
|
|
310
|
+
resetLookAt({ position = this.perspectiveCamera.position, center = this.controls.target, }) {
|
|
311
|
+
// 停止控制器的惯性
|
|
312
|
+
const originEnableDamping = this.controls.enableDamping;
|
|
313
|
+
this.controls.saveState();
|
|
314
|
+
this.controls.update();
|
|
315
|
+
this.controls.reset();
|
|
316
|
+
this.controls.enableDamping = originEnableDamping;
|
|
317
|
+
this.controlsDistanceTransition.reset({
|
|
318
|
+
distance: new Vector3(position.x, position.y, position.z).distanceTo(new Vector3(center.x, center.y, center.z))
|
|
319
|
+
});
|
|
320
|
+
position &&
|
|
321
|
+
this.controlsPosition.reset({
|
|
322
|
+
x: position.x,
|
|
323
|
+
y: position.y,
|
|
324
|
+
z: position.z,
|
|
325
|
+
});
|
|
326
|
+
center &&
|
|
327
|
+
this.controlsCenter.reset({ x: center.x, y: center.y, z: center.z });
|
|
328
|
+
this.needRender = true;
|
|
329
|
+
}
|
|
330
|
+
// 设置控制器的位置 以动画的方式移动控制器
|
|
331
|
+
async setLookAt({ position = this.perspectiveCamera.position, center = this.controls.target, duration = 1000, }) {
|
|
332
|
+
// 停止控制器的惯性
|
|
333
|
+
const originEnableDamping = this.controls.enableDamping;
|
|
334
|
+
this.controls.saveState();
|
|
335
|
+
this.controls.update();
|
|
336
|
+
this.controls.reset();
|
|
337
|
+
this.controls.enableDamping = originEnableDamping;
|
|
338
|
+
let currentPosition = this.perspectiveCamera.position.clone();
|
|
339
|
+
let currentCenter = this.controls.target.clone();
|
|
340
|
+
let currentDistance = this.perspectiveCamera.position.distanceTo(this.controls.target);
|
|
341
|
+
let targetPosition = new Vector3(position.x, position.y, position.z);
|
|
342
|
+
let targetCenter = new Vector3(center.x, center.y, center.z);
|
|
343
|
+
let targetDistance = targetPosition.distanceTo(targetCenter);
|
|
344
|
+
// 等待动画执行完成
|
|
345
|
+
await Promise.all([
|
|
346
|
+
new Promise(async (resolve, reject) => {
|
|
347
|
+
this.controlsDistanceTransition.setDuration(duration).reset({
|
|
348
|
+
distance: currentDistance,
|
|
349
|
+
});
|
|
350
|
+
await this.controlsDistanceTransition.set({
|
|
351
|
+
distance: targetDistance
|
|
352
|
+
});
|
|
353
|
+
resolve();
|
|
354
|
+
}),
|
|
355
|
+
new Promise(async (resolve, reject) => {
|
|
356
|
+
this.controlsPosition.setDuration(duration).reset({
|
|
357
|
+
x: currentPosition.x,
|
|
358
|
+
y: currentPosition.y,
|
|
359
|
+
z: currentPosition.z,
|
|
360
|
+
});
|
|
361
|
+
await this.controlsPosition.set({
|
|
362
|
+
x: position.x,
|
|
363
|
+
y: position.y,
|
|
364
|
+
z: position.z,
|
|
365
|
+
});
|
|
366
|
+
resolve();
|
|
367
|
+
}),
|
|
368
|
+
new Promise(async (resolve, reject) => {
|
|
369
|
+
this.controlsCenter.setDuration(duration).reset({
|
|
370
|
+
x: currentCenter.x,
|
|
371
|
+
y: currentCenter.y,
|
|
372
|
+
z: currentCenter.z,
|
|
373
|
+
});
|
|
374
|
+
await this.controlsCenter.set({
|
|
375
|
+
x: center.x,
|
|
376
|
+
y: center.y,
|
|
377
|
+
z: center.z,
|
|
378
|
+
});
|
|
379
|
+
resolve();
|
|
380
|
+
}),
|
|
381
|
+
]);
|
|
382
|
+
}
|
|
383
|
+
// 获取控制的位置和朝向 保留3位小数
|
|
384
|
+
getLookAt() {
|
|
385
|
+
//判断需要使用那个相机
|
|
386
|
+
const position = this.perspectiveCamera.position
|
|
387
|
+
.toArray()
|
|
388
|
+
.map((v) => round(v, 3));
|
|
389
|
+
const center = this.controls.target.toArray().map((v) => round(v, 3));
|
|
390
|
+
return {
|
|
391
|
+
center: new Vector3(...center),
|
|
392
|
+
position: new Vector3(...position),
|
|
393
|
+
centerPosition: `center: new Vector3(${center.join(",")}),position: new Vector3(${position.join(",")})`,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
// 查看对象
|
|
397
|
+
lookatObjects({ objects, duration = 1000, direction = new Vector3(0.3, 0.3, 1), offset = new Vector3(0, 0, 0), distance = 1, }) {
|
|
398
|
+
const box3 = new Box3();
|
|
399
|
+
objects.map((o) => {
|
|
400
|
+
o.traverseVisible((oo) => {
|
|
401
|
+
box3.expandByObject(oo);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
const size = new Vector3();
|
|
405
|
+
box3.getSize(size);
|
|
406
|
+
const center = new Vector3();
|
|
407
|
+
box3.getCenter(center);
|
|
408
|
+
center.add(offset);
|
|
409
|
+
const modelSize = size.length() * distance;
|
|
410
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
411
|
+
const { fov } = this.getCamera();
|
|
412
|
+
const cameraDistance = modelSize / (2 * Math.tan((fov * Math.PI) / 360));
|
|
413
|
+
const position = direction.setLength(cameraDistance).add(center);
|
|
414
|
+
this.setLookAt({ center, position, duration });
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
// 三维向量转化成屏幕上的2维向量
|
|
418
|
+
v3tov2(v3) {
|
|
419
|
+
//判断需要使用那个相机
|
|
420
|
+
const camera = this.getCamera();
|
|
421
|
+
v3.project(camera);
|
|
422
|
+
const v2 = new Vector2((v3.x + 1) * this.size.x * 0.5, (-v3.y + 1) * this.size.y * 0.5);
|
|
423
|
+
return v2;
|
|
424
|
+
}
|
|
425
|
+
// 控制器类型
|
|
426
|
+
controlType = "OrbitControls";
|
|
427
|
+
// 记录一下空间平移方式 在切换控制器的时候保留一下平移方式
|
|
428
|
+
originScreenSpacePanning = false;
|
|
429
|
+
//改变控制器的类型
|
|
430
|
+
setControlType(controlType) {
|
|
431
|
+
if (controlType === "OrbitControls") {
|
|
432
|
+
this.controls.screenSpacePanning = this.originScreenSpacePanning;
|
|
433
|
+
this.controls.mouseButtons = {
|
|
434
|
+
LEFT: MOUSE.ROTATE,
|
|
435
|
+
MIDDLE: MOUSE.DOLLY,
|
|
436
|
+
RIGHT: MOUSE.PAN,
|
|
437
|
+
};
|
|
438
|
+
this.controls.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
|
|
439
|
+
}
|
|
440
|
+
else if (controlType === "MapControls") {
|
|
441
|
+
this.originScreenSpacePanning = this.controls.screenSpacePanning;
|
|
442
|
+
// pan orthogonal to world-space direction camera.up
|
|
443
|
+
this.controls.screenSpacePanning = false;
|
|
444
|
+
this.controls.mouseButtons.LEFT = MOUSE.PAN;
|
|
445
|
+
this.controls.mouseButtons.RIGHT = MOUSE.ROTATE;
|
|
446
|
+
this.controls.touches.ONE = TOUCH.PAN;
|
|
447
|
+
this.controls.touches.TWO = TOUCH.DOLLY_ROTATE;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
// 是否自动渲染
|
|
451
|
+
autoRender = true;
|
|
452
|
+
//是否需要渲染, 当autoRender=false的时候就需要 手动触发渲染
|
|
453
|
+
needRender = false;
|
|
454
|
+
// SSAO 配置
|
|
455
|
+
ssao = {
|
|
456
|
+
enable: false,
|
|
457
|
+
radius: 0.5,
|
|
458
|
+
bias: 0.025,
|
|
459
|
+
intensity: 1.5,
|
|
460
|
+
sampleCount: 16,
|
|
461
|
+
blurRadius: 2.0,
|
|
462
|
+
strength: 1.0,
|
|
463
|
+
};
|
|
464
|
+
// 停止渲染
|
|
465
|
+
stopRender() {
|
|
466
|
+
threeUtils.removeEventListener("requestAnimationFrame", this.renderBindThis);
|
|
467
|
+
if (!this.rendererLost) {
|
|
468
|
+
this.renderer.forceContextLoss();
|
|
469
|
+
this.setSize(100, 100);
|
|
470
|
+
this.renderer.domElement.style.opacity = "0";
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
// 渲染场景
|
|
474
|
+
render() {
|
|
475
|
+
// canvas 没有在页面上面的话 就停止渲染
|
|
476
|
+
if (!threeUtils.inDocument(this.renderer.domElement)) {
|
|
477
|
+
this.stopRender();
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
// 相机的朝向
|
|
481
|
+
const center = new Vector3(...this.controls.target.toArray().map((v) => round(v, 3)));
|
|
482
|
+
//透视相机和正交相机参数的时时转换
|
|
483
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
484
|
+
const { fov } = this.perspectiveCamera;
|
|
485
|
+
const distaned = this.perspectiveCamera.position
|
|
486
|
+
.clone()
|
|
487
|
+
.sub(center)
|
|
488
|
+
.length();
|
|
489
|
+
const zoom = 1 / (Math.tan(degToRad(fov) / 2) * distaned);
|
|
490
|
+
this.orthographicCamera.zoom = zoom;
|
|
491
|
+
this.orthographicCamera.position
|
|
492
|
+
.copy(this.perspectiveCamera.position.clone().sub(center))
|
|
493
|
+
.setLength(2500)
|
|
494
|
+
.add(center);
|
|
495
|
+
}
|
|
496
|
+
else if (this.cameraType === "orthographicCamera") {
|
|
497
|
+
const { fov } = this.perspectiveCamera;
|
|
498
|
+
const { zoom } = this.orthographicCamera;
|
|
499
|
+
const distaned = 1 / zoom / Math.tan(degToRad(fov) / 2);
|
|
500
|
+
this.perspectiveCamera.position
|
|
501
|
+
.copy(this.orthographicCamera.position.clone().sub(center))
|
|
502
|
+
.setLength(distaned)
|
|
503
|
+
.add(center);
|
|
504
|
+
}
|
|
505
|
+
//判断需要使用那个相机
|
|
506
|
+
const camera = this.getCamera();
|
|
507
|
+
//判断尺寸发生改变时重置尺寸
|
|
508
|
+
const parentNode = this.renderer.domElement.parentNode;
|
|
509
|
+
if (parentNode.clientWidth !== this.size.x ||
|
|
510
|
+
parentNode.clientHeight !== this.size.y) {
|
|
511
|
+
this.setSize(parentNode.clientWidth, parentNode.clientHeight);
|
|
512
|
+
}
|
|
513
|
+
// 分发鼠标事件
|
|
514
|
+
this.threeMouse.dispatchEvent();
|
|
515
|
+
// 在不自动渲染时, needRender=false 就跳过渲染
|
|
516
|
+
if (!this.autoRender && !this.needRender) {
|
|
517
|
+
return; // 标识未渲染
|
|
518
|
+
}
|
|
519
|
+
this.needRender = false;
|
|
520
|
+
if (this.controls.enabled) {
|
|
521
|
+
this.controls.update();
|
|
522
|
+
}
|
|
523
|
+
// 渲染场景
|
|
524
|
+
this.dispatchEvent({ type: "beforeRender" });
|
|
525
|
+
// 把基础场景渲染到 帧缓冲区 在后期通道中会用到
|
|
526
|
+
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
527
|
+
this.renderer.clear();
|
|
528
|
+
this.renderer.render(this.scene, camera);
|
|
529
|
+
// 深度单独渲染到无 MSAA 的渲染目标,避免 MSAA resolve 导致深度跳变闪烁
|
|
530
|
+
this.renderer.setRenderTarget(this.depthRenderTarget);
|
|
531
|
+
this.renderer.clear();
|
|
532
|
+
this.renderer.render(this.scene, camera);
|
|
533
|
+
// 切回 baseRenderTarget,后处理 pass 需要把效果叠加到颜色渲染目标上
|
|
534
|
+
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
535
|
+
// 后处理特效渲染(描边,发光,颜色覆盖,bloom,磨砂玻璃)
|
|
536
|
+
this.scene.traverseVisible((obj) => {
|
|
537
|
+
stroke_pass(this.renderer, camera, obj, this.baseDepthTexture, Math.max(window.devicePixelRatio, this.minDevicePixelRatio));
|
|
538
|
+
glow_pass(this.renderer, camera, obj, this.baseDepthTexture, Math.max(window.devicePixelRatio, this.minDevicePixelRatio));
|
|
539
|
+
bloom_pass(this.renderer, camera, obj, this.baseRenderTarget, this.baseDepthTexture, Math.max(window.devicePixelRatio, this.minDevicePixelRatio));
|
|
540
|
+
});
|
|
541
|
+
this.renderer.setRenderTarget(null);
|
|
542
|
+
this.renderer.clear();
|
|
543
|
+
this.renderRendererTarget(this.baseRenderTarget);
|
|
544
|
+
this.scene.traverseVisible((obj) => {
|
|
545
|
+
cover_pass(this.renderer, camera, obj, this.baseDepthTexture, Math.max(window.devicePixelRatio, this.minDevicePixelRatio));
|
|
546
|
+
});
|
|
547
|
+
// 一次性处理所有支持毛玻璃特效的对象
|
|
548
|
+
frosted_glass_pass(this.renderer, camera, this.scene, this.baseRenderTarget, this.baseDepthTexture, Math.max(window.devicePixelRatio, this.minDevicePixelRatio));
|
|
549
|
+
// SSAO 放在最后,叠加在所有特效之上
|
|
550
|
+
if (this.ssao.enable) {
|
|
551
|
+
ssao_pass(this.renderer, camera, this.baseDepthTexture, Math.max(window.devicePixelRatio, this.minDevicePixelRatio), {
|
|
552
|
+
...this.ssao,
|
|
553
|
+
cameraFar: camera.far,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
this.dispatchEvent({ type: "afterRender" });
|
|
557
|
+
}
|
|
558
|
+
renderBindThis = this.render.bind(this);
|
|
559
|
+
//获取渲染图blob对象
|
|
560
|
+
async getThumbBlob() {
|
|
561
|
+
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
562
|
+
this.renderer.clear();
|
|
563
|
+
this.renderer.render(this.scene, this.getCamera());
|
|
564
|
+
this.renderer.setRenderTarget(null);
|
|
565
|
+
this.renderer.clear();
|
|
566
|
+
this.renderRendererTarget(this.baseRenderTarget);
|
|
567
|
+
const blob = await new Promise((resolve, reject) => {
|
|
568
|
+
this.renderer.domElement.toBlob((blob) => {
|
|
569
|
+
resolve(blob);
|
|
570
|
+
}, "image/png", 1);
|
|
571
|
+
});
|
|
572
|
+
return blob;
|
|
573
|
+
}
|
|
574
|
+
//获取渲染图base64
|
|
575
|
+
getThumbBase64() {
|
|
576
|
+
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
577
|
+
this.renderer.clear();
|
|
578
|
+
this.renderer.render(this.scene, this.getCamera());
|
|
579
|
+
this.renderer.setRenderTarget(null);
|
|
580
|
+
this.renderer.clear();
|
|
581
|
+
this.renderRendererTarget(this.baseRenderTarget);
|
|
582
|
+
const base64 = this.renderer.domElement.toDataURL("image/png");
|
|
583
|
+
return base64;
|
|
584
|
+
}
|
|
585
|
+
//获取渲染图文件对象
|
|
586
|
+
async getThumbFile() {
|
|
587
|
+
const blob = await this.getThumbBlob();
|
|
588
|
+
const file = new File([blob], "thumb.png", { type: blob.type });
|
|
589
|
+
return file;
|
|
590
|
+
}
|
|
591
|
+
// 下载渲染图
|
|
592
|
+
async download(fileName = "download") {
|
|
593
|
+
const blob = await this.getThumbBlob();
|
|
594
|
+
let saveLink = document.createElement("a");
|
|
595
|
+
saveLink.download = `${fileName}.png`;
|
|
596
|
+
saveLink.href = URL.createObjectURL(blob);
|
|
597
|
+
document.body.append(saveLink);
|
|
598
|
+
saveLink.click();
|
|
599
|
+
}
|
|
600
|
+
//把渲染目标渲染到canvas上
|
|
601
|
+
renderRendererTarget(renderTarget) {
|
|
602
|
+
baseTextureToRenderShader.uniforms.resolution.value.set(1 / this.size.x, 1 / this.size.y);
|
|
603
|
+
baseTextureToRenderShader.uniforms.tDiffuse.value = renderTarget.texture;
|
|
604
|
+
fsQuad.render(this.renderer, baseTextureToRenderShader);
|
|
605
|
+
}
|
|
606
|
+
// 渲染纹理
|
|
607
|
+
renderTexture(texture, alpha = false) {
|
|
608
|
+
copyShader.uniforms.map.value = texture;
|
|
609
|
+
copyShader.uniforms.opacity.value = 1.0;
|
|
610
|
+
copyShader.blending = alpha ? NoBlending : NormalBlending;
|
|
611
|
+
fsQuad.render(this.renderer, copyShader);
|
|
612
|
+
}
|
|
613
|
+
}
|