lythreeframe 1.0.0

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.
Files changed (68) hide show
  1. package/lythreeFrame/exporter.ts +36 -0
  2. package/lythreeFrame/src/AssetManagement/AssetDefines.ts +8 -0
  3. package/lythreeFrame/src/AssetManagement/AssetManager.ts +286 -0
  4. package/lythreeFrame/src/AssetManagement/AssetPointer/AssetPointer.ts +41 -0
  5. package/lythreeFrame/src/AssetManagement/Task/Task.ts +24 -0
  6. package/lythreeFrame/src/Container/SmartPointer.ts +54 -0
  7. package/lythreeFrame/src/Defines.ts +11 -0
  8. package/lythreeFrame/src/Delegate.ts +37 -0
  9. package/lythreeFrame/src/Factory/CameraFactory.ts +81 -0
  10. package/lythreeFrame/src/Factory/RendererFactory.ts +7 -0
  11. package/lythreeFrame/src/Frame/Controller.ts +261 -0
  12. package/lythreeFrame/src/Frame/Viewport.ts +516 -0
  13. package/lythreeFrame/src/Frame/World.ts +59 -0
  14. package/lythreeFrame/src/Frame.ts +511 -0
  15. package/lythreeFrame/src/Geometry/PlaneGeometry.ts +53 -0
  16. package/lythreeFrame/src/Geometry/TriangleGeometry.ts +50 -0
  17. package/lythreeFrame/src/Library/ContainerLibrary.ts +21 -0
  18. package/lythreeFrame/src/Library/MaterialLibrary.ts +288 -0
  19. package/lythreeFrame/src/Library/Math.ts +154 -0
  20. package/lythreeFrame/src/Library/ResourceLibrary.ts +21 -0
  21. package/lythreeFrame/src/Object/Actor.ts +571 -0
  22. package/lythreeFrame/src/Object/Actors/Camera/CameraActor.ts +11 -0
  23. package/lythreeFrame/src/Object/Actors/Light/DirectionalLightActor.ts +18 -0
  24. package/lythreeFrame/src/Object/Actors/Shape/BoxActor.ts +20 -0
  25. package/lythreeFrame/src/Object/Actors/Shape/CurveActor.ts +11 -0
  26. package/lythreeFrame/src/Object/Actors/Shape/PlaneActor.ts +27 -0
  27. package/lythreeFrame/src/Object/Actors/Shape/TubeActor.ts +37 -0
  28. package/lythreeFrame/src/Object/BaseObject.ts +45 -0
  29. package/lythreeFrame/src/Object/Components/2D/2DComponent.ts +64 -0
  30. package/lythreeFrame/src/Object/Components/Camera/CameraComponent.ts +113 -0
  31. package/lythreeFrame/src/Object/Components/Component.ts +67 -0
  32. package/lythreeFrame/src/Object/Components/Light/DirectionalLight/DirectionalLightComponent.ts +78 -0
  33. package/lythreeFrame/src/Object/Components/Light/LightComponent.ts +54 -0
  34. package/lythreeFrame/src/Object/Components/Mesh/InstanceMesh/InstanceMeshComponent.ts +39 -0
  35. package/lythreeFrame/src/Object/Components/Mesh/InstanceMesh/MultiInstanceMeshComponent.ts +115 -0
  36. package/lythreeFrame/src/Object/Components/Mesh/Line/CurveComponent.ts +221 -0
  37. package/lythreeFrame/src/Object/Components/Mesh/MeshComponent.ts +162 -0
  38. package/lythreeFrame/src/Object/Components/Mesh/Shape/BoxComponent.ts +17 -0
  39. package/lythreeFrame/src/Object/Components/Mesh/Shape/PlaneComponent.ts +10 -0
  40. package/lythreeFrame/src/Object/Components/Mesh/Shape/SphereComponent.ts +10 -0
  41. package/lythreeFrame/src/Object/Components/Mesh/Sprite/SpriteComponent.ts +32 -0
  42. package/lythreeFrame/src/Object/Components/SceneComponent.ts +809 -0
  43. package/lythreeFrame/src/Object/Controller/Controller.ts +764 -0
  44. package/lythreeFrame/src/Object/Pawn/CameraStatus.ts +262 -0
  45. package/lythreeFrame/src/Object/Pawn/FirstPerson.ts +230 -0
  46. package/lythreeFrame/src/Object/Pawn/Oribital.ts +276 -0
  47. package/lythreeFrame/src/Object/Pawn/PawnBase.ts +189 -0
  48. package/lythreeFrame/src/Object/Pawn/TopView.ts +205 -0
  49. package/lythreeFrame/src/Object/Pawn/TransformControl.ts +215 -0
  50. package/lythreeFrame/src/Object/Pawn/staticCamera.ts +80 -0
  51. package/lythreeFrame/src/Object/PawnV2/FirstPerson.ts +12 -0
  52. package/lythreeFrame/src/Object/PawnV2/Oribital.ts +45 -0
  53. package/lythreeFrame/src/Object/PawnV2/Pawn.ts +50 -0
  54. package/lythreeFrame/src/Object/PawnV2/TransformControl.ts +201 -0
  55. package/lythreeFrame/src/PostProcess/Param/Bloom.ts +12 -0
  56. package/lythreeFrame/src/PostProcess/Param/DOF.ts +14 -0
  57. package/lythreeFrame/src/PostProcess/Param/Denoise.ts +14 -0
  58. package/lythreeFrame/src/PostProcess/Param/GTAO.ts +21 -0
  59. package/lythreeFrame/src/PostProcess/Param/LensFlare.ts +11 -0
  60. package/lythreeFrame/src/PostProcess/Param/MotionBlur.ts +8 -0
  61. package/lythreeFrame/src/PostProcess/Param/Outline.ts +20 -0
  62. package/lythreeFrame/src/PostProcess/Param/SSR.ts +11 -0
  63. package/lythreeFrame/src/PostProcess/Param/ToneMapping.ts +31 -0
  64. package/lythreeFrame/src/PostProcess/PostProcessParam.ts +26 -0
  65. package/lythreeFrame/src/PostProcess/WebGPUPostProcessFactory.ts +217 -0
  66. package/lythreeFrame/src/Shader/Postprocess/ColorShader.ts +49 -0
  67. package/lythreeFrame/src/ThreeJsApp.ts +124 -0
  68. package/package.json +21 -0
