@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,462 @@
1
+ import { AABB, Vector2, Matrix4x4, Vector3, ClipState } from '@zephyr3d/base';
2
+ import { Primitive } from './primitive.js';
3
+
4
+ const tmpAABB = new AABB();
5
+ const tmpExtents = new Vector2(0, 0);
6
+ const modelMatrices = [
7
+ Matrix4x4.identity(),
8
+ // rotation z 270
9
+ Matrix4x4.rotationZ(270 * Math.PI / 180),
10
+ //new Matrix4x4(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1),
11
+ // rotation z 90
12
+ Matrix4x4.rotationZ(90 * Math.PI / 180),
13
+ //new Matrix4x4(0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1),
14
+ // rotation z 180
15
+ Matrix4x4.rotationZ(180 * Math.PI / 180)
16
+ ];
17
+ /** @internal */ class Clipmap {
18
+ _tileResolution;
19
+ _tileMesh;
20
+ _tileMeshBBox;
21
+ _emptyTileMesh;
22
+ _emptyTileMeshBBox;
23
+ _fillerMesh;
24
+ _fillerMeshAABB;
25
+ _trimMesh;
26
+ _trimMeshAABB;
27
+ _crossMesh;
28
+ _crossMeshAABB;
29
+ _seamMesh;
30
+ _seamMeshAABB;
31
+ constructor(resolution){
32
+ this._tileResolution = resolution;
33
+ this.generateCrossMesh();
34
+ this.generateEmptyTileMesh();
35
+ this.generateFillerMesh();
36
+ this.generateSeamMesh();
37
+ this.generateTileMesh();
38
+ this.generateTrimMesh();
39
+ }
40
+ get tileResolution() {
41
+ return this.tileResolution;
42
+ }
43
+ set tileResolution(val) {
44
+ if (val !== this._tileResolution) {
45
+ this._tileResolution = val;
46
+ this.generateCrossMesh();
47
+ this.generateEmptyTileMesh();
48
+ this.generateFillerMesh();
49
+ this.generateSeamMesh();
50
+ this.generateTileMesh();
51
+ this.generateTrimMesh();
52
+ }
53
+ }
54
+ patch2d(tileResolution, x, y) {
55
+ return y * (tileResolution + 1) + x;
56
+ }
57
+ calcAABB(vertices) {
58
+ let maxX = -Number.MAX_VALUE;
59
+ let maxY = -Number.MAX_VALUE;
60
+ let minX = Number.MAX_VALUE;
61
+ let minY = Number.MAX_VALUE;
62
+ for(let i = 0; i < vertices.length / 3; i++){
63
+ const x = vertices[i * 3 + 0];
64
+ const y = vertices[i * 3 + 1];
65
+ if (x > maxX) {
66
+ maxX = x;
67
+ }
68
+ if (x < minX) {
69
+ minX = x;
70
+ }
71
+ if (y > maxY) {
72
+ maxY = y;
73
+ }
74
+ if (y < minY) {
75
+ minY = y;
76
+ }
77
+ }
78
+ return new AABB(new Vector3(minX, minY, 0), new Vector3(maxX, maxY, 0));
79
+ }
80
+ generateTileMesh() {
81
+ this._tileMesh?.dispose();
82
+ this._tileMesh = new Primitive();
83
+ const patchVertResolution = this._tileResolution + 1;
84
+ const vertices = new Float32Array(patchVertResolution * patchVertResolution * 3);
85
+ let n = 0;
86
+ for(let y = 0; y < patchVertResolution; y++){
87
+ for(let x = 0; x < patchVertResolution; x++){
88
+ vertices[n++] = x;
89
+ vertices[n++] = y;
90
+ vertices[n++] = 0;
91
+ }
92
+ }
93
+ n = 0;
94
+ const indices = new Uint16Array(this._tileResolution * this._tileResolution * 6);
95
+ for(let y = 0; y < this._tileResolution; y++){
96
+ for(let x = 0; x < this._tileResolution; x++){
97
+ indices[n++] = this.patch2d(this._tileResolution, x, y);
98
+ indices[n++] = this.patch2d(this._tileResolution, x + 1, y + 1);
99
+ indices[n++] = this.patch2d(this._tileResolution, x, y + 1);
100
+ indices[n++] = this.patch2d(this._tileResolution, x, y);
101
+ indices[n++] = this.patch2d(this._tileResolution, x + 1, y);
102
+ indices[n++] = this.patch2d(this._tileResolution, x + 1, y + 1);
103
+ }
104
+ }
105
+ this._tileMesh.createAndSetVertexBuffer('position_f32x3', vertices);
106
+ this._tileMesh.createAndSetIndexBuffer(indices);
107
+ this._tileMesh.indexStart = 0;
108
+ this._tileMesh.indexCount = indices.length;
109
+ this._tileMesh.primitiveType = 'triangle-list';
110
+ this._tileMeshBBox = this.calcAABB(vertices);
111
+ }
112
+ generateEmptyTileMesh() {
113
+ this._emptyTileMesh?.dispose();
114
+ this._emptyTileMesh = new Primitive();
115
+ const vertices = new Float32Array((this._tileResolution * 4 + 1) * 3);
116
+ vertices[0] = this._tileResolution * 0.5;
117
+ vertices[1] = this._tileResolution * 0.5;
118
+ vertices[2] = 0;
119
+ for(let i = 0; i < this._tileResolution; i++){
120
+ vertices[(i + 1 + 0 * this._tileResolution) * 3 + 0] = i;
121
+ vertices[(i + 1 + 0 * this._tileResolution) * 3 + 1] = 0;
122
+ vertices[(i + 1 + 0 * this._tileResolution) * 3 + 2] = 0;
123
+ vertices[(i + 1 + 1 * this._tileResolution) * 3 + 0] = this._tileResolution;
124
+ vertices[(i + 1 + 1 * this._tileResolution) * 3 + 1] = i;
125
+ vertices[(i + 1 + 1 * this._tileResolution) * 3 + 2] = 0;
126
+ vertices[(i + 1 + 2 * this._tileResolution) * 3 + 0] = this._tileResolution - i;
127
+ vertices[(i + 1 + 2 * this._tileResolution) * 3 + 1] = this._tileResolution;
128
+ vertices[(i + 1 + 2 * this._tileResolution) * 3 + 2] = 0;
129
+ vertices[(i + 1 + 3 * this._tileResolution) * 3 + 0] = 0;
130
+ vertices[(i + 1 + 3 * this._tileResolution) * 3 + 1] = this._tileResolution - i;
131
+ vertices[(i + 1 + 3 * this._tileResolution) * 3 + 2] = 0;
132
+ }
133
+ const indices = new Uint16Array(this._tileResolution * 12);
134
+ for(let i = 0; i < this._tileResolution * 4; i++){
135
+ indices[i * 3 + 0] = 0;
136
+ indices[i * 3 + 1] = i + 1;
137
+ indices[i * 3 + 2] = i + 2;
138
+ }
139
+ this._emptyTileMesh.createAndSetVertexBuffer('position_f32x3', vertices);
140
+ this._emptyTileMesh.createAndSetIndexBuffer(indices);
141
+ this._emptyTileMesh.indexStart = 0;
142
+ this._emptyTileMesh.indexCount = indices.length;
143
+ this._emptyTileMesh.primitiveType = 'triangle-list';
144
+ this._emptyTileMeshBBox = this.calcAABB(vertices);
145
+ }
146
+ generateFillerMesh() {
147
+ this._fillerMesh?.dispose();
148
+ this._fillerMesh = new Primitive();
149
+ const patchVertResolution = this._tileResolution + 1;
150
+ const vertices = new Float32Array(patchVertResolution * 8 * 3);
151
+ let n = 0;
152
+ const offset = this._tileResolution;
153
+ for(let i = 0; i < patchVertResolution; i++){
154
+ vertices[n++] = offset + i + 1;
155
+ vertices[n++] = 0;
156
+ vertices[n++] = 0;
157
+ vertices[n++] = offset + i + 1;
158
+ vertices[n++] = 1;
159
+ vertices[n++] = 0;
160
+ }
161
+ for(let i = 0; i < patchVertResolution; i++){
162
+ vertices[n++] = 1;
163
+ vertices[n++] = offset + i + 1;
164
+ vertices[n++] = 0;
165
+ vertices[n++] = 0;
166
+ vertices[n++] = offset + i + 1;
167
+ vertices[n++] = 0;
168
+ }
169
+ for(let i = 0; i < patchVertResolution; i++){
170
+ vertices[n++] = -offset - i;
171
+ vertices[n++] = 1;
172
+ vertices[n++] = 0;
173
+ vertices[n++] = -offset - i;
174
+ vertices[n++] = 0;
175
+ vertices[n++] = 0;
176
+ }
177
+ for(let i = 0; i < patchVertResolution; i++){
178
+ vertices[n++] = 0;
179
+ vertices[n++] = -offset - i;
180
+ vertices[n++] = 0;
181
+ vertices[n++] = 1;
182
+ vertices[n++] = -offset - i;
183
+ vertices[n++] = 0;
184
+ }
185
+ n = 0;
186
+ const indices = new Uint16Array(this._tileResolution * 24);
187
+ for(let i = 0; i < this._tileResolution * 4; i++){
188
+ const arm = i / this._tileResolution >> 0;
189
+ const bl = (arm + i) * 2 + 0;
190
+ const br = (arm + i) * 2 + 1;
191
+ const tl = (arm + i) * 2 + 2;
192
+ const tr = (arm + i) * 2 + 3;
193
+ if (arm % 2 === 0) {
194
+ indices[n++] = br;
195
+ indices[n++] = bl;
196
+ indices[n++] = tr;
197
+ indices[n++] = bl;
198
+ indices[n++] = tl;
199
+ indices[n++] = tr;
200
+ } else {
201
+ indices[n++] = br;
202
+ indices[n++] = bl;
203
+ indices[n++] = tl;
204
+ indices[n++] = br;
205
+ indices[n++] = tl;
206
+ indices[n++] = tr;
207
+ }
208
+ }
209
+ this._fillerMesh.createAndSetVertexBuffer('position_f32x3', vertices);
210
+ this._fillerMesh.createAndSetIndexBuffer(indices);
211
+ this._fillerMesh.indexStart = 0;
212
+ this._fillerMesh.indexCount = indices.length;
213
+ this._fillerMesh.primitiveType = 'triangle-list';
214
+ this._fillerMeshAABB = this.calcAABB(vertices);
215
+ }
216
+ generateTrimMesh() {
217
+ this._trimMesh?.dispose();
218
+ this._trimMesh = new Primitive();
219
+ const clipmapVertResolution = this._tileResolution * 4 + 2;
220
+ const vertices = new Float32Array((clipmapVertResolution * 2 + 1) * 2 * 3);
221
+ let n = 0;
222
+ const d = 0.5 * (clipmapVertResolution + 1);
223
+ for(let i = 0; i < clipmapVertResolution + 1; i++){
224
+ vertices[n++] = 0 - d;
225
+ vertices[n++] = clipmapVertResolution - i - d;
226
+ vertices[n++] = 0;
227
+ vertices[n++] = 1 - d;
228
+ vertices[n++] = clipmapVertResolution - i - d;
229
+ vertices[n++] = 0;
230
+ }
231
+ const startOfHorizonal = n / 3;
232
+ for(let i = 0; i < clipmapVertResolution; i++){
233
+ vertices[n++] = i + 1 - d;
234
+ vertices[n++] = 0 - d;
235
+ vertices[n++] = 0;
236
+ vertices[n++] = i + 1 - d;
237
+ vertices[n++] = 1 - d;
238
+ vertices[n++] = 0;
239
+ }
240
+ n = 0;
241
+ const indices = new Uint16Array((clipmapVertResolution * 2 - 1) * 6);
242
+ for(let i = 0; i < clipmapVertResolution; i++){
243
+ indices[n++] = (i + 0) * 2 + 1;
244
+ indices[n++] = (i + 0) * 2 + 0;
245
+ indices[n++] = (i + 1) * 2 + 0;
246
+ indices[n++] = (i + 1) * 2 + 1;
247
+ indices[n++] = (i + 0) * 2 + 1;
248
+ indices[n++] = (i + 1) * 2 + 0;
249
+ }
250
+ for(let i = 0; i < clipmapVertResolution - 1; i++){
251
+ indices[n++] = startOfHorizonal + (i + 0) * 2 + 1;
252
+ indices[n++] = startOfHorizonal + (i + 0) * 2 + 0;
253
+ indices[n++] = startOfHorizonal + (i + 1) * 2 + 0;
254
+ indices[n++] = startOfHorizonal + (i + 1) * 2 + 1;
255
+ indices[n++] = startOfHorizonal + (i + 0) * 2 + 1;
256
+ indices[n++] = startOfHorizonal + (i + 1) * 2 + 0;
257
+ }
258
+ this._trimMesh.createAndSetVertexBuffer('position_f32x3', vertices);
259
+ this._trimMesh.createAndSetIndexBuffer(indices);
260
+ this._trimMesh.indexStart = 0;
261
+ this._trimMesh.indexCount = indices.length;
262
+ this._trimMesh.primitiveType = 'triangle-list';
263
+ this._trimMeshAABB = this.calcAABB(vertices);
264
+ }
265
+ generateCrossMesh() {
266
+ this._crossMesh?.dispose();
267
+ this._crossMesh = new Primitive();
268
+ const patchVertResolution = this._tileResolution + 1;
269
+ const vertices = new Float32Array(patchVertResolution * 8 * 3);
270
+ let n = 0;
271
+ for(let i = 0; i < patchVertResolution * 2; i++){
272
+ vertices[n++] = i - this._tileResolution;
273
+ vertices[n++] = 0;
274
+ vertices[n++] = 0;
275
+ vertices[n++] = i - this._tileResolution;
276
+ vertices[n++] = 1;
277
+ vertices[n++] = 0;
278
+ }
279
+ const startOfVertical = n / 3;
280
+ for(let i = 0; i < patchVertResolution * 2; i++){
281
+ vertices[n++] = 0;
282
+ vertices[n++] = i - this._tileResolution;
283
+ vertices[n++] = 0;
284
+ vertices[n++] = 1;
285
+ vertices[n++] = i - this._tileResolution;
286
+ vertices[n++] = 0;
287
+ }
288
+ n = 0;
289
+ const indices = new Uint16Array(this._tileResolution * 24 + 6);
290
+ for(let i = 0; i < this._tileResolution * 2 + 1; i++){
291
+ const bl = i * 2 + 0;
292
+ const br = i * 2 + 1;
293
+ const tl = i * 2 + 2;
294
+ const tr = i * 2 + 3;
295
+ indices[n++] = br;
296
+ indices[n++] = bl;
297
+ indices[n++] = tr;
298
+ indices[n++] = bl;
299
+ indices[n++] = tl;
300
+ indices[n++] = tr;
301
+ }
302
+ for(let i = 0; i < this._tileResolution * 2 + 1; i++){
303
+ if (i === this._tileResolution) {
304
+ continue;
305
+ }
306
+ const bl = i * 2 + 0;
307
+ const br = i * 2 + 1;
308
+ const tl = i * 2 + 2;
309
+ const tr = i * 2 + 3;
310
+ indices[n++] = startOfVertical + br;
311
+ indices[n++] = startOfVertical + tr;
312
+ indices[n++] = startOfVertical + bl;
313
+ indices[n++] = startOfVertical + bl;
314
+ indices[n++] = startOfVertical + tr;
315
+ indices[n++] = startOfVertical + tl;
316
+ }
317
+ this._crossMesh.createAndSetVertexBuffer('position_f32x3', vertices);
318
+ this._crossMesh.createAndSetIndexBuffer(indices);
319
+ this._crossMesh.indexStart = 0;
320
+ this._crossMesh.indexCount = indices.length;
321
+ this._crossMesh.primitiveType = 'triangle-list';
322
+ this._crossMeshAABB = this.calcAABB(vertices);
323
+ }
324
+ generateSeamMesh() {
325
+ this._seamMesh?.dispose();
326
+ this._seamMesh = new Primitive();
327
+ const clipmapVertResolution = this._tileResolution * 4 + 2;
328
+ const vertices = new Float32Array(clipmapVertResolution * 4 * 3);
329
+ for(let i = 0; i < clipmapVertResolution; i++){
330
+ vertices[(clipmapVertResolution * 0 + i) * 3 + 0] = i;
331
+ vertices[(clipmapVertResolution * 0 + i) * 3 + 1] = 0;
332
+ vertices[(clipmapVertResolution * 0 + i) * 3 + 2] = 0;
333
+ vertices[(clipmapVertResolution * 1 + i) * 3 + 0] = clipmapVertResolution;
334
+ vertices[(clipmapVertResolution * 1 + i) * 3 + 1] = i;
335
+ vertices[(clipmapVertResolution * 1 + i) * 3 + 2] = 0;
336
+ vertices[(clipmapVertResolution * 2 + i) * 3 + 0] = clipmapVertResolution - i;
337
+ vertices[(clipmapVertResolution * 2 + i) * 3 + 1] = clipmapVertResolution;
338
+ vertices[(clipmapVertResolution * 2 + i) * 3 + 2] = 0;
339
+ vertices[(clipmapVertResolution * 3 + i) * 3 + 0] = 0;
340
+ vertices[(clipmapVertResolution * 3 + i) * 3 + 1] = clipmapVertResolution - i;
341
+ vertices[(clipmapVertResolution * 3 + i) * 3 + 2] = 0;
342
+ }
343
+ const indices = new Uint16Array(clipmapVertResolution * 6);
344
+ let n = 0;
345
+ for(let i = 0; i < clipmapVertResolution * 4; i += 2){
346
+ indices[n++] = i + 1;
347
+ indices[n++] = i;
348
+ indices[n++] = i + 2;
349
+ }
350
+ indices[indices.length - 1] = 0;
351
+ this._seamMesh.createAndSetVertexBuffer('position_f32x3', vertices);
352
+ this._seamMesh.createAndSetIndexBuffer(indices);
353
+ this._seamMesh.indexStart = 0;
354
+ this._seamMesh.indexCount = indices.length;
355
+ this._seamMesh.primitiveType = 'triangle-list';
356
+ this._seamMeshAABB = this.calcAABB(vertices);
357
+ }
358
+ intervalsOverlap(a0, a1, b0, b1) {
359
+ return a0 <= b1 && b0 <= a1;
360
+ }
361
+ visible(aabb, camera, modelMatrix, offset, scale, AABBExtents, gridScale) {
362
+ if (!modelMatrix) {
363
+ tmpAABB.minPoint.set(aabb.minPoint);
364
+ tmpAABB.maxPoint.set(aabb.maxPoint);
365
+ } else {
366
+ AABB.transform(aabb, modelMatrix, tmpAABB);
367
+ }
368
+ tmpAABB.minPoint.x = (tmpAABB.minPoint.x * scale + offset.x) * gridScale - AABBExtents.x;
369
+ tmpAABB.minPoint.z = (tmpAABB.minPoint.y * scale + offset.y) * gridScale - AABBExtents.x;
370
+ tmpAABB.minPoint.y = -AABBExtents.y;
371
+ tmpAABB.maxPoint.x = (tmpAABB.maxPoint.x * scale + offset.x) * gridScale + AABBExtents.x;
372
+ tmpAABB.maxPoint.z = (tmpAABB.maxPoint.y * scale + offset.y) * gridScale + AABBExtents.x;
373
+ tmpAABB.maxPoint.y = AABBExtents.y;
374
+ return tmpAABB.getClipStateWithFrustum(camera.frustum) !== ClipState.NOT_CLIPPED;
375
+ }
376
+ draw(context, numMipmaps) {
377
+ let drawn = 0;
378
+ const distX = Math.max(Math.abs(context.position.x - context.minWorldPos.x), Math.abs(context.position.x - context.maxWorldPos.x));
379
+ const distY = Math.max(Math.abs(context.position.y - context.minWorldPos.y), Math.abs(context.position.y - context.maxWorldPos.y));
380
+ const maxDist = Math.min(Math.max(distX, distY), context.camera.getFarPlane());
381
+ const mipLevels = Math.ceil(Math.log2(maxDist / (this._tileResolution * context.gridScale))) + 1;
382
+ const snappedPos = new Vector2();
383
+ const tileSize = new Vector2();
384
+ const base = new Vector2();
385
+ const offset = new Vector2();
386
+ const posX = context.position.x / context.gridScale;
387
+ const posY = context.position.y / context.gridScale;
388
+ const h = context.AABBExtents ?? tmpExtents;
389
+ // draw cross
390
+ snappedPos.setXY(Math.floor(posX), Math.floor(posY));
391
+ if (this.visible(this._crossMeshAABB, context.camera, null, snappedPos, 1, h, context.gridScale)) {
392
+ context.drawPrimitive(this._crossMesh, modelMatrices[0], snappedPos, 1, context.gridScale);
393
+ drawn++;
394
+ }
395
+ for(let l = 0; l < mipLevels; l++){
396
+ const scale = 1 << l;
397
+ snappedPos.setXY(Math.floor(posX / scale) * scale, Math.floor(posY / scale) * scale);
398
+ // draw tiles
399
+ tileSize.setXY(this._tileResolution << l, this._tileResolution << l);
400
+ base.setXY(snappedPos.x - (this._tileResolution << l + 1), snappedPos.y - (this._tileResolution << l + 1));
401
+ for(let x = 0; x < 4; x++){
402
+ for(let y = 0; y < 4; y++){
403
+ if (l !== 0 && (x === 1 || x === 2) && (y === 1 || y === 2)) {
404
+ continue;
405
+ }
406
+ const fillX = x >= 2 ? scale : 0;
407
+ const fillY = y >= 2 ? scale : 0;
408
+ offset.setXY(base.x + x * tileSize.x + fillX, base.y + y * tileSize.y + fillY);
409
+ if (this.intervalsOverlap(offset.x * context.gridScale, (offset.x + tileSize.x) * context.gridScale, context.minWorldPos.x, context.maxWorldPos.x) && this.intervalsOverlap(offset.y * context.gridScale, (offset.y + tileSize.y) * context.gridScale, context.minWorldPos.y, context.maxWorldPos.y)) {
410
+ if (this.visible(this._tileMeshBBox, context.camera, null, offset, scale, h, context.gridScale)) {
411
+ context.drawPrimitive(this._tileMesh, modelMatrices[0], offset, scale, context.gridScale);
412
+ drawn++;
413
+ }
414
+ }
415
+ }
416
+ }
417
+ // draw filler
418
+ if (this.visible(this._fillerMeshAABB, context.camera, null, snappedPos, scale, h, context.gridScale)) {
419
+ context.drawPrimitive(this._fillerMesh, modelMatrices[0], snappedPos, scale, context.gridScale);
420
+ drawn++;
421
+ }
422
+ if (l !== numMipmaps - 1) {
423
+ const nextScale = scale * 2;
424
+ const nextSnappedPos = new Vector2(Math.floor(posX / nextScale) * nextScale, Math.floor(posY / nextScale) * nextScale);
425
+ // draw trim
426
+ const tileCentre = new Vector2(snappedPos.x + scale * 0.5, snappedPos.y + scale * 0.5);
427
+ const d = new Vector2(posX - nextSnappedPos.x, posY - nextSnappedPos.y);
428
+ let r = 0;
429
+ r |= d.x >= scale ? 0 : 2;
430
+ r |= d.y >= scale ? 0 : 1;
431
+ if (this.visible(this._trimMeshAABB, context.camera, r === 0 ? null : modelMatrices[r], tileCentre, scale, h, context.gridScale)) {
432
+ context.drawPrimitive(this._trimMesh, modelMatrices[r], tileCentre, scale, context.gridScale);
433
+ drawn++;
434
+ }
435
+ // draw seam
436
+ const nextBase = new Vector2(nextSnappedPos.x - (this._tileResolution << l + 1), nextSnappedPos.y - (this._tileResolution << l + 1));
437
+ if (this.visible(this._seamMeshAABB, context.camera, null, nextBase, scale, h, context.gridScale)) {
438
+ context.drawPrimitive(this._seamMesh, modelMatrices[0], nextBase, scale, context.gridScale);
439
+ drawn++;
440
+ }
441
+ }
442
+ }
443
+ return drawn;
444
+ }
445
+ dispose() {
446
+ this._crossMesh.dispose();
447
+ this._crossMesh = null;
448
+ this._emptyTileMesh.dispose();
449
+ this._emptyTileMesh = null;
450
+ this._fillerMesh.dispose();
451
+ this._fillerMesh = null;
452
+ this._seamMesh.dispose();
453
+ this._seamMesh = null;
454
+ this._trimMesh.dispose();
455
+ this._trimMesh = null;
456
+ this._tileMesh.dispose();
457
+ this._tileMesh = null;
458
+ }
459
+ }
460
+
461
+ export { Clipmap };
462
+ //# sourceMappingURL=clipmap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clipmap.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}