@zephyr3d/scene 0.4.0 → 0.6.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 (233) hide show
  1. package/dist/animation/animation.js +25 -117
  2. package/dist/animation/animation.js.map +1 -1
  3. package/dist/animation/animationset.js +191 -51
  4. package/dist/animation/animationset.js.map +1 -1
  5. package/dist/animation/animationtrack.js +6 -18
  6. package/dist/animation/animationtrack.js.map +1 -1
  7. package/dist/animation/eulerrotationtrack.js +16 -6
  8. package/dist/animation/eulerrotationtrack.js.map +1 -1
  9. package/dist/animation/morphtarget.js +104 -0
  10. package/dist/animation/morphtarget.js.map +1 -0
  11. package/dist/animation/morphtrack.js +70 -0
  12. package/dist/animation/morphtrack.js.map +1 -0
  13. package/dist/animation/rotationtrack.js +15 -7
  14. package/dist/animation/rotationtrack.js.map +1 -1
  15. package/dist/animation/scaletrack.js +15 -7
  16. package/dist/animation/scaletrack.js.map +1 -1
  17. package/dist/animation/skeleton.js +107 -5
  18. package/dist/animation/skeleton.js.map +1 -1
  19. package/dist/animation/translationtrack.js +15 -7
  20. package/dist/animation/translationtrack.js.map +1 -1
  21. package/dist/app.js +4 -26
  22. package/dist/app.js.map +1 -1
  23. package/dist/asset/assetmanager.js +60 -109
  24. package/dist/asset/assetmanager.js.map +1 -1
  25. package/dist/asset/loaders/dds/dds.js +77 -3
  26. package/dist/asset/loaders/dds/dds.js.map +1 -1
  27. package/dist/asset/loaders/dds/dds_loader.js +1 -1
  28. package/dist/asset/loaders/gltf/gltf_loader.js +287 -40
  29. package/dist/asset/loaders/gltf/gltf_loader.js.map +1 -1
  30. package/dist/asset/loaders/image/tga_Loader.js +1 -1
  31. package/dist/asset/loaders/image/webimage_loader.js +16 -0
  32. package/dist/asset/loaders/image/webimage_loader.js.map +1 -1
  33. package/dist/asset/model.js +16 -9
  34. package/dist/asset/model.js.map +1 -1
  35. package/dist/blitter/bilateralblur.js +222 -0
  36. package/dist/{render/temporalcache.js.map → blitter/bilateralblur.js.map} +1 -1
  37. package/dist/blitter/blitter.js +9 -3
  38. package/dist/blitter/blitter.js.map +1 -1
  39. package/dist/blitter/depthlimitedgaussion.js +96 -39
  40. package/dist/blitter/depthlimitedgaussion.js.map +1 -1
  41. package/dist/blitter/gaussianblur.js +21 -21
  42. package/dist/camera/camera.js +200 -4
  43. package/dist/camera/camera.js.map +1 -1
  44. package/dist/index.d.ts +6406 -5786
  45. package/dist/index.js +12 -10
  46. package/dist/index.js.map +1 -1
  47. package/dist/material/blinn.js +15 -4
  48. package/dist/material/blinn.js.map +1 -1
  49. package/dist/material/lambert.js +26 -17
  50. package/dist/material/lambert.js.map +1 -1
  51. package/dist/material/material.js +13 -2
  52. package/dist/material/material.js.map +1 -1
  53. package/dist/material/meshmaterial.js +103 -31
  54. package/dist/material/meshmaterial.js.map +1 -1
  55. package/dist/material/mixins/albedocolor.js +5 -4
  56. package/dist/material/mixins/albedocolor.js.map +1 -1
  57. package/dist/material/mixins/lightmodel/blinnphong.js +17 -7
  58. package/dist/material/mixins/lightmodel/blinnphong.js.map +1 -1
  59. package/dist/material/mixins/lightmodel/lambert.js +5 -5
  60. package/dist/material/mixins/lightmodel/pbrmetallicroughness.js +16 -7
  61. package/dist/material/mixins/lightmodel/pbrmetallicroughness.js.map +1 -1
  62. package/dist/material/mixins/lightmodel/pbrspecularglossness.js +16 -7
  63. package/dist/material/mixins/lightmodel/pbrspecularglossness.js.map +1 -1
  64. package/dist/material/mixins/lit.js +2 -2
  65. package/dist/material/mixins/pbr/common.js +454 -19
  66. package/dist/material/mixins/pbr/common.js.map +1 -1
  67. package/dist/material/pbrmr.js +28 -6
  68. package/dist/material/pbrmr.js.map +1 -1
  69. package/dist/material/pbrsg.js +27 -9
  70. package/dist/material/pbrsg.js.map +1 -1
  71. package/dist/material/shader/helper.js +128 -23
  72. package/dist/material/shader/helper.js.map +1 -1
  73. package/dist/material/unlit.js +8 -4
  74. package/dist/material/unlit.js.map +1 -1
  75. package/dist/posteffect/bloom.js +34 -53
  76. package/dist/posteffect/bloom.js.map +1 -1
  77. package/dist/posteffect/compositor.js +48 -58
  78. package/dist/posteffect/compositor.js.map +1 -1
  79. package/dist/posteffect/fxaa.js +3 -11
  80. package/dist/posteffect/fxaa.js.map +1 -1
  81. package/dist/posteffect/grayscale.js +3 -11
  82. package/dist/posteffect/grayscale.js.map +1 -1
  83. package/dist/posteffect/posteffect.js +4 -0
  84. package/dist/posteffect/posteffect.js.map +1 -1
  85. package/dist/posteffect/sao.js +55 -48
  86. package/dist/posteffect/sao.js.map +1 -1
  87. package/dist/posteffect/ssr.js +536 -0
  88. package/dist/{material/lit.js.map → posteffect/ssr.js.map} +1 -1
  89. package/dist/posteffect/tonemap.js +3 -11
  90. package/dist/posteffect/tonemap.js.map +1 -1
  91. package/dist/posteffect/water.js +306 -340
  92. package/dist/posteffect/water.js.map +1 -1
  93. package/dist/render/abuffer_oit.js +2 -2
  94. package/dist/render/clipmap.js +16 -19
  95. package/dist/render/clipmap.js.map +1 -1
  96. package/dist/render/cull_visitor.js +8 -6
  97. package/dist/render/cull_visitor.js.map +1 -1
  98. package/dist/render/depthpass.js +30 -14
  99. package/dist/render/depthpass.js.map +1 -1
  100. package/dist/render/drawable_mixin.js +70 -22
  101. package/dist/render/drawable_mixin.js.map +1 -1
  102. package/dist/render/envlight.js +169 -33
  103. package/dist/render/envlight.js.map +1 -1
  104. package/dist/render/fft_wavegenerator.js +989 -0
  105. package/dist/{shaders/framework.js.map → render/fft_wavegenerator.js.map} +1 -1
  106. package/dist/render/gerstner_wavegenerator.js +265 -0
  107. package/dist/{material/standard.js.map → render/gerstner_wavegenerator.js.map} +1 -1
  108. package/dist/render/globalbindgroup_allocator.js +2 -1
  109. package/dist/render/globalbindgroup_allocator.js.map +1 -1
  110. package/dist/render/hzb.js +273 -0
  111. package/dist/{material/terrainlightmodel.js.map → render/hzb.js.map} +1 -1
  112. package/dist/render/lightpass.js +68 -28
  113. package/dist/render/lightpass.js.map +1 -1
  114. package/dist/render/objectcolorpass.js +51 -0
  115. package/dist/render/objectcolorpass.js.map +1 -0
  116. package/dist/render/render_queue.js +211 -158
  117. package/dist/render/render_queue.js.map +1 -1
  118. package/dist/render/renderbundle_wrapper.js +79 -0
  119. package/dist/render/renderbundle_wrapper.js.map +1 -1
  120. package/dist/render/renderer.js +151 -35
  121. package/dist/render/renderer.js.map +1 -1
  122. package/dist/render/renderpass.js +27 -20
  123. package/dist/render/renderpass.js.map +1 -1
  124. package/dist/render/shadowmap_pass.js +20 -14
  125. package/dist/render/shadowmap_pass.js.map +1 -1
  126. package/dist/render/sky.js +12 -13
  127. package/dist/render/sky.js.map +1 -1
  128. package/dist/render/watermesh.js +94 -828
  129. package/dist/render/watermesh.js.map +1 -1
  130. package/dist/render/wavegenerator.js +8 -0
  131. package/dist/render/wavegenerator.js.map +1 -0
  132. package/dist/render/weightedblended_oit.js +11 -28
  133. package/dist/render/weightedblended_oit.js.map +1 -1
  134. package/dist/scene/batchgroup.js +60 -14
  135. package/dist/scene/batchgroup.js.map +1 -1
  136. package/dist/scene/environment.js +24 -3
  137. package/dist/scene/environment.js.map +1 -1
  138. package/dist/scene/graph_node.js +0 -14
  139. package/dist/scene/graph_node.js.map +1 -1
  140. package/dist/scene/light.js +5 -5
  141. package/dist/scene/mesh.js +62 -15
  142. package/dist/scene/mesh.js.map +1 -1
  143. package/dist/scene/octree.js +5 -2
  144. package/dist/scene/octree.js.map +1 -1
  145. package/dist/scene/raycast_visitor.js +4 -2
  146. package/dist/scene/raycast_visitor.js.map +1 -1
  147. package/dist/scene/scene.js +6 -9
  148. package/dist/scene/scene.js.map +1 -1
  149. package/dist/scene/scene_node.js +11 -8
  150. package/dist/scene/scene_node.js.map +1 -1
  151. package/dist/scene/terrain/grass.js +10 -2
  152. package/dist/scene/terrain/grass.js.map +1 -1
  153. package/dist/scene/terrain/heightfield.js +135 -53
  154. package/dist/scene/terrain/heightfield.js.map +1 -1
  155. package/dist/scene/terrain/patch.js +10 -2
  156. package/dist/scene/terrain/patch.js.map +1 -1
  157. package/dist/scene/terrain/quadtree.js +2 -2
  158. package/dist/scene/terrain/terrain.js +1 -1
  159. package/dist/scene/xform.js +7 -9
  160. package/dist/scene/xform.js.map +1 -1
  161. package/dist/shaders/misc.js +10 -1
  162. package/dist/shaders/misc.js.map +1 -1
  163. package/dist/shaders/noise.js +81 -16
  164. package/dist/shaders/noise.js.map +1 -1
  165. package/dist/shaders/shadow.js +1 -9
  166. package/dist/shaders/shadow.js.map +1 -1
  167. package/dist/shaders/ssr.js +442 -0
  168. package/dist/{material/terrainmat.js.map → shaders/ssr.js.map} +1 -1
  169. package/dist/shaders/water.js +377 -250
  170. package/dist/shaders/water.js.map +1 -1
  171. package/dist/shadow/esm.js +4 -22
  172. package/dist/shadow/esm.js.map +1 -1
  173. package/dist/shadow/shadowmapper.js +56 -31
  174. package/dist/shadow/shadowmapper.js.map +1 -1
  175. package/dist/shadow/vsm.js +4 -24
  176. package/dist/shadow/vsm.js.map +1 -1
  177. package/dist/shapes/cylinder.js +6 -5
  178. package/dist/shapes/cylinder.js.map +1 -1
  179. package/dist/utility/bounding_volume.js +1 -53
  180. package/dist/utility/bounding_volume.js.map +1 -1
  181. package/dist/utility/draco/decoder.js +116 -0
  182. package/dist/utility/draco/decoder.js.map +1 -0
  183. package/dist/utility/misc.js +93 -0
  184. package/dist/utility/misc.js.map +1 -0
  185. package/dist/utility/shprojection.js +2 -7
  186. package/dist/utility/shprojection.js.map +1 -1
  187. package/dist/utility/textures/ggxlut.js +213 -0
  188. package/dist/utility/textures/ggxlut.js.map +1 -0
  189. package/dist/utility/textures/gradientnoise.js +61 -0
  190. package/dist/utility/textures/gradientnoise.js.map +1 -0
  191. package/dist/utility/textures/randomnoise.js +41 -0
  192. package/dist/utility/textures/randomnoise.js.map +1 -0
  193. package/dist/values.js +25 -1
  194. package/dist/values.js.map +1 -1
  195. package/package.json +5 -8
  196. package/dist/animation/usertrack.js +0 -47
  197. package/dist/animation/usertrack.js.map +0 -1
  198. package/dist/material/grassmat.js +0 -127
  199. package/dist/material/grassmat.js.map +0 -1
  200. package/dist/material/lightmodel.js +0 -2074
  201. package/dist/material/lightmodel.js.map +0 -1
  202. package/dist/material/lit.js +0 -578
  203. package/dist/material/mixins/pbr/metallicroughness.js +0 -126
  204. package/dist/material/mixins/pbr/metallicroughness.js.map +0 -1
  205. package/dist/material/mixins/pbr/specularglossness.js +0 -104
  206. package/dist/material/mixins/pbr/specularglossness.js.map +0 -1
  207. package/dist/material/pbr.js +0 -27
  208. package/dist/material/pbr.js.map +0 -1
  209. package/dist/material/standard.js +0 -282
  210. package/dist/material/terrainlightmodel.js +0 -259
  211. package/dist/material/terrainmat.js +0 -357
  212. package/dist/render/depth_pass.js +0 -47
  213. package/dist/render/depth_pass.js.map +0 -1
  214. package/dist/render/forward.js +0 -186
  215. package/dist/render/forward.js.map +0 -1
  216. package/dist/render/forward_pass.js +0 -137
  217. package/dist/render/forward_pass.js.map +0 -1
  218. package/dist/render/helper.js +0 -38
  219. package/dist/render/helper.js.map +0 -1
  220. package/dist/render/renderscheme.js +0 -61
  221. package/dist/render/renderscheme.js.map +0 -1
  222. package/dist/render/temporalcache.js +0 -222
  223. package/dist/scene/model.js +0 -111
  224. package/dist/scene/model.js.map +0 -1
  225. package/dist/scene/octree_update_visitor.js +0 -20
  226. package/dist/scene/octree_update_visitor.js.map +0 -1
  227. package/dist/shaders/builtins.js +0 -110
  228. package/dist/shaders/builtins.js.map +0 -1
  229. package/dist/shaders/framework.js +0 -723
  230. package/dist/shaders/lighting.js +0 -335
  231. package/dist/shaders/lighting.js.map +0 -1
  232. package/dist/utility/sheenlut.js +0 -196
  233. package/dist/utility/sheenlut.js.map +0 -1