@@ -0,0 +1,288 @@
1
+ import {
2
+ AdditiveBlending,
3
+ Color, CustomBlending,
4
+ Material,
5
+ MeshStandardMaterial,
6
+ MultiplyBlending,
7
+ NoBlending,
8
+ NormalBlending,
9
+ RepeatWrapping,
10
+
11
+ SubtractiveBlending,
12
+ Texture,
13
+ TextureLoader,
14
+ Vector2
15
+ } from "three";
16
+ import {LYThreeWorld} from "@/script/scene/LYCustomFrame/src/Frame";
17
+ import {Blending} from "three/src/constants";
18
+ import {AssetType} from "../AssetManagement/AssetDefines.ts";
19
+
20
+ export class LYMaterialLibrary
21
+ {
22
+ static Deserialize(data, world: LYThreeWorld, onFinished:Function = ()=>{})
23
+ {
24
+ let mat = new MeshStandardMaterial();
25
+ LYMaterialLibrary.UpdateMatererialByData(data, mat, world, onFinished)
26
+ return mat
27
+ }
28
+
29
+ static UpdateMatererialByData(data, mat: MeshStandardMaterial, world: LYThreeWorld, onFinished:Function = ()=>{})
30
+ {
31
+ let params = data;
32
+ // color
33
+ if (params.color)
34
+ {
35
+ mat.color = new Color(params.color)
36
+ }
37
+ // roughness
38
+ if (params.roughness)
39
+ {
40
+ mat.roughness = params.roughness;
41
+ }
42
+ // metalness
43
+ if (params.metalness)
44
+ {
45
+ mat.metalness = params.metalness;
46
+ }
47
+ // side
48
+ if (params.side)
49
+ {
50
+ mat.side = params.side;
51
+ }
52
+ // blending
53
+ function toBlendingType(value) {
54
+ switch (value) {
55
+ case 0:
56
+ return NoBlending;
57
+ case 1:
58
+ return NormalBlending;
59
+ case 2:
60
+ return AdditiveBlending;
61
+ case 3:
62
+ return SubtractiveBlending;
63
+ case 4:
64
+ return MultiplyBlending;
65
+ case 5:
66
+ return CustomBlending;
67
+ default:
68
+ console.log("no blending type matched")
69
+ return NormalBlending; // 默认值或其他错误处理
70
+ }
71
+ }
72
+ if (params.blending)
73
+ {
74
+ mat.blending = toBlendingType(Number.parseInt(params.blending))
75
+ }
76
+ // opacity
77
+ if (params.opacity)
78
+ {
79
+ mat.opacity = params.opacity;
80
+ }
81
+ // transparent
82
+ mat.transparent = params.transparent ? true : false;
83
+ // depthTest
84
+ // mat.depthTest = params.depthTest === false ? false : true;
85
+ // // depthWrite
86
+ // mat.depthWrite = params.depthWrite === false ? false : true;
87
+
88
+ // depthTest7
89
+ mat.depthTest = true;
90
+ // depthWrite
91
+ mat.depthWrite = true;
92
+ // wireframe
93
+ mat.wireframe = params.wireframe ? true : false;
94
+ if(params.name)
95
+ {
96
+ mat.name = params.name
97
+ }
98
+ let processCount = 0
99
+ let onTextureLoaded = ()=>{}
100
+ if(onFinished)
101
+ {
102
+ onTextureLoaded = ()=>{
103
+ processCount--
104
+ if(processCount===0)
105
+ {
106
+ console.log("onfin")
107
+ onFinished()
108
+ }
109
+ }
110
+ }
111
+
112
+ // map
113
+ if (params.map)
114
+ {
115
+ processCount++
116
+ LYMaterialLibrary.loadTexture("map", mat, world, params.map, onTextureLoaded)
117
+ }
118
+
119
+ // emissiveMap
120
+ if (params.emissiveMap)
121
+ {
122
+ processCount++
123
+ LYMaterialLibrary.loadTexture("emissiveMap", mat, world, params.emissiveMap, onTextureLoaded)
124
+ }
125
+
126
+ // bump map
127
+ if (params.alphaMap)
128
+ {
129
+ processCount++
130
+ LYMaterialLibrary.loadTexture("alphaMap", mat, world, params.alphaMap, onTextureLoaded)
131
+ }
132
+ // normalMap
133
+ if (params.normalMap)
134
+ {
135
+ processCount++
136
+ LYMaterialLibrary.loadTexture("normalMap", mat, world, params.normalMap, onTextureLoaded)
137
+ }
138
+
139
+ // roughnessMap
140
+ if (params.roughnessMap)
141
+ {
142
+ processCount++
143
+ LYMaterialLibrary.loadTexture("roughnessMap", mat, world, params.roughnessMap, onTextureLoaded)
144
+ }
145
+
146
+ // metalnessMap
147
+ if (params.metalnessMap)
148
+ {
149
+ processCount++
150
+ LYMaterialLibrary.loadTexture("metalnessMap", mat, world, params.metalnessMap, onTextureLoaded)
151
+ }
152
+ mat.needsUpdate = true
153
+ }
154
+
155
+ static Serialize(mat: MeshStandardMaterial, world: LYThreeWorld)
156
+ {
157
+ interface obj
158
+ {
159
+ [idx: string]: any
160
+ }
161
+
162
+ let params: obj = {}
163
+
164
+ if (mat.color)
165
+ {
166
+ params.color = `rgb\(${Math.trunc(mat.color.r * 255)}, ${Math.trunc(mat.color.g * 255)}, ${Math.trunc(mat.color.b * 255)}\)`
167
+ }
168
+ // roughness
169
+ if (mat.roughness)
170
+ {
171
+ params.roughness = mat.roughness;
172
+ }
173
+ if (mat.metalness)
174
+ {
175
+ params.metalness = mat.metalness;
176
+ }
177
+ // side
178
+ if (mat.side)
179
+ {
180
+ params.side = mat.side;
181
+ }
182
+ // blending
183
+ if (mat.blending)
184
+ {
185
+ params.blending = mat.blending;
186
+ }
187
+ // opacity
188
+ if (mat.opacity)
189
+ {
190
+ params.opacity = mat.opacity;
191
+ }
192
+ // transparent
193
+ params.transparent = mat.transparent ? true : false;
194
+ // depthTest
195
+ params.depthTest = mat.depthTest ? true : false;
196
+ // depthWrite
197
+ params.depthWrite = mat.depthWrite ? true : false;
198
+ // wireframe
199
+ params.wireframe = mat.wireframe ? true : false;
200
+
201
+ params.name = mat.name
202
+ // world.AssetManager?.findAssetByName(AssetType.texture, )
203
+ // map
204
+
205
+ LYMaterialLibrary.SerializeTexture("map", mat, world, params)
206
+ LYMaterialLibrary.SerializeTexture("emissiveMap", mat, world, params)
207
+ LYMaterialLibrary.SerializeTexture("alphaMap", mat, world, params)
208
+ LYMaterialLibrary.SerializeTexture("normalMap", mat, world, params)
209
+ LYMaterialLibrary.SerializeTexture("roughnessMap", mat, world, params)
210
+ LYMaterialLibrary.SerializeTexture("metalnessMap", mat, world, params)
211
+
212
+ return {
213
+ type : "material",
214
+ params: params,
215
+ }
216
+ }
217
+
218
+ static SerializeTexture(key: string, material: Material, world: LYThreeWorld, paramsToMod)
219
+ {
220
+ if (!material[key] || !(material[key] instanceof Texture))
221
+ {
222
+ return
223
+ }
224
+ let asset = world.AssetManager?.findAssetByUUID(AssetType.texture, material[key].uuid)
225
+ if (asset && asset.Path !== "")
226
+ {
227
+ paramsToMod[key] = {
228
+ url : asset.Path,
229
+ offset: material[key].offset.toArray(),
230
+ repeat: material[key].repeat.toArray()
231
+ }
232
+ }
233
+ }
234
+
235
+ static loadTexture(key: string, material: Material, world: LYThreeWorld, data, onTextureLoaded: Function = ()=>{})
236
+ {
237
+ if (!data.url || data.url.length == 0)
238
+ {
239
+ material[key] = null;
240
+ material.needsUpdate = true
241
+ onTextureLoaded()
242
+
243
+ return
244
+ }
245
+ let texture = new TextureLoader(world.AssetManager.LoadingManager).load(data.url, (dataLoad: Texture) =>
246
+ {
247
+ console.log("loadTexture")
248
+ onTextureLoaded()
249
+ world.markRenderStateDirty();
250
+ }, (event: ProgressEvent) => {}, (err: unknown) => {});
251
+ texture.wrapS = RepeatWrapping;
252
+ texture.wrapT = RepeatWrapping;
253
+ texture.flipY = false
254
+ if (data.repeat)
255
+ {
256
+ texture.repeat = new Vector2().fromArray(data.repeat);
257
+ }
258
+ else
259
+ {
260
+ texture.repeat = new Vector2().fromArray([1, 1]);
261
+ }
262
+ if (data.offset)
263
+ {
264
+ texture.offset = new Vector2().fromArray(data.offset);
265
+ }
266
+ else
267
+ {
268
+ texture.offset = new Vector2().fromArray([0, 0]);
269
+ }
270
+ material[key] = texture;
271
+ world.AssetManager?.addTextureAsset(texture, data.url, data.url);
272
+ world.markRenderStateDirty();
273
+ }
274
+
275
+ static convertTextureToBase64(texture: Texture)
276
+ {
277
+ // 创建一个canvas元素
278
+ let canvas = document.createElement('canvas');
279
+ canvas.width = texture.image.width;
280
+ canvas.height = texture.image.height;
281
+
282
+ // 将贴图绘制到canvas上
283
+ let ctx = canvas.getContext('2d');
284
+ ctx.drawImage(texture.image, 0, 0, canvas.width, canvas.height);
285
+ return canvas.toDataURL()
286
+ }
287
+ }
288
+
@@ -0,0 +1,154 @@
1
+ import {Vector3, Matrix4, Quaternion, Object3D, Box3, Plane} from "three";
2
+
3
+ export interface Segment
4
+ {
5
+ pointA: Vector3
6
+ pointB: Vector3
7
+ }
8
+
9
+ export class LYMath
10
+ {
11
+ static degreeToRadians(degree): number
12
+ {
13
+ return degree * (Math.PI / 180);
14
+ }
15
+
16
+ static radiansToDegree(radians): number
17
+ {
18
+ return radians * (180 / Math.PI);
19
+ }
20
+
21
+ static threeJsEulerAngleAsDegree(euler)
22
+ {
23
+ return {
24
+ x: LYMath.radiansToDegree(euler.x),
25
+ y: LYMath.radiansToDegree(euler.y),
26
+ z: LYMath.radiansToDegree(euler.z),
27
+ }
28
+ }
29
+
30
+
31
+ static getQuatFromAxis(forwardVector: Vector3, upVector: Vector3 = new Vector3(0, 1, 0))
32
+ {
33
+ let f = forwardVector
34
+ let u = upVector
35
+ let r = u.clone().cross(f)
36
+
37
+ let matrix = new Matrix4(r.x, u.x, f.x, 0, r.y, u.y, f.y, 0, r.z, u.z, f.z, 0, 0, 0, 0, 1)
38
+ return new Quaternion().setFromRotationMatrix(matrix)
39
+ }
40
+
41
+ static MaxVector(v1: Vector3, v2: Vector3)
42
+ {
43
+ return new Vector3(Math.max(v1.x, v2.x), Math.max(v1.y, v2.y), Math.max(v1.z, v2.z))
44
+ }
45
+
46
+ static clamp(value: number, min: number, max: number)
47
+ {
48
+ return Math.max(min, Math.min(value, max));
49
+ }
50
+
51
+ static getPlaneConstantFromPointAndNormal(point: Vector3, normal: Vector3): number
52
+ {
53
+ return point.dot(normal) * -1;
54
+ }
55
+
56
+
57
+ static getSizeAndCenterOfObject3D(object: Object3D): { boundingBox: Box3, size: Vector3, center: Vector3 }
58
+ {
59
+ let bounds = new Box3()
60
+ bounds.setFromObject(object)
61
+ let size = new Vector3();
62
+ let center = new Vector3();
63
+ bounds.getSize(size)
64
+ bounds.getCenter(center)
65
+ return {
66
+ boundingBox: bounds,
67
+ size : size,
68
+ center : center
69
+ }
70
+ }
71
+
72
+ static SegmentsIntersection(segmentA: Segment, segmentB: Segment): Vector3 | null
73
+ {
74
+ // Define the start and end points of both segments
75
+ const line1Start = segmentA.pointA.clone();
76
+ const line1End = segmentA.pointB.clone();
77
+
78
+ const line2Start = segmentB.pointA.clone();
79
+ const line2End = segmentB.pointB.clone();
80
+
81
+ // Calculate direction vectors
82
+ const dir1 = new Vector3().subVectors(line1End, line1Start);
83
+ const dir2 = new Vector3().subVectors(line2End, line2Start);
84
+
85
+ // Calculate the cross product of direction vectors
86
+ const crossDir = new Vector3().crossVectors(dir1, dir2);
87
+ const denom = crossDir.lengthSq();
88
+
89
+ if (denom === 0) {
90
+ // Segments are parallel or collinear
91
+ console.log('Segments are parallel or collinear, no intersection.');
92
+ return null;
93
+ }
94
+
95
+ // Calculate intersection parameters t1 and t2
96
+ const diffStart = new Vector3().subVectors(line2Start, line1Start);
97
+ const t1 = diffStart.clone().cross(dir2).dot(crossDir) / denom;
98
+ const t2 = diffStart.clone().cross(dir1).dot(crossDir) / denom;
99
+
100
+ // Check if intersection lies within both segments
101
+ const isOnLine1 = t1 > 0 && t1 < 1;
102
+ const isOnLine2 = t2 > 0 && t2 < 1;
103
+
104
+ if (isOnLine1 && isOnLine2) {
105
+ // Calculate the exact intersection point
106
+ const intersection = line1Start.add(dir1.multiplyScalar(t1));
107
+ console.log('Intersection found at:', intersection, t1, t2);
108
+ return intersection;
109
+ } else {
110
+ console.log('Intersection does not lie within the segment bounds.');
111
+ return null;
112
+ }
113
+ }
114
+
115
+ static getCenterPointInPoints(points:Vector3[]):Vector3
116
+ {
117
+ let center = new Vector3()
118
+
119
+ points.forEach((elem)=>{
120
+ center.x += elem.x
121
+ center.y += elem.y
122
+ center.z += elem.z
123
+ })
124
+
125
+ return new Vector3(center.x / points.length,center.y / points.length,center.z / points.length)
126
+ }
127
+
128
+ static contructPlane(dirction: Vector3, position: Vector3, quaternion: Quaternion, scale: Vector3) {
129
+ let matrix = new Matrix4()
130
+ matrix.makeRotationFromQuaternion(quaternion)
131
+ matrix.scale(scale)
132
+
133
+ let pos = new Vector3(dirction.x * 0.5 * -1, 0.5, dirction.z * 0.5 * -1)
134
+ pos.applyMatrix4(matrix).add(position)
135
+ dirction.applyQuaternion(quaternion).normalize()
136
+ let distanceToOrigin = LYMath.getPlaneConstantFromPointAndNormal(pos, dirction)
137
+
138
+ return new Plane(dirction, distanceToOrigin)
139
+ }
140
+
141
+ static intersectRayPlane(rayOrigin:Vector3, rayDirection:Vector3, planeNormal:Vector3, planePoint:Vector3)
142
+ {
143
+ let denom = planeNormal.dot(rayDirection);
144
+ if (Math.abs(denom) > 1e-6) { // 确保射线不是平行于平面
145
+ var w = new Vector3().subVectors(rayOrigin, planePoint);
146
+ var t = -planeNormal.dot(w) / denom;
147
+ if (t >= 0) { // 如果 t >= 0,则交点在射线的前方
148
+ var point = new Vector3().copy(rayOrigin).addScaledVector(rayDirection, t);
149
+ return point; // 返回交点
150
+ }
151
+ }
152
+ return null; // 没有交点或者射线与平面平行
153
+ }
154
+ }
@@ -0,0 +1,21 @@
1
+ import {Mesh} from "three";
2
+
3
+ export class ResourceLibrary
4
+ {
5
+ static disposeMeshMaterial(mesh:Mesh)
6
+ {
7
+ let mats = Array.isArray(mesh.material) ? mesh.material : [mesh.material]
8
+ mats.forEach((elem)=>{
9
+ elem.dispose()
10
+ })
11
+ }
12
+ static disposeMeshGeometry(mesh:Mesh)
13
+ {
14
+ mesh.geometry.dispose()
15
+ }
16
+ static disposeMeshResource(mesh:Mesh)
17
+ {
18
+ ResourceLibrary.disposeMeshMaterial(mesh)
19
+ ResourceLibrary.disposeMeshGeometry(mesh)
20
+ }
21
+ }