three-base 2.0.87 → 2.0.88

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.ts DELETED
@@ -1,254 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-08-04 17:17:15
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-01-20 05:33:51
6
- */
7
-
8
- import { BufferGeometry, Camera, Color, DoubleSide, Group, HalfFloatType, Material, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, Scene, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer } from "three";
9
-
10
-
11
- // 镜子模型
12
- export default class Mirror extends Mesh {
13
-
14
- constructor(geometry: BufferGeometry = Mirror.geometry) {
15
- super(geometry);
16
- }
17
-
18
- // 正方形网格
19
- static geometry = (() => {
20
- const geometry = new PlaneGeometry(10, 10, 1, 1);
21
- return geometry;
22
- })();
23
-
24
- isReflector = true;
25
-
26
- type = 'Reflector';
27
-
28
- camera = new PerspectiveCamera();
29
-
30
- reflectorPlane = new Plane();
31
- normal = new Vector3();
32
- reflectorWorldPosition = new Vector3();
33
- cameraWorldPosition = new Vector3();
34
- rotationMatrix = new Matrix4();
35
- lookAtPosition = new Vector3(0, 0, - 1);
36
- clipPlane = new Vector4();
37
-
38
- view = new Vector3();
39
- target = new Vector3();
40
- q = new Vector4();
41
-
42
- textureMatrix = new Matrix4();
43
- virtualCamera = this.camera;
44
-
45
- renderTarget = new WebGLRenderTarget(2048, 2048, { samples: 0, type: HalfFloatType });
46
-
47
- material = new ShaderMaterial({
48
- uniforms: {
49
- 'color': {
50
- value: new Color("#b6b6b6")
51
- },
52
-
53
- 'tDiffuse': {
54
- value: this.renderTarget.texture
55
- },
56
-
57
- 'textureMatrix': {
58
- value: this.textureMatrix
59
- },
60
- blurAmount: {
61
- value: 0.5
62
- } // 模糊程度,可以根据需求调整
63
-
64
- },
65
-
66
- vertexShader: /* glsl */`
67
- uniform mat4 textureMatrix;
68
- varying vec4 vUv;
69
-
70
- #include <common>
71
- #include <logdepthbuf_pars_vertex>
72
-
73
- void main() {
74
-
75
- vUv = textureMatrix * vec4( position, 1.0 );
76
-
77
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
78
-
79
- #include <logdepthbuf_vertex>
80
-
81
- }`,
82
-
83
- fragmentShader: /* glsl */`
84
- uniform vec3 color;
85
- uniform sampler2D tDiffuse;
86
- varying vec4 vUv;
87
- uniform float blurAmount;
88
-
89
- #include <logdepthbuf_pars_fragment>
90
-
91
- float blendOverlay( float base, float blend ) {
92
-
93
- return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );
94
-
95
- }
96
-
97
- vec3 blendOverlay( vec3 base, vec3 blend ) {
98
-
99
- return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );
100
-
101
- }
102
-
103
- void main() {
104
-
105
- #include <logdepthbuf_fragment>
106
-
107
- vec2 texelSize = 1.0 / vec2(1024,1024);
108
- vec4 fragColor = vec4(0.0);
109
-
110
- for (float offsetX = -blurAmount; offsetX <= blurAmount; offsetX += texelSize.x) {
111
- for (float offsetY = -blurAmount; offsetY <= blurAmount; offsetY += texelSize.y) {
112
- // fragColor += texture2DProj(tDiffuse, vUv + vec4(offsetX, offsetY, 0.0, 0.0) * texelSize);
113
- fragColor += texture2DProj(tDiffuse, vUv + vec4(offsetX * texelSize.x, offsetY * texelSize.y, 0.0, 0.0) );
114
- }
115
- }
116
-
117
- // fragColor /= pow(2.0 * blurAmount + 1.0, 2.0);
118
- // gl_FragColor = color;
119
-
120
- fragColor = texture2DProj( tDiffuse, vUv );
121
- gl_FragColor = vec4( blendOverlay( fragColor.rgb, color ), 1.0 );
122
-
123
- #include <tonemapping_fragment>
124
- #include <colorspace_fragment>
125
- }`,
126
- depthTest: true,
127
- depthWrite: true,
128
- transparent: true,
129
- side: DoubleSide
130
- });
131
-
132
-
133
- onBeforeRender = (renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group) => {
134
-
135
- this.reflectorWorldPosition.setFromMatrixPosition(this.matrixWorld);
136
- this.cameraWorldPosition.setFromMatrixPosition(camera.matrixWorld);
137
-
138
- this.rotationMatrix.extractRotation(this.matrixWorld);
139
-
140
- this.normal.set(0, 0, 1);
141
- this.normal.applyMatrix4(this.rotationMatrix);
142
-
143
- this.view.subVectors(this.reflectorWorldPosition, this.cameraWorldPosition);
144
-
145
- // Avoid rendering when reflector is facing away
146
-
147
- if (this.view.dot(this.normal) > 0) {
148
- this.normal.set(0, 0, -1);
149
- };
150
-
151
- this.view.reflect(this.normal).negate();
152
- this.view.add(this.reflectorWorldPosition);
153
-
154
- this.rotationMatrix.extractRotation(camera.matrixWorld);
155
-
156
- this.lookAtPosition.set(0, 0, - 1);
157
- this.lookAtPosition.applyMatrix4(this.rotationMatrix);
158
- this.lookAtPosition.add(this.cameraWorldPosition);
159
-
160
- this.target.subVectors(this.reflectorWorldPosition, this.lookAtPosition);
161
- this.target.reflect(this.normal).negate();
162
- this.target.add(this.reflectorWorldPosition);
163
-
164
- this.virtualCamera.position.copy(this.view);
165
- this.virtualCamera.up.set(0, 1, 0);
166
- this.virtualCamera.up.applyMatrix4(this.rotationMatrix);
167
- this.virtualCamera.up.reflect(this.normal);
168
- this.virtualCamera.lookAt(this.target);
169
-
170
- this.virtualCamera.far = this.camera.far; // Used in WebGLBackground
171
-
172
- this.virtualCamera.updateMatrixWorld();
173
- this.virtualCamera.projectionMatrix.copy(camera.projectionMatrix);
174
-
175
- // Update the texture matrix
176
- this.textureMatrix.set(
177
- 0.5, 0.0, 0.0, 0.5,
178
- 0.0, 0.5, 0.0, 0.5,
179
- 0.0, 0.0, 0.5, 0.5,
180
- 0.0, 0.0, 0.0, 1.0
181
- );
182
- this.textureMatrix.multiply(this.virtualCamera.projectionMatrix);
183
- this.textureMatrix.multiply(this.virtualCamera.matrixWorldInverse);
184
- this.textureMatrix.multiply(this.matrixWorld);
185
-
186
- // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
187
- // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
188
- this.reflectorPlane.setFromNormalAndCoplanarPoint(this.normal, this.reflectorWorldPosition);
189
- this.reflectorPlane.applyMatrix4(this.virtualCamera.matrixWorldInverse);
190
-
191
- this.clipPlane.set(this.reflectorPlane.normal.x, this.reflectorPlane.normal.y, this.reflectorPlane.normal.z, this.reflectorPlane.constant);
192
-
193
- const projectionMatrix = this.virtualCamera.projectionMatrix;
194
-
195
- this.q.x = (Math.sign(this.clipPlane.x) + projectionMatrix.elements[8]) / projectionMatrix.elements[0];
196
- this.q.y = (Math.sign(this.clipPlane.y) + projectionMatrix.elements[9]) / projectionMatrix.elements[5];
197
- this.q.z = - 1.0;
198
- this.q.w = (1.0 + projectionMatrix.elements[10]) / projectionMatrix.elements[14];
199
-
200
- // Calculate the scaled plane vector
201
- this.clipPlane.multiplyScalar(2.0 / this.clipPlane.dot(this.q));
202
-
203
- // Replacing the third row of the projection matrix
204
- projectionMatrix.elements[2] = this.clipPlane.x;
205
- projectionMatrix.elements[6] = this.clipPlane.y;
206
- projectionMatrix.elements[10] = this.clipPlane.z + 1.0 - 0.003;
207
- projectionMatrix.elements[14] = this.clipPlane.w;
208
-
209
- // Render
210
- this.visible = false;
211
-
212
- const currentRenderTarget = renderer.getRenderTarget();
213
-
214
- const currentXrEnabled = renderer.xr.enabled;
215
- const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
216
-
217
- renderer.xr.enabled = false; // Avoid camera modification
218
- renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
219
-
220
- renderer.setRenderTarget(this.renderTarget);
221
-
222
- renderer.state.buffers.depth.setMask(true); // make sure the depth buffer is writable so it can be properly cleared, see #18897
223
-
224
- if (renderer.autoClear === false) renderer.clear();
225
- renderer.render(scene, this.virtualCamera);
226
-
227
- renderer.xr.enabled = currentXrEnabled;
228
- renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
229
-
230
- renderer.setRenderTarget(currentRenderTarget);
231
-
232
- // Restore viewport
233
- //@ts-ignore
234
- const viewport = this.camera.viewport;
235
- if (viewport !== undefined) {
236
- renderer.state.viewport(viewport);
237
- }
238
-
239
- this.visible = true;
240
-
241
- }
242
-
243
- getRenderTarget() {
244
-
245
- return this.renderTarget;
246
-
247
- };
248
-
249
- dispose() {
250
- this.renderTarget.dispose();
251
- // this.material.dispose();
252
- };
253
-
254
- }
@@ -1,151 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-03-09 18:51:11
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-03-09 19:15:35
6
- */
7
-
8
- import { DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector2 } from "three";
9
- import threeLoader from "../threeLoader";
10
- import planeModalPNG from "./planeModal.png";
11
-
12
-
13
- export type PlaneModal1Config = {
14
- size?: number,
15
- title?: string,
16
- titleColor?: string,
17
- texts?: Array<{
18
- text?: string,
19
- color?: string,
20
- size?: number
21
- }>
22
- }
23
-
24
- // 平面模型弹窗
25
- export default class PlaneModal1 extends Mesh {
26
-
27
- constructor(config: PlaneModal1Config = {}) {
28
- super();
29
- this.updata(config);
30
- this.material.map = this.texture;
31
- this.scale.set(10, 10, 10);
32
- }
33
-
34
- geometry = new PlaneGeometry(1 * 0.65, 0.728 * 0.65, 1, 1);
35
-
36
- material = new MeshBasicMaterial({
37
- side: DoubleSide,
38
- transparent: true,
39
- alphaTest: 0.01
40
- });
41
-
42
- // 背景图
43
- static img = (() => {
44
- const img = new Image();
45
- img.src = planeModalPNG;
46
- return img;
47
- })();
48
-
49
- canvas = (() => {
50
- const canvas = document.createElement("canvas");
51
- canvas.width = 660;
52
- canvas.height = 481;
53
- return canvas;
54
- })();
55
-
56
- context = this.canvas.getContext("2d")!;
57
-
58
- texture = threeLoader.getTexture(this.canvas);
59
-
60
- // 配置
61
- _config: {
62
- size: number,
63
- title: string,
64
- titleColor: string,
65
- texts: Array<{
66
- text: string,
67
- color: string,
68
- size: number
69
- }>
70
- } = {
71
- size: 10,
72
- title: "PlaneModal1",
73
- titleColor: "#ffffff",
74
- texts: []
75
- // [
76
- // {
77
- // text: "1",
78
- // color: "#ffff00",
79
- // size: 40
80
- // },
81
- // {
82
- // text: "2",
83
- // color: "#ff00ff",
84
- // size: 40
85
- // },
86
- // ]
87
- }
88
-
89
- async updata(config: PlaneModal1Config) {
90
-
91
- Object.assign(this._config, config)
92
-
93
- //绘制背景
94
- this.context.clearRect(0, 0, 660, 481);
95
-
96
- while (!PlaneModal1.img.complete) {
97
- await new Promise(resolve => setTimeout(resolve, 100));
98
- }
99
-
100
- // 设置大小
101
- this.scale.set(this._config.size, this._config.size, this._config.size);
102
-
103
- // this.context.fillStyle = "#00000088";
104
- // this.context.fillRect(0, 0, 660, 481);
105
-
106
- //绘制背景
107
- this.context.drawImage(PlaneModal1.img, 0, 0);
108
-
109
- // 绘制标题
110
- this.context.fillStyle = this._config.titleColor;
111
- this.context.font = '40px serif';// 设置字体大小
112
- this.context.textAlign = "left";
113
- this.context.textBaseline = "top";
114
- this.context.fillText(this._config.title, 60, 72);
115
-
116
- let textPos = new Vector2(60, 150);
117
- let maxWidth = 530;
118
- // 绘制内容
119
- this._config.texts.map(({ text = "text", color = "#ffffff", size = 40 }) => {
120
- this.context.fillStyle = color;
121
- this.context.font = `${size}px serif`;// 设置字体大小
122
- this.context.textAlign = "left";
123
- this.context.textBaseline = "top";
124
-
125
- // 字符分隔为数组
126
- var arrText = text.split('');
127
- var line = '';
128
-
129
- for (var n = 0; n < arrText.length; n++) {
130
- var metrics = this.context.measureText(line + arrText[n]);
131
- var testWidth = metrics.width;
132
- if ((testWidth > maxWidth && n > 0) || arrText[n] === "\n") {
133
- this.context.fillText(line, textPos.x, textPos.y);
134
- line = "";
135
- textPos.y += size;
136
- } else {
137
- line += arrText[n];
138
- }
139
- }
140
-
141
- this.context.fillText(line, textPos.x, textPos.y);
142
- textPos.y += size;
143
- });
144
-
145
- this.texture.needsUpdate = true;
146
-
147
- this.dispatchEvent({ type: "loaded" } as never);
148
- }
149
-
150
-
151
- }
@@ -1 +0,0 @@
1
- export { default } from "./PlaneModal1";
package/PlaneText.ts DELETED
@@ -1,58 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2022-05-23 13:53:06
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-05-03 02:54:52
6
- */
7
-
8
- import { DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector3 } from "three";
9
- import SpriteText from "./SpriteText";
10
-
11
-
12
- type Props = {
13
- text?: string;
14
- textHeight?: number;
15
- color?: string;
16
- backgroundColor?: string;
17
- fontFace?: string;
18
- fontSize?: number;
19
- fontWeight?: string;
20
- padding?: number | [number, number];
21
- borderWidth?: number;
22
- borderRadius?: number;
23
- borderColor?: string;
24
- strokeWidth?: number;
25
- strokeColor?: string;
26
- }
27
-
28
- export default class PlaneText extends Mesh {
29
-
30
- constructor(text: string = "planeText") {
31
- super(PlaneText.geometry);
32
- this.set({ text });
33
- }
34
-
35
- type = "PlaneText";
36
-
37
- static geometry = new PlaneGeometry(1, 1, 1, 1);
38
-
39
- spriteText = new SpriteText("");
40
-
41
- material = new MeshBasicMaterial({ transparent: true, side: DoubleSide, map: this.spriteText.material.map });
42
-
43
- set(parame: Props) {
44
- for (let key in parame) {
45
- //@ts-ignore
46
- this.spriteText[key] = parame[key];
47
- }
48
-
49
- this.scale.copy(this.spriteText.scale);
50
- this.scale.max(new Vector3(1, 1, 1));
51
- this.material.map = this.spriteText.material.map;
52
- }
53
-
54
- get(prop: keyof SpriteText) {
55
- return this.spriteText[prop];
56
- }
57
-
58
- }
package/PlaneWithHtml.ts DELETED
@@ -1,85 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-04-15 11:26:32
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-04-15 12:38:09
6
- */
7
-
8
- import html2canvas from "html2canvas";
9
- import { CanvasTexture, DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector2 } from "three";
10
- import { degToRad } from "three/src/math/MathUtils";
11
- import threeLoader from "./threeLoader";
12
-
13
-
14
- // html 文本面板
15
- export default class PlaneWithHtml extends Mesh {
16
-
17
- constructor(html: string = "", backgroundColor: `#${string}` = "#00000000") {
18
- super();
19
- this.setHtml(html, backgroundColor);
20
- }
21
-
22
- visible = false;
23
-
24
- size = new Vector2(10, 10);
25
-
26
- geometry = (() => {
27
- const geometry = new PlaneGeometry(10, 10, 1, 1);
28
- geometry.rotateX(degToRad(-90));
29
- return geometry;
30
- })();
31
-
32
- material = new MeshBasicMaterial({
33
- side: DoubleSide,
34
- transparent: true,
35
- alphaTest: 0.1
36
- });
37
-
38
- div = (() => {
39
- const div = document.createElement("div");
40
- div.style.position = "fixed";
41
- div.style.top = "100%";
42
- div.style.left = "100%";
43
- div.style.pointerEvents = "none";
44
- div.style.userSelect = "none";
45
- div.style.lineHeight = "1";
46
- div.style.whiteSpace = "nowrap";
47
- return div;
48
- })();
49
-
50
- // 设置网格尺寸
51
- setGeoSize(width: number, height: number) {
52
- this.size.set(width, height);
53
- this.geometry.dispose();
54
- const geometry = new PlaneGeometry(width, height, 1, 1);
55
- geometry.rotateX(degToRad(-90));
56
- this.geometry = geometry;
57
- }
58
-
59
- async setHtml(html: string = "", backgroundColor: `#${string}` = "#00000000") {
60
- this.div.innerHTML = html;
61
- document.body.appendChild(this.div);
62
-
63
- await new Promise<void>((resolve, reject) => {
64
-
65
- html2canvas(this.div, {
66
- backgroundColor,
67
- removeContainer: true
68
- }).then(async (canvas) => {
69
- this.setGeoSize(canvas.width, canvas.height);
70
- this.material.map = new CanvasTexture(canvas);
71
- this.material.map.anisotropy = threeLoader.anisotropy;
72
- this.material.needsUpdate = true;
73
- await new Promise(resolve => requestAnimationFrame(resolve));
74
- this.visible = true;
75
- this.div.remove();
76
- resolve();
77
- });
78
-
79
- });
80
-
81
- this.dispatchEvent({ type: "loaded" } as never);
82
- }
83
-
84
-
85
- }
@@ -1,75 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2022-05-18 16:02:55
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-05-03 02:55:28
6
- */
7
-
8
- import { Sprite, PointLight as THREEPointLight } from "three";
9
- import threeLoader from "../threeLoader";
10
- import threeUtils from "../threeUtils";
11
- import pointLight_mapPNG from "./pointLight_map.png";
12
- import SpriteText from "../SpriteText";
13
-
14
-
15
- /**
16
- * 点光源
17
- * @export
18
- * @class PointLight
19
- * @extends {THREEPointLight}
20
- */
21
- export default class PointLight extends THREEPointLight {
22
-
23
- constructor(color = "#fff", intensity?: number, distance?: number, decay?: number) {
24
- super(color, intensity, distance, decay);
25
- }
26
-
27
- // 灯光辅助器
28
- pointLinghtHelper = threeUtils.isDev && (() => {
29
- const pointLinghtHelper = new Sprite();
30
- pointLinghtHelper.scale.set(0.05, 0.05, 0.05);
31
- pointLinghtHelper.material.alphaTest = 0.1;
32
- pointLinghtHelper.material.color = this.color;
33
- pointLinghtHelper.material.map = threeLoader.getTexture(pointLight_mapPNG);
34
- pointLinghtHelper.material.sizeAttenuation = false;
35
-
36
- pointLinghtHelper.userData = {
37
- cursor: "pointer"
38
- };
39
-
40
- pointLinghtHelper.addEventListener("mouseenter", () => {
41
- if (this.label) {
42
- this.label.visible = true;
43
- }
44
- });
45
-
46
- pointLinghtHelper.addEventListener("mouseleave", () => {
47
- if (this.label) {
48
- this.label.visible = false;
49
- }
50
- });
51
-
52
- this.add(pointLinghtHelper);
53
- return pointLinghtHelper;
54
- })();
55
-
56
- label = threeUtils.isDev && (() => {
57
- const label = new SpriteText("点光源\n\n\n");
58
- label.renderOrder = Infinity;
59
- // label.backgroundColor = "rgba(0, 0, 0, 0.5)";
60
- label.padding = 4;
61
- label.strokeColor = "#333";
62
- label.strokeWidth = 2;
63
- // label.borderRadius = 4;
64
- // label.borderWidth = 1;
65
- label.position.set(0, 0, 0);
66
- label.visible = false;
67
- label.material.color = this.color;
68
- // label.material.depthTest = false;
69
- label.material.sizeAttenuation = false;
70
- label.scale.multiplyScalar(0.002);
71
- this.add(label);
72
- return label;
73
- })();
74
-
75
- }
@@ -1 +0,0 @@
1
- export { default } from "./PointLight";
package/Sky/Sky.ts DELETED
@@ -1,36 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2022-05-20 18:03:58
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2022-12-27 09:54:23
6
- */
7
-
8
- import { DoubleSide, Mesh, MeshBasicMaterial, SphereGeometry } from "three";
9
- import threeLoader from "../threeLoader";
10
-
11
- import blueskyJPG from "./blueSky.jpg";
12
-
13
- /**
14
- * 天空球
15
- */
16
- export default class Sky extends Mesh {
17
-
18
- constructor() {
19
- super(Sky.geometry);
20
- }
21
-
22
- static geometry = new SphereGeometry(5000, 100, 100);
23
-
24
- material = new MeshBasicMaterial({
25
- side: DoubleSide,
26
- map: threeLoader.getTexture(blueskyJPG)
27
- });
28
-
29
- /**
30
- *设置贴图
31
- */
32
- setMap(src: string) {
33
- this.material.map = threeLoader.getTexture(src);
34
- }
35
-
36
- }
package/Sky/index.ts DELETED
@@ -1 +0,0 @@
1
- export { default } from "./Sky";