@@ -1,304 +1,97 @@
1
- import { Vector2, Vector4, Vector3, PRNG } from '@zephyr3d/base';
1
+ import { Vector4, Vector3 } from '@zephyr3d/base';
2
2
  import { Application } from '../app.js';
3
3
  import { Clipmap } from './clipmap.js';
4
- import { Primitive } from './primitive.js';
5
- import { createProgramH0, createProgramHk, createProgramFFT2H, createProgramFFT2V, createProgramPostFFT2, createProgramOcean } from '../shaders/water.js';
4
+ import { createProgramOcean } from '../shaders/water.js';
6
5
 
7
- /** @internal */ const defaultBuildParams = {
8
- cascades: [
9
- {
10
- size: 450.0,
11
- strength: 0.8,
12
- croppiness: -1.2,
13
- minWave: 0,
14
- maxWave: 100
15
- },
16
- {
17
- size: 103.0,
18
- strength: 0.8,
19
- croppiness: -1.5,
20
- minWave: 0,
21
- maxWave: 100
22
- },
23
- {
24
- size: 13,
25
- strength: 0.9,
26
- croppiness: -1.5,
27
- minWave: 0,
28
- maxWave: 7
29
- }
30
- ],
31
- resolution: 256,
32
- wind: new Vector2(2, 2),
33
- alignment: 0.01,
34
- foamParams: new Vector2(1, 2),
35
- randomSeed: 0
36
- };
37
- const RENDER_NONE = 0;
38
- const RENDER_NORMAL = 1;
39
- const RENDER_TWO_PASS = 2;
40
6
  /** @internal */ class WaterMesh {
41
- static _globals = null;
42
- _h0BindGroup;
43
- _hkBindGroup;
44
- _hkBindGroup2;
45
- _hkBindGroup4;
46
- _fft2hBindGroup;
47
- _fft2vBindGroup;
48
- _fft2hBindGroup2Used;
49
- _fft2hBindGroup2Free;
50
- _fft2hBindGroup4Used;
51
- _fft2hBindGroup4Free;
52
- _fft2vBindGroup2Used;
53
- _fft2vBindGroup2Free;
54
- _fft2vBindGroup4Used;
55
- _fft2vBindGroup4Free;
56
- _postfft2BindGroup;
57
- _postfft2BindGroup2;
58
- _postfft2BindGroup4;
7
+ _impl;
8
+ _waveGenerator;
59
9
  _waterBindGroup;
60
10
  _usedClipmapBindGroups;
61
11
  _freeClipmapBindGroups;
62
- _nearestRepeatSampler;
63
- _linearRepeatSampler;
64
- _updateRenderStates;
65
12
  _waterRenderStates;
66
13
  _wireframe;
67
14
  _gridScale;
15
+ _speed;
68
16
  _level;
69
17
  _tileSize;
70
- _regionMin;
71
- _regionMax;
72
- _sizes;
73
- _croppinesses;
74
- _params;
75
- _instanceData;
76
- _ifftTextures;
18
+ _region;
77
19
  _clipmap;
78
- _aabbExtents;
79
- _cascades;
80
- _paramsChanged;
81
- _resolutionChanged;
82
- _textureFormat;
83
- _h0TextureFormat;
84
- _dataTextureFormat;
85
- _renderMode;
86
20
  _updateFrameStamp;
87
21
  _waterProgram;
88
- constructor(device, impl){
89
- const renderTargetFloat32 = device.getDeviceCaps().textureCaps.supportFloatColorBuffer;
90
- const linearFloat32 = renderTargetFloat32 && device.getDeviceCaps().textureCaps.supportLinearFloatTexture;
91
- const renderTargetFloat16 = device.getDeviceCaps().textureCaps.supportHalfFloatColorBuffer;
92
- const maxDrawBuffers = /*device.type !== 'webgl' && */ renderTargetFloat16 ? device.getDeviceCaps().framebufferCaps.maxDrawBuffers : 0;
93
- const maxSampleBytes = device.getDeviceCaps().framebufferCaps.maxColorAttachmentBytesPerSample;
94
- if (maxDrawBuffers >= 6 && maxSampleBytes >= 96) {
95
- this._renderMode = RENDER_NORMAL;
96
- this._textureFormat = renderTargetFloat32 ? 'rgba32f' : 'rgba16f';
97
- this._h0TextureFormat = renderTargetFloat32 ? 'rgba32f' : 'rgba16f';
98
- this._dataTextureFormat = linearFloat32 ? 'rgba32f' : 'rgba16f';
99
- } else if (maxDrawBuffers >= 6 && maxSampleBytes >= 48) {
100
- this._renderMode = RENDER_NORMAL;
101
- this._textureFormat = 'rgba16f';
102
- this._h0TextureFormat = renderTargetFloat32 ? 'rgba32f' : 'rgba16f';
103
- this._dataTextureFormat = 'rgba16f';
104
- } else if (maxDrawBuffers >= 4 && maxSampleBytes >= 64) {
105
- this._renderMode = RENDER_TWO_PASS;
106
- this._textureFormat = renderTargetFloat32 ? 'rgba32f' : 'rgba16f';
107
- this._h0TextureFormat = renderTargetFloat32 ? 'rgba32f' : 'rgba16f';
108
- this._dataTextureFormat = renderTargetFloat32 && linearFloat32 ? 'rgba32f' : 'rgba16f';
109
- } else if (maxDrawBuffers >= 4 && maxSampleBytes >= 32) {
110
- this._renderMode = RENDER_TWO_PASS;
111
- this._textureFormat = 'rgba16f';
112
- this._h0TextureFormat = 'rgba16f';
113
- this._dataTextureFormat = 'rgba16f';
114
- } else {
115
- this._renderMode = RENDER_NONE;
116
- this._textureFormat = null;
117
- this._h0TextureFormat = null;
118
- this._dataTextureFormat = null;
119
- }
120
- if (this._renderMode !== RENDER_NONE) {
121
- WaterMesh._globals = WaterMesh._globals ?? {
122
- programs: {
123
- h0Program: createProgramH0(),
124
- hkProgram: this._renderMode === RENDER_NORMAL ? createProgramHk() : null,
125
- hkProgram2: this._renderMode === RENDER_TWO_PASS ? createProgramHk(2) : null,
126
- hkProgram4: this._renderMode === RENDER_TWO_PASS ? createProgramHk(4) : null,
127
- fft2hProgram: this._renderMode === RENDER_NORMAL ? createProgramFFT2H() : null,
128
- fft2hProgram2: this._renderMode === RENDER_TWO_PASS ? createProgramFFT2H(2) : null,
129
- fft2hProgram4: this._renderMode === RENDER_TWO_PASS ? createProgramFFT2H(4) : null,
130
- fft2vProgram: this._renderMode === RENDER_NORMAL ? createProgramFFT2V() : null,
131
- fft2vProgram2: this._renderMode === RENDER_TWO_PASS ? createProgramFFT2V(2) : null,
132
- fft2vProgram4: this._renderMode === RENDER_TWO_PASS ? createProgramFFT2V(4) : null,
133
- postfft2Program: this._renderMode === RENDER_NORMAL ? createProgramPostFFT2() : null,
134
- postfft2Program2: this._renderMode === RENDER_TWO_PASS ? createProgramPostFFT2(2) : null,
135
- postfft2Program4: this._renderMode === RENDER_TWO_PASS ? createProgramPostFFT2(4) : null
136
- },
137
- quad: WaterMesh.createQuad(device),
138
- noiseTextures: new Map(),
139
- butterflyTextures: new Map()
140
- };
141
- this._waterProgram = createProgramOcean(impl);
142
- this._params = defaultBuildParams;
143
- const programs = WaterMesh._globals.programs;
144
- this._h0BindGroup = device.createBindGroup(programs.h0Program.bindGroupLayouts[0]);
145
- this._hkBindGroup = programs.hkProgram ? device.createBindGroup(WaterMesh._globals.programs.hkProgram.bindGroupLayouts[0]) : null;
146
- this._hkBindGroup2 = programs.hkProgram2 ? device.createBindGroup(WaterMesh._globals.programs.hkProgram2.bindGroupLayouts[0]) : null;
147
- this._hkBindGroup4 = programs.hkProgram4 ? device.createBindGroup(WaterMesh._globals.programs.hkProgram4.bindGroupLayouts[0]) : null;
148
- this._fft2hBindGroup = programs.fft2hProgram ? device.createBindGroup(WaterMesh._globals.programs.fft2hProgram.bindGroupLayouts[0]) : null;
149
- this._fft2vBindGroup = programs.fft2vProgram ? device.createBindGroup(WaterMesh._globals.programs.fft2vProgram.bindGroupLayouts[0]) : null;
150
- this._fft2hBindGroup2Used = [
151
- [],
152
- []
153
- ];
154
- this._fft2hBindGroup2Free = [
155
- [],
156
- []
157
- ];
158
- this._fft2hBindGroup4Used = [
159
- [],
160
- []
161
- ];
162
- this._fft2hBindGroup4Free = [
163
- [],
164
- []
165
- ];
166
- this._fft2vBindGroup2Used = [
167
- [],
168
- []
169
- ];
170
- this._fft2vBindGroup2Free = [
171
- [],
172
- []
173
- ];
174
- this._fft2vBindGroup4Used = [
175
- [],
176
- []
177
- ];
178
- this._fft2vBindGroup4Free = [
179
- [],
180
- []
181
- ];
182
- this._postfft2BindGroup = programs.postfft2Program ? device.createBindGroup(WaterMesh._globals.programs.postfft2Program.bindGroupLayouts[0]) : null;
183
- this._postfft2BindGroup2 = programs.postfft2Program2 ? device.createBindGroup(WaterMesh._globals.programs.postfft2Program2.bindGroupLayouts[0]) : null;
184
- this._postfft2BindGroup4 = programs.postfft2Program4 ? device.createBindGroup(WaterMesh._globals.programs.postfft2Program4.bindGroupLayouts[0]) : null;
22
+ _waveGeneratorHash;
23
+ constructor(){
24
+ this._waterProgram = null; //this._impl ? createProgramOcean(this._useComputeShader, this._impl) : null;
25
+ this._waterBindGroup = null; //this._waterProgram ? device.createBindGroup(this._waterProgram.bindGroupLayouts[0]) : null;
26
+ this._wireframe = false;
27
+ this._gridScale = 1;
28
+ this._speed = 1.5;
29
+ this._level = 0;
30
+ this._region = new Vector4(-99999, -99999, 99999, 99999);
31
+ this._tileSize = 32;
32
+ this._waterRenderStates = Application.instance.device.createRenderStateSet();
33
+ this._waterRenderStates.useRasterizerState().setCullMode('none');
34
+ this._waterRenderStates.useDepthState().enableTest(true).enableWrite(true).setCompareFunc('le');
35
+ this._clipmap = new Clipmap(this._tileSize);
36
+ this._updateFrameStamp = -1;
37
+ this._usedClipmapBindGroups = [];
38
+ this._freeClipmapBindGroups = [];
39
+ this._waveGeneratorHash = '';
40
+ }
41
+ get speed() {
42
+ return this._speed;
43
+ }
44
+ set speed(val) {
45
+ this._speed = val;
46
+ }
47
+ /** @internal */ get shadingImpl() {
48
+ return this._impl;
49
+ }
50
+ /** @internal */ set shadingImpl(val) {
51
+ if (val && val !== this._impl) {
52
+ this._impl = val;
53
+ this._waterProgram?.dispose();
54
+ this._waterProgram = null;
55
+ this._waterBindGroup?.dispose();
56
+ this._waterBindGroup = null;
57
+ }
58
+ }
59
+ /** @internal */ get waveImpl() {
60
+ return this._waveGenerator;
61
+ }
62
+ /** @internal */ set waveImpl(val) {
63
+ if (val && val !== this._waveGenerator) {
64
+ this._waveGenerator = val;
65
+ this._waterProgram?.dispose();
66
+ this._waterProgram = null;
67
+ this._waterBindGroup?.dispose();
68
+ this._waterBindGroup = null;
69
+ }
70
+ }
71
+ /** @internal */ getWaterBindGroup(device) {
72
+ return this.prepareForRender(device) ? this._waterBindGroup : null;
73
+ }
74
+ /** @internal */ prepareForRender(device) {
75
+ if (!this._impl || !this._waveGenerator?.isOk(device)) {
76
+ return false;
77
+ }
78
+ if (this._waterProgram && this._waveGeneratorHash !== this._waveGenerator.getHash(device)) {
79
+ this._waterProgram.dispose();
80
+ this._waterProgram = null;
81
+ this._waterBindGroup.dispose();
82
+ this._waterBindGroup = null;
83
+ }
84
+ if (!this._waterProgram) {
85
+ this._waterProgram = createProgramOcean(this._waveGenerator, this._impl);
185
86
  this._waterBindGroup = device.createBindGroup(this._waterProgram.bindGroupLayouts[0]);
186
- this._instanceData = null;
187
- this._ifftTextures = null;
188
- this._wireframe = false;
189
- this._gridScale = 1;
190
- this._level = 0;
191
- this._regionMin = new Vector2(-99999, -99999);
192
- this._regionMax = new Vector2(99999, 99999);
193
- this._sizes = new Vector4();
194
- this._tileSize = 32;
195
- this._croppinesses = new Vector4();
196
- this._cascades = [
197
- new Vector4(),
198
- new Vector4(),
199
- new Vector4(),
200
- new Vector4()
201
- ];
202
- this._nearestRepeatSampler = Application.instance.device.createSampler({
203
- minFilter: 'nearest',
204
- magFilter: 'nearest',
205
- mipFilter: 'none',
206
- addressU: 'repeat',
207
- addressV: 'repeat'
208
- });
209
- this._linearRepeatSampler = Application.instance.device.createSampler({
210
- minFilter: 'linear',
211
- magFilter: 'linear',
212
- mipFilter: 'none',
213
- addressU: 'repeat',
214
- addressV: 'repeat'
215
- });
216
- this._updateRenderStates = Application.instance.device.createRenderStateSet();
217
- this._updateRenderStates.useRasterizerState().setCullMode('none');
218
- this._updateRenderStates.useDepthState().enableTest(false).enableWrite(false);
219
- this._waterRenderStates = Application.instance.device.createRenderStateSet();
87
+ this._waveGeneratorHash = this._waveGenerator.getHash(device);
88
+ }
89
+ if (!this._waterRenderStates) {
90
+ this._waterRenderStates = device.createRenderStateSet();
220
91
  this._waterRenderStates.useRasterizerState().setCullMode('none');
221
92
  this._waterRenderStates.useDepthState().enableTest(true).enableWrite(true).setCompareFunc('le');
222
- this._clipmap = new Clipmap(this._tileSize);
223
- this._aabbExtents = new Vector2();
224
- this._paramsChanged = true;
225
- this._resolutionChanged = true;
226
- this._updateFrameStamp = -1;
227
- this._usedClipmapBindGroups = [];
228
- this._freeClipmapBindGroups = [];
229
- }
230
- }
231
- get params() {
232
- return this._params;
233
- }
234
- paramsChanged() {
235
- this._paramsChanged = true;
236
- }
237
- /*
238
- private resolutionChanged() {
239
- this._resolutionChanged = true;
240
- this._paramsChanged = true;
241
- }
242
- */ get alignment() {
243
- return this._params.alignment;
244
- }
245
- set alignment(val) {
246
- if (this._params.alignment !== val) {
247
- this._params.alignment = val;
248
- this.paramsChanged();
249
- }
250
- }
251
- get wind() {
252
- return this._params.wind;
253
- }
254
- set wind(val) {
255
- if (val !== this._params.wind && (val.x !== this._params.wind.x || val.y !== this._params.wind.y)) {
256
- this._params.wind.x = val.x;
257
- this._params.wind.y = val.y;
258
- this.paramsChanged();
259
93
  }
260
- }
261
- get foamWidth() {
262
- return this._params.foamParams.x;
263
- }
264
- set foamWidth(val) {
265
- this._params.foamParams.x = val;
266
- }
267
- get foamContrast() {
268
- return this._params.foamParams.y;
269
- }
270
- set foamContrast(val) {
271
- this._params.foamParams.y = val;
272
- }
273
- getWaveLength(cascade) {
274
- return this._params.cascades[cascade].size;
275
- }
276
- setWaveLength(cascade, size) {
277
- if (this._params.cascades[cascade].size !== size) {
278
- this._params.cascades[cascade].size = size;
279
- this.paramsChanged();
280
- }
281
- }
282
- getWaveStrength(cascade) {
283
- return this._params.cascades[cascade].strength;
284
- }
285
- setWaveStrength(cascade, strength) {
286
- if (this._params.cascades[cascade].strength !== strength) {
287
- this._params.cascades[cascade].strength = strength;
288
- this.paramsChanged();
289
- }
290
- }
291
- getWaveCroppiness(cascade) {
292
- return this._params.cascades[cascade].croppiness;
293
- }
294
- setWaveCroppiness(cascade, croppiness) {
295
- if (this._params.cascades[cascade].croppiness !== croppiness) {
296
- this._params.cascades[cascade].croppiness = croppiness;
297
- this.paramsChanged();
298
- }
299
- }
300
- get bindGroup() {
301
- return this._waterBindGroup;
94
+ return true;
302
95
  }
303
96
  get level() {
304
97
  return this._level;
@@ -331,17 +124,11 @@ const RENDER_TWO_PASS = 2;
331
124
  }
332
125
  }
333
126
  }
334
- get regionMin() {
335
- return this._regionMin;
336
- }
337
- set regionMin(val) {
338
- this._regionMin.set(val);
127
+ get region() {
128
+ return this._region;
339
129
  }
340
- get regionMax() {
341
- return this._regionMax;
342
- }
343
- set regionMax(val) {
344
- this._regionMax.set(val);
130
+ set region(val) {
131
+ this._region.set(val);
345
132
  }
346
133
  getClipmapBindGroup(device) {
347
134
  let bindGroup = this._usedClipmapBindGroups.pop();
@@ -351,54 +138,41 @@ const RENDER_TWO_PASS = 2;
351
138
  this._freeClipmapBindGroups.push(bindGroup);
352
139
  return bindGroup;
353
140
  }
354
- render(camera, flip) {
355
- if (this._renderMode === RENDER_NONE) {
141
+ render(device, camera, flip) {
142
+ if (!this.prepareForRender(device)) {
356
143
  return;
357
144
  }
358
- const device = Application.instance.device;
359
- device.pushDeviceStates();
360
- {
145
+ if (device.frameInfo.frameCounter !== this._updateFrameStamp) {
361
146
  this._updateFrameStamp = device.frameInfo.frameCounter;
362
- this.update(device, device.frameInfo.elapsedOverall * 0.001);
147
+ this._waveGenerator.update(device.frameInfo.elapsedOverall * 0.001 * this._speed);
363
148
  }
364
- device.popDeviceStates();
365
149
  const cameraPos = camera.getWorldPosition();
366
- const instanceData = this.getInstanceData();
367
150
  device.setProgram(this._waterProgram);
368
151
  device.setBindGroup(0, this._waterBindGroup);
369
152
  device.setRenderStates(this._waterRenderStates);
370
- const sampler = this._linearRepeatSampler;
371
- this._waterBindGroup.setTexture('dx_hy_dz_dxdz0', instanceData.dataTextures[0], sampler);
372
- this._waterBindGroup.setTexture('sx_sz_dxdx_dzdz0', instanceData.dataTextures[1], sampler);
373
- this._waterBindGroup.setTexture('dx_hy_dz_dxdz1', instanceData.dataTextures[2], sampler);
374
- this._waterBindGroup.setTexture('sx_sz_dxdx_dzdz1', instanceData.dataTextures[3], this._linearRepeatSampler);
375
- this._waterBindGroup.setTexture('dx_hy_dz_dxdz2', instanceData.dataTextures[4], this._linearRepeatSampler);
376
- this._waterBindGroup.setTexture('sx_sz_dxdx_dzdz2', instanceData.dataTextures[5], this._linearRepeatSampler);
377
- this._waterBindGroup.setValue('foamParams', this._params.foamParams);
378
- this._waterBindGroup.setValue('sizes', this._sizes);
379
- this._waterBindGroup.setValue('regionMin', this._regionMin);
380
- this._waterBindGroup.setValue('regionMax', this._regionMax);
381
- this._waterBindGroup.setValue('croppinesses', this._croppinesses);
153
+ this._waveGenerator.applyWaterBindGroup(this._waterBindGroup);
154
+ this._waterBindGroup.setValue('region', this._region);
382
155
  this._waterBindGroup.setValue('viewProjMatrix', camera.viewProjectionMatrix);
383
156
  this._waterBindGroup.setValue('level', this._level);
384
157
  this._waterBindGroup.setValue('pos', cameraPos);
385
158
  this._waterBindGroup.setValue('flip', flip ? 1 : 0);
386
159
  const that = this;
387
160
  const position = new Vector3(cameraPos.x, cameraPos.z, 0);
388
- const distX = Math.max(Math.abs(position.x - this._regionMin.x), Math.abs(position.x - this._regionMax.x));
389
- const distY = Math.max(Math.abs(position.y - this._regionMin.y), Math.abs(position.y - this._regionMax.y));
161
+ const distX = Math.max(Math.abs(position.x - this._region.x), Math.abs(position.x - this._region.z));
162
+ const distY = Math.max(Math.abs(position.y - this._region.y), Math.abs(position.y - this._region.w));
390
163
  const maxDist = Math.min(Math.max(distX, distY), camera.getFarPlane());
391
164
  const gridScale = Math.max(0.01, this._gridScale);
392
165
  const mipLevels = Math.ceil(Math.log2(maxDist / (this._tileSize * gridScale))) + 1;
393
- const disturb = Math.max(this.wind.x, this.wind.y, 2);
394
- this._aabbExtents.setXY(disturb * 2, disturb * 8 + this._level);
395
166
  this._clipmap.draw({
396
167
  camera,
397
168
  position,
398
- minWorldPos: this._regionMin,
399
- maxWorldPos: this._regionMax,
169
+ minMaxWorldPos: this._region,
400
170
  gridScale: gridScale,
401
- AABBExtents: this._aabbExtents,
171
+ userData: this,
172
+ calcAABB (userData, minX, maxX, minZ, maxZ, outAABB) {
173
+ const wm = userData;
174
+ that._waveGenerator.calcClipmapTileAABB(minX, maxX, minZ, maxZ, wm.level, outAABB);
175
+ },
402
176
  drawPrimitive (prim, modelMatrix, offset, scale, gridScale) {
403
177
  const clipmapBindGroup = that.getClipmapBindGroup(device);
404
178
  clipmapBindGroup.setValue('modelMatrix', modelMatrix);
@@ -413,515 +187,7 @@ const RENDER_TWO_PASS = 2;
413
187
  this._usedClipmapBindGroups.push(...this._freeClipmapBindGroups);
414
188
  this._freeClipmapBindGroups.length = 0;
415
189
  }
416
- update(device, time) {
417
- device.setRenderStates(this._updateRenderStates);
418
- if (this._resolutionChanged) {
419
- this.disposeInstanceData();
420
- }
421
- if (this._paramsChanged) {
422
- this.generateInitialSpectrum();
423
- }
424
- this._resolutionChanged = false;
425
- this._paramsChanged = false;
426
- for(let i = 0; i < 3; i++){
427
- this._sizes[i] = this._params.cascades[i].size;
428
- this._croppinesses[i] = this._params.cascades[i].croppiness;
429
- }
430
- if (this._renderMode === RENDER_NORMAL) {
431
- this.generateSpectrum(time);
432
- this.ifft2();
433
- this.postIfft2();
434
- } else {
435
- this.generateSpectrumTwoPass(time);
436
- this.ifft2TwoPass();
437
- this.postIfft2TwoPass();
438
- }
439
- }
440
- disposeInstanceData() {
441
- if (this._instanceData) {
442
- this._instanceData.dataTextures.forEach((tex)=>tex.dispose());
443
- this._instanceData.dataTextures = null;
444
- this._instanceData.h0Textures.forEach((tex)=>tex.dispose());
445
- this._instanceData.h0Textures = null;
446
- this._instanceData.pingpongTextures.forEach((tex)=>tex.dispose());
447
- this._instanceData.pingpongTextures = null;
448
- this._instanceData.spectrumTextures.forEach((tex)=>tex.dispose());
449
- this._instanceData.spectrumTextures = null;
450
- this._instanceData.h0Framebuffer.dispose();
451
- this._instanceData.pingpongFramebuffer?.dispose();
452
- this._instanceData.pingpongFramebuffer2?.dispose();
453
- this._instanceData.pingpongFramebuffer4?.dispose();
454
- this._instanceData.spectrumFramebuffer?.dispose();
455
- this._instanceData.spectrumFramebuffer2?.dispose();
456
- this._instanceData.spectrumFramebuffer4?.dispose();
457
- this._instanceData.postIfft2Framebuffer?.dispose();
458
- this._instanceData.postIfft2Framebuffer2?.dispose();
459
- this._instanceData.postIfft2Framebuffer4?.dispose();
460
- this._instanceData = null;
461
- }
462
- }
463
- generateSpectrum(time) {
464
- const device = Application.instance.device;
465
- const instanceData = this.getInstanceData();
466
- device.setProgram(WaterMesh._globals.programs.hkProgram);
467
- device.setBindGroup(0, this._hkBindGroup);
468
- this._hkBindGroup.setValue('resolution', this._params.resolution);
469
- this._hkBindGroup.setValue('sizes', this._sizes);
470
- this._hkBindGroup.setTexture('h0Texture0', instanceData.h0Textures[0], this._nearestRepeatSampler);
471
- this._hkBindGroup.setTexture('h0Texture1', instanceData.h0Textures[1], this._nearestRepeatSampler);
472
- this._hkBindGroup.setTexture('h0Texture2', instanceData.h0Textures[2], this._nearestRepeatSampler);
473
- this._hkBindGroup.setValue('t', time);
474
- if (device.type === 'webgl') {
475
- this._hkBindGroup.setValue('h0TexSize', new Vector2(instanceData.h0Textures[0].width, instanceData.h0Textures[0].height));
476
- }
477
- device.setFramebuffer(instanceData.spectrumFramebuffer);
478
- WaterMesh._globals.quad.draw();
479
- }
480
- generateSpectrumTwoPass(time) {
481
- const device = Application.instance.device;
482
- const instanceData = this.getInstanceData();
483
- device.setProgram(WaterMesh._globals.programs.hkProgram4);
484
- device.setBindGroup(0, this._hkBindGroup4);
485
- this._hkBindGroup4.setValue('resolution', this._params.resolution);
486
- this._hkBindGroup4.setValue('sizes', this._sizes);
487
- this._hkBindGroup4.setTexture('h0Texture0', instanceData.h0Textures[0], this._nearestRepeatSampler);
488
- this._hkBindGroup4.setTexture('h0Texture1', instanceData.h0Textures[1], this._nearestRepeatSampler);
489
- this._hkBindGroup4.setValue('t', time);
490
- if (device.type === 'webgl') {
491
- this._hkBindGroup4.setValue('h0TexSize', new Vector2(instanceData.h0Textures[0].width, instanceData.h0Textures[0].height));
492
- }
493
- device.setFramebuffer(instanceData.spectrumFramebuffer4);
494
- WaterMesh._globals.quad.draw();
495
- device.setProgram(WaterMesh._globals.programs.hkProgram2);
496
- device.setBindGroup(0, this._hkBindGroup2);
497
- this._hkBindGroup2.setValue('resolution', this._params.resolution);
498
- this._hkBindGroup2.setValue('sizes', this._sizes);
499
- this._hkBindGroup2.setTexture('h0Texture2', instanceData.h0Textures[2], this._nearestRepeatSampler);
500
- this._hkBindGroup2.setValue('t', time);
501
- if (device.type === 'webgl') {
502
- this._hkBindGroup2.setValue('h0TexSize', new Vector2(instanceData.h0Textures[0].width, instanceData.h0Textures[0].height));
503
- }
504
- device.setFramebuffer(instanceData.spectrumFramebuffer2);
505
- WaterMesh._globals.quad.draw();
506
- }
507
- ifft2() {
508
- const device = Application.instance.device;
509
- const instanceData = this.getInstanceData();
510
- const phases = Math.log2(this._params.resolution);
511
- const pingPongTextures = [
512
- instanceData.spectrumTextures,
513
- instanceData.pingpongTextures
514
- ];
515
- const pingPongFramebuffers = [
516
- instanceData.pingpongFramebuffer,
517
- instanceData.spectrumFramebuffer
518
- ];
519
- const butterflyTex = this.getButterflyTexture(this._params.resolution);
520
- // horizontal ifft
521
- let pingPong = 0;
522
- device.setProgram(WaterMesh._globals.programs.fft2hProgram);
523
- device.setBindGroup(0, this._fft2hBindGroup);
524
- this._fft2hBindGroup.setTexture('butterfly', butterflyTex, this._nearestRepeatSampler);
525
- for(let phase = 0; phase < phases; phase++){
526
- device.setFramebuffer(pingPongFramebuffers[pingPong]);
527
- this._fft2hBindGroup.setValue('phase', phase);
528
- this._fft2hBindGroup.setTexture('spectrum0', pingPongTextures[pingPong][0], this._nearestRepeatSampler);
529
- this._fft2hBindGroup.setTexture('spectrum1', pingPongTextures[pingPong][1], this._nearestRepeatSampler);
530
- this._fft2hBindGroup.setTexture('spectrum2', pingPongTextures[pingPong][2], this._nearestRepeatSampler);
531
- this._fft2hBindGroup.setTexture('spectrum3', pingPongTextures[pingPong][3], this._nearestRepeatSampler);
532
- this._fft2hBindGroup.setTexture('spectrum4', pingPongTextures[pingPong][4], this._nearestRepeatSampler);
533
- this._fft2hBindGroup.setTexture('spectrum5', pingPongTextures[pingPong][5], this._nearestRepeatSampler);
534
- if (device.type === 'webgl') {
535
- this._fft2hBindGroup.setValue('texSize', new Vector4(pingPongTextures[pingPong][0].width, pingPongTextures[pingPong][0].height, butterflyTex.width, butterflyTex.height));
536
- }
537
- WaterMesh._globals.quad.draw();
538
- pingPong = 1 - pingPong; //(pingPong + 1) % 2;
539
- }
540
- // vertical ifft
541
- device.setProgram(WaterMesh._globals.programs.fft2vProgram);
542
- device.setBindGroup(0, this._fft2vBindGroup);
543
- this._fft2vBindGroup.setTexture('butterfly', butterflyTex, this._nearestRepeatSampler);
544
- for(let phase = 0; phase < phases; phase++){
545
- device.setFramebuffer(pingPongFramebuffers[pingPong]);
546
- this._fft2vBindGroup.setValue('phase', phase);
547
- this._fft2vBindGroup.setTexture('spectrum0', pingPongTextures[pingPong][0], this._nearestRepeatSampler);
548
- this._fft2vBindGroup.setTexture('spectrum1', pingPongTextures[pingPong][1], this._nearestRepeatSampler);
549
- this._fft2vBindGroup.setTexture('spectrum2', pingPongTextures[pingPong][2], this._nearestRepeatSampler);
550
- this._fft2vBindGroup.setTexture('spectrum3', pingPongTextures[pingPong][3], this._nearestRepeatSampler);
551
- this._fft2vBindGroup.setTexture('spectrum4', pingPongTextures[pingPong][4], this._nearestRepeatSampler);
552
- this._fft2vBindGroup.setTexture('spectrum5', pingPongTextures[pingPong][5], this._nearestRepeatSampler);
553
- if (device.type === 'webgl') {
554
- this._fft2vBindGroup.setValue('texSize', new Vector4(pingPongTextures[pingPong][0].width, pingPongTextures[pingPong][0].height, butterflyTex.width, butterflyTex.height));
555
- }
556
- WaterMesh._globals.quad.draw();
557
- pingPong = (pingPong + 1) % 2;
558
- }
559
- this._ifftTextures = pingPongTextures[pingPong];
560
- }
561
- getFFT2hBindGroup2(device, pingpong) {
562
- let bindGroup = this._fft2hBindGroup2Used[pingpong].pop();
563
- if (!bindGroup) {
564
- bindGroup = device.createBindGroup(WaterMesh._globals.programs.fft2hProgram2.bindGroupLayouts[0]);
565
- }
566
- this._fft2hBindGroup2Free[pingpong].push(bindGroup);
567
- return bindGroup;
568
- }
569
- getFFT2hBindGroup4(device, pingpong) {
570
- let bindGroup = this._fft2hBindGroup4Used[pingpong].pop();
571
- if (!bindGroup) {
572
- bindGroup = device.createBindGroup(WaterMesh._globals.programs.fft2hProgram4.bindGroupLayouts[0]);
573
- }
574
- this._fft2hBindGroup4Free[pingpong].push(bindGroup);
575
- return bindGroup;
576
- }
577
- getFFT2vBindGroup2(device, pingpong) {
578
- let bindGroup = this._fft2vBindGroup2Used[pingpong].pop();
579
- if (!bindGroup) {
580
- bindGroup = device.createBindGroup(WaterMesh._globals.programs.fft2vProgram2.bindGroupLayouts[0]);
581
- }
582
- this._fft2vBindGroup2Free[pingpong].push(bindGroup);
583
- return bindGroup;
584
- }
585
- getFFT2vBindGroup4(device, pingpong) {
586
- let bindGroup = this._fft2vBindGroup4Used[pingpong].pop();
587
- if (!bindGroup) {
588
- bindGroup = device.createBindGroup(WaterMesh._globals.programs.fft2vProgram4.bindGroupLayouts[0]);
589
- }
590
- this._fft2vBindGroup4Free[pingpong].push(bindGroup);
591
- return bindGroup;
592
- }
593
- ifft2TwoPass() {
594
- const device = Application.instance.device;
595
- const instanceData = this.getInstanceData();
596
- const phases = Math.log2(this._params.resolution);
597
- const pingPongTextures = [
598
- instanceData.spectrumTextures,
599
- instanceData.pingpongTextures
600
- ];
601
- const pingPongFramebuffers4 = [
602
- instanceData.pingpongFramebuffer4,
603
- instanceData.spectrumFramebuffer4
604
- ];
605
- const pingPongFramebuffers2 = [
606
- instanceData.pingpongFramebuffer2,
607
- instanceData.spectrumFramebuffer2
608
- ];
609
- const butterflyTex = this.getButterflyTexture(this._params.resolution);
610
- // horizontal ifft
611
- let pingPong = 0;
612
- for(let phase = 0; phase < phases; phase++){
613
- device.setFramebuffer(pingPongFramebuffers4[pingPong]);
614
- device.setProgram(WaterMesh._globals.programs.fft2hProgram4);
615
- const fft2hBindGroup4 = this.getFFT2hBindGroup4(device, pingPong);
616
- device.setBindGroup(0, fft2hBindGroup4);
617
- fft2hBindGroup4.setTexture('butterfly', butterflyTex, this._nearestRepeatSampler);
618
- fft2hBindGroup4.setValue('phase', phase);
619
- fft2hBindGroup4.setTexture('spectrum0', pingPongTextures[pingPong][0], this._nearestRepeatSampler);
620
- fft2hBindGroup4.setTexture('spectrum1', pingPongTextures[pingPong][1], this._nearestRepeatSampler);
621
- fft2hBindGroup4.setTexture('spectrum2', pingPongTextures[pingPong][2], this._nearestRepeatSampler);
622
- fft2hBindGroup4.setTexture('spectrum3', pingPongTextures[pingPong][3], this._nearestRepeatSampler);
623
- if (device.type === 'webgl') {
624
- fft2hBindGroup4.setValue('texSize', new Vector4(pingPongTextures[pingPong][0].width, pingPongTextures[pingPong][0].height, butterflyTex.width, butterflyTex.height));
625
- }
626
- WaterMesh._globals.quad.draw();
627
- device.setFramebuffer(pingPongFramebuffers2[pingPong]);
628
- device.setProgram(WaterMesh._globals.programs.fft2hProgram2);
629
- const fft2hBindGroup2 = this.getFFT2hBindGroup2(device, pingPong);
630
- device.setBindGroup(0, fft2hBindGroup2);
631
- fft2hBindGroup2.setTexture('butterfly', butterflyTex, this._nearestRepeatSampler);
632
- fft2hBindGroup2.setValue('phase', phase);
633
- fft2hBindGroup2.setTexture('spectrum4', pingPongTextures[pingPong][4], this._nearestRepeatSampler);
634
- fft2hBindGroup2.setTexture('spectrum5', pingPongTextures[pingPong][5], this._nearestRepeatSampler);
635
- if (device.type === 'webgl') {
636
- fft2hBindGroup2.setValue('texSize', new Vector4(pingPongTextures[pingPong][0].width, pingPongTextures[pingPong][0].height, butterflyTex.width, butterflyTex.height));
637
- }
638
- WaterMesh._globals.quad.draw();
639
- pingPong = (pingPong + 1) % 2;
640
- }
641
- // vertical ifft
642
- for(let phase = 0; phase < phases; phase++){
643
- device.setFramebuffer(pingPongFramebuffers4[pingPong]);
644
- device.setProgram(WaterMesh._globals.programs.fft2vProgram4);
645
- const fft2vBindGroup4 = this.getFFT2vBindGroup4(device, pingPong);
646
- device.setBindGroup(0, fft2vBindGroup4);
647
- fft2vBindGroup4.setTexture('butterfly', butterflyTex, this._nearestRepeatSampler);
648
- fft2vBindGroup4.setValue('phase', phase);
649
- fft2vBindGroup4.setTexture('spectrum0', pingPongTextures[pingPong][0], this._nearestRepeatSampler);
650
- fft2vBindGroup4.setTexture('spectrum1', pingPongTextures[pingPong][1], this._nearestRepeatSampler);
651
- fft2vBindGroup4.setTexture('spectrum2', pingPongTextures[pingPong][2], this._nearestRepeatSampler);
652
- fft2vBindGroup4.setTexture('spectrum3', pingPongTextures[pingPong][3], this._nearestRepeatSampler);
653
- if (device.type === 'webgl') {
654
- fft2vBindGroup4.setValue('texSize', new Vector4(pingPongTextures[pingPong][0].width, pingPongTextures[pingPong][0].height, butterflyTex.width, butterflyTex.height));
655
- }
656
- WaterMesh._globals.quad.draw();
657
- device.setFramebuffer(pingPongFramebuffers2[pingPong]);
658
- device.setProgram(WaterMesh._globals.programs.fft2vProgram2);
659
- const fft2vBindGroup2 = this.getFFT2vBindGroup2(device, pingPong);
660
- device.setBindGroup(0, fft2vBindGroup2);
661
- fft2vBindGroup2.setTexture('butterfly', butterflyTex, this._nearestRepeatSampler);
662
- fft2vBindGroup2.setValue('phase', phase);
663
- fft2vBindGroup2.setTexture('spectrum4', pingPongTextures[pingPong][4], this._nearestRepeatSampler);
664
- fft2vBindGroup2.setTexture('spectrum5', pingPongTextures[pingPong][5], this._nearestRepeatSampler);
665
- if (device.type === 'webgl') {
666
- fft2vBindGroup2.setValue('texSize', new Vector4(pingPongTextures[pingPong][0].width, pingPongTextures[pingPong][0].height, butterflyTex.width, butterflyTex.height));
667
- }
668
- WaterMesh._globals.quad.draw();
669
- pingPong = (pingPong + 1) % 2;
670
- }
671
- this._ifftTextures = pingPongTextures[pingPong];
672
- for(let i = 0; i < 2; i++){
673
- this._fft2hBindGroup2Used[i].push(...this._fft2hBindGroup2Free[i]);
674
- this._fft2hBindGroup2Free[i].length = 0;
675
- this._fft2hBindGroup4Used[i].push(...this._fft2hBindGroup4Free[i]);
676
- this._fft2hBindGroup4Free[i].length = 0;
677
- this._fft2vBindGroup2Used[i].push(...this._fft2vBindGroup2Free[i]);
678
- this._fft2vBindGroup2Free[i].length = 0;
679
- this._fft2vBindGroup4Used[i].push(...this._fft2vBindGroup4Free[i]);
680
- this._fft2vBindGroup4Free[i].length = 0;
681
- }
682
- }
683
- postIfft2() {
684
- const device = Application.instance.device;
685
- const instanceData = this.getInstanceData();
686
- device.setFramebuffer(instanceData.postIfft2Framebuffer);
687
- device.setProgram(WaterMesh._globals.programs.postfft2Program);
688
- device.setBindGroup(0, this._postfft2BindGroup);
689
- this._postfft2BindGroup.setValue('N2', this._params.resolution * this._params.resolution);
690
- this._postfft2BindGroup.setTexture('ifft0', this._ifftTextures[0], this._nearestRepeatSampler);
691
- this._postfft2BindGroup.setTexture('ifft1', this._ifftTextures[1], this._nearestRepeatSampler);
692
- this._postfft2BindGroup.setTexture('ifft2', this._ifftTextures[2], this._nearestRepeatSampler);
693
- this._postfft2BindGroup.setTexture('ifft3', this._ifftTextures[3], this._nearestRepeatSampler);
694
- this._postfft2BindGroup.setTexture('ifft4', this._ifftTextures[4], this._nearestRepeatSampler);
695
- this._postfft2BindGroup.setTexture('ifft5', this._ifftTextures[5], this._nearestRepeatSampler);
696
- if (device.type === 'webgl') {
697
- this._postfft2BindGroup.setValue('ifftTexSize', new Vector2(this._ifftTextures[0].width, this._ifftTextures[0].height));
698
- }
699
- WaterMesh._globals.quad.draw();
700
- }
701
- postIfft2TwoPass() {
702
- const device = Application.instance.device;
703
- const instanceData = this.getInstanceData();
704
- device.setFramebuffer(instanceData.postIfft2Framebuffer4);
705
- device.setProgram(WaterMesh._globals.programs.postfft2Program4);
706
- device.setBindGroup(0, this._postfft2BindGroup4);
707
- this._postfft2BindGroup4.setValue('N2', this._params.resolution * this._params.resolution);
708
- this._postfft2BindGroup4.setTexture('ifft0', this._ifftTextures[0], this._nearestRepeatSampler);
709
- this._postfft2BindGroup4.setTexture('ifft1', this._ifftTextures[1], this._nearestRepeatSampler);
710
- this._postfft2BindGroup4.setTexture('ifft2', this._ifftTextures[2], this._nearestRepeatSampler);
711
- this._postfft2BindGroup4.setTexture('ifft3', this._ifftTextures[3], this._nearestRepeatSampler);
712
- if (device.type === 'webgl') {
713
- this._postfft2BindGroup4.setValue('ifftTexSize', new Vector2(this._ifftTextures[0].width, this._ifftTextures[0].height));
714
- }
715
- WaterMesh._globals.quad.draw();
716
- device.setFramebuffer(instanceData.postIfft2Framebuffer2);
717
- device.setProgram(WaterMesh._globals.programs.postfft2Program2);
718
- device.setBindGroup(0, this._postfft2BindGroup2);
719
- this._postfft2BindGroup2.setValue('N2', this._params.resolution * this._params.resolution);
720
- this._postfft2BindGroup2.setTexture('ifft4', this._ifftTextures[4], this._nearestRepeatSampler);
721
- this._postfft2BindGroup2.setTexture('ifft5', this._ifftTextures[5], this._nearestRepeatSampler);
722
- if (device.type === 'webgl') {
723
- this._postfft2BindGroup2.setValue('ifftTexSize', new Vector2(this._ifftTextures[0].width, this._ifftTextures[0].height));
724
- }
725
- WaterMesh._globals.quad.draw();
726
- }
727
- createNTextures(device, format, size, num, name, linear) {
728
- return Array.from({
729
- length: num
730
- }).map((val, index)=>{
731
- const tex = device.createTexture2D(format, size, size, {
732
- samplerOptions: {
733
- mipFilter: 'none'
734
- }
735
- });
736
- tex.name = `${name}-${index}`;
737
- tex.samplerOptions = {
738
- minFilter: linear ? 'linear' : 'nearest',
739
- magFilter: linear ? 'linear' : 'nearest',
740
- mipFilter: 'none',
741
- addressU: 'repeat',
742
- addressV: 'repeat'
743
- };
744
- return tex;
745
- });
746
- }
747
- getInstanceData() {
748
- if (!this._instanceData) {
749
- const device = Application.instance.device;
750
- const h0Textures = this.createNTextures(device, this._h0TextureFormat, this._params.resolution, 3, 'Water-h0', false);
751
- const dataTextures = this.createNTextures(device, this._dataTextureFormat, this._params.resolution, 6, 'Water-data', true);
752
- const spectrumTextures = this.createNTextures(device, this._textureFormat, this._params.resolution, 6, 'Water-spectrum', false);
753
- const pingpongTextures = this.createNTextures(device, this._textureFormat, this._params.resolution, 6, 'Water-pingpong', false);
754
- this._instanceData = {
755
- dataTextures,
756
- h0Textures: h0Textures,
757
- pingpongTextures: pingpongTextures,
758
- spectrumTextures: spectrumTextures,
759
- h0Framebuffer: device.createFrameBuffer(h0Textures, null),
760
- spectrumFramebuffer: this._renderMode === RENDER_NORMAL ? device.createFrameBuffer(spectrumTextures, null) : null,
761
- spectrumFramebuffer2: this._renderMode === RENDER_TWO_PASS ? device.createFrameBuffer(spectrumTextures.slice(4, 6), null) : null,
762
- spectrumFramebuffer4: this._renderMode === RENDER_TWO_PASS ? device.createFrameBuffer(spectrumTextures.slice(0, 4), null) : null,
763
- pingpongFramebuffer: device.createFrameBuffer(pingpongTextures, null),
764
- pingpongFramebuffer2: this._renderMode === RENDER_TWO_PASS ? device.createFrameBuffer(pingpongTextures.slice(4, 6), null) : null,
765
- pingpongFramebuffer4: this._renderMode === RENDER_TWO_PASS ? device.createFrameBuffer(pingpongTextures.slice(0, 4), null) : null,
766
- postIfft2Framebuffer: device.createFrameBuffer(dataTextures, null),
767
- postIfft2Framebuffer2: this._renderMode === RENDER_TWO_PASS ? device.createFrameBuffer(dataTextures.slice(4, 6), null) : null,
768
- postIfft2Framebuffer4: this._renderMode === RENDER_TWO_PASS ? device.createFrameBuffer(dataTextures.slice(0, 4), null) : null
769
- };
770
- }
771
- return this._instanceData;
772
- }
773
- static createQuad(device) {
774
- const vertexData = new Float32Array([
775
- -1,
776
- -1,
777
- 0,
778
- 0.0,
779
- 0.0,
780
- 1,
781
- -1,
782
- 0,
783
- 1.0,
784
- 0.0,
785
- 1,
786
- 1,
787
- 0,
788
- 1.0,
789
- 1.0,
790
- -1,
791
- 1,
792
- 0,
793
- 0.0,
794
- 1.0
795
- ]);
796
- const indexData = new Uint32Array([
797
- 0,
798
- 1,
799
- 2,
800
- 0,
801
- 2,
802
- 3
803
- ]);
804
- const prim = new Primitive();
805
- const vb = device.createInterleavedVertexBuffer([
806
- 'position_f32x3',
807
- 'tex0_f32x2'
808
- ], vertexData);
809
- const ib = device.createIndexBuffer(indexData);
810
- prim.setVertexBuffer(vb);
811
- prim.setIndexBuffer(ib);
812
- prim.primitiveType = 'triangle-list';
813
- prim.indexStart = 0;
814
- prim.indexCount = ib.length;
815
- return prim;
816
- }
817
- getButterflyTexture(size) {
818
- const device = Application.instance.device;
819
- let tex = WaterMesh._globals.butterflyTextures.get(size);
820
- if (!tex) {
821
- tex = device.createTexture2D('rgba32f', Math.log2(size), size, {
822
- samplerOptions: {
823
- mipFilter: 'none'
824
- }
825
- });
826
- tex.name = `butterfly${size}`;
827
- tex.update(this.createButterflyTexture(size), 0, 0, tex.width, tex.height);
828
- WaterMesh._globals.butterflyTextures.set(size, tex);
829
- }
830
- return tex;
831
- }
832
- generateInitialSpectrum() {
833
- const device = Application.instance.device;
834
- const instanceData = this.getInstanceData();
835
- device.setFramebuffer(instanceData.h0Framebuffer);
836
- device.clearFrameBuffer(Vector4.zero(), null, null);
837
- device.setProgram(WaterMesh._globals.programs.h0Program);
838
- device.setBindGroup(0, this._h0BindGroup);
839
- this._h0BindGroup.setTexture('noise', this.getNoiseTexture(this._params.resolution, this._params.randomSeed), this._nearestRepeatSampler);
840
- this._h0BindGroup.setValue('resolution', this._params.resolution);
841
- this._h0BindGroup.setValue('wind', this._params.wind);
842
- this._h0BindGroup.setValue('alignment', this._params.alignment);
843
- for(let i = 0; i < this._params.cascades.length; i++){
844
- this._cascades[i].x = this._params.cascades[i].size;
845
- this._cascades[i].y = this._params.cascades[i].strength * 0.081 / (this._params.cascades[i].size * this._params.cascades[i].size);
846
- this._cascades[i].z = 2 * Math.PI / this._params.cascades[i].maxWave;
847
- this._cascades[i].w = 2 * Math.PI / this._params.cascades[i].minWave;
848
- }
849
- this._h0BindGroup.setValue('cascade0', this._cascades[0]);
850
- this._h0BindGroup.setValue('cascade1', this._cascades[1]);
851
- this._h0BindGroup.setValue('cascade2', this._cascades[2]);
852
- WaterMesh._globals.quad.draw();
853
- }
854
- getNoiseTexture(size, randomSeed) {
855
- const device = Application.instance.device;
856
- let tex = WaterMesh._globals.noiseTextures.get(size);
857
- if (!tex) {
858
- tex = device.createTexture2D(device.type === 'webgl' ? 'rgba32f' : 'rg32f', size, size, {
859
- samplerOptions: {
860
- mipFilter: 'none'
861
- }
862
- });
863
- tex.name = `noiseTex${size}`;
864
- tex.update(this.getNoise2d(size, randomSeed, device.type === 'webgl'), 0, 0, size, size);
865
- WaterMesh._globals.noiseTextures.set(size, tex);
866
- }
867
- return tex;
868
- }
869
- getNoise2d(size, randomSeed, rgba) {
870
- const rand = new PRNG(randomSeed);
871
- if (rgba) {
872
- const array = new Float32Array(size * size * 4);
873
- for(let i = 0; i < size * size; i++){
874
- array[i * 4 + 0] = rand.get();
875
- array[i * 4 + 1] = rand.get();
876
- }
877
- return array;
878
- } else {
879
- return Float32Array.from([
880
- ...Array(size * size * 2)
881
- ].map(()=>rand.get()));
882
- }
883
- }
884
- reverseBits(v, width) {
885
- return parseInt(v.toString(2).padStart(width, '0').split('').reverse().join(''), 2);
886
- }
887
- createButterflyTexture(size) {
888
- const width = Math.log2(size);
889
- const height = size;
890
- const texture = new Float32Array(width * height * 4);
891
- const w = 2.0 * Math.PI / size;
892
- const bitReversed = [
893
- ...Array(size).keys()
894
- ].map((v)=>this.reverseBits(v, width));
895
- for(let j = 0; j < width; j++){
896
- for(let i = 0; i < height; i++){
897
- const k = i * (size >> j + 1);
898
- const c = Math.cos(k * w);
899
- const s = Math.sin(k * w);
900
- const span = 2 ** j;
901
- const wing = i % 2 ** (j + 1) < span ? 0 : 1; // 0 - top wing, 1 - bottom wing
902
- const texel = new Vector4();
903
- if (j === 0) {
904
- if (wing === 0) {
905
- texel.setXYZW(c, s, bitReversed[i], bitReversed[i + 1]);
906
- } else {
907
- texel.setXYZW(c, s, bitReversed[i - 1], bitReversed[i]);
908
- }
909
- } else {
910
- if (wing === 0) {
911
- texel.setXYZW(c, s, i, i + span);
912
- } else {
913
- texel.setXYZW(c, s, i - span, i);
914
- }
915
- }
916
- texture[(width * i + j) * 4] = texel[0];
917
- texture[(width * i + j) * 4 + 1] = texel[1];
918
- texture[(width * i + j) * 4 + 2] = texel[2];
919
- texture[(width * i + j) * 4 + 3] = texel[3];
920
- }
921
- }
922
- return texture;
923
- }
924
190
  }
925
191
 
926
- export { WaterMesh, defaultBuildParams };
192
+ export { WaterMesh };
927
193
  //# sourceMappingURL=watermesh.js.map