three-base 2.0.21 → 2.0.23
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/Mirror.js +1 -2
- package/Mirror.ts +1 -2
- package/ThreeBase.d.ts +1 -0
- package/ThreeBase.js +38 -25
- package/ThreeBase.ts +44 -26
- package/package.json +1 -1
- package/pass/glow_pass.js +1 -1
- package/pass/glow_pass.ts +1 -1
- package/pass/stroke_pass.js +1 -1
- package/pass/stroke_pass.ts +1 -1
- package/shaders/FXAAShaderMaterial.d.ts +3 -0
- package/shaders/FXAAShaderMaterial.js +244 -0
- package/shaders/FXAAShaderMaterial.ts +248 -0
- package/lib/draco/draco_encoder.js +0 -33
package/Mirror.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2023-08-04 17:17:15
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time:
|
|
5
|
+
* @Last Modified time: 2024-01-20 05:33:51
|
|
6
6
|
*/
|
|
7
7
|
import { Color, DoubleSide, HalfFloatType, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget } from "three";
|
|
8
8
|
// 镜子模型
|
|
@@ -180,7 +180,6 @@ export default class Mirror extends Mesh {
|
|
|
180
180
|
//@ts-ignore
|
|
181
181
|
const viewport = this.camera.viewport;
|
|
182
182
|
if (viewport !== undefined) {
|
|
183
|
-
debugger;
|
|
184
183
|
renderer.state.viewport(viewport);
|
|
185
184
|
}
|
|
186
185
|
this.visible = true;
|
package/Mirror.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2023-08-04 17:17:15
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time:
|
|
5
|
+
* @Last Modified time: 2024-01-20 05:33:51
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { BufferGeometry, Camera, Color, DoubleSide, Group, HalfFloatType, Material, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, Scene, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
@@ -233,7 +233,6 @@ export default class Mirror extends Mesh {
|
|
|
233
233
|
//@ts-ignore
|
|
234
234
|
const viewport = this.camera.viewport;
|
|
235
235
|
if (viewport !== undefined) {
|
|
236
|
-
debugger;
|
|
237
236
|
renderer.state.viewport(viewport);
|
|
238
237
|
}
|
|
239
238
|
|
package/ThreeBase.d.ts
CHANGED
package/ThreeBase.js
CHANGED
|
@@ -2,19 +2,20 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2021-12-20 13:38:22
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time:
|
|
5
|
+
* @Last Modified time: 2024-01-20 05:33:28
|
|
6
6
|
*/
|
|
7
7
|
import { round } from "lodash";
|
|
8
|
-
import { AxesHelper, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, Mesh, MOUSE, NoBlending, NormalBlending, PerspectiveCamera, RGBAFormat, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderer, WebGLRenderTarget } from "three";
|
|
8
|
+
import { AxesHelper, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, LinearSRGBColorSpace, Mesh, MOUSE, NoBlending, NormalBlending, PerspectiveCamera, RGBAFormat, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderer, WebGLRenderTarget } from "three";
|
|
9
9
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
10
10
|
import { generateUUID } from "three/src/math/MathUtils";
|
|
11
|
+
import glow_pass from "./pass/glow_pass";
|
|
12
|
+
import stroke_pass from "./pass/stroke_pass";
|
|
11
13
|
import { clearShader, coptyShader, fullScreenMesh, gaussBlurShader, strokeShader } from "./shaders";
|
|
12
14
|
import threeLoader from "./threeLoader";
|
|
13
15
|
import ThreeMouse from "./ThreeMouse";
|
|
14
16
|
import threeUtils from "./threeUtils";
|
|
15
17
|
import Transition from "./Transition";
|
|
16
|
-
import
|
|
17
|
-
import glow_pass from "./pass/glow_pass";
|
|
18
|
+
import FXAAShaderMaterial from "./shaders/FXAAShaderMaterial";
|
|
18
19
|
// three.js 的基础对象
|
|
19
20
|
export default class ThreeBase extends EventDispatcher {
|
|
20
21
|
constructor() {
|
|
@@ -29,7 +30,6 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
29
30
|
baseDepthTexture.anisotropy = 16;
|
|
30
31
|
return baseDepthTexture;
|
|
31
32
|
})();
|
|
32
|
-
// TODO 渲染目标的颜色空间问题 还没有处理好
|
|
33
33
|
// 基础纹理渲染对象
|
|
34
34
|
baseRenderTarget = (() => {
|
|
35
35
|
const baseRenderTarget = new WebGLRenderTarget(128, 128, {
|
|
@@ -64,6 +64,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
64
64
|
});
|
|
65
65
|
renderer.domElement.id = generateUUID();
|
|
66
66
|
renderer.localClippingEnabled = true;
|
|
67
|
+
renderer.outputColorSpace = LinearSRGBColorSpace;
|
|
67
68
|
renderer.setPixelRatio(window.devicePixelRatio);
|
|
68
69
|
renderer.autoClear = false;
|
|
69
70
|
renderer.clear();
|
|
@@ -95,9 +96,6 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
95
96
|
div.style.overflow = "hidden";
|
|
96
97
|
div.innerHTML = "";
|
|
97
98
|
div.append(this.renderer.domElement);
|
|
98
|
-
// this.renderer.forceContextLoss();
|
|
99
|
-
// await new Promise(resolve => requestAnimationFrame(resolve));
|
|
100
|
-
// await new Promise(resolve => requestAnimationFrame(resolve));
|
|
101
99
|
this.renderer.forceContextRestore();
|
|
102
100
|
this.renderer.domElement.style.opacity = "1";
|
|
103
101
|
this.setSize(div.clientWidth, div.clientHeight);
|
|
@@ -114,7 +112,11 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
114
112
|
}
|
|
115
113
|
this.renderer.setPixelRatio(window.devicePixelRatio);
|
|
116
114
|
this.renderer.setSize(width, height);
|
|
115
|
+
// 设置帧缓存区的尺寸
|
|
117
116
|
this.baseRenderTarget.setSize(width * window.devicePixelRatio, height * window.devicePixelRatio);
|
|
117
|
+
// 设置深度纹理的尺寸
|
|
118
|
+
this.baseDepthTexture.image.width = width * window.devicePixelRatio;
|
|
119
|
+
this.baseDepthTexture.image.height = height * window.devicePixelRatio;
|
|
118
120
|
this.size.set(width, height);
|
|
119
121
|
// 渲染场景
|
|
120
122
|
// this.renderer.clear();
|
|
@@ -142,11 +144,11 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
142
144
|
const controlsPosition = new Transition({ x: 100, y: 100, z: 100 });
|
|
143
145
|
controlsPosition.setDuration(1000);
|
|
144
146
|
controlsPosition.onChange(({ x, y, z }) => {
|
|
145
|
-
const distance = this.controlsDistance.get("distance");
|
|
146
|
-
this.camera.position.set(x + 0.01, y, z + 0.01)
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
// const distance = this.controlsDistance.get("distance");
|
|
148
|
+
this.camera.position.set(x + 0.01, y, z + 0.01);
|
|
149
|
+
// .sub(this.controls.target)
|
|
150
|
+
// .setLength(distance)
|
|
151
|
+
// .add(this.controls.target);
|
|
150
152
|
// // 创建初始方向向量
|
|
151
153
|
// const directionVecA = new THREE.Vector3(1, 0, 0); // 需要旋转的初始方向向量
|
|
152
154
|
// const referenceVecB = new THREE.Vector3(0, 1, 0); // 参考方向向量,用于确定旋转轴
|
|
@@ -181,6 +183,8 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
181
183
|
}
|
|
182
184
|
return axesHelper;
|
|
183
185
|
})();
|
|
186
|
+
//TODO 设置帧频率 默认50帧
|
|
187
|
+
frame = 50;
|
|
184
188
|
init() {
|
|
185
189
|
// 资源加载进度有更新时, 需要渲染
|
|
186
190
|
threeLoader.addEventListener("progress", () => {
|
|
@@ -236,7 +240,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
236
240
|
return {
|
|
237
241
|
center: new Vector3(...center),
|
|
238
242
|
position: new Vector3(...position),
|
|
239
|
-
centerPosition: `
|
|
243
|
+
centerPosition: `center: new Vector3(${center.join(",")}),position: new Vector3(${position.join(",")})`
|
|
240
244
|
};
|
|
241
245
|
}
|
|
242
246
|
// 三维向量转化成屏幕上的2维向量
|
|
@@ -280,7 +284,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
280
284
|
// 渲染场景
|
|
281
285
|
render() {
|
|
282
286
|
// 控制器在移动时鼠标不能操作
|
|
283
|
-
|
|
287
|
+
this.renderer.domElement.style.pointerEvents = (!this.controlsPosition.isDone() || !this.controlsCenter.isDone()) ? "none" : "all";
|
|
284
288
|
// canvas 没有在页面上面的话 就跳过渲染
|
|
285
289
|
if (!threeUtils.inDocument(this.renderer.domElement)) {
|
|
286
290
|
this.stopRender();
|
|
@@ -303,14 +307,15 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
303
307
|
}
|
|
304
308
|
// 渲染场景
|
|
305
309
|
this.dispatchEvent({ type: "beforeRender" });
|
|
306
|
-
//
|
|
310
|
+
// 把基础场景渲染到 帧缓冲区 在后期通道中会用到
|
|
311
|
+
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
307
312
|
this.renderer.clear();
|
|
308
313
|
this.renderer.render(this.scene, this.camera);
|
|
309
|
-
//
|
|
310
|
-
|
|
311
|
-
|
|
314
|
+
// 清空主画布
|
|
315
|
+
this.renderer.setRenderTarget(null);
|
|
316
|
+
this.renderer.clear();
|
|
312
317
|
// this.renderer.setScissorTest(false);
|
|
313
|
-
|
|
318
|
+
this.renderRendererTarget(this.baseRenderTarget);
|
|
314
319
|
// this.renderer.setScissorTest(true);
|
|
315
320
|
// this.renderer.setScissor(0, 0, 800, 1000);
|
|
316
321
|
// this.renderer.render(this.scene, this.camera);
|
|
@@ -442,13 +447,21 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
442
447
|
this.renderTargetgaussBlur(renderTarget, 1, 1);
|
|
443
448
|
this.renderTargetgaussBlur(renderTarget, 1, 1);
|
|
444
449
|
}
|
|
445
|
-
// 渲染 渲染目标
|
|
450
|
+
// 需要做康锯齿处理 渲染 渲染目标
|
|
446
451
|
renderRendererTarget(renderTarget, alpha = false) {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
fullScreenMesh.material = coptyShader;
|
|
450
|
-
coptyShader.blending = alpha ? NoBlending : NormalBlending;
|
|
452
|
+
FXAAShaderMaterial.uniforms.tDiffuse.value = renderTarget.texture;
|
|
453
|
+
fullScreenMesh.material = FXAAShaderMaterial;
|
|
451
454
|
fullScreenMesh.render(this.renderer);
|
|
455
|
+
// const FXAAShaderMaterial = new ShaderMaterial({
|
|
456
|
+
// uniforms: {
|
|
457
|
+
// tDiffuse: { value: null }, // 贴图纹理
|
|
458
|
+
// resolution: { value: new Vector2(1 / 1024, 1 / 1024) }
|
|
459
|
+
// },
|
|
460
|
+
// coptyShader.uniforms.map.value = renderTarget.texture;
|
|
461
|
+
// coptyShader.uniforms.opacity.value = 1.0;
|
|
462
|
+
// fullScreenMesh.material = coptyShader;
|
|
463
|
+
// coptyShader.blending = alpha ? NoBlending : NormalBlending;
|
|
464
|
+
// fullScreenMesh.render(this.renderer);
|
|
452
465
|
}
|
|
453
466
|
// 渲染纹理
|
|
454
467
|
renderTexture(texture, alpha = false) {
|
package/ThreeBase.ts
CHANGED
|
@@ -2,20 +2,21 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2021-12-20 13:38:22
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time:
|
|
5
|
+
* @Last Modified time: 2024-01-20 05:33:28
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { round } from "lodash";
|
|
9
|
-
import { AxesHelper, BufferGeometry, Camera, DepthFormat, DepthTexture,
|
|
9
|
+
import { AxesHelper, BufferGeometry, Camera, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, LinearSRGBColorSpace, Mesh, MeshBasicMaterial, MOUSE, NoBlending, NormalBlending, Object3D, PerspectiveCamera, RGBAFormat, Scene, Texture, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderer, WebGLRenderTarget } from "three";
|
|
10
10
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
11
11
|
import { generateUUID } from "three/src/math/MathUtils";
|
|
12
|
+
import glow_pass from "./pass/glow_pass";
|
|
13
|
+
import stroke_pass from "./pass/stroke_pass";
|
|
12
14
|
import { clearShader, coptyShader, fullScreenMesh, gaussBlurShader, strokeShader } from "./shaders";
|
|
13
15
|
import threeLoader from "./threeLoader";
|
|
14
16
|
import ThreeMouse from "./ThreeMouse";
|
|
15
17
|
import threeUtils from "./threeUtils";
|
|
16
18
|
import Transition from "./Transition";
|
|
17
|
-
import
|
|
18
|
-
import glow_pass from "./pass/glow_pass";
|
|
19
|
+
import FXAAShaderMaterial from "./shaders/FXAAShaderMaterial";
|
|
19
20
|
|
|
20
21
|
// three.js 的基础对象
|
|
21
22
|
export default class ThreeBase extends EventDispatcher {
|
|
@@ -34,7 +35,6 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
34
35
|
return baseDepthTexture;
|
|
35
36
|
})();
|
|
36
37
|
|
|
37
|
-
// TODO 渲染目标的颜色空间问题 还没有处理好
|
|
38
38
|
// 基础纹理渲染对象
|
|
39
39
|
baseRenderTarget = (() => {
|
|
40
40
|
const baseRenderTarget = new WebGLRenderTarget(128, 128, {
|
|
@@ -69,13 +69,14 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
69
69
|
alpha: true,// 透明背景
|
|
70
70
|
antialias: true, // 阴影抗锯齿
|
|
71
71
|
precision: "highp", //precision:highp/mediump/lowp着色精度选择
|
|
72
|
-
logarithmicDepthBuffer: true, // 对数深度缓冲区 避免当2
|
|
72
|
+
logarithmicDepthBuffer: true, // 对数深度缓冲区 避免当2个面挨的很近时闪烁
|
|
73
73
|
preserveDrawingBuffer: true, // 是否保存绘图缓冲
|
|
74
74
|
stencil: false, // 模版
|
|
75
75
|
});
|
|
76
76
|
|
|
77
77
|
renderer.domElement.id = generateUUID();
|
|
78
78
|
renderer.localClippingEnabled = true;
|
|
79
|
+
renderer.outputColorSpace = LinearSRGBColorSpace;
|
|
79
80
|
renderer.setPixelRatio(window.devicePixelRatio);
|
|
80
81
|
renderer.autoClear = false;
|
|
81
82
|
renderer.clear();
|
|
@@ -113,9 +114,6 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
113
114
|
div.innerHTML = "";
|
|
114
115
|
div.append(this.renderer.domElement);
|
|
115
116
|
|
|
116
|
-
// this.renderer.forceContextLoss();
|
|
117
|
-
// await new Promise(resolve => requestAnimationFrame(resolve));
|
|
118
|
-
// await new Promise(resolve => requestAnimationFrame(resolve));
|
|
119
117
|
this.renderer.forceContextRestore();
|
|
120
118
|
this.renderer.domElement.style.opacity = "1";
|
|
121
119
|
|
|
@@ -137,8 +135,13 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
137
135
|
this.renderer.setPixelRatio(window.devicePixelRatio);
|
|
138
136
|
this.renderer.setSize(width, height);
|
|
139
137
|
|
|
138
|
+
// 设置帧缓存区的尺寸
|
|
140
139
|
this.baseRenderTarget.setSize(width * window.devicePixelRatio, height * window.devicePixelRatio);
|
|
141
140
|
|
|
141
|
+
// 设置深度纹理的尺寸
|
|
142
|
+
this.baseDepthTexture.image.width = width * window.devicePixelRatio;
|
|
143
|
+
this.baseDepthTexture.image.height = height * window.devicePixelRatio;
|
|
144
|
+
|
|
142
145
|
this.size.set(width, height);
|
|
143
146
|
// 渲染场景
|
|
144
147
|
// this.renderer.clear();
|
|
@@ -169,11 +172,11 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
169
172
|
const controlsPosition = new Transition({ x: 100, y: 100, z: 100 });
|
|
170
173
|
controlsPosition.setDuration(1000);
|
|
171
174
|
controlsPosition.onChange(({ x, y, z }) => {
|
|
172
|
-
const distance = this.controlsDistance.get("distance");
|
|
175
|
+
// const distance = this.controlsDistance.get("distance");
|
|
173
176
|
this.camera.position.set(x + 0.01, y, z + 0.01)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
+
// .sub(this.controls.target)
|
|
178
|
+
// .setLength(distance)
|
|
179
|
+
// .add(this.controls.target);
|
|
177
180
|
|
|
178
181
|
// // 创建初始方向向量
|
|
179
182
|
// const directionVecA = new THREE.Vector3(1, 0, 0); // 需要旋转的初始方向向量
|
|
@@ -219,6 +222,9 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
219
222
|
return axesHelper;
|
|
220
223
|
})();
|
|
221
224
|
|
|
225
|
+
//TODO 设置帧频率 默认50帧
|
|
226
|
+
frame = 50;
|
|
227
|
+
|
|
222
228
|
init() {
|
|
223
229
|
// 资源加载进度有更新时, 需要渲染
|
|
224
230
|
threeLoader.addEventListener("progress", () => {
|
|
@@ -291,7 +297,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
291
297
|
return {
|
|
292
298
|
center: new Vector3(...center),
|
|
293
299
|
position: new Vector3(...position),
|
|
294
|
-
centerPosition: `
|
|
300
|
+
centerPosition: `center: new Vector3(${center.join(",")}),position: new Vector3(${position.join(",")})`
|
|
295
301
|
}
|
|
296
302
|
}
|
|
297
303
|
|
|
@@ -343,9 +349,8 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
343
349
|
|
|
344
350
|
// 渲染场景
|
|
345
351
|
render() {
|
|
346
|
-
|
|
347
352
|
// 控制器在移动时鼠标不能操作
|
|
348
|
-
|
|
353
|
+
this.renderer.domElement.style.pointerEvents = (!this.controlsPosition.isDone() || !this.controlsCenter.isDone()) ? "none" : "all";
|
|
349
354
|
|
|
350
355
|
// canvas 没有在页面上面的话 就跳过渲染
|
|
351
356
|
if (!threeUtils.inDocument(this.renderer.domElement)) {
|
|
@@ -375,16 +380,19 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
375
380
|
// 渲染场景
|
|
376
381
|
this.dispatchEvent({ type: "beforeRender" } as never);
|
|
377
382
|
|
|
378
|
-
//
|
|
383
|
+
// 把基础场景渲染到 帧缓冲区 在后期通道中会用到
|
|
384
|
+
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
379
385
|
this.renderer.clear();
|
|
380
386
|
this.renderer.render(this.scene, this.camera);
|
|
381
387
|
|
|
382
|
-
//
|
|
383
|
-
|
|
388
|
+
// 清空主画布
|
|
389
|
+
this.renderer.setRenderTarget(null);
|
|
390
|
+
this.renderer.clear();
|
|
384
391
|
|
|
385
|
-
//TODO 渲染了2次, 还有优化空间
|
|
386
392
|
// this.renderer.setScissorTest(false);
|
|
387
|
-
|
|
393
|
+
|
|
394
|
+
this.renderRendererTarget(this.baseRenderTarget);
|
|
395
|
+
|
|
388
396
|
// this.renderer.setScissorTest(true);
|
|
389
397
|
// this.renderer.setScissor(0, 0, 800, 1000);
|
|
390
398
|
// this.renderer.render(this.scene, this.camera);
|
|
@@ -542,13 +550,23 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
542
550
|
this.renderTargetgaussBlur(renderTarget, 1, 1);
|
|
543
551
|
}
|
|
544
552
|
|
|
545
|
-
// 渲染 渲染目标
|
|
553
|
+
// 需要做康锯齿处理 渲染 渲染目标
|
|
546
554
|
renderRendererTarget(renderTarget: WebGLRenderTarget, alpha: boolean = false) {
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
fullScreenMesh.material =
|
|
550
|
-
coptyShader.blending = alpha ? NoBlending : NormalBlending;
|
|
555
|
+
|
|
556
|
+
FXAAShaderMaterial.uniforms.tDiffuse.value = renderTarget.texture;
|
|
557
|
+
fullScreenMesh.material = FXAAShaderMaterial;
|
|
551
558
|
fullScreenMesh.render(this.renderer);
|
|
559
|
+
|
|
560
|
+
// const FXAAShaderMaterial = new ShaderMaterial({
|
|
561
|
+
// uniforms: {
|
|
562
|
+
// tDiffuse: { value: null }, // 贴图纹理
|
|
563
|
+
// resolution: { value: new Vector2(1 / 1024, 1 / 1024) }
|
|
564
|
+
// },
|
|
565
|
+
// coptyShader.uniforms.map.value = renderTarget.texture;
|
|
566
|
+
// coptyShader.uniforms.opacity.value = 1.0;
|
|
567
|
+
// fullScreenMesh.material = coptyShader;
|
|
568
|
+
// coptyShader.blending = alpha ? NoBlending : NormalBlending;
|
|
569
|
+
// fullScreenMesh.render(this.renderer);
|
|
552
570
|
}
|
|
553
571
|
|
|
554
572
|
// 渲染纹理
|
package/package.json
CHANGED
package/pass/glow_pass.js
CHANGED
|
@@ -482,7 +482,7 @@ export default function glow_pass(renderer, camera, object3d, baseDepthTexture)
|
|
|
482
482
|
renderer.setRenderTarget(selectTextureTarget);
|
|
483
483
|
scene.children = [object3d];
|
|
484
484
|
renderer.clear();
|
|
485
|
-
renderer.render(
|
|
485
|
+
renderer.render(scene, camera);
|
|
486
486
|
// 设置描边着色器的分辨率
|
|
487
487
|
blurShader.uniforms.resolution.value.set(1 / size.x, 1 / size.y);
|
|
488
488
|
// 设置抗锯齿着色器的分辨率
|
package/pass/glow_pass.ts
CHANGED
|
@@ -504,7 +504,7 @@ export default function glow_pass(renderer: WebGLRenderer, camera: Camera, objec
|
|
|
504
504
|
renderer.setRenderTarget(selectTextureTarget);
|
|
505
505
|
scene.children = [object3d];
|
|
506
506
|
renderer.clear();
|
|
507
|
-
renderer.render(
|
|
507
|
+
renderer.render(scene, camera);
|
|
508
508
|
|
|
509
509
|
// 设置描边着色器的分辨率
|
|
510
510
|
blurShader.uniforms.resolution.value.set(1 / size.x, 1 / size.y);
|
package/pass/stroke_pass.js
CHANGED
|
@@ -478,7 +478,7 @@ export default function stroke_pass(renderer, camera, object3d, baseDepthTexture
|
|
|
478
478
|
renderer.setRenderTarget(selectTextureTarget);
|
|
479
479
|
scene.children = [object3d];
|
|
480
480
|
renderer.clear();
|
|
481
|
-
renderer.render(
|
|
481
|
+
renderer.render(scene, camera);
|
|
482
482
|
// 设置描边着色器的分辨率
|
|
483
483
|
blurShader.uniforms.resolution.value.set(1 / size.x, 1 / size.y);
|
|
484
484
|
// 设置抗锯齿着色器的分辨率
|
package/pass/stroke_pass.ts
CHANGED
|
@@ -497,7 +497,7 @@ export default function stroke_pass(renderer: WebGLRenderer, camera: Camera, obj
|
|
|
497
497
|
renderer.setRenderTarget(selectTextureTarget);
|
|
498
498
|
scene.children = [object3d];
|
|
499
499
|
renderer.clear();
|
|
500
|
-
renderer.render(
|
|
500
|
+
renderer.render(scene, camera);
|
|
501
501
|
|
|
502
502
|
// 设置描边着色器的分辨率
|
|
503
503
|
blurShader.uniforms.resolution.value.set(1 / size.x, 1 / size.y);
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2024-01-20 05:10:37
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2024-01-20 05:10:57
|
|
6
|
+
*/
|
|
7
|
+
import { NoBlending, ShaderChunk, ShaderMaterial, Vector2 } from "three";
|
|
8
|
+
// 抗锯齿着色器
|
|
9
|
+
const FXAAShaderMaterial = new ShaderMaterial({
|
|
10
|
+
uniforms: {
|
|
11
|
+
tDiffuse: { value: null },
|
|
12
|
+
resolution: { value: new Vector2(1 / 1024, 1 / 1024) }
|
|
13
|
+
},
|
|
14
|
+
vertexShader: `
|
|
15
|
+
${ShaderChunk.common}
|
|
16
|
+
${ShaderChunk.logdepthbuf_pars_vertex}
|
|
17
|
+
varying vec2 vUv;
|
|
18
|
+
void main() {
|
|
19
|
+
vUv = uv;
|
|
20
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
|
|
21
|
+
${ShaderChunk.logdepthbuf_vertex}
|
|
22
|
+
}
|
|
23
|
+
`,
|
|
24
|
+
fragmentShader: `
|
|
25
|
+
${ShaderChunk.logdepthbuf_pars_fragment}
|
|
26
|
+
precision highp float;
|
|
27
|
+
|
|
28
|
+
uniform sampler2D tDiffuse;
|
|
29
|
+
|
|
30
|
+
uniform vec2 resolution;
|
|
31
|
+
|
|
32
|
+
varying vec2 vUv;
|
|
33
|
+
|
|
34
|
+
#ifndef FXAA_DISCARD
|
|
35
|
+
//
|
|
36
|
+
// Only valid for PC OpenGL currently.
|
|
37
|
+
// Probably will not work when FXAA_GREEN_AS_LUMA = 1.
|
|
38
|
+
//
|
|
39
|
+
// 1 = Use discard on pixels which don't need AA.
|
|
40
|
+
// For APIs which enable concurrent TEX+ROP from same surface.
|
|
41
|
+
// 0 = Return unchanged color on pixels which don't need AA.
|
|
42
|
+
//
|
|
43
|
+
#define FXAA_DISCARD 0
|
|
44
|
+
#endif
|
|
45
|
+
|
|
46
|
+
/*--------------------------------------------------------------------------*/
|
|
47
|
+
#define FxaaTexTop(t, p) texture2D(t, p, -100.0)
|
|
48
|
+
#define FxaaTexOff(t, p, o, r) texture2D(t, p + (o * r), -100.0)
|
|
49
|
+
/*--------------------------------------------------------------------------*/
|
|
50
|
+
|
|
51
|
+
#define NUM_SAMPLES 5
|
|
52
|
+
|
|
53
|
+
// assumes colors have premultipliedAlpha, so that the calculated color contrast is scaled by alpha
|
|
54
|
+
float contrast( vec4 a, vec4 b ) {
|
|
55
|
+
vec4 diff = abs( a - b );
|
|
56
|
+
return max( max( max( diff.r, diff.g ), diff.b ), diff.a );
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/*============================================================================
|
|
60
|
+
|
|
61
|
+
FXAA3 QUALITY - PC
|
|
62
|
+
|
|
63
|
+
============================================================================*/
|
|
64
|
+
|
|
65
|
+
/*--------------------------------------------------------------------------*/
|
|
66
|
+
vec4 FxaaPixelShader(
|
|
67
|
+
vec2 posM,
|
|
68
|
+
sampler2D tex,
|
|
69
|
+
vec2 fxaaQualityRcpFrame,
|
|
70
|
+
float fxaaQualityEdgeThreshold,
|
|
71
|
+
float fxaaQualityinvEdgeThreshold
|
|
72
|
+
) {
|
|
73
|
+
vec4 rgbaM = FxaaTexTop(tex, posM);
|
|
74
|
+
vec4 rgbaS = FxaaTexOff(tex, posM, vec2( 0.0, 1.0), fxaaQualityRcpFrame.xy);
|
|
75
|
+
vec4 rgbaE = FxaaTexOff(tex, posM, vec2( 1.0, 0.0), fxaaQualityRcpFrame.xy);
|
|
76
|
+
vec4 rgbaN = FxaaTexOff(tex, posM, vec2( 0.0,-1.0), fxaaQualityRcpFrame.xy);
|
|
77
|
+
vec4 rgbaW = FxaaTexOff(tex, posM, vec2(-1.0, 0.0), fxaaQualityRcpFrame.xy);
|
|
78
|
+
// . S .
|
|
79
|
+
// W M E
|
|
80
|
+
// . N .
|
|
81
|
+
|
|
82
|
+
bool earlyExit = max( max( max(
|
|
83
|
+
contrast( rgbaM, rgbaN ),
|
|
84
|
+
contrast( rgbaM, rgbaS ) ),
|
|
85
|
+
contrast( rgbaM, rgbaE ) ),
|
|
86
|
+
contrast( rgbaM, rgbaW ) )
|
|
87
|
+
< fxaaQualityEdgeThreshold;
|
|
88
|
+
// . 0 .
|
|
89
|
+
// 0 0 0
|
|
90
|
+
// . 0 .
|
|
91
|
+
|
|
92
|
+
#if (FXAA_DISCARD == 1)
|
|
93
|
+
if(earlyExit) FxaaDiscard;
|
|
94
|
+
#else
|
|
95
|
+
if(earlyExit) return rgbaM;
|
|
96
|
+
#endif
|
|
97
|
+
|
|
98
|
+
float contrastN = contrast( rgbaM, rgbaN );
|
|
99
|
+
float contrastS = contrast( rgbaM, rgbaS );
|
|
100
|
+
float contrastE = contrast( rgbaM, rgbaE );
|
|
101
|
+
float contrastW = contrast( rgbaM, rgbaW );
|
|
102
|
+
|
|
103
|
+
float relativeVContrast = ( contrastN + contrastS ) - ( contrastE + contrastW );
|
|
104
|
+
relativeVContrast *= fxaaQualityinvEdgeThreshold;
|
|
105
|
+
|
|
106
|
+
bool horzSpan = relativeVContrast > 0.;
|
|
107
|
+
// . 1 .
|
|
108
|
+
// 0 0 0
|
|
109
|
+
// . 1 .
|
|
110
|
+
|
|
111
|
+
// 45 deg edge detection and corners of objects, aka V/H contrast is too similar
|
|
112
|
+
if( abs( relativeVContrast ) < .3 ) {
|
|
113
|
+
// locate the edge
|
|
114
|
+
vec2 dirToEdge;
|
|
115
|
+
dirToEdge.x = contrastE > contrastW ? 1. : -1.;
|
|
116
|
+
dirToEdge.y = contrastS > contrastN ? 1. : -1.;
|
|
117
|
+
// . 2 . . 1 .
|
|
118
|
+
// 1 0 2 ~= 0 0 1
|
|
119
|
+
// . 1 . . 0 .
|
|
120
|
+
|
|
121
|
+
// tap 2 pixels and see which ones are "outside" the edge, to
|
|
122
|
+
// determine if the edge is vertical or horizontal
|
|
123
|
+
|
|
124
|
+
vec4 rgbaAlongH = FxaaTexOff(tex, posM, vec2( dirToEdge.x, -dirToEdge.y ), fxaaQualityRcpFrame.xy);
|
|
125
|
+
float matchAlongH = contrast( rgbaM, rgbaAlongH );
|
|
126
|
+
// . 1 .
|
|
127
|
+
// 0 0 1
|
|
128
|
+
// . 0 H
|
|
129
|
+
|
|
130
|
+
vec4 rgbaAlongV = FxaaTexOff(tex, posM, vec2( -dirToEdge.x, dirToEdge.y ), fxaaQualityRcpFrame.xy);
|
|
131
|
+
float matchAlongV = contrast( rgbaM, rgbaAlongV );
|
|
132
|
+
// V 1 .
|
|
133
|
+
// 0 0 1
|
|
134
|
+
// . 0 .
|
|
135
|
+
|
|
136
|
+
relativeVContrast = matchAlongV - matchAlongH;
|
|
137
|
+
relativeVContrast *= fxaaQualityinvEdgeThreshold;
|
|
138
|
+
|
|
139
|
+
if( abs( relativeVContrast ) < .3 ) { // 45 deg edge
|
|
140
|
+
// 1 1 .
|
|
141
|
+
// 0 0 1
|
|
142
|
+
// . 0 1
|
|
143
|
+
|
|
144
|
+
// do a simple blur
|
|
145
|
+
return mix(
|
|
146
|
+
rgbaM,
|
|
147
|
+
(rgbaN + rgbaS + rgbaE + rgbaW) * .25,
|
|
148
|
+
.4
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
horzSpan = relativeVContrast > 0.;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if(!horzSpan) rgbaN = rgbaW;
|
|
156
|
+
if(!horzSpan) rgbaS = rgbaE;
|
|
157
|
+
// . 0 . 1
|
|
158
|
+
// 1 0 1 -> 0
|
|
159
|
+
// . 0 . 1
|
|
160
|
+
|
|
161
|
+
bool pairN = contrast( rgbaM, rgbaN ) > contrast( rgbaM, rgbaS );
|
|
162
|
+
if(!pairN) rgbaN = rgbaS;
|
|
163
|
+
|
|
164
|
+
vec2 offNP;
|
|
165
|
+
offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
|
|
166
|
+
offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
|
|
167
|
+
|
|
168
|
+
bool doneN = false;
|
|
169
|
+
bool doneP = false;
|
|
170
|
+
|
|
171
|
+
float nDist = 0.;
|
|
172
|
+
float pDist = 0.;
|
|
173
|
+
|
|
174
|
+
vec2 posN = posM;
|
|
175
|
+
vec2 posP = posM;
|
|
176
|
+
|
|
177
|
+
int iterationsUsed = 0;
|
|
178
|
+
int iterationsUsedN = 0;
|
|
179
|
+
int iterationsUsedP = 0;
|
|
180
|
+
for( int i = 0; i < NUM_SAMPLES; i++ ) {
|
|
181
|
+
iterationsUsed = i;
|
|
182
|
+
|
|
183
|
+
float increment = float(i + 1);
|
|
184
|
+
|
|
185
|
+
if(!doneN) {
|
|
186
|
+
nDist += increment;
|
|
187
|
+
posN = posM + offNP * nDist;
|
|
188
|
+
vec4 rgbaEndN = FxaaTexTop(tex, posN.xy);
|
|
189
|
+
doneN = contrast( rgbaEndN, rgbaM ) > contrast( rgbaEndN, rgbaN );
|
|
190
|
+
iterationsUsedN = i;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if(!doneP) {
|
|
194
|
+
pDist += increment;
|
|
195
|
+
posP = posM - offNP * pDist;
|
|
196
|
+
vec4 rgbaEndP = FxaaTexTop(tex, posP.xy);
|
|
197
|
+
doneP = contrast( rgbaEndP, rgbaM ) > contrast( rgbaEndP, rgbaN );
|
|
198
|
+
iterationsUsedP = i;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if(doneN || doneP) break;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
if ( !doneP && !doneN ) return rgbaM; // failed to find end of edge
|
|
206
|
+
|
|
207
|
+
float dist = min(
|
|
208
|
+
doneN ? float( iterationsUsedN ) / float( NUM_SAMPLES - 1 ) : 1.,
|
|
209
|
+
doneP ? float( iterationsUsedP ) / float( NUM_SAMPLES - 1 ) : 1.
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
// hacky way of reduces blurriness of mostly diagonal edges
|
|
213
|
+
// but reduces AA quality
|
|
214
|
+
dist = pow(dist, .5);
|
|
215
|
+
|
|
216
|
+
dist = 1. - dist;
|
|
217
|
+
|
|
218
|
+
return mix(
|
|
219
|
+
rgbaM,
|
|
220
|
+
rgbaN,
|
|
221
|
+
dist * .5
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
void main() {
|
|
226
|
+
const float edgeDetectionQuality = .2;
|
|
227
|
+
const float invEdgeDetectionQuality = 1. / edgeDetectionQuality;
|
|
228
|
+
|
|
229
|
+
gl_FragColor = FxaaPixelShader(
|
|
230
|
+
vUv,
|
|
231
|
+
tDiffuse,
|
|
232
|
+
resolution,
|
|
233
|
+
edgeDetectionQuality, // [0,1] contrast needed, otherwise early discard
|
|
234
|
+
invEdgeDetectionQuality
|
|
235
|
+
);
|
|
236
|
+
${ShaderChunk.logdepthbuf_fragment}
|
|
237
|
+
}
|
|
238
|
+
`,
|
|
239
|
+
blending: NoBlending,
|
|
240
|
+
depthTest: false,
|
|
241
|
+
depthWrite: false,
|
|
242
|
+
transparent: true
|
|
243
|
+
});
|
|
244
|
+
export default FXAAShaderMaterial;
|