@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,259 +0,0 @@
1
- import { PBRLightModelMR } from './lightmodel.js';
2
- import { Vector4 } from '@zephyr3d/base';
3
- import { RENDER_PASS_TYPE_FORWARD } from '../values.js';
4
- import { Application } from '../app.js';
5
- import { drawFullscreenQuad } from '../render/helper.js';
6
-
7
- const TEX_NAME_SPLATMAP = 'splat';
8
- const TEX_NAME_DETAIL_COLOR = 'detailColor';
9
- const TEX_NAME_DETAIL_NORMAL = 'detailNormal';
10
- class TerrainLightModel extends PBRLightModelMR {
11
- static _metallicRoughnessGenerationProgram = null;
12
- static _metallicRoughnessGenerationBindGroup = null;
13
- _options;
14
- _uvScales;
15
- _numDetailMaps;
16
- constructor(options){
17
- super();
18
- this._normalMapMode = 'object-space';
19
- this._options = null;
20
- this._numDetailMaps = 0;
21
- this._uvScales = null;
22
- if (options && options.splatMap && options.detailMaps && options.detailMaps.albedoTextures) {
23
- this._options = Object.assign({}, options);
24
- const albedoTextures = this._options.detailMaps.albedoTextures;
25
- this._numDetailMaps = Array.isArray(albedoTextures) ? albedoTextures.length : albedoTextures.depth;
26
- if (!this._numDetailMaps) {
27
- throw new Error(`TerrainLightModel(): Invalid detail textures`);
28
- }
29
- if (this._numDetailMaps > 4) {
30
- throw new Error(`TerrainLightModel(): The maximum detail levels is 4`);
31
- }
32
- if (!this._options.detailMaps.uvScale || this._options.detailMaps.uvScale.length !== this._numDetailMaps) {
33
- throw new Error(`TerrainLightModel(): Invalid uv scale`);
34
- }
35
- this._uvScales = new Float32Array(this._numDetailMaps * 4);
36
- for(let i = 0; i < this._numDetailMaps; i++){
37
- this._uvScales[i * 4] = this._options.detailMaps.uvScale[i];
38
- this._uvScales[i * 4 + 1] = 1;
39
- this._uvScales[i * 4 + 2] = 0.01;
40
- this._uvScales[i * 4 + 3] = 0.99;
41
- }
42
- if (this._options.detailMaps.metallic) {
43
- if (this._options.detailMaps.metallic.length !== this._numDetailMaps) {
44
- throw new Error(`TerrainLightModel(): Invalid metallic values`);
45
- }
46
- for(let i = 0; i < this._numDetailMaps; i++){
47
- this._uvScales[i * 4 + 2] = this._options.detailMaps.metallic[i];
48
- }
49
- }
50
- if (this._options.detailMaps.roughness) {
51
- if (this._options.detailMaps.roughness.length !== this._numDetailMaps) {
52
- throw new Error(`TerrainLightModel(): Invalid roughness values`);
53
- }
54
- for(let i = 0; i < this._numDetailMaps; i++){
55
- this._uvScales[i * 4 + 3] = this._options.detailMaps.roughness[i];
56
- }
57
- }
58
- const normalTextures = options.detailMaps.normalTextures;
59
- if (normalTextures) {
60
- const m = Array.isArray(normalTextures) ? normalTextures.length : normalTextures.depth;
61
- if (m !== this._numDetailMaps) {
62
- throw new Error(`TerrainLightModel(): The number of normal textures not match the number of albedo textures`);
63
- }
64
- if (options.detailMaps.normalScale) {
65
- if (options.detailMaps.normalScale.length !== this._numDetailMaps) {
66
- throw new Error(`TerrainLightModel(): Invalid normal scale`);
67
- }
68
- for(let i = 0; i < this._numDetailMaps; i++){
69
- this._uvScales[i * 4 + 1] = options.detailMaps.normalScale[i];
70
- }
71
- }
72
- }
73
- this._options = Object.assign({}, options);
74
- this._options.splatMapTexCoordIndex = this.setTextureOptions(TEX_NAME_SPLATMAP, this._options.splatMap, null, 0, null);
75
- if (Array.isArray(albedoTextures)) {
76
- this._options.detailMaps.albedoTexCoordIndex = [];
77
- for(let i = 0; i < albedoTextures.length; i++){
78
- if (!albedoTextures[i]) {
79
- throw new Error(`TerrainLightModel(): Invalid detail albedo texture`);
80
- }
81
- this._options.detailMaps.albedoTexCoordIndex[i] = this.setTextureOptions(`${TEX_NAME_DETAIL_COLOR}${i}`, albedoTextures[i], null, -1, null);
82
- albedoTextures[i].samplerOptions = {
83
- addressU: 'repeat',
84
- addressV: 'repeat'
85
- };
86
- }
87
- } else {
88
- this._options.detailMaps.albedoTexCoordIndex = this.setTextureOptions(TEX_NAME_DETAIL_COLOR, albedoTextures, null, -1, null);
89
- albedoTextures.samplerOptions = {
90
- addressU: 'repeat',
91
- addressV: 'repeat'
92
- };
93
- }
94
- if (Array.isArray(normalTextures)) {
95
- this._options.detailMaps.normalTexCoordIndex = [];
96
- for(let i = 0; i < normalTextures.length; i++){
97
- if (!normalTextures[i]) {
98
- throw new Error(`TerrainLightModel(): Invalid detail normal texture`);
99
- }
100
- this._options.detailMaps.normalTexCoordIndex[i] = this.setTextureOptions(`${TEX_NAME_DETAIL_NORMAL}${i}`, normalTextures[i], null, -1, null);
101
- normalTextures[i].samplerOptions = {
102
- addressU: 'repeat',
103
- addressV: 'repeat'
104
- };
105
- }
106
- } else if (normalTextures) {
107
- this._options.detailMaps.normalTexCoordIndex = this.setTextureOptions(TEX_NAME_DETAIL_NORMAL, normalTextures, null, -1, null);
108
- normalTextures.samplerOptions = {
109
- addressU: 'repeat',
110
- addressV: 'repeat'
111
- };
112
- }
113
- }
114
- this.setMetallicMap(this.generateMetallicRoughnessMap(), null, -1);
115
- }
116
- setupUniforms(scope, ctx) {
117
- super.setupUniforms(scope, ctx);
118
- const pb = scope.$builder;
119
- if (pb.shaderKind === 'fragment' && this._options) {
120
- scope.detailScales = pb.vec4[this._numDetailMaps]().uniform(2);
121
- }
122
- }
123
- applyUniforms(bindGroup, ctx) {
124
- super.applyUniforms(bindGroup, ctx);
125
- if (ctx.renderPass.type === RENDER_PASS_TYPE_FORWARD) {
126
- if (this._options) {
127
- bindGroup.setValue('detailScales', this._uvScales);
128
- }
129
- }
130
- }
131
- fillSurfaceData(scope, envLight) {
132
- super.fillSurfaceData(scope, envLight);
133
- }
134
- calculateAlbedo(scope) {
135
- if (!this._options) {
136
- return super.calculateAlbedo(scope);
137
- }
138
- const that = this;
139
- const pb = scope.$builder;
140
- const funcName = 'getTerrainAlbedo';
141
- pb.func(funcName, [], function() {
142
- this.$l.mask = pb.textureSample(this[that.getTextureUniformName(TEX_NAME_SPLATMAP)], this.$inputs[`texcoord${that._options.splatMapTexCoordIndex}`]);
143
- this.$l.color = pb.vec3(0);
144
- const useTextureArray = !Array.isArray(that._options.detailMaps.albedoTextures);
145
- for(let i = 0; i < that._numDetailMaps; i++){
146
- const texCoordIndex = useTextureArray ? that._options.detailMaps.albedoTexCoordIndex : that._options.detailMaps.albedoTexCoordIndex[i];
147
- const uv = pb.mul(this.$inputs[`texcoord${texCoordIndex}`], this.detailScales.at(i).x);
148
- const sample = useTextureArray ? pb.textureArraySample(this[that.getTextureUniformName(TEX_NAME_DETAIL_COLOR)], uv, i).rgb : pb.textureSample(this[that.getTextureUniformName(`${TEX_NAME_DETAIL_COLOR}${i}`)], uv).rgb;
149
- this.color = pb.add(this.color, pb.mul(sample, this.mask[i]));
150
- }
151
- this.$return(pb.vec4(this.color, 1));
152
- });
153
- return pb.getGlobalScope()[funcName]();
154
- }
155
- calculateNormal(scope, worldPosition, worldNormal, worldTangent, worldBinormal) {
156
- scope.$l.terrainBaseNormal = super.calculateNormal(scope, worldPosition, worldNormal, worldTangent, worldBinormal);
157
- const pb = scope.$builder;
158
- let calcNormal = false;
159
- if (this._options && this._options.detailMaps.normalTextures) {
160
- if (Array.isArray(this._options.detailMaps.normalTextures)) {
161
- for(let i = 0; i < this._options.detailMaps.normalTextures.length; i++){
162
- const tex = scope[this.getTextureUniformName(`${TEX_NAME_DETAIL_NORMAL}${i}`)];
163
- const scale = scope.detailScales.at(i).y;
164
- const texCoord = pb.mul(scope.$inputs[`texcoord${this._options.detailMaps.normalTexCoordIndex[i]}`], scope.detailScales.at(i).x);
165
- scope.terrainBaseNormal.normal = pb.add(scope.terrainBaseNormal.normal, this.sampleNormalMapWithTBN(scope, tex, texCoord, scale, scope.terrainBaseNormal.TBN));
166
- calcNormal = true;
167
- }
168
- } else {
169
- const tex = scope[this.getTextureUniformName(TEX_NAME_DETAIL_NORMAL)];
170
- for(let i = 0; i < this._numDetailMaps; i++){
171
- const scale = scope.detailScales.at(i).y;
172
- const texCoord = pb.mul(scope.$inputs[`texcoord${this._options.detailMaps.normalTexCoordIndex}`], scope.detailScales.at(i).x);
173
- const pixel = pb.sub(pb.mul(pb.textureArraySample(tex, texCoord, i).rgb, 2), pb.vec3(1));
174
- const normalTex = pb.mul(pixel, pb.vec3(pb.vec3(scale).xx, 1));
175
- const detailNormal = pb.normalize(pb.mul(scope.terrainBaseNormal.TBN, normalTex));
176
- scope.terrainBaseNormal.normal = pb.add(scope.terrainBaseNormal.normal, detailNormal);
177
- calcNormal = true;
178
- }
179
- }
180
- }
181
- if (calcNormal) {
182
- scope.terrainBaseNormal.normal = pb.normalize(scope.terrainBaseNormal.normal);
183
- }
184
- return scope.terrainBaseNormal;
185
- }
186
- generateMetallicRoughnessMap() {
187
- const device = Application.instance.device;
188
- if (!this._options) {
189
- const tex = device.createTexture2D('rgba8unorm', 1, 1, {
190
- samplerOptions: {
191
- mipFilter: 'none'
192
- }
193
- });
194
- tex.update(new Uint8Array([
195
- 0,
196
- 1,
197
- 0,
198
- 0
199
- ]), 0, 0, 1, 1);
200
- tex.name = 'TerrainMetallicRoughnessMap';
201
- return tex;
202
- }
203
- if (!TerrainLightModel._metallicRoughnessGenerationProgram) {
204
- TerrainLightModel._metallicRoughnessGenerationProgram = device.buildRenderProgram({
205
- vertex (pb) {
206
- this.$inputs.pos = pb.vec2().attrib('position');
207
- this.$outputs.uv = pb.vec2();
208
- pb.main(function() {
209
- this.$builtins.position = pb.vec4(this.$inputs.pos, 0, 1);
210
- this.$outputs.uv = pb.add(pb.mul(this.$inputs.pos.xy, 0.5), pb.vec2(0.5));
211
- if (device.type === 'webgpu') {
212
- this.$builtins.position.y = pb.neg(this.$builtins.position.y);
213
- }
214
- });
215
- },
216
- fragment (pb) {
217
- this.$outputs.outColor = pb.vec4();
218
- this.roughness = pb.vec4().uniform(0);
219
- this.metallic = pb.vec4().uniform(0);
220
- this.splatMap = pb.tex2D().uniform(0);
221
- pb.main(function() {
222
- this.weights = pb.textureSample(this.splatMap, this.$inputs.uv);
223
- this.roughnessValue = pb.dot(this.weights, this.roughness);
224
- this.metallicValue = pb.dot(this.weights, this.metallic);
225
- this.$outputs.outColor = pb.vec4(0, this.roughnessValue, this.metallicValue, 1);
226
- });
227
- }
228
- });
229
- TerrainLightModel._metallicRoughnessGenerationBindGroup = device.createBindGroup(TerrainLightModel._metallicRoughnessGenerationProgram.bindGroupLayouts[0]);
230
- }
231
- const roughnessValues = Vector4.one();
232
- const metallicValues = Vector4.zero();
233
- for(let i = 0; i < this._numDetailMaps; i++){
234
- metallicValues[i] = this._uvScales[i * 4 + 2];
235
- roughnessValues[i] = this._uvScales[i * 4 + 3];
236
- }
237
- const tex = device.createTexture2D('rgba8unorm', this._options.splatMap.width, this._options.splatMap.height);
238
- tex.name = 'TerrainMetallicRoughnessMap';
239
- const program = TerrainLightModel._metallicRoughnessGenerationProgram;
240
- const bindgroup = TerrainLightModel._metallicRoughnessGenerationBindGroup;
241
- bindgroup.setValue('roughness', roughnessValues);
242
- bindgroup.setValue('metallic', metallicValues);
243
- bindgroup.setTexture('splatMap', this._options.splatMap);
244
- const fb = device.createFrameBuffer([
245
- tex
246
- ], null);
247
- device.pushDeviceStates();
248
- device.setFramebuffer(fb);
249
- device.setProgram(program);
250
- device.setBindGroup(0, bindgroup);
251
- drawFullscreenQuad();
252
- device.popDeviceStates();
253
- fb.dispose();
254
- return tex;
255
- }
256
- }
257
-
258
- export { TerrainLightModel };
259
- //# sourceMappingURL=terrainlightmodel.js.map
@@ -1,357 +0,0 @@
1
- import { applyMaterialMixins, MeshMaterial } from './meshmaterial.js';
2
- import { mixinPBRMetallicRoughness } from './mixins/pbr/metallicroughness.js';
3
- import { mixinLight } from './mixins/lit.js';
4
- import { Application } from '../app.js';
5
- import { Vector4 } from '@zephyr3d/base';
6
- import { drawFullscreenQuad } from '../render/helper.js';
7
-
8
- class NewTerrainMaterial extends applyMaterialMixins(MeshMaterial, mixinLight, mixinPBRMetallicRoughness) {
9
- static _metallicRoughnessGenerationProgram = null;
10
- static _metallicRoughnessGenerationBindGroup = null;
11
- _options;
12
- _uvScales;
13
- _numDetailMaps;
14
- _terrainInfo;
15
- constructor(options){
16
- super();
17
- this.normalMapMode = 'object-space';
18
- this._options = null;
19
- this._numDetailMaps = 0;
20
- this._uvScales = null;
21
- this._terrainInfo = null;
22
- if (options && options.splatMap && options.detailMaps && options.detailMaps.albedoTextures) {
23
- this._options = Object.assign({}, options);
24
- const albedoTextures = this._options.detailMaps.albedoTextures;
25
- this._numDetailMaps = Array.isArray(albedoTextures) ? albedoTextures.length : albedoTextures.depth;
26
- if (!this._numDetailMaps) {
27
- throw new Error(`TerrainLightModel(): Invalid detail textures`);
28
- }
29
- if (this._numDetailMaps > 4) {
30
- throw new Error(`TerrainLightModel(): The maximum detail levels is 4`);
31
- }
32
- if (!this._options.detailMaps.uvScale || this._options.detailMaps.uvScale.length !== this._numDetailMaps) {
33
- throw new Error(`TerrainLightModel(): Invalid uv scale`);
34
- }
35
- this._uvScales = new Float32Array(this._numDetailMaps * 4);
36
- for(let i = 0; i < this._numDetailMaps; i++){
37
- this._uvScales[i * 4] = this._options.detailMaps.uvScale[i];
38
- this._uvScales[i * 4 + 1] = 1;
39
- this._uvScales[i * 4 + 2] = 0.01;
40
- this._uvScales[i * 4 + 3] = 0.99;
41
- }
42
- if (this._options.detailMaps.metallic) {
43
- if (this._options.detailMaps.metallic.length !== this._numDetailMaps) {
44
- throw new Error(`TerrainLightModel(): Invalid metallic values`);
45
- }
46
- for(let i = 0; i < this._numDetailMaps; i++){
47
- this._uvScales[i * 4 + 2] = this._options.detailMaps.metallic[i];
48
- }
49
- }
50
- if (this._options.detailMaps.roughness) {
51
- if (this._options.detailMaps.roughness.length !== this._numDetailMaps) {
52
- throw new Error(`TerrainLightModel(): Invalid roughness values`);
53
- }
54
- for(let i = 0; i < this._numDetailMaps; i++){
55
- this._uvScales[i * 4 + 3] = this._options.detailMaps.roughness[i];
56
- }
57
- }
58
- const normalTextures = options.detailMaps.normalTextures;
59
- if (normalTextures) {
60
- const m = Array.isArray(normalTextures) ? normalTextures.length : normalTextures.depth;
61
- if (m !== this._numDetailMaps) {
62
- throw new Error(`TerrainLightModel(): The number of normal textures not match the number of albedo textures`);
63
- }
64
- if (options.detailMaps.normalScale) {
65
- if (options.detailMaps.normalScale.length !== this._numDetailMaps) {
66
- throw new Error(`TerrainLightModel(): Invalid normal scale`);
67
- }
68
- for(let i = 0; i < this._numDetailMaps; i++){
69
- this._uvScales[i * 4 + 1] = options.detailMaps.normalScale[i];
70
- }
71
- }
72
- }
73
- this._options = Object.assign({}, options);
74
- if (Array.isArray(albedoTextures)) {
75
- for(let i = 0; i < albedoTextures.length; i++){
76
- if (!albedoTextures[i]) {
77
- throw new Error(`TerrainLightModel(): Invalid detail albedo texture`);
78
- }
79
- albedoTextures[i].samplerOptions = {
80
- addressU: 'repeat',
81
- addressV: 'repeat'
82
- };
83
- }
84
- } else {
85
- albedoTextures.samplerOptions = {
86
- addressU: 'repeat',
87
- addressV: 'repeat'
88
- };
89
- }
90
- if (Array.isArray(normalTextures)) {
91
- for(let i = 0; i < normalTextures.length; i++){
92
- if (!normalTextures[i]) {
93
- throw new Error(`TerrainLightModel(): Invalid detail normal texture`);
94
- }
95
- normalTextures[i].samplerOptions = {
96
- addressU: 'repeat',
97
- addressV: 'repeat'
98
- };
99
- }
100
- } else if (normalTextures) {
101
- normalTextures.samplerOptions = {
102
- addressU: 'repeat',
103
- addressV: 'repeat'
104
- };
105
- }
106
- }
107
- this.vertexNormal = true;
108
- this.metallicRoughnessTexture = this.generateMetallicRoughnessMap();
109
- this.metallicRoughnessTexCoordIndex = -1;
110
- this.albedoTexCoordIndex = -1;
111
- this.normalTexCoordIndex = -1;
112
- }
113
- get terrainInfo() {
114
- return this._terrainInfo;
115
- }
116
- set terrainInfo(val) {
117
- this._terrainInfo = val;
118
- this.optionChanged(false);
119
- }
120
- /**
121
- * {@inheritDoc Material.isTransparent}
122
- * @override
123
- */ isTransparent() {
124
- return false;
125
- }
126
- /**
127
- * {@inheritDoc Material.supportLighting}
128
- * @override
129
- */ supportLighting() {
130
- return true;
131
- }
132
- applyUniformValues(bindGroup, ctx) {
133
- super.applyUniformValues(bindGroup, ctx);
134
- if (this.needFragmentColor(ctx)) {
135
- bindGroup.setValue('terrainInfo', this._terrainInfo);
136
- if (this._options) {
137
- bindGroup.setValue('kkDetailScales', this._uvScales);
138
- bindGroup.setTexture('kkSplatMap', this._options.splatMap);
139
- if (Array.isArray(this._options.detailMaps.albedoTextures)) {
140
- for(let i = 0; i < this._numDetailMaps; i++){
141
- bindGroup.setTexture(`kkDetailAlbedoMap${i}`, this._options.detailMaps.albedoTextures[i]);
142
- }
143
- } else {
144
- bindGroup.setTexture('kkDetailAlbedoMap', this._options.detailMaps.albedoTextures);
145
- }
146
- if (Array.isArray(this._options.detailMaps.normalTextures)) {
147
- for(let i = 0; i < this._numDetailMaps; i++){
148
- bindGroup.setTexture(`kkDetailNormalMap${i}`, this._options.detailMaps.normalTextures[i]);
149
- }
150
- } else {
151
- bindGroup.setTexture('kkDetailNormalMap', this._options.detailMaps.normalTextures);
152
- }
153
- }
154
- }
155
- }
156
- /** @ts-ignore */ getMetallicRoughnessTexCoord(scope) {
157
- return scope.$inputs.mapUV;
158
- }
159
- /** @ts-ignore */ getNormalTexCoord(scope) {
160
- return scope.$inputs.mapUV;
161
- }
162
- /** @ts-ignore */ getAlbedoTexCoord(scope) {
163
- return scope.$inputs.mapUV;
164
- }
165
- calculateAlbedoColor(scope, ctx) {
166
- if (!this._options) {
167
- return super.calculateAlbedoColor(scope, ctx);
168
- }
169
- const that = this;
170
- const pb = scope.$builder;
171
- const funcName = 'getTerrainAlbedo';
172
- pb.func(funcName, [], function() {
173
- this.$l.mask = pb.textureSample(this.kkSplatMap, this.$inputs.mapUV);
174
- this.$l.color = pb.vec3(0);
175
- const useTextureArray = !Array.isArray(that._options.detailMaps.albedoTextures);
176
- for(let i = 0; i < that._numDetailMaps; i++){
177
- const uv = pb.mul(this.$inputs.mapUV, this.kkDetailScales.at(i).x);
178
- const sample = useTextureArray ? pb.textureArraySample(this.kkDetailAlbedoMap, uv, i).rgb : pb.textureSample(this[`kkDetailAlbedoMap${i}`], uv).rgb;
179
- this.color = pb.add(this.color, pb.mul(sample, this.mask[i]));
180
- }
181
- this.$return(pb.vec4(this.color, 1));
182
- });
183
- return pb.getGlobalScope()[funcName]();
184
- }
185
- sampleNormalMapWithTBN(scope, tex, texCoord, normalScale, TBN) {
186
- const pb = scope.$builder;
187
- const pixel = pb.sub(pb.mul(pb.textureSample(tex, texCoord).rgb, 2), pb.vec3(1));
188
- const normalTex = pb.mul(pixel, pb.vec3(pb.vec3(normalScale).xx, 1));
189
- return pb.normalize(pb.mul(TBN, normalTex));
190
- }
191
- calculateNormalAndTBN(scope, ctx) {
192
- scope.$l.normalInfo = super.calculateNormalAndTBN(scope, ctx);
193
- const pb = scope.$builder;
194
- let calcNormal = false;
195
- if (this._options && this._options.detailMaps.normalTextures) {
196
- scope.$l.detailMask = pb.textureSample(scope.kkSplatMap, scope.$inputs.mapUV);
197
- if (Array.isArray(this._options.detailMaps.normalTextures)) {
198
- for(let i = 0; i < this._options.detailMaps.normalTextures.length; i++){
199
- const tex = scope[`kkDetailNormalMap${i}`];
200
- const scale = scope.kkDetailScales.at(i).y;
201
- const texCoord = pb.mul(scope.$inputs.mapUV, scope.kkDetailScales.at(i).x);
202
- scope.normalInfo.normal = pb.add(scope.normalInfo.normal, pb.mul(this.sampleNormalMapWithTBN(scope, tex, texCoord, scale, scope.normalInfo.TBN), scope.detailMask[i]));
203
- calcNormal = true;
204
- }
205
- } else {
206
- const tex = scope.kkDetailNormalMap;
207
- for(let i = 0; i < this._numDetailMaps; i++){
208
- const scale = scope.kkDetailScales.at(i).y;
209
- const texCoord = pb.mul(scope.$inputs.mapUV, scope.kkDetailScales.at(i).x);
210
- const pixel = pb.sub(pb.mul(pb.textureArraySample(tex, texCoord, i).rgb, 2), pb.vec3(1));
211
- const normalTex = pb.mul(pixel, pb.vec3(pb.vec3(scale).xx, 1));
212
- const detailNormal = pb.normalize(pb.mul(scope.normalInfo.TBN, normalTex));
213
- scope.normalInfo.normal = pb.add(scope.normalInfo.normal, pb.mul(detailNormal, scope.detailMask[i]));
214
- calcNormal = true;
215
- }
216
- }
217
- }
218
- if (calcNormal) {
219
- scope.normalInfo.normal = pb.normalize(scope.normalInfo.normal);
220
- }
221
- return scope.normalInfo;
222
- }
223
- vertexShader(scope, ctx) {
224
- super.vertexShader(scope, ctx);
225
- const pb = scope.$builder;
226
- scope.$inputs.zPos = pb.vec3().attrib('position');
227
- if (this.needFragmentColor(ctx)) {
228
- scope.$g.terrainInfo = pb.vec4().uniform(2);
229
- scope.$outputs.mapUV = pb.div(scope.$inputs.zPos.xz, scope.terrainInfo.xy);
230
- }
231
- this.transformVertexAndNormal(scope);
232
- }
233
- fragmentShader(scope, ctx) {
234
- super.fragmentShader(scope, ctx);
235
- const pb = scope.$builder;
236
- const that = this;
237
- if (this.needFragmentColor(ctx)) {
238
- if (this._options) {
239
- scope.$g.kkDetailScales = pb.vec4[this._numDetailMaps]().uniform(2);
240
- scope.$g.kkSplatMap = pb.tex2D().uniform(2);
241
- const useAlbedoTextureArray = !Array.isArray(that._options.detailMaps.albedoTextures);
242
- if (useAlbedoTextureArray) {
243
- scope.$g.kkDetailAlbedoMap = pb.tex2DArray().uniform(2);
244
- } else {
245
- for(let i = 0; i < that._numDetailMaps; i++){
246
- scope.$g[`kkDetailAlbedoMap${i}`] = pb.tex2D().uniform(2);
247
- }
248
- }
249
- const useNormalTextureArray = !Array.isArray(that._options.detailMaps.normalTextures);
250
- if (useNormalTextureArray) {
251
- scope.$g.kkDetailNormalMap = pb.tex2DArray().uniform(2);
252
- } else {
253
- for(let i = 0; i < that._numDetailMaps; i++){
254
- scope.$g[`kkDetailNormalMap${i}`] = pb.tex2D().uniform(2);
255
- }
256
- }
257
- }
258
- scope.$l.albedo = this.calculateAlbedoColor(scope, ctx);
259
- scope.$l.normalInfo = this.calculateNormalAndTBN(scope, ctx);
260
- scope.$l.normal = scope.normalInfo.normal;
261
- scope.$l.viewVec = this.calculateViewVector(scope);
262
- scope.$l.pbrData = this.getCommonData(scope, scope.albedo, scope.viewVec, scope.normalInfo.TBN);
263
- scope.$l.lightingColor = pb.vec3(0);
264
- scope.$l.emissiveColor = this.calculateEmissiveColor(scope);
265
- this.indirectLighting(scope, scope.normal, scope.viewVec, scope.pbrData, scope.lightingColor, ctx);
266
- this.forEachLight(scope, ctx, function(type, posRange, dirCutoff, colorIntensity, shadow) {
267
- this.$l.diffuse = pb.vec3();
268
- this.$l.specular = pb.vec3();
269
- this.$l.lightAtten = that.calculateLightAttenuation(this, type, posRange, dirCutoff);
270
- this.$l.lightDir = that.calculateLightDirection(this, type, posRange, dirCutoff);
271
- this.$l.NoL = pb.clamp(pb.dot(this.normal, this.lightDir), 0, 1);
272
- this.$l.lightColor = pb.mul(colorIntensity.rgb, colorIntensity.a, this.lightAtten, this.NoL);
273
- if (shadow) {
274
- this.lightColor = pb.mul(this.lightColor, that.calculateShadow(this, this.NoL, ctx));
275
- }
276
- that.directLighting(this, this.lightDir, this.lightColor, this.normal, this.viewVec, this.pbrData, this.lightingColor);
277
- });
278
- scope.$l.litColor = pb.add(scope.lightingColor, scope.emissiveColor);
279
- this.outputFragmentColor(scope, pb.vec4(scope.litColor, scope.albedo.a), ctx);
280
- } else {
281
- this.outputFragmentColor(scope, null, ctx);
282
- }
283
- }
284
- generateMetallicRoughnessMap() {
285
- const device = Application.instance.device;
286
- if (!this._options) {
287
- const tex = device.createTexture2D('rgba8unorm', 1, 1, {
288
- samplerOptions: {
289
- mipFilter: 'none'
290
- }
291
- });
292
- tex.update(new Uint8Array([
293
- 0,
294
- 1,
295
- 0,
296
- 0
297
- ]), 0, 0, 1, 1);
298
- tex.name = 'TerrainMetallicRoughnessMap';
299
- return tex;
300
- }
301
- if (!NewTerrainMaterial._metallicRoughnessGenerationProgram) {
302
- NewTerrainMaterial._metallicRoughnessGenerationProgram = device.buildRenderProgram({
303
- vertex (pb) {
304
- this.$inputs.pos = pb.vec2().attrib('position');
305
- this.$outputs.uv = pb.vec2();
306
- pb.main(function() {
307
- this.$builtins.position = pb.vec4(this.$inputs.pos, 0, 1);
308
- this.$outputs.uv = pb.add(pb.mul(this.$inputs.pos.xy, 0.5), pb.vec2(0.5));
309
- if (device.type === 'webgpu') {
310
- this.$builtins.position.y = pb.neg(this.$builtins.position.y);
311
- }
312
- });
313
- },
314
- fragment (pb) {
315
- this.$outputs.outColor = pb.vec4();
316
- this.roughness = pb.vec4().uniform(0);
317
- this.metallic = pb.vec4().uniform(0);
318
- this.splatMap = pb.tex2D().uniform(0);
319
- pb.main(function() {
320
- this.weights = pb.textureSample(this.splatMap, this.$inputs.uv);
321
- this.roughnessValue = pb.dot(this.weights, this.roughness);
322
- this.metallicValue = pb.dot(this.weights, this.metallic);
323
- this.$outputs.outColor = pb.vec4(0, this.roughnessValue, this.metallicValue, 1);
324
- });
325
- }
326
- });
327
- NewTerrainMaterial._metallicRoughnessGenerationBindGroup = device.createBindGroup(NewTerrainMaterial._metallicRoughnessGenerationProgram.bindGroupLayouts[0]);
328
- }
329
- const roughnessValues = Vector4.one();
330
- const metallicValues = Vector4.zero();
331
- for(let i = 0; i < this._numDetailMaps; i++){
332
- metallicValues[i] = this._uvScales[i * 4 + 2];
333
- roughnessValues[i] = this._uvScales[i * 4 + 3];
334
- }
335
- const tex = device.createTexture2D('rgba8unorm', this._options.splatMap.width, this._options.splatMap.height);
336
- tex.name = 'TerrainMetallicRoughnessMap';
337
- const program = NewTerrainMaterial._metallicRoughnessGenerationProgram;
338
- const bindgroup = NewTerrainMaterial._metallicRoughnessGenerationBindGroup;
339
- bindgroup.setValue('roughness', roughnessValues);
340
- bindgroup.setValue('metallic', metallicValues);
341
- bindgroup.setTexture('splatMap', this._options.splatMap);
342
- const fb = device.createFrameBuffer([
343
- tex
344
- ], null);
345
- device.pushDeviceStates();
346
- device.setFramebuffer(fb);
347
- device.setProgram(program);
348
- device.setBindGroup(0, bindgroup);
349
- drawFullscreenQuad();
350
- device.popDeviceStates();
351
- fb.dispose();
352
- return tex;
353
- }
354
- }
355
-
356
- export { NewTerrainMaterial };
357
- //# sourceMappingURL=terrainmat.js.map
@@ -1,47 +0,0 @@
1
- import { RenderPass } from './renderpass.js';
2
- import { RENDER_PASS_TYPE_DEPTH_ONLY } from '../values.js';
3
- import { ShaderFramework } from '../shaders/framework.js';
4
- import '@zephyr3d/device';
5
- import { Application } from '../app.js';
6
-
7
- /**
8
- * Depth render pass
9
- *
10
- * Scene depth render pass
11
- *
12
- * @public
13
- */ class DepthRenderPass extends RenderPass {
14
- /**
15
- * Creates an instance of DepthRenderPass
16
- */ constructor(){
17
- super(RENDER_PASS_TYPE_DEPTH_ONLY);
18
- }
19
- /** @internal */ _getGlobalBindGroupHash(ctx) {
20
- return '';
21
- }
22
- /** @internal */ renderItems(ctx, renderQueue) {
23
- ctx.target = null;
24
- ctx.applyFog = false;
25
- ctx.drawEnvLight = false;
26
- ctx.env = null;
27
- ctx.renderPassHash = null;
28
- ctx.flip = this.isAutoFlip();
29
- const device = Application.instance.device;
30
- const bindGroup = this.getGlobalBindGroupInfo(ctx).bindGroup;
31
- device.setBindGroup(0, bindGroup);
32
- ShaderFramework.setCameraUniforms(bindGroup, ctx, true);
33
- ctx.renderPassHash = this.getGlobalBindGroupHash(ctx);
34
- const reverseWinding = ctx.camera.worldMatrixDet < 0;
35
- for (const order of Object.keys(renderQueue.items).map((val)=>Number(val)).sort((a, b)=>a - b)){
36
- const renderItems = renderQueue.items[order];
37
- for (const item of renderItems.opaqueList){
38
- ctx.instanceData = item.instanceData;
39
- ctx.target = item.drawable;
40
- this.drawItem(device, item, ctx, reverseWinding);
41
- }
42
- }
43
- }
44
- }
45
-
46
- export { DepthRenderPass };
47
- //# sourceMappingURL=depth_pass.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"depth_pass.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}