@zephyr3d/scene 0.1.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 (236) hide show
  1. package/dist/animation/animation.js +173 -0
  2. package/dist/animation/animation.js.map +1 -0
  3. package/dist/animation/animationset.js +95 -0
  4. package/dist/animation/animationset.js.map +1 -0
  5. package/dist/animation/animationtrack.js +38 -0
  6. package/dist/animation/animationtrack.js.map +1 -0
  7. package/dist/animation/eulerrotationtrack.js +33 -0
  8. package/dist/animation/eulerrotationtrack.js.map +1 -0
  9. package/dist/animation/rotationtrack.js +37 -0
  10. package/dist/animation/rotationtrack.js.map +1 -0
  11. package/dist/animation/scaletrack.js +36 -0
  12. package/dist/animation/scaletrack.js.map +1 -0
  13. package/dist/animation/skeleton.js +97 -0
  14. package/dist/animation/skeleton.js.map +1 -0
  15. package/dist/animation/translationtrack.js +36 -0
  16. package/dist/animation/translationtrack.js.map +1 -0
  17. package/dist/animation/usertrack.js +47 -0
  18. package/dist/animation/usertrack.js.map +1 -0
  19. package/dist/app.js +173 -0
  20. package/dist/app.js.map +1 -0
  21. package/dist/asset/assetmanager.js +476 -0
  22. package/dist/asset/assetmanager.js.map +1 -0
  23. package/dist/asset/builtin.js +373 -0
  24. package/dist/asset/builtin.js.map +1 -0
  25. package/dist/asset/loaders/dds/dds.js +472 -0
  26. package/dist/asset/loaders/dds/dds.js.map +1 -0
  27. package/dist/asset/loaders/dds/dds_loader.js +38 -0
  28. package/dist/asset/loaders/dds/dds_loader.js.map +1 -0
  29. package/dist/asset/loaders/gltf/gltf_loader.js +981 -0
  30. package/dist/asset/loaders/gltf/gltf_loader.js.map +1 -0
  31. package/dist/asset/loaders/gltf/helpers.js +314 -0
  32. package/dist/asset/loaders/gltf/helpers.js.map +1 -0
  33. package/dist/asset/loaders/hdr/hdr.js +175 -0
  34. package/dist/asset/loaders/hdr/hdr.js.map +1 -0
  35. package/dist/asset/loaders/image/tga_Loader.js +117 -0
  36. package/dist/asset/loaders/image/tga_Loader.js.map +1 -0
  37. package/dist/asset/loaders/image/webimage_loader.js +50 -0
  38. package/dist/asset/loaders/image/webimage_loader.js.map +1 -0
  39. package/dist/asset/loaders/loader.js +45 -0
  40. package/dist/asset/loaders/loader.js.map +1 -0
  41. package/dist/asset/model.js +264 -0
  42. package/dist/asset/model.js.map +1 -0
  43. package/dist/blitter/blitter.js +389 -0
  44. package/dist/blitter/blitter.js.map +1 -0
  45. package/dist/blitter/box.js +118 -0
  46. package/dist/blitter/box.js.map +1 -0
  47. package/dist/blitter/copy.js +22 -0
  48. package/dist/blitter/copy.js.map +1 -0
  49. package/dist/blitter/depthlimitedgaussion.js +166 -0
  50. package/dist/blitter/depthlimitedgaussion.js.map +1 -0
  51. package/dist/blitter/gaussianblur.js +229 -0
  52. package/dist/blitter/gaussianblur.js.map +1 -0
  53. package/dist/camera/base.js +90 -0
  54. package/dist/camera/base.js.map +1 -0
  55. package/dist/camera/camera.js +358 -0
  56. package/dist/camera/camera.js.map +1 -0
  57. package/dist/camera/fps.js +246 -0
  58. package/dist/camera/fps.js.map +1 -0
  59. package/dist/camera/orbit.js +157 -0
  60. package/dist/camera/orbit.js.map +1 -0
  61. package/dist/camera/orthocamera.js +126 -0
  62. package/dist/camera/orthocamera.js.map +1 -0
  63. package/dist/camera/perspectivecamera.js +133 -0
  64. package/dist/camera/perspectivecamera.js.map +1 -0
  65. package/dist/index.d.ts +8402 -0
  66. package/dist/index.js +87 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/input/inputmgr.js +242 -0
  69. package/dist/input/inputmgr.js.map +1 -0
  70. package/dist/material/blinn.js +75 -0
  71. package/dist/material/blinn.js.map +1 -0
  72. package/dist/material/grassmaterial.js +221 -0
  73. package/dist/material/grassmaterial.js.map +1 -0
  74. package/dist/material/lambert.js +52 -0
  75. package/dist/material/lambert.js.map +1 -0
  76. package/dist/material/lightmodel.js +2074 -0
  77. package/dist/material/lightmodel.js.map +1 -0
  78. package/dist/material/lit.js +578 -0
  79. package/dist/material/lit.js.map +1 -0
  80. package/dist/material/material.js +458 -0
  81. package/dist/material/material.js.map +1 -0
  82. package/dist/material/meshmaterial.js +311 -0
  83. package/dist/material/meshmaterial.js.map +1 -0
  84. package/dist/material/mixins/albedocolor.js +130 -0
  85. package/dist/material/mixins/albedocolor.js.map +1 -0
  86. package/dist/material/mixins/texture.js +110 -0
  87. package/dist/material/mixins/texture.js.map +1 -0
  88. package/dist/material/mixins/vertexcolor.js +45 -0
  89. package/dist/material/mixins/vertexcolor.js.map +1 -0
  90. package/dist/material/pbr.js +27 -0
  91. package/dist/material/pbr.js.map +1 -0
  92. package/dist/material/standard.js +282 -0
  93. package/dist/material/standard.js.map +1 -0
  94. package/dist/material/terrainlightmodel.js +259 -0
  95. package/dist/material/terrainlightmodel.js.map +1 -0
  96. package/dist/material/terrainmaterial.js +139 -0
  97. package/dist/material/terrainmaterial.js.map +1 -0
  98. package/dist/material/unlit.js +29 -0
  99. package/dist/material/unlit.js.map +1 -0
  100. package/dist/posteffect/bloom.js +398 -0
  101. package/dist/posteffect/bloom.js.map +1 -0
  102. package/dist/posteffect/compositor.js +264 -0
  103. package/dist/posteffect/compositor.js.map +1 -0
  104. package/dist/posteffect/fxaa.js +291 -0
  105. package/dist/posteffect/fxaa.js.map +1 -0
  106. package/dist/posteffect/grayscale.js +87 -0
  107. package/dist/posteffect/grayscale.js.map +1 -0
  108. package/dist/posteffect/posteffect.js +165 -0
  109. package/dist/posteffect/posteffect.js.map +1 -0
  110. package/dist/posteffect/sao.js +327 -0
  111. package/dist/posteffect/sao.js.map +1 -0
  112. package/dist/posteffect/tonemap.js +112 -0
  113. package/dist/posteffect/tonemap.js.map +1 -0
  114. package/dist/posteffect/water.js +535 -0
  115. package/dist/posteffect/water.js.map +1 -0
  116. package/dist/render/clipmap.js +462 -0
  117. package/dist/render/clipmap.js.map +1 -0
  118. package/dist/render/cluster_light.js +329 -0
  119. package/dist/render/cluster_light.js.map +1 -0
  120. package/dist/render/cull_visitor.js +124 -0
  121. package/dist/render/cull_visitor.js.map +1 -0
  122. package/dist/render/depth_pass.js +47 -0
  123. package/dist/render/depth_pass.js.map +1 -0
  124. package/dist/render/envlight.js +282 -0
  125. package/dist/render/envlight.js.map +1 -0
  126. package/dist/render/forward.js +186 -0
  127. package/dist/render/forward.js.map +1 -0
  128. package/dist/render/forward_pass.js +137 -0
  129. package/dist/render/forward_pass.js.map +1 -0
  130. package/dist/render/helper.js +38 -0
  131. package/dist/render/helper.js.map +1 -0
  132. package/dist/render/primitive.js +246 -0
  133. package/dist/render/primitive.js.map +1 -0
  134. package/dist/render/render_queue.js +163 -0
  135. package/dist/render/render_queue.js.map +1 -0
  136. package/dist/render/renderpass.js +151 -0
  137. package/dist/render/renderpass.js.map +1 -0
  138. package/dist/render/renderscheme.js +61 -0
  139. package/dist/render/renderscheme.js.map +1 -0
  140. package/dist/render/scatteringlut.js +634 -0
  141. package/dist/render/scatteringlut.js.map +1 -0
  142. package/dist/render/shadowmap_pass.js +70 -0
  143. package/dist/render/shadowmap_pass.js.map +1 -0
  144. package/dist/render/sky.js +881 -0
  145. package/dist/render/sky.js.map +1 -0
  146. package/dist/render/temporalcache.js +222 -0
  147. package/dist/render/temporalcache.js.map +1 -0
  148. package/dist/render/watermesh.js +835 -0
  149. package/dist/render/watermesh.js.map +1 -0
  150. package/dist/scene/environment.js +146 -0
  151. package/dist/scene/environment.js.map +1 -0
  152. package/dist/scene/graph_node.js +69 -0
  153. package/dist/scene/graph_node.js.map +1 -0
  154. package/dist/scene/light.js +436 -0
  155. package/dist/scene/light.js.map +1 -0
  156. package/dist/scene/mesh.js +215 -0
  157. package/dist/scene/mesh.js.map +1 -0
  158. package/dist/scene/model.js +111 -0
  159. package/dist/scene/model.js.map +1 -0
  160. package/dist/scene/octree.js +651 -0
  161. package/dist/scene/octree.js.map +1 -0
  162. package/dist/scene/octree_update_visitor.js +16 -0
  163. package/dist/scene/octree_update_visitor.js.map +1 -0
  164. package/dist/scene/raycast_visitor.js +72 -0
  165. package/dist/scene/raycast_visitor.js.map +1 -0
  166. package/dist/scene/scene.js +225 -0
  167. package/dist/scene/scene.js.map +1 -0
  168. package/dist/scene/scene_node.js +299 -0
  169. package/dist/scene/scene_node.js.map +1 -0
  170. package/dist/scene/terrain/grass.js +277 -0
  171. package/dist/scene/terrain/grass.js.map +1 -0
  172. package/dist/scene/terrain/heightfield.js +391 -0
  173. package/dist/scene/terrain/heightfield.js.map +1 -0
  174. package/dist/scene/terrain/patch.js +530 -0
  175. package/dist/scene/terrain/patch.js.map +1 -0
  176. package/dist/scene/terrain/quadtree.js +430 -0
  177. package/dist/scene/terrain/quadtree.js.map +1 -0
  178. package/dist/scene/terrain/terrain.js +258 -0
  179. package/dist/scene/terrain/terrain.js.map +1 -0
  180. package/dist/scene/xform.js +224 -0
  181. package/dist/scene/xform.js.map +1 -0
  182. package/dist/shaders/builtins.js +110 -0
  183. package/dist/shaders/builtins.js.map +1 -0
  184. package/dist/shaders/framework.js +709 -0
  185. package/dist/shaders/framework.js.map +1 -0
  186. package/dist/shaders/lighting.js +335 -0
  187. package/dist/shaders/lighting.js.map +1 -0
  188. package/dist/shaders/misc.js +405 -0
  189. package/dist/shaders/misc.js.map +1 -0
  190. package/dist/shaders/noise.js +157 -0
  191. package/dist/shaders/noise.js.map +1 -0
  192. package/dist/shaders/pbr.js +132 -0
  193. package/dist/shaders/pbr.js.map +1 -0
  194. package/dist/shaders/shadow.js +642 -0
  195. package/dist/shaders/shadow.js.map +1 -0
  196. package/dist/shaders/water.js +630 -0
  197. package/dist/shaders/water.js.map +1 -0
  198. package/dist/shadow/esm.js +235 -0
  199. package/dist/shadow/esm.js.map +1 -0
  200. package/dist/shadow/pcf_opt.js +182 -0
  201. package/dist/shadow/pcf_opt.js.map +1 -0
  202. package/dist/shadow/pcf_pd.js +190 -0
  203. package/dist/shadow/pcf_pd.js.map +1 -0
  204. package/dist/shadow/shadow_impl.js +15 -0
  205. package/dist/shadow/shadow_impl.js.map +1 -0
  206. package/dist/shadow/shadowmapper.js +709 -0
  207. package/dist/shadow/shadowmapper.js.map +1 -0
  208. package/dist/shadow/ssm.js +194 -0
  209. package/dist/shadow/ssm.js.map +1 -0
  210. package/dist/shadow/vsm.js +298 -0
  211. package/dist/shadow/vsm.js.map +1 -0
  212. package/dist/shapes/box.js +313 -0
  213. package/dist/shapes/box.js.map +1 -0
  214. package/dist/shapes/cylinder.js +74 -0
  215. package/dist/shapes/cylinder.js.map +1 -0
  216. package/dist/shapes/plane.js +48 -0
  217. package/dist/shapes/plane.js.map +1 -0
  218. package/dist/shapes/shape.js +33 -0
  219. package/dist/shapes/shape.js.map +1 -0
  220. package/dist/shapes/sphere.js +91 -0
  221. package/dist/shapes/sphere.js.map +1 -0
  222. package/dist/shapes/torus.js +100 -0
  223. package/dist/shapes/torus.js.map +1 -0
  224. package/dist/utility/aabbtree.js +390 -0
  225. package/dist/utility/aabbtree.js.map +1 -0
  226. package/dist/utility/bounding_volume.js +78 -0
  227. package/dist/utility/bounding_volume.js.map +1 -0
  228. package/dist/utility/panorama.js +163 -0
  229. package/dist/utility/panorama.js.map +1 -0
  230. package/dist/utility/pmrem.js +345 -0
  231. package/dist/utility/pmrem.js.map +1 -0
  232. package/dist/utility/shprojection.js +448 -0
  233. package/dist/utility/shprojection.js.map +1 -0
  234. package/dist/values.js +48 -0
  235. package/dist/values.js.map +1 -0
  236. package/package.json +70 -0
@@ -0,0 +1,390 @@
1
+ import { Vector3, AABB } from '@zephyr3d/base';
2
+
3
+ const tmpTriangle = [
4
+ 0,
5
+ 0,
6
+ 0
7
+ ];
8
+ /**
9
+ * Axis-Aligned Bounding Box Tree
10
+ * @public
11
+ */ class AABBTree {
12
+ /** @internal */ _primitivesInfo;
13
+ /** @internal */ _nodes;
14
+ constructor(other){
15
+ this._primitivesInfo = null;
16
+ this._nodes = null;
17
+ if (other) {
18
+ this._primitivesInfo = other._primitivesInfo ? {
19
+ vertices: other._primitivesInfo.vertices?.map((value)=>new Vector3(value)),
20
+ indices: other._primitivesInfo.indices?.map((value)=>value),
21
+ primitiveType: other._primitivesInfo.primitiveType
22
+ } : null;
23
+ this._nodes = other._nodes ? other._nodes.map((value)=>{
24
+ return {
25
+ box: new AABB(value.box),
26
+ triangles: value.triangles?.map((val)=>val),
27
+ axis: value.axis,
28
+ left: value.left,
29
+ right: value.right
30
+ };
31
+ }) : null;
32
+ }
33
+ }
34
+ /**
35
+ * Build the AABB tree from a polygon soup
36
+ * @param vertices - Vertices of the polygon soup
37
+ * @param indices - indices of the polygon soup
38
+ * @param primitiveType - Prmitive type of the polygon soup
39
+ */ buildFromPrimitives(vertices, indices, primitiveType) {
40
+ this._primitivesInfo = {
41
+ vertices: [],
42
+ indices: null,
43
+ primitiveType: primitiveType
44
+ };
45
+ for(let i = 0; i < vertices.length; i += 3){
46
+ this._primitivesInfo.vertices.push(new Vector3(vertices[i], vertices[i + 1], vertices[i + 2]));
47
+ }
48
+ if (indices) {
49
+ this._primitivesInfo.indices = [];
50
+ for (const index of indices){
51
+ this._primitivesInfo.indices.push(index);
52
+ }
53
+ }
54
+ this._buildSubNodes();
55
+ }
56
+ /**
57
+ * Checks for intersection between a ray and the AABB tree without calculating the intersection point
58
+ * @param ray - The ray being traced
59
+ * @returns true if the ray hits the AABB tree, false otherwise
60
+ */ rayIntersectionTest(ray) {
61
+ return this._nodes.length > 0 ? this._rayIntersectionTest(0, ray) : false;
62
+ }
63
+ /**
64
+ * Checks for intersection between a ray and the AABB tree
65
+ * @param ray - The ray being traced
66
+ * @returns The distance between the ray origin and the hit point if the ray hits the AABB tree, null otherwise
67
+ */ rayIntersectionDistance(ray) {
68
+ return this._nodes.length > 0 ? this._rayIntersectionDistance(0, ray) : null;
69
+ }
70
+ /**
71
+ * Gets the top level bounding box of the tree
72
+ * @returns The top level bounding box
73
+ */ getTopLevelAABB() {
74
+ return this._nodes.length > 0 ? this._nodes[0].box : null;
75
+ }
76
+ /**
77
+ * Transform the tree by a matrix
78
+ * @param matrix - The transform matrix
79
+ */ transform(matrix) {
80
+ if (matrix && this._primitivesInfo && this._primitivesInfo.vertices) {
81
+ for (const vert of this._primitivesInfo.vertices){
82
+ matrix.transformPointAffine(vert, vert);
83
+ }
84
+ this._buildSubNodes();
85
+ }
86
+ }
87
+ /** @internal */ verify() {
88
+ const numTris = this._verifyNode(0);
89
+ const n = this._getNumTriangles();
90
+ if (numTris !== n) {
91
+ throw new Error(`AABB tree verification failed: triangle count mismatch, got ${numTris}, expect ${n}`);
92
+ }
93
+ }
94
+ /** @internal */ _verifyNode(nodeIndex) {
95
+ const node = this._nodes[nodeIndex];
96
+ if (!node) {
97
+ throw new Error(`AABB tree verification failed: invalid node index: ${nodeIndex}`);
98
+ }
99
+ let numTris = 0;
100
+ const extents = node.box.extents[node.axis];
101
+ const tmpTri = [
102
+ 0,
103
+ 0,
104
+ 0
105
+ ];
106
+ for (const tri of node.triangles){
107
+ this._getTriangle(tri, tmpTri);
108
+ let max = Number.MIN_VALUE;
109
+ let min = Number.MAX_VALUE;
110
+ for (const v of tmpTri){
111
+ const p = this._primitivesInfo.vertices[v];
112
+ if (!node.box.containsPoint(p)) {
113
+ throw new Error(`AABB tree verification failed: triangle not inside AABB`);
114
+ }
115
+ const t = p[node.axis];
116
+ if (t < min) min = t;
117
+ if (t > max) max = t;
118
+ if (max - min <= extents) {
119
+ throw new Error(`AABB tree verification failed: extents test failed`);
120
+ }
121
+ }
122
+ }
123
+ numTris += node.triangles.length;
124
+ if (node.left >= 0) {
125
+ numTris += this._verifyNode(node.left);
126
+ }
127
+ if (node.right >= 0) {
128
+ numTris += this._verifyNode(node.right);
129
+ }
130
+ return numTris;
131
+ }
132
+ /** @internal */ _buildNode(nodeIndex, triangles, triangleMin, triangleMax) {
133
+ const node = this._nodes[nodeIndex];
134
+ if (triangles.length === 1) {
135
+ node.triangles.push(triangles[0]);
136
+ } else {
137
+ const splitAxis = node.axis = this._selectBestSplitAxis(node.box.extents, triangles, triangleMin, triangleMax);
138
+ const sizeMax = node.box.extents[splitAxis];
139
+ const sizeCenter = node.box.center[splitAxis];
140
+ const sizeLeft = sizeCenter - node.box.extents[splitAxis] * 0.5;
141
+ const leftTriangles = [];
142
+ const rightTriangles = [];
143
+ const leftbox = new AABB();
144
+ leftbox.beginExtend();
145
+ const rightbox = new AABB();
146
+ rightbox.beginExtend();
147
+ for (const tri of triangles){
148
+ const min = triangleMin[tri];
149
+ const max = triangleMax[tri];
150
+ const size = max[splitAxis] - min[splitAxis];
151
+ if (size >= sizeMax) {
152
+ node.triangles.push(tri);
153
+ } else if (max[splitAxis] <= sizeCenter || min[splitAxis] <= sizeLeft) {
154
+ leftTriangles.push(tri);
155
+ leftbox.extend3(min[0], min[1], min[2]);
156
+ leftbox.extend3(max[0], max[1], max[2]);
157
+ } else {
158
+ rightTriangles.push(tri);
159
+ rightbox.extend3(min[0], min[1], min[2]);
160
+ rightbox.extend3(max[0], max[1], max[2]);
161
+ }
162
+ }
163
+ if (leftTriangles.length) {
164
+ this._nodes.push({
165
+ box: leftbox,
166
+ triangles: [],
167
+ axis: -1,
168
+ left: -1,
169
+ right: -1
170
+ });
171
+ node.left = this._nodes.length - 1;
172
+ this._buildNode(this._nodes.length - 1, leftTriangles, triangleMin, triangleMax);
173
+ }
174
+ if (rightTriangles.length) {
175
+ this._nodes.push({
176
+ box: rightbox,
177
+ triangles: [],
178
+ axis: -1,
179
+ left: -1,
180
+ right: -1
181
+ });
182
+ node.right = this._nodes.length - 1;
183
+ this._buildNode(this._nodes.length - 1, rightTriangles, triangleMin, triangleMax);
184
+ }
185
+ }
186
+ }
187
+ /** @internal */ _selectBestSplitAxis(extents, triangles, triangleMin, triangleMax) {
188
+ const dx = extents.x, dy = extents.y, dz = extents.z;
189
+ let nx = 0, ny = 0, nz = 0;
190
+ for (const tri of triangles){
191
+ const max = triangleMax[tri];
192
+ const min = triangleMin[tri];
193
+ if (max[0] - min[0] >= dx) nx++;
194
+ if (max[1] - min[1] >= dy) ny++;
195
+ if (max[2] - min[2] >= dz) nz++;
196
+ }
197
+ if (nx < ny) {
198
+ if (nx < nz) {
199
+ return 0;
200
+ } else {
201
+ return nx === nz ? dx > dz ? 0 : 2 : 2;
202
+ }
203
+ } else if (nx == ny) {
204
+ if (nz < nx) {
205
+ return 2;
206
+ } else if (nz > nx) {
207
+ return dx > dy ? 0 : 1;
208
+ } else {
209
+ if (dx < dy) {
210
+ return dy > dz ? 1 : 2;
211
+ } else {
212
+ return dx > dz ? 0 : 2;
213
+ }
214
+ }
215
+ } else {
216
+ if (ny < nz) {
217
+ return 1;
218
+ } else {
219
+ return ny === nz ? dy > dz ? 1 : 2 : 2;
220
+ }
221
+ }
222
+ }
223
+ /** @internal */ _getNumTriangles() {
224
+ const numIndices = this._primitivesInfo.indices ? this._primitivesInfo.indices.length : this._primitivesInfo.vertices.length;
225
+ switch(this._primitivesInfo.primitiveType){
226
+ case 'triangle-list':
227
+ return Math.floor(numIndices / 3);
228
+ case 'triangle-strip':
229
+ return numIndices - 2;
230
+ case 'triangle-fan':
231
+ return numIndices - 2;
232
+ default:
233
+ return 0;
234
+ }
235
+ }
236
+ /** @internal */ _rayIntersectionDistance(nodeIndex, ray) {
237
+ const node = this._nodes[nodeIndex];
238
+ if (!ray.bboxIntersectionTest(node.box)) {
239
+ return null;
240
+ }
241
+ let minDist = Number.MAX_VALUE;
242
+ for (const tri of node.triangles){
243
+ this._getTriangle(tri, tmpTriangle);
244
+ const v0 = this._primitivesInfo.vertices[tmpTriangle[0]];
245
+ const v1 = this._primitivesInfo.vertices[tmpTriangle[1]];
246
+ const v2 = this._primitivesInfo.vertices[tmpTriangle[2]];
247
+ const dist = ray.intersectionTestTriangle(v0, v1, v2, false);
248
+ if (dist !== null && dist >= 0 && dist < minDist) {
249
+ minDist = dist;
250
+ }
251
+ }
252
+ let a, b;
253
+ if (node.axis >= 0) {
254
+ if (ray.direction[node.axis] < 0) {
255
+ a = node.right;
256
+ b = node.left;
257
+ } else {
258
+ a = node.left;
259
+ b = node.right;
260
+ }
261
+ if (a >= 0) {
262
+ const dist = this._rayIntersectionDistance(a, ray);
263
+ if (dist !== null && dist >= 0 && dist < minDist) {
264
+ minDist = dist;
265
+ }
266
+ }
267
+ if (b >= 0) {
268
+ const dist = this._rayIntersectionDistance(b, ray);
269
+ if (dist !== null && dist >= 0 && dist < minDist) {
270
+ minDist = dist;
271
+ }
272
+ }
273
+ }
274
+ return minDist === Number.MAX_VALUE ? null : minDist;
275
+ }
276
+ /** @internal */ _rayIntersectionTest(nodeIndex, ray) {
277
+ const node = this._nodes[nodeIndex];
278
+ if (!ray.bboxIntersectionTest(node.box)) {
279
+ return false;
280
+ }
281
+ for (const tri of node.triangles){
282
+ this._getTriangle(tri, tmpTriangle);
283
+ const v0 = this._primitivesInfo.vertices[tmpTriangle[0]];
284
+ const v1 = this._primitivesInfo.vertices[tmpTriangle[1]];
285
+ const v2 = this._primitivesInfo.vertices[tmpTriangle[2]];
286
+ const dist = ray.intersectionTestTriangle(v0, v1, v2, false);
287
+ if (dist !== null && dist > 0) {
288
+ return true;
289
+ }
290
+ }
291
+ let a, b;
292
+ if (node.axis >= 0) {
293
+ if (ray.direction[node.axis] < 0) {
294
+ a = node.right;
295
+ b = node.left;
296
+ } else {
297
+ a = node.left;
298
+ b = node.right;
299
+ }
300
+ if (a >= 0 && this._rayIntersectionTest(a, ray)) {
301
+ return true;
302
+ }
303
+ if (b >= 0 && this._rayIntersectionTest(b, ray)) {
304
+ return true;
305
+ }
306
+ }
307
+ return false;
308
+ }
309
+ /** @internal */ _getTriangle(n, out) {
310
+ const indices = this._primitivesInfo.indices;
311
+ let v0, v1, v2;
312
+ out = out || [
313
+ 0,
314
+ 0,
315
+ 0
316
+ ];
317
+ switch(this._primitivesInfo.primitiveType){
318
+ case 'triangle-list':
319
+ v0 = n * 3;
320
+ v1 = v0 + 1;
321
+ v2 = v0 + 2;
322
+ break;
323
+ case 'triangle-strip':
324
+ {
325
+ const r = n % 2;
326
+ v0 = n + r;
327
+ v1 = n - r + 1;
328
+ v2 = n + 2;
329
+ break;
330
+ }
331
+ case 'triangle-fan':
332
+ v0 = 0;
333
+ v1 = n + 1;
334
+ v2 = n + 2;
335
+ break;
336
+ default:
337
+ return null;
338
+ }
339
+ out[0] = indices ? indices[v0] : v0;
340
+ out[1] = indices ? indices[v1] : v1;
341
+ out[2] = indices ? indices[v2] : v2;
342
+ return out;
343
+ }
344
+ /** @internal */ _buildSubNodes() {
345
+ const triangleMin = [];
346
+ const triangleMax = [];
347
+ const triangles = [];
348
+ const numTriangles = this._getNumTriangles();
349
+ const rootNode = {
350
+ box: new AABB(),
351
+ triangles: [],
352
+ axis: -1,
353
+ left: -1,
354
+ right: -1
355
+ };
356
+ rootNode.box.beginExtend();
357
+ for(let i = 0; i < numTriangles; i++){
358
+ triangles.push(i);
359
+ this._getTriangle(i, tmpTriangle);
360
+ const v0 = this._primitivesInfo.vertices[tmpTriangle[0]];
361
+ const v1 = this._primitivesInfo.vertices[tmpTriangle[1]];
362
+ const v2 = this._primitivesInfo.vertices[tmpTriangle[2]];
363
+ const min = [
364
+ Math.min(Math.min(v0.x, v1.x), v2.x),
365
+ Math.min(Math.min(v0.y, v1.y), v2.y),
366
+ Math.min(Math.min(v0.z, v1.z), v2.z)
367
+ ];
368
+ rootNode.box.extend3(min[0], min[1], min[2]);
369
+ triangleMin.push(min);
370
+ const max = [
371
+ Math.max(Math.max(v0.x, v1.x), v2.x),
372
+ Math.max(Math.max(v0.y, v1.y), v2.y),
373
+ Math.max(Math.max(v0.z, v1.z), v2.z)
374
+ ];
375
+ rootNode.box.extend3(max[0], max[1], max[2]);
376
+ triangleMax.push(max);
377
+ }
378
+ if (rootNode.box.isValid()) {
379
+ this._nodes = [
380
+ rootNode
381
+ ];
382
+ this._buildNode(0, triangles, triangleMin, triangleMax);
383
+ } else {
384
+ this._nodes = [];
385
+ }
386
+ }
387
+ }
388
+
389
+ export { AABBTree };
390
+ //# sourceMappingURL=aabbtree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aabbtree.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,78 @@
1
+ import { AABB, Frustum, ClipState } from '@zephyr3d/base';
2
+ import { AABBTree } from './aabbtree.js';
3
+
4
+ /**
5
+ * The bounding box class
6
+ * @public
7
+ */ class BoundingBox extends AABB {
8
+ constructor(arg0, arg1){
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ super(arg0, arg1);
11
+ }
12
+ /** {@inheritDoc BoundingVolume.clone} */ clone() {
13
+ return new BoundingBox(this);
14
+ }
15
+ /** {@inheritDoc BoundingVolume.transform} */ transform(matrix) {
16
+ return new BoundingBox(AABB.transform(this, matrix));
17
+ }
18
+ /** {@inheritDoc BoundingVolume.outsideFrustum} */ outsideFrustum(frustum) {
19
+ return (frustum instanceof Frustum ? this.getClipStateWithFrustum(frustum) : this.getClipState(frustum)) === ClipState.NOT_CLIPPED;
20
+ }
21
+ /** {@inheritDoc BoundingVolume.toAABB} */ toAABB() {
22
+ return this;
23
+ }
24
+ }
25
+ /**
26
+ * Bounding box tree
27
+ * @public
28
+ */ class BoundingBoxTree extends AABBTree {
29
+ constructor(arg){
30
+ super(arg);
31
+ }
32
+ /** {@inheritDoc BoundingVolume.clone} */ clone() {
33
+ return new BoundingBoxTree(this);
34
+ }
35
+ /** {@inheritDoc BoundingVolume.transform} */ transform(matrix) {
36
+ const newBV = new BoundingBoxTree(this);
37
+ newBV.transform(matrix);
38
+ return newBV;
39
+ }
40
+ /** {@inheritDoc BoundingVolume.behindPlane} */ behindPlane(plane) {
41
+ return this.toAABB().behindPlane(plane);
42
+ }
43
+ /** {@inheritDoc BoundingVolume.outsideFrustum} */ outsideFrustum(frustum) {
44
+ const aabb = this.getTopLevelAABB();
45
+ if (aabb) {
46
+ return (frustum instanceof Frustum ? aabb.getClipStateWithFrustum(frustum) : aabb.getClipState(frustum)) === ClipState.NOT_CLIPPED;
47
+ } else {
48
+ return false;
49
+ }
50
+ }
51
+ /** {@inheritDoc BoundingVolume.toAABB} */ toAABB() {
52
+ return this.getTopLevelAABB();
53
+ }
54
+ } /*
55
+ export class BoundingFrustum implements BoundingVolume {
56
+ protected _frustum: Frustum;
57
+ constructor ();
58
+ constructor (other: BoundingFrustum|Frustum|Matrix4x4);
59
+ constructor (arg0?: BoundingFrustum|Frustum|Matrix4x4) {
60
+ if (arg0 instanceof BoundingFrustum) {
61
+ this._frustum = arg0._frustum ? new Frustum (arg0._frustum) : null;
62
+ } else if (arg0 instanceof Frustum) {
63
+ this._frustum = new Frustum (arg0);
64
+ } else if (arg0 instanceof Matrix4x4) {
65
+ this._frustum = new Frustum (arg0);
66
+ } else {
67
+ this._frustum = null;
68
+ }
69
+ }
70
+ clone (): BoundingVolume {
71
+ return new BoundingFrustum (this);
72
+ }
73
+ transform (matrix: Matrix4x4)
74
+ }
75
+ */
76
+
77
+ export { BoundingBox, BoundingBoxTree };
78
+ //# sourceMappingURL=bounding_volume.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bounding_volume.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,163 @@
1
+ import { Vector3 } from '@zephyr3d/base';
2
+ import { Application } from '../app.js';
3
+ import { gammaToLinear } from '../shaders/misc.js';
4
+
5
+ let vertexLayout = null;
6
+ let renderStates = null;
7
+ let panoramaToCubemapProgram = null;
8
+ let panoramaToCubemapBindGroup = null;
9
+ let panoramaToCubemapProgramRGBM = null;
10
+ let panoramaToCubemapBindGroupRGBM = null;
11
+ const faceDirections = [
12
+ [
13
+ new Vector3(0, 0, -1),
14
+ new Vector3(0, -1, 0),
15
+ new Vector3(1, 0, 0)
16
+ ],
17
+ [
18
+ new Vector3(0, 0, 1),
19
+ new Vector3(0, -1, 0),
20
+ new Vector3(-1, 0, 0)
21
+ ],
22
+ [
23
+ new Vector3(1, 0, 0),
24
+ new Vector3(0, 0, 1),
25
+ new Vector3(0, 1, 0)
26
+ ],
27
+ [
28
+ new Vector3(1, 0, 0),
29
+ new Vector3(0, 0, -1),
30
+ new Vector3(0, -1, 0)
31
+ ],
32
+ [
33
+ new Vector3(1, 0, 0),
34
+ new Vector3(0, -1, 0),
35
+ new Vector3(0, 0, 1)
36
+ ],
37
+ [
38
+ new Vector3(-1, 0, 0),
39
+ new Vector3(0, -1, 0),
40
+ new Vector3(0, 0, -1)
41
+ ]
42
+ ];
43
+ function init() {
44
+ const device = Application.instance.device;
45
+ const vertices = new Float32Array([
46
+ 1,
47
+ 1,
48
+ -1,
49
+ 1,
50
+ -1,
51
+ -1,
52
+ 1,
53
+ -1
54
+ ]);
55
+ const indices = new Uint16Array([
56
+ 0,
57
+ 1,
58
+ 2,
59
+ 0,
60
+ 2,
61
+ 3
62
+ ]);
63
+ vertexLayout = device.createVertexLayout({
64
+ vertexBuffers: [
65
+ {
66
+ buffer: device.createVertexBuffer('position_f32x2', vertices)
67
+ }
68
+ ],
69
+ indexBuffer: device.createIndexBuffer(indices)
70
+ });
71
+ renderStates = device.createRenderStateSet();
72
+ renderStates.useRasterizerState().setCullMode('none');
73
+ renderStates.useDepthState().enableTest(false).enableWrite(false);
74
+ panoramaToCubemapProgram = createPanoramaToCubemapProgram(false);
75
+ panoramaToCubemapBindGroup = device.createBindGroup(panoramaToCubemapProgram.bindGroupLayouts[0]);
76
+ panoramaToCubemapProgramRGBM = createPanoramaToCubemapProgram(true);
77
+ panoramaToCubemapBindGroupRGBM = device.createBindGroup(panoramaToCubemapProgramRGBM.bindGroupLayouts[0]);
78
+ }
79
+ function createPanoramaToCubemapProgram(rgbm) {
80
+ const device = Application.instance.device;
81
+ const pb = device;
82
+ return pb.buildRenderProgram({
83
+ vertex (pb) {
84
+ this.$inputs.pos = pb.vec2().attrib('position');
85
+ this.up = pb.vec3().uniform(0);
86
+ this.right = pb.vec3().uniform(0);
87
+ this.front = pb.vec3().uniform(0);
88
+ pb.main(function() {
89
+ this.$builtins.position = pb.vec4(this.$inputs.pos, 0, 1);
90
+ this.$outputs.direction = pb.mul(pb.mat3(this.up, this.right, this.front), pb.vec3(this.$inputs.pos, 1));
91
+ if (pb.getDevice().type === 'webgpu') {
92
+ this.$builtins.position.y = pb.neg(this.$builtins.position.y);
93
+ }
94
+ });
95
+ },
96
+ fragment (pb) {
97
+ this.u_panoramaTexture = pb.tex2D().uniform(0);
98
+ this.$outputs.outcolor = pb.vec4();
99
+ pb.func('dirToUV', [
100
+ pb.vec3('dir')
101
+ ], function() {
102
+ this.$l.x = pb.add(0.5, pb.div(pb.atan2(this.dir.z, this.dir.x), 2 * Math.PI));
103
+ this.$l.y = pb.div(pb.acos(this.dir.y), Math.PI);
104
+ this.$return(pb.vec2(this.x, this.y));
105
+ });
106
+ pb.main(function() {
107
+ this.$l.uv = this.dirToUV(pb.normalize(this.$inputs.direction));
108
+ this.$l.color = pb.textureSampleLevel(this.u_panoramaTexture, this.uv, 0);
109
+ if (rgbm) {
110
+ this.$l.rgb = pb.mul(this.color.rgb, this.color.a, 6);
111
+ this.rgb = gammaToLinear(this, this.rgb);
112
+ this.$outputs.outcolor = pb.vec4(this.rgb, 1);
113
+ } else {
114
+ this.$outputs.outcolor = pb.vec4(this.color.rgb, 1);
115
+ }
116
+ });
117
+ }
118
+ });
119
+ }
120
+ function doConvertPanoramaToCubemap(srcTexture, dstTexture) {
121
+ const device = Application.instance.device;
122
+ const rgbm = srcTexture.format === 'rgba8unorm';
123
+ const program = rgbm ? panoramaToCubemapProgramRGBM : panoramaToCubemapProgram;
124
+ const bindgroup = rgbm ? panoramaToCubemapBindGroupRGBM : panoramaToCubemapBindGroup;
125
+ const framebuffer = device.createFrameBuffer([
126
+ dstTexture
127
+ ], null);
128
+ device.pushDeviceStates();
129
+ device.setFramebuffer(framebuffer);
130
+ for(let i = 0; i < 6; i++){
131
+ framebuffer.setColorAttachmentCubeFace(0, i);
132
+ device.setVertexLayout(vertexLayout);
133
+ device.setRenderStates(renderStates);
134
+ device.setProgram(program);
135
+ device.setBindGroup(0, bindgroup);
136
+ bindgroup.setValue('up', faceDirections[i][0]);
137
+ bindgroup.setValue('right', faceDirections[i][1]);
138
+ bindgroup.setValue('front', faceDirections[i][2]);
139
+ bindgroup.setTexture('u_panoramaTexture', srcTexture);
140
+ device.draw('triangle-list', 0, 6);
141
+ }
142
+ device.popDeviceStates();
143
+ }
144
+ /**
145
+ * Converts an equirectangular image to cubemap
146
+ *
147
+ * @param tex - The equirectangular image to be converted
148
+ * @param textureSize - cubemap size
149
+ * @returns The converted cubemap
150
+ *
151
+ * @public
152
+ */ function panoramaToCubemap(tex, outputCubeMap) {
153
+ const device = Application.instance.device;
154
+ if (!vertexLayout) {
155
+ init();
156
+ }
157
+ device.pushDeviceStates();
158
+ doConvertPanoramaToCubemap(tex, outputCubeMap);
159
+ device.popDeviceStates();
160
+ }
161
+
162
+ export { panoramaToCubemap };
163
+ //# sourceMappingURL=panorama.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"panorama.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}