three-base 1.129.9 → 1.129.10

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/lib/shaders.ts ADDED
@@ -0,0 +1,264 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-03-24 16:38:35
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2022-04-02 18:28:38
6
+ */
7
+
8
+ import { BufferGeometry, Color, Float32BufferAttribute, Mesh, NoBlending, OrthographicCamera, ShaderChunk, ShaderMaterial, Vector2, WebGLRenderer } from "three";
9
+
10
+
11
+
12
+ // https://github.com/mrdoob/three.js/pull/21358
13
+
14
+ const _geometry = new BufferGeometry();
15
+ _geometry.setAttribute("position", new Float32BufferAttribute([- 1, 3, 0, - 1, - 1, 0, 3, - 1, 0], 3));
16
+ _geometry.setAttribute("uv", new Float32BufferAttribute([0, 2, 0, 0, 2, 0], 2));
17
+
18
+ class FullScreenQuad {
19
+ constructor() {
20
+ this._mesh = new Mesh(_geometry);
21
+ }
22
+ _mesh!: Mesh;
23
+ dispose() {
24
+ this._mesh.geometry.dispose();
25
+ }
26
+ render(renderer: WebGLRenderer) {
27
+ renderer.render(this._mesh, orthographicCamera);
28
+ }
29
+ get material() {
30
+ return this._mesh.material;
31
+ }
32
+ set material(value) {
33
+ this._mesh.material = value;
34
+ }
35
+ }
36
+
37
+
38
+ // 常用着色器及工具
39
+
40
+
41
+ // 全屏渲染模型
42
+ export const fullScreenMesh = new FullScreenQuad();
43
+
44
+ export const orthographicCamera = new OrthographicCamera(- 1, 1, 1, - 1, 0, 1);
45
+
46
+ // 清理着色器
47
+ export const clearShader = new ShaderMaterial({
48
+ vertexShader: `
49
+ varying vec2 vUv;
50
+ void main() {
51
+ vUv = uv;
52
+ gl_Position = vec4(position, 1.0);
53
+ }
54
+ `,
55
+ fragmentShader: `
56
+ void main() {
57
+ gl_FragColor = vec4(0.0);
58
+ }
59
+ `,
60
+ blending: NoBlending, // 渲染时不与背景融合计算 直接替换
61
+ depthTest: false,
62
+ depthWrite: false,
63
+ transparent: true
64
+ });
65
+
66
+ // 交集着色器提取相同的像素点
67
+ export const intersectionShader = new ShaderMaterial({
68
+ uniforms: {
69
+ map1: { value: null }, // 贴图纹理1
70
+ map2: { value: null }, // 贴图纹理2
71
+ },
72
+ vertexShader: `
73
+ ${ShaderChunk.common}
74
+ ${ShaderChunk.logdepthbuf_pars_vertex}
75
+ varying vec2 vUv;
76
+ void main() {
77
+ vUv = uv;
78
+ gl_Position = vec4(position, 1.0);
79
+ ${ShaderChunk.logdepthbuf_vertex}
80
+ }
81
+ `,
82
+ fragmentShader: `
83
+ ${ShaderChunk.logdepthbuf_pars_fragment}
84
+ uniform sampler2D map1;
85
+ uniform sampler2D map2;
86
+ varying vec2 vUv;
87
+ void main() {
88
+ if(texture2D(map1, vUv) == texture2D(map2, vUv)){
89
+ gl_FragColor = texture2D(map1, vUv);
90
+ }else{
91
+ discard;
92
+ }
93
+ ${ShaderChunk.logdepthbuf_fragment}
94
+ }
95
+ `,
96
+ blending: NoBlending, // 渲染时不与背景融合计算 直接替换
97
+ depthTest: true,
98
+ depthWrite: true,
99
+ transparent: true
100
+ });
101
+
102
+
103
+ // 拷贝着色器
104
+ export const coptyShader = new ShaderMaterial({
105
+ uniforms: {
106
+ map: { value: null }, // 纹理
107
+ opacity: { value: 1.0 } // 透明度
108
+ },
109
+ vertexShader: `
110
+ varying vec2 vUv;
111
+ void main() {
112
+ vUv = uv;
113
+ gl_Position = vec4(position, 1.0);
114
+ }
115
+ `,
116
+ fragmentShader: `
117
+ uniform sampler2D map;
118
+ uniform float opacity;
119
+ varying vec2 vUv;
120
+ void main() {
121
+
122
+ gl_FragColor = texture2D(map, vUv) * opacity;
123
+
124
+ }
125
+ `,
126
+ // blending: NoBlending, // 渲染时不与背景融合计算 直接替换
127
+ depthTest: true,
128
+ depthWrite: true,
129
+ transparent: true,
130
+ });
131
+
132
+
133
+ // 描边着色器
134
+ export const strokeShader = new ShaderMaterial({
135
+ uniforms: {
136
+ map: { value: null }, // 贴图纹理
137
+ resolution: { value: new Vector2(512, 512) }, // 分辨率
138
+ strokeWidth: { value: 1.0 }, // 描边宽度
139
+ strokeColor: { value: new Color("#B2F2FF") }, // 描边颜色
140
+ opacity: { value: 1.0 } // 透明度
141
+ },
142
+ vertexShader: `
143
+ varying vec2 vUv;
144
+ void main() {
145
+ vUv = uv;
146
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
147
+ }
148
+ `,
149
+ fragmentShader: `
150
+ uniform sampler2D map;
151
+ uniform vec2 resolution;
152
+ uniform vec3 strokeColor; // 描边颜色
153
+ uniform float strokeWidth; // 描边厚度
154
+ uniform float opacity; // 透明度
155
+
156
+ varying vec2 vUv;
157
+
158
+ // 获取当前叠加vu坐标偏移量的颜色值
159
+ vec4 offsetColor(vec2 vu){
160
+ return texture2D(map, vUv + (vu * (strokeWidth / resolution) ) );
161
+ }
162
+
163
+ vec4 fragColor(){
164
+
165
+ vec4 currentColor = offsetColor(vec2(0.0, 0.0));
166
+
167
+ if(
168
+ currentColor.a < min(0.2, opacity) && (
169
+ offsetColor(vec2(0.0, 1.0)).a > 0.0 ||
170
+ offsetColor(vec2(0.0, -1.0)).a > 0.0 ||
171
+ offsetColor(vec2(1.0, 0.0)).a > 0.0 ||
172
+ offsetColor(vec2(-1.0, 0.0)).a > 0.0 ||
173
+
174
+ offsetColor(vec2(1.0) * 0.7).a > 0.0 ||
175
+ offsetColor(vec2(-1.0) * 0.7).a > 0.0 ||
176
+ offsetColor(vec2(-1.0, 1.0) * 0.7).a > 0.0 ||
177
+ offsetColor(vec2(1.0, -1.0) * 0.7).a > 0.0
178
+ )
179
+ ){
180
+ return vec4( strokeColor, opacity);
181
+ }else{
182
+ // discard;
183
+ return offsetColor(vec2(0.0, 0.0));
184
+ }
185
+
186
+ }
187
+
188
+ void main() {
189
+ gl_FragColor = fragColor();
190
+ }
191
+ `,
192
+ blending: NoBlending, // 渲染时不与背景融合计算 直接替换
193
+ depthTest: true,
194
+ depthWrite: true,
195
+ transparent: true
196
+ });
197
+
198
+
199
+ // 高斯模糊着色器 让颜色过渡更自然
200
+ export const gaussBlurShader = new ShaderMaterial({
201
+ uniforms: {
202
+ map: { value: null }, // 纹理
203
+ opacity: { value: 1.0 }, // 透明度
204
+ radius: { value: 3.0 }, // 模糊半径
205
+ resolution: { value: new Vector2(512, 512) }, // 渲染器的分辨率
206
+ },
207
+ vertexShader: `
208
+ varying vec2 vUv;
209
+ void main() {
210
+ vUv = uv;
211
+ gl_Position = vec4(position, 1.0);
212
+ }
213
+ `,
214
+ fragmentShader: `
215
+ uniform sampler2D map;
216
+ uniform float opacity;
217
+ uniform vec2 resolution;
218
+ uniform float radius;
219
+ varying vec2 vUv;
220
+
221
+ vec4 offsetColor( vec2 uv){
222
+ return texture2D(map, vUv + (uv / resolution * radius) );
223
+ }
224
+
225
+ vec4 blurColor(){
226
+
227
+ vec4 leftColor = offsetColor( vec2(-1.0, 0.0) );
228
+
229
+ vec4 rightColor = offsetColor( vec2(1.0, 0.0) );
230
+
231
+ // 左右颜色混合
232
+ vec4 leftRightColor = vec4(
233
+ mix( leftColor.rgb ,rightColor.rgb, rightColor.a / (leftColor.a + rightColor.a)),
234
+ (leftColor.a + rightColor.a) / 2.0
235
+ );
236
+
237
+ vec4 downColor = offsetColor( vec2(0.0, -1.0));
238
+
239
+ vec4 upColor = offsetColor( vec2(0.0, 1.0));
240
+
241
+ // 上下颜色混合
242
+ vec4 upDownColor = vec4(
243
+ mix( downColor.rgb, upColor.rgb, upColor.a / (downColor.a + upColor.a) ),
244
+ (downColor.a + upColor.a) / 2.0
245
+ );
246
+
247
+ vec4 color = vec4(
248
+ mix( leftRightColor.rgb, upDownColor.rgb, upDownColor.a / (leftRightColor.a + upDownColor.a) ),
249
+ (leftRightColor.a + upDownColor.a) / 2.0
250
+ );
251
+
252
+ return color;
253
+ }
254
+
255
+ void main() {
256
+ gl_FragColor = blurColor();
257
+ gl_FragColor.a *= opacity;
258
+ }
259
+ `,
260
+ blending: NoBlending, // 渲染时不与背景融合计算
261
+ depthTest: false,
262
+ depthWrite: false,
263
+ transparent: true
264
+ });
@@ -0,0 +1,160 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-03-08 00:01:34
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-03-08 02:05:25
6
+ */
7
+
8
+
9
+
10
+
11
+
12
+ import { Sprite, CanvasTexture, Vector2, Raycaster, Intersection } from "three";
13
+ import spriteModal1PNG from "./spriteModal1.png";
14
+
15
+ export type SpritModal1Config = {
16
+ size?: number,
17
+ title?: string,
18
+ titleColor?: string,
19
+ texts?: Array<{
20
+ text?: string,
21
+ color?: string,
22
+ size?: number
23
+ }>
24
+ }
25
+
26
+ // sprit弹窗1
27
+ export default class SpritModal1 extends Sprite {
28
+
29
+ constructor(config: SpritModal1Config = {}) {
30
+ super();
31
+ this.updata(config);
32
+ this.material.map = this.texture;
33
+ this.material.transparent = true;
34
+ this.material.alphaTest = 0.01;
35
+ }
36
+
37
+ renderOrder = 99;
38
+
39
+ // 背景图
40
+ static img = (() => {
41
+ const img = new Image();
42
+ img.src = spriteModal1PNG;
43
+ return img;
44
+ })();
45
+
46
+ canvas = (() => {
47
+ const canvas = document.createElement("canvas");
48
+ canvas.width = 1024;
49
+ canvas.height = 1024;
50
+ return canvas;
51
+ })();
52
+
53
+ context = this.canvas.getContext("2d")!;
54
+
55
+ texture = new CanvasTexture(this.canvas);
56
+
57
+ // 配置
58
+ _config: {
59
+ size: number,
60
+ title: string,
61
+ titleColor: string,
62
+ texts: Array<{
63
+ text: string,
64
+ color: string,
65
+ size: number
66
+ }>
67
+ } = {
68
+ size: 10,
69
+ title: "SpritModal1",
70
+ titleColor: "#ffffff",
71
+ texts: []
72
+ // [
73
+ // {
74
+ // text: "1",
75
+ // color: "#ffff00",
76
+ // size: 40
77
+ // },
78
+ // {
79
+ // text: "2",
80
+ // color: "#ff00ff",
81
+ // size: 40
82
+ // },
83
+ // ]
84
+ }
85
+
86
+ async updata(config: SpritModal1Config) {
87
+
88
+ Object.assign(this._config, config)
89
+
90
+ //绘制背景
91
+ this.context.clearRect(0, 0, 1024, 1024);
92
+
93
+ while (!SpritModal1.img.complete) {
94
+ await new Promise(resolve => setTimeout(resolve, 100));
95
+ }
96
+
97
+ // 设置大小
98
+ this.scale.set(this._config.size, this._config.size, this._config.size);
99
+
100
+ // this.context.fillStyle = "#00000088";
101
+ // this.context.fillRect(0, 0, 1024, 1024);
102
+
103
+ //绘制背景
104
+ this.context.drawImage(SpritModal1.img, 186, 0);
105
+
106
+ // 绘制标题
107
+ this.context.fillStyle = this._config.titleColor;
108
+ this.context.font = '40px serif';// 设置字体大小
109
+ this.context.textAlign = "left";
110
+ this.context.textBaseline = "top";
111
+ this.context.fillText(this._config.title, 250, 72);
112
+
113
+ let textPos = new Vector2(250, 150);
114
+ let maxWidth = 530;
115
+ // 绘制内容
116
+ this._config.texts.map(({ text = "text", color = "#ffffff", size = 40 }) => {
117
+ this.context.fillStyle = color;
118
+ this.context.font = `${size}px serif`;// 设置字体大小
119
+ this.context.textAlign = "left";
120
+ this.context.textBaseline = "top";
121
+
122
+ // 字符分隔为数组
123
+ var arrText = text.split('');
124
+ var line = '';
125
+
126
+ // 文字自动换行计算
127
+ for (var n = 0; n < arrText.length; n++) {
128
+ var metrics = this.context.measureText(line + arrText[n]);
129
+ var testWidth = metrics.width;
130
+ if ((testWidth > maxWidth && n > 0) || arrText[n] === "\n") {
131
+ this.context.fillText(line, textPos.x, textPos.y);
132
+ line = "";
133
+ textPos.y += size;
134
+ } else {
135
+ line += arrText[n];
136
+ }
137
+ }
138
+
139
+ this.context.fillText(line, textPos.x, textPos.y);
140
+ textPos.y += size;
141
+ });
142
+
143
+ this.texture.needsUpdate = true;
144
+
145
+ }
146
+
147
+ //鼠标拾取计算
148
+ raycast(raycaster: Raycaster, intersects: Intersection[]) {
149
+
150
+ let intersect: Intersection[] = [];
151
+ super.raycast(raycaster, intersect);
152
+ let inte = intersect[0];
153
+ if (inte && (inte.uv!.x > 0.22 && inte.uv!.x < 0.78) && (inte.uv!.y > 0.54 && inte.uv!.y < 0.94)) {
154
+ intersects.push(inte);
155
+ }
156
+
157
+ }
158
+
159
+ }
160
+
@@ -0,0 +1,136 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-04-07 16:42:26
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-03-30 10:52:27
6
+ */
7
+
8
+ import { DefaultLoadingManager, EventDispatcher, RepeatWrapping, Texture, TextureLoader, VideoTexture } from "three";
9
+ import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
10
+ import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
11
+ import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
12
+ import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
13
+ import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
14
+
15
+ // 引入静态资源
16
+ //@ts-ignore
17
+ import dracoWasmWrapperJS from "./draco/draco_wasm_wrapper.js?url";
18
+ //@ts-ignore
19
+ import dracoDecoderJS from "./draco/draco_decoder.js?url";
20
+ //@ts-ignore
21
+ import dracoDecoderWASM from "./draco/draco_decoder.wasm?url";
22
+
23
+ // 加载器
24
+ class ThreeLoader extends EventDispatcher {
25
+
26
+ constructor() {
27
+ super();
28
+
29
+ DefaultLoadingManager.onStart = () => {
30
+ this.onload = false;
31
+ }
32
+
33
+ // 有资源加载完成时通知渲染器
34
+ DefaultLoadingManager.onProgress = () => {
35
+ this.dispatchEvent({ type: "progress" });
36
+ }
37
+
38
+ DefaultLoadingManager.onLoad = () => {
39
+ this.onload = true;
40
+ }
41
+ }
42
+
43
+ // 各向异性
44
+ anisotropy = 1;
45
+
46
+ // 设置各向异性 由渲染器来调用并设置
47
+ setAnisotropy(v: number) {
48
+ this.anisotropy = v;
49
+ };
50
+
51
+ // 纹理对向缓存
52
+ textureMap = new Map() as Map<string, Texture>;
53
+
54
+ // 纹理加载器
55
+ textureLoader = new TextureLoader();
56
+
57
+ // 获取地址对应的单例纹理对象
58
+ getTexture(src: string) {
59
+
60
+ let texture = this.textureMap.get(src);
61
+
62
+ if (!texture) {
63
+ if (/\.mp4$/.test(src)) {
64
+ const videoDom = document.createElement("video");
65
+ videoDom.autoplay = true;
66
+ videoDom.loop = true;
67
+ videoDom.muted = true;
68
+ videoDom.preload = "auto";
69
+ videoDom.playsInline = true;
70
+ videoDom.src = src;
71
+ document.addEventListener("pointerup", () => {
72
+ videoDom.pause();
73
+ videoDom.play();
74
+ });
75
+ texture = new VideoTexture(videoDom);
76
+ } else {
77
+
78
+ texture = this.textureLoader.load(src, tex => {
79
+ tex.image.crossOrigin = "anonymous";
80
+ });
81
+ }
82
+
83
+ this.textureMap.set(src, texture);
84
+ }
85
+
86
+ texture.wrapS = texture.wrapT = RepeatWrapping;
87
+
88
+ texture.anisotropy = this.anisotropy;
89
+
90
+ return texture as Texture;
91
+ };
92
+
93
+ // fbx模型加载器
94
+ fbxLoader = new FBXLoader();
95
+
96
+ //obj 加载器
97
+ objLoader = new OBJLoader();
98
+
99
+ // 材质加载器
100
+ mtlLoader = new MTLLoader();
101
+
102
+ // gltf 加载器
103
+ gltfLoader = (() => {
104
+ const loader = new GLTFLoader();
105
+ const dracoLoader = new class ThreeBaseDRACOLoader extends DRACOLoader {
106
+
107
+ _loadLibrary(url: string, responseType: string) {
108
+ //@ts-ignore
109
+ let urlConf = (process.env.NODE_ENV === "development") ? {
110
+ "draco_decoder.js": "draco_decoder.js",
111
+ "draco_wasm_wrapper.js": "draco_wasm_wrapper.js",
112
+ "draco_decoder.wasm": "draco_decoder.wasm"
113
+ } : {
114
+ "draco_decoder.js": dracoWasmWrapperJS,
115
+ "draco_wasm_wrapper.js": dracoDecoderJS,
116
+ "draco_decoder.wasm": dracoDecoderWASM
117
+ }
118
+ //@ts-ignore
119
+ return super._loadLibrary(urlConf[url], responseType);
120
+ }
121
+
122
+ }();
123
+ //@ts-ignore
124
+ dracoLoader.setDecoderPath((process.env.NODE_ENV === "development") ? '/node_modules/three/examples/js/libs/draco/' : '');
125
+ loader.setDRACOLoader(dracoLoader);
126
+ return loader;
127
+ })()
128
+
129
+ // 是否加载完成
130
+ onload = true;
131
+
132
+ }
133
+
134
+ const threeLoader = new ThreeLoader();
135
+ export default threeLoader;
136
+