@zephyr3d/scene 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/dist/animation/animation.js +173 -0
  2. package/dist/animation/animation.js.map +1 -0
  3. package/dist/animation/animationset.js +95 -0
  4. package/dist/animation/animationset.js.map +1 -0
  5. package/dist/animation/animationtrack.js +38 -0
  6. package/dist/animation/animationtrack.js.map +1 -0
  7. package/dist/animation/eulerrotationtrack.js +33 -0
  8. package/dist/animation/eulerrotationtrack.js.map +1 -0
  9. package/dist/animation/rotationtrack.js +37 -0
  10. package/dist/animation/rotationtrack.js.map +1 -0
  11. package/dist/animation/scaletrack.js +36 -0
  12. package/dist/animation/scaletrack.js.map +1 -0
  13. package/dist/animation/skeleton.js +97 -0
  14. package/dist/animation/skeleton.js.map +1 -0
  15. package/dist/animation/translationtrack.js +36 -0
  16. package/dist/animation/translationtrack.js.map +1 -0
  17. package/dist/animation/usertrack.js +47 -0
  18. package/dist/animation/usertrack.js.map +1 -0
  19. package/dist/app.js +173 -0
  20. package/dist/app.js.map +1 -0
  21. package/dist/asset/assetmanager.js +476 -0
  22. package/dist/asset/assetmanager.js.map +1 -0
  23. package/dist/asset/builtin.js +373 -0
  24. package/dist/asset/builtin.js.map +1 -0
  25. package/dist/asset/loaders/dds/dds.js +472 -0
  26. package/dist/asset/loaders/dds/dds.js.map +1 -0
  27. package/dist/asset/loaders/dds/dds_loader.js +38 -0
  28. package/dist/asset/loaders/dds/dds_loader.js.map +1 -0
  29. package/dist/asset/loaders/gltf/gltf_loader.js +981 -0
  30. package/dist/asset/loaders/gltf/gltf_loader.js.map +1 -0
  31. package/dist/asset/loaders/gltf/helpers.js +314 -0
  32. package/dist/asset/loaders/gltf/helpers.js.map +1 -0
  33. package/dist/asset/loaders/hdr/hdr.js +175 -0
  34. package/dist/asset/loaders/hdr/hdr.js.map +1 -0
  35. package/dist/asset/loaders/image/tga_Loader.js +117 -0
  36. package/dist/asset/loaders/image/tga_Loader.js.map +1 -0
  37. package/dist/asset/loaders/image/webimage_loader.js +50 -0
  38. package/dist/asset/loaders/image/webimage_loader.js.map +1 -0
  39. package/dist/asset/loaders/loader.js +45 -0
  40. package/dist/asset/loaders/loader.js.map +1 -0
  41. package/dist/asset/model.js +264 -0
  42. package/dist/asset/model.js.map +1 -0
  43. package/dist/blitter/blitter.js +389 -0
  44. package/dist/blitter/blitter.js.map +1 -0
  45. package/dist/blitter/box.js +118 -0
  46. package/dist/blitter/box.js.map +1 -0
  47. package/dist/blitter/copy.js +22 -0
  48. package/dist/blitter/copy.js.map +1 -0
  49. package/dist/blitter/depthlimitedgaussion.js +166 -0
  50. package/dist/blitter/depthlimitedgaussion.js.map +1 -0
  51. package/dist/blitter/gaussianblur.js +229 -0
  52. package/dist/blitter/gaussianblur.js.map +1 -0
  53. package/dist/camera/base.js +90 -0
  54. package/dist/camera/base.js.map +1 -0
  55. package/dist/camera/camera.js +358 -0
  56. package/dist/camera/camera.js.map +1 -0
  57. package/dist/camera/fps.js +246 -0
  58. package/dist/camera/fps.js.map +1 -0
  59. package/dist/camera/orbit.js +157 -0
  60. package/dist/camera/orbit.js.map +1 -0
  61. package/dist/camera/orthocamera.js +126 -0
  62. package/dist/camera/orthocamera.js.map +1 -0
  63. package/dist/camera/perspectivecamera.js +133 -0
  64. package/dist/camera/perspectivecamera.js.map +1 -0
  65. package/dist/index.d.ts +8402 -0
  66. package/dist/index.js +87 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/input/inputmgr.js +242 -0
  69. package/dist/input/inputmgr.js.map +1 -0
  70. package/dist/material/blinn.js +75 -0
  71. package/dist/material/blinn.js.map +1 -0
  72. package/dist/material/grassmaterial.js +221 -0
  73. package/dist/material/grassmaterial.js.map +1 -0
  74. package/dist/material/lambert.js +52 -0
  75. package/dist/material/lambert.js.map +1 -0
  76. package/dist/material/lightmodel.js +2074 -0
  77. package/dist/material/lightmodel.js.map +1 -0
  78. package/dist/material/lit.js +578 -0
  79. package/dist/material/lit.js.map +1 -0
  80. package/dist/material/material.js +458 -0
  81. package/dist/material/material.js.map +1 -0
  82. package/dist/material/meshmaterial.js +311 -0
  83. package/dist/material/meshmaterial.js.map +1 -0
  84. package/dist/material/mixins/albedocolor.js +130 -0
  85. package/dist/material/mixins/albedocolor.js.map +1 -0
  86. package/dist/material/mixins/texture.js +110 -0
  87. package/dist/material/mixins/texture.js.map +1 -0
  88. package/dist/material/mixins/vertexcolor.js +45 -0
  89. package/dist/material/mixins/vertexcolor.js.map +1 -0
  90. package/dist/material/pbr.js +27 -0
  91. package/dist/material/pbr.js.map +1 -0
  92. package/dist/material/standard.js +282 -0
  93. package/dist/material/standard.js.map +1 -0
  94. package/dist/material/terrainlightmodel.js +259 -0
  95. package/dist/material/terrainlightmodel.js.map +1 -0
  96. package/dist/material/terrainmaterial.js +139 -0
  97. package/dist/material/terrainmaterial.js.map +1 -0
  98. package/dist/material/unlit.js +29 -0
  99. package/dist/material/unlit.js.map +1 -0
  100. package/dist/posteffect/bloom.js +398 -0
  101. package/dist/posteffect/bloom.js.map +1 -0
  102. package/dist/posteffect/compositor.js +264 -0
  103. package/dist/posteffect/compositor.js.map +1 -0
  104. package/dist/posteffect/fxaa.js +291 -0
  105. package/dist/posteffect/fxaa.js.map +1 -0
  106. package/dist/posteffect/grayscale.js +87 -0
  107. package/dist/posteffect/grayscale.js.map +1 -0
  108. package/dist/posteffect/posteffect.js +165 -0
  109. package/dist/posteffect/posteffect.js.map +1 -0
  110. package/dist/posteffect/sao.js +327 -0
  111. package/dist/posteffect/sao.js.map +1 -0
  112. package/dist/posteffect/tonemap.js +112 -0
  113. package/dist/posteffect/tonemap.js.map +1 -0
  114. package/dist/posteffect/water.js +535 -0
  115. package/dist/posteffect/water.js.map +1 -0
  116. package/dist/render/clipmap.js +462 -0
  117. package/dist/render/clipmap.js.map +1 -0
  118. package/dist/render/cluster_light.js +329 -0
  119. package/dist/render/cluster_light.js.map +1 -0
  120. package/dist/render/cull_visitor.js +124 -0
  121. package/dist/render/cull_visitor.js.map +1 -0
  122. package/dist/render/depth_pass.js +47 -0
  123. package/dist/render/depth_pass.js.map +1 -0
  124. package/dist/render/envlight.js +282 -0
  125. package/dist/render/envlight.js.map +1 -0
  126. package/dist/render/forward.js +186 -0
  127. package/dist/render/forward.js.map +1 -0
  128. package/dist/render/forward_pass.js +137 -0
  129. package/dist/render/forward_pass.js.map +1 -0
  130. package/dist/render/helper.js +38 -0
  131. package/dist/render/helper.js.map +1 -0
  132. package/dist/render/primitive.js +246 -0
  133. package/dist/render/primitive.js.map +1 -0
  134. package/dist/render/render_queue.js +163 -0
  135. package/dist/render/render_queue.js.map +1 -0
  136. package/dist/render/renderpass.js +151 -0
  137. package/dist/render/renderpass.js.map +1 -0
  138. package/dist/render/renderscheme.js +61 -0
  139. package/dist/render/renderscheme.js.map +1 -0
  140. package/dist/render/scatteringlut.js +634 -0
  141. package/dist/render/scatteringlut.js.map +1 -0
  142. package/dist/render/shadowmap_pass.js +70 -0
  143. package/dist/render/shadowmap_pass.js.map +1 -0
  144. package/dist/render/sky.js +881 -0
  145. package/dist/render/sky.js.map +1 -0
  146. package/dist/render/temporalcache.js +222 -0
  147. package/dist/render/temporalcache.js.map +1 -0
  148. package/dist/render/watermesh.js +835 -0
  149. package/dist/render/watermesh.js.map +1 -0
  150. package/dist/scene/environment.js +146 -0
  151. package/dist/scene/environment.js.map +1 -0
  152. package/dist/scene/graph_node.js +69 -0
  153. package/dist/scene/graph_node.js.map +1 -0
  154. package/dist/scene/light.js +436 -0
  155. package/dist/scene/light.js.map +1 -0
  156. package/dist/scene/mesh.js +215 -0
  157. package/dist/scene/mesh.js.map +1 -0
  158. package/dist/scene/model.js +111 -0
  159. package/dist/scene/model.js.map +1 -0
  160. package/dist/scene/octree.js +651 -0
  161. package/dist/scene/octree.js.map +1 -0
  162. package/dist/scene/octree_update_visitor.js +16 -0
  163. package/dist/scene/octree_update_visitor.js.map +1 -0
  164. package/dist/scene/raycast_visitor.js +72 -0
  165. package/dist/scene/raycast_visitor.js.map +1 -0
  166. package/dist/scene/scene.js +225 -0
  167. package/dist/scene/scene.js.map +1 -0
  168. package/dist/scene/scene_node.js +299 -0
  169. package/dist/scene/scene_node.js.map +1 -0
  170. package/dist/scene/terrain/grass.js +277 -0
  171. package/dist/scene/terrain/grass.js.map +1 -0
  172. package/dist/scene/terrain/heightfield.js +391 -0
  173. package/dist/scene/terrain/heightfield.js.map +1 -0
  174. package/dist/scene/terrain/patch.js +530 -0
  175. package/dist/scene/terrain/patch.js.map +1 -0
  176. package/dist/scene/terrain/quadtree.js +430 -0
  177. package/dist/scene/terrain/quadtree.js.map +1 -0
  178. package/dist/scene/terrain/terrain.js +258 -0
  179. package/dist/scene/terrain/terrain.js.map +1 -0
  180. package/dist/scene/xform.js +224 -0
  181. package/dist/scene/xform.js.map +1 -0
  182. package/dist/shaders/builtins.js +110 -0
  183. package/dist/shaders/builtins.js.map +1 -0
  184. package/dist/shaders/framework.js +709 -0
  185. package/dist/shaders/framework.js.map +1 -0
  186. package/dist/shaders/lighting.js +335 -0
  187. package/dist/shaders/lighting.js.map +1 -0
  188. package/dist/shaders/misc.js +405 -0
  189. package/dist/shaders/misc.js.map +1 -0
  190. package/dist/shaders/noise.js +157 -0
  191. package/dist/shaders/noise.js.map +1 -0
  192. package/dist/shaders/pbr.js +132 -0
  193. package/dist/shaders/pbr.js.map +1 -0
  194. package/dist/shaders/shadow.js +642 -0
  195. package/dist/shaders/shadow.js.map +1 -0
  196. package/dist/shaders/water.js +630 -0
  197. package/dist/shaders/water.js.map +1 -0
  198. package/dist/shadow/esm.js +235 -0
  199. package/dist/shadow/esm.js.map +1 -0
  200. package/dist/shadow/pcf_opt.js +182 -0
  201. package/dist/shadow/pcf_opt.js.map +1 -0
  202. package/dist/shadow/pcf_pd.js +190 -0
  203. package/dist/shadow/pcf_pd.js.map +1 -0
  204. package/dist/shadow/shadow_impl.js +15 -0
  205. package/dist/shadow/shadow_impl.js.map +1 -0
  206. package/dist/shadow/shadowmapper.js +709 -0
  207. package/dist/shadow/shadowmapper.js.map +1 -0
  208. package/dist/shadow/ssm.js +194 -0
  209. package/dist/shadow/ssm.js.map +1 -0
  210. package/dist/shadow/vsm.js +298 -0
  211. package/dist/shadow/vsm.js.map +1 -0
  212. package/dist/shapes/box.js +313 -0
  213. package/dist/shapes/box.js.map +1 -0
  214. package/dist/shapes/cylinder.js +74 -0
  215. package/dist/shapes/cylinder.js.map +1 -0
  216. package/dist/shapes/plane.js +48 -0
  217. package/dist/shapes/plane.js.map +1 -0
  218. package/dist/shapes/shape.js +33 -0
  219. package/dist/shapes/shape.js.map +1 -0
  220. package/dist/shapes/sphere.js +91 -0
  221. package/dist/shapes/sphere.js.map +1 -0
  222. package/dist/shapes/torus.js +100 -0
  223. package/dist/shapes/torus.js.map +1 -0
  224. package/dist/utility/aabbtree.js +390 -0
  225. package/dist/utility/aabbtree.js.map +1 -0
  226. package/dist/utility/bounding_volume.js +78 -0
  227. package/dist/utility/bounding_volume.js.map +1 -0
  228. package/dist/utility/panorama.js +163 -0
  229. package/dist/utility/panorama.js.map +1 -0
  230. package/dist/utility/pmrem.js +345 -0
  231. package/dist/utility/pmrem.js.map +1 -0
  232. package/dist/utility/shprojection.js +448 -0
  233. package/dist/utility/shprojection.js.map +1 -0
  234. package/dist/values.js +48 -0
  235. package/dist/values.js.map +1 -0
  236. package/package.json +70 -0
@@ -0,0 +1,448 @@
1
+ import { Vector3, halfToFloat, unpackFloat3, SH, nextPowerOf2 } from '@zephyr3d/base';
2
+ import { Application } from '../app.js';
3
+ import { Blitter } from '../blitter/blitter.js';
4
+ import '../shaders/framework.js';
5
+ import '@zephyr3d/device';
6
+ import { CopyBlitter } from '../blitter/copy.js';
7
+
8
+ class ReduceBlitter extends Blitter {
9
+ _width;
10
+ constructor(width){
11
+ super();
12
+ this._width = width ?? 0;
13
+ }
14
+ get width() {
15
+ return this._width;
16
+ }
17
+ set width(val) {
18
+ this._width = val;
19
+ }
20
+ setup(scope, type) {
21
+ const pb = scope.$builder;
22
+ if (pb.shaderKind === 'fragment') {
23
+ scope.width = pb.float().uniform(0);
24
+ }
25
+ }
26
+ setUniforms(bindGroup) {
27
+ bindGroup.setValue('width', this._width);
28
+ }
29
+ filter(scope, type, srcTex, srcUV, srcLayer) {
30
+ const pb = scope.$builder;
31
+ pb.func('reduce', [
32
+ pb.vec2('uv')
33
+ ], function() {
34
+ this.$l.h = pb.div(0.5, this.width);
35
+ //this.$l.uv1 = pb.add(this.uv, pb.vec2(this.h));
36
+ this.$l.uv1 = this.uv;
37
+ this.$l.tl = pb.textureSampleLevel(srcTex, pb.vec2(pb.sub(this.uv1.x, this.h), pb.sub(this.uv1.y, this.h)), 0);
38
+ this.$l.tr = pb.textureSampleLevel(srcTex, pb.vec2(pb.add(this.uv1.x, this.h), pb.sub(this.uv1.y, this.h)), 0);
39
+ this.$l.bl = pb.textureSampleLevel(srcTex, pb.vec2(pb.sub(this.uv1.x, this.h), pb.add(this.uv1.y, this.h)), 0);
40
+ this.$l.br = pb.textureSampleLevel(srcTex, pb.vec2(pb.add(this.uv1.x, this.h), pb.add(this.uv1.y, this.h)), 0);
41
+ this.$return(pb.vec4(pb.add(this.tl, this.tr, this.bl, this.br).rgb, 1));
42
+ });
43
+ return scope.reduce(srcUV);
44
+ }
45
+ calcHash() {
46
+ return '';
47
+ }
48
+ }
49
+ let vertexLayout = null;
50
+ let renderStates = null;
51
+ let projectionProgram = null;
52
+ let projectionBindgroup = null;
53
+ const faceDirections = [
54
+ [
55
+ new Vector3(0, 0, -1),
56
+ new Vector3(0, -1, 0),
57
+ new Vector3(1, 0, 0)
58
+ ],
59
+ [
60
+ new Vector3(0, 0, 1),
61
+ new Vector3(0, -1, 0),
62
+ new Vector3(-1, 0, 0)
63
+ ],
64
+ [
65
+ new Vector3(1, 0, 0),
66
+ new Vector3(0, 0, 1),
67
+ new Vector3(0, 1, 0)
68
+ ],
69
+ [
70
+ new Vector3(1, 0, 0),
71
+ new Vector3(0, 0, -1),
72
+ new Vector3(0, -1, 0)
73
+ ],
74
+ [
75
+ new Vector3(1, 0, 0),
76
+ new Vector3(0, -1, 0),
77
+ new Vector3(0, 0, 1)
78
+ ],
79
+ [
80
+ new Vector3(-1, 0, 0),
81
+ new Vector3(0, -1, 0),
82
+ new Vector3(0, 0, -1)
83
+ ]
84
+ ];
85
+ function init() {
86
+ const device = Application.instance.device;
87
+ const vertices = new Float32Array([
88
+ 1,
89
+ 1,
90
+ -1,
91
+ 1,
92
+ -1,
93
+ -1,
94
+ 1,
95
+ -1
96
+ ]);
97
+ const indices = new Uint16Array([
98
+ 0,
99
+ 1,
100
+ 2,
101
+ 0,
102
+ 2,
103
+ 3
104
+ ]);
105
+ vertexLayout = device.createVertexLayout({
106
+ vertexBuffers: [
107
+ {
108
+ buffer: device.createVertexBuffer('position_f32x2', vertices)
109
+ }
110
+ ],
111
+ indexBuffer: device.createIndexBuffer(indices)
112
+ });
113
+ renderStates = device.createRenderStateSet();
114
+ renderStates.useRasterizerState().setCullMode('none');
115
+ renderStates.useDepthState().enableTest(false).enableWrite(false);
116
+ projectionProgram = createProjectionProgram();
117
+ //console.log(projectionProgram.getShaderSource('fragment'));
118
+ projectionBindgroup = device.createBindGroup(projectionProgram.bindGroupLayouts[0]);
119
+ }
120
+ function createProjectionProgram() {
121
+ const device = Application.instance.device;
122
+ const pb = device;
123
+ return pb.buildRenderProgram({
124
+ vertex (pb) {
125
+ this.$inputs.pos = pb.vec2().attrib('position');
126
+ this.up = pb.vec3().uniform(0);
127
+ this.right = pb.vec3().uniform(0);
128
+ this.front = pb.vec3().uniform(0);
129
+ pb.main(function() {
130
+ this.$builtins.position = pb.vec4(this.$inputs.pos, 0, 1);
131
+ this.$outputs.direction = pb.mul(pb.mat3(this.up, this.right, this.front), pb.vec3(this.$inputs.pos, 1));
132
+ if (device.type === 'webgpu') {
133
+ this.$builtins.position.y = pb.neg(this.$builtins.position.y);
134
+ }
135
+ });
136
+ },
137
+ fragment (pb) {
138
+ this.u_cubeMap = pb.texCube().uniform(0);
139
+ this.u_width = pb.int().uniform(0);
140
+ this.u_coeff = pb.int().uniform(0);
141
+ this.$outputs.outcolor = pb.vec4();
142
+ pb.func('dirToUV', [
143
+ pb.vec3('dir')
144
+ ], function() {
145
+ this.$return(pb.vec2(pb.add(0.5, pb.div(pb.mul(0.5, pb.atan2(this.dir.z, this.dir.x)), Math.PI)), pb.sub(1, pb.div(pb.acos(this.dir.y), Math.PI))));
146
+ });
147
+ pb.func('areaElement', [
148
+ pb.float('x'),
149
+ pb.float('y')
150
+ ], function() {
151
+ this.$return(pb.atan2(pb.mul(this.x, this.y), pb.sqrt(pb.add(pb.mul(this.x, this.x), pb.mul(this.y, this.y), 1))));
152
+ });
153
+ pb.func('solidAngle', [
154
+ pb.vec2('uv')
155
+ ], function() {
156
+ this.$l.inv = pb.div(1, pb.float(this.u_width));
157
+ this.$l.uv2 = pb.sub(pb.mul(pb.add(this.uv, pb.mul(this.inv, 0.5)), 2), pb.vec2(1));
158
+ this.$l.xy0 = pb.sub(this.uv2, pb.vec2(this.inv));
159
+ this.$l.xy1 = pb.add(this.uv2, pb.vec2(this.inv));
160
+ this.$return(pb.sub(pb.add(this.areaElement(this.xy0.x, this.xy0.y), this.areaElement(this.xy1.x, this.xy1.y)), pb.add(this.areaElement(this.xy0.x, this.xy1.y), this.areaElement(this.xy1.x, this.xy0.y))));
161
+ });
162
+ pb.func('Y0', [
163
+ pb.vec3('v')
164
+ ], function() {
165
+ this.$return(0.2820947917);
166
+ });
167
+ pb.func('Y1', [
168
+ pb.vec3('v')
169
+ ], function() {
170
+ this.$return(pb.mul(this.v.y, -0.4886025119));
171
+ });
172
+ pb.func('Y2', [
173
+ pb.vec3('v')
174
+ ], function() {
175
+ this.$return(pb.mul(this.v.z, 0.4886025119));
176
+ });
177
+ pb.func('Y3', [
178
+ pb.vec3('v')
179
+ ], function() {
180
+ this.$return(pb.mul(this.v.x, -0.4886025119));
181
+ });
182
+ pb.func('Y4', [
183
+ pb.vec3('v')
184
+ ], function() {
185
+ this.$return(pb.mul(this.v.x, this.v.y, 1.0925484306));
186
+ });
187
+ pb.func('Y5', [
188
+ pb.vec3('v')
189
+ ], function() {
190
+ this.$return(pb.mul(this.v.y, this.v.z, -1.0925484306));
191
+ });
192
+ pb.func('Y6', [
193
+ pb.vec3('v')
194
+ ], function() {
195
+ this.$return(pb.mul(pb.sub(pb.mul(this.v.z, this.v.z, 3), 1), 0.3153915652));
196
+ });
197
+ pb.func('Y7', [
198
+ pb.vec3('v')
199
+ ], function() {
200
+ this.$return(pb.mul(this.v.x, this.v.z, -1.0925484306));
201
+ });
202
+ pb.func('Y8', [
203
+ pb.vec3('v')
204
+ ], function() {
205
+ this.$return(pb.mul(pb.sub(pb.mul(this.v.x, this.v.x), pb.mul(this.v.y, this.v.y)), 0.5462742153));
206
+ });
207
+ pb.func('evalBasis', [
208
+ pb.vec3('dir'),
209
+ pb.int('c')
210
+ ], function() {
211
+ this.$if(pb.equal(this.c, 0), function() {
212
+ this.$return(this.Y0(this.dir));
213
+ }).$elseif(pb.equal(this.c, 1), function() {
214
+ this.$return(this.Y1(this.dir));
215
+ }).$elseif(pb.equal(this.c, 2), function() {
216
+ this.$return(this.Y2(this.dir));
217
+ }).$elseif(pb.equal(this.c, 3), function() {
218
+ this.$return(this.Y3(this.dir));
219
+ }).$elseif(pb.equal(this.c, 4), function() {
220
+ this.$return(this.Y4(this.dir));
221
+ }).$elseif(pb.equal(this.c, 5), function() {
222
+ this.$return(this.Y5(this.dir));
223
+ }).$elseif(pb.equal(this.c, 6), function() {
224
+ this.$return(this.Y6(this.dir));
225
+ }).$elseif(pb.equal(this.c, 7), function() {
226
+ this.$return(this.Y7(this.dir));
227
+ }).$elseif(pb.equal(this.c, 8), function() {
228
+ this.$return(this.Y8(this.dir));
229
+ }).$else(function() {
230
+ this.$return(0);
231
+ });
232
+ });
233
+ pb.main(function() {
234
+ this.$l.dir = pb.normalize(this.$inputs.direction);
235
+ this.$l.radiance = pb.textureSampleLevel(this.u_cubeMap, this.dir, 0).rgb;
236
+ this.$l.uv = this.dirToUV(this.dir);
237
+ this.$l.omega = this.solidAngle(this.uv);
238
+ this.$l.sh = this.evalBasis(this.dir, this.u_coeff);
239
+ this.$outputs.outcolor = pb.vec4(pb.mul(this.radiance, this.sh, this.omega), 1);
240
+ });
241
+ }
242
+ });
243
+ }
244
+ async function doProjectCubemap(srcTexture, coeff) {
245
+ const device = Application.instance.device;
246
+ const result = Vector3.zero();
247
+ let w = nextPowerOf2(srcTexture.width);
248
+ if (w > srcTexture.width) {
249
+ w = w >> 1;
250
+ }
251
+ const tmpTextures = [];
252
+ while(w > 0){
253
+ tmpTextures.push(device.createTexture2D('rgba32f', w, w, {
254
+ samplerOptions: {
255
+ mipFilter: 'none'
256
+ }
257
+ }));
258
+ w = w >> 1;
259
+ }
260
+ for(let i = 0; i < 6; i++){
261
+ const dstTex = tmpTextures[0]; //device.createTexture2D('rgba32f', srcTexture.width, w);
262
+ const framebuffer = device.createFrameBuffer([
263
+ dstTex
264
+ ], null);
265
+ framebuffer.setColorAttachmentGenerateMipmaps(0, false);
266
+ device.setVertexLayout(vertexLayout);
267
+ device.setRenderStates(renderStates);
268
+ device.setProgram(projectionProgram);
269
+ device.setBindGroup(0, projectionBindgroup);
270
+ device.setFramebuffer(framebuffer);
271
+ projectionBindgroup.setValue('up', faceDirections[i][0]);
272
+ projectionBindgroup.setValue('right', faceDirections[i][1]);
273
+ projectionBindgroup.setValue('front', faceDirections[i][2]);
274
+ projectionBindgroup.setValue('u_width', srcTexture.width);
275
+ projectionBindgroup.setValue('u_coeff', coeff);
276
+ projectionBindgroup.setTexture('u_cubeMap', srcTexture);
277
+ device.draw('triangle-list', 0, 6);
278
+ device.setFramebuffer(null);
279
+ framebuffer.dispose();
280
+ const blitter = new ReduceBlitter();
281
+ const sampler = device.createSampler({
282
+ addressU: 'clamp',
283
+ addressV: 'clamp',
284
+ magFilter: 'nearest',
285
+ minFilter: 'nearest',
286
+ mipFilter: 'none'
287
+ });
288
+ for(let i = 1; i < tmpTextures.length; i++){
289
+ blitter.width = tmpTextures[i - 1].width;
290
+ blitter.blit(tmpTextures[i - 1], tmpTextures[i], sampler);
291
+ }
292
+ const buffer = new Float32Array(4);
293
+ await tmpTextures[tmpTextures.length - 1].readPixels(0, 0, 1, 1, 0, 0, buffer);
294
+ result.x += buffer[0];
295
+ result.y += buffer[1];
296
+ result.z += buffer[2];
297
+ }
298
+ for (const tmpTex of tmpTextures){
299
+ tmpTex.dispose();
300
+ }
301
+ return result;
302
+ }
303
+ /**
304
+ * Projects a function represented in a cubemap into spherical harmonics using GPU
305
+ *
306
+ * @param input - The input cubemap
307
+ * @returns The evaluated SH data
308
+ *
309
+ * @public
310
+ */ async function projectCubemap(tex) {
311
+ const device = Application.instance.device;
312
+ const srcTex = tex;
313
+ if (!device.getDeviceCaps().textureCaps.supportFloatColorBuffer) {
314
+ throw new Error(`projectCubemap(): device does not support rendering to float color buffer`);
315
+ }
316
+ if (!vertexLayout) {
317
+ init();
318
+ }
319
+ device.pushDeviceStates();
320
+ const result = [];
321
+ for(let i = 0; i < 9; i++){
322
+ const v = await doProjectCubemap(srcTex, i);
323
+ result.push(v);
324
+ }
325
+ device.popDeviceStates();
326
+ if (srcTex !== tex) {
327
+ srcTex.dispose();
328
+ }
329
+ return result;
330
+ }
331
+ function directionFromCubemapTexel(face, x, y, invSize) {
332
+ const dir = Vector3.zero();
333
+ switch(face){
334
+ case 0:
335
+ dir.x = 1;
336
+ dir.y = 1 - (2 * y + 1) * invSize;
337
+ dir.z = 1 - (2 * x + 1) * invSize;
338
+ break;
339
+ case 1:
340
+ dir.x = -1;
341
+ dir.y = 1 - (2 * y + 1) * invSize;
342
+ dir.z = 1 - (2 * x + 1) * invSize;
343
+ break;
344
+ case 2:
345
+ dir.x = -1 + (2 * x + 1) * invSize;
346
+ dir.y = 1;
347
+ dir.z = -1 + (2 * y + 1) * invSize;
348
+ break;
349
+ case 3:
350
+ dir.x = -1 + (2 * x + 1) * invSize;
351
+ dir.y = -1;
352
+ dir.z = 1 - (2 * y + 1) * invSize;
353
+ break;
354
+ case 4:
355
+ dir.x = -1 + (2 * x + 1) * invSize;
356
+ dir.y = 1 - (2 * y + 1) * invSize;
357
+ dir.z = 1;
358
+ break;
359
+ case 5:
360
+ dir.x = 1 - (2 * x + 1) * invSize;
361
+ dir.y = 1 - (2 * y + 1) * invSize;
362
+ dir.z = -1;
363
+ break;
364
+ }
365
+ return dir.inplaceNormalize();
366
+ }
367
+ /**
368
+ * Projects a function represented in a cubemap into spherical harmonics using CPU
369
+ *
370
+ * @param input - The input cubemap
371
+ * @returns The evaluated SH data
372
+ *
373
+ * @public
374
+ */ async function projectCubemapCPU(input) {
375
+ let srcTex = input;
376
+ const device = Application.instance.device;
377
+ if (device.getDeviceCaps().textureCaps.supportFloatColorBuffer && input.format === 'rgba16f' && device.type === 'webgl') {
378
+ const dstTex = device.createCubeTexture('rgba32f', input.width, {
379
+ samplerOptions: {
380
+ mipFilter: 'none'
381
+ }
382
+ });
383
+ const blitter = new CopyBlitter();
384
+ blitter.blit(input, dstTex);
385
+ srcTex = dstTex;
386
+ }
387
+ const size = srcTex.width;
388
+ const output = Array.from({
389
+ length: 9
390
+ }).map(()=>Vector3.zero());
391
+ const radiance = new Vector3();
392
+ const input_face = srcTex.format === 'rgba8unorm' || srcTex.format === 'rgba8unorm-srgb' ? new Uint8Array(size * size * 4) : srcTex.format === 'rgba32f' ? new Float32Array(size * size * 4) : srcTex.format === 'rgba16f' ? new Uint16Array(size * size * 4) : srcTex.format === 'rg11b10uf' ? new Uint32Array(size * size) : null;
393
+ if (!input_face) {
394
+ throw new Error(`invalid input texture format: ${input.format}`);
395
+ }
396
+ const fB = -1 + 1 / size;
397
+ const fS = size > 1 ? 2 * (1 - 1 / size) / (size - 1) : 0;
398
+ let fWt = 0;
399
+ for(let face = 0; face < 6; face++){
400
+ await srcTex.readPixels(0, 0, size, size, face, 0, input_face);
401
+ for(let texel = 0; texel < size * size; texel++){
402
+ const x = texel % size;
403
+ const y = Math.floor(texel / size);
404
+ const u = x * fS + fB;
405
+ const v = y * fS + fB;
406
+ const dir = directionFromCubemapTexel(face, x, y, 1 / size);
407
+ const solidAngle = 4 / ((1 + u * u + v * v) * Math.sqrt(1 + u * u + v * v));
408
+ fWt += solidAngle;
409
+ if (srcTex.format === 'rgba8unorm' || srcTex.format === 'rgba8unorm-srgb') {
410
+ radiance.x = input_face[texel * 4 + 0] / 255;
411
+ radiance.y = input_face[texel * 4 + 1] / 255;
412
+ radiance.z = input_face[texel * 4 + 2] / 255;
413
+ if (srcTex.format === 'rgba8unorm-srgb') {
414
+ radiance.x = Math.pow(radiance.x, 2.2);
415
+ radiance.y = Math.pow(radiance.y, 2.2);
416
+ radiance.z = Math.pow(radiance.z, 2.2);
417
+ }
418
+ } else if (srcTex.format === 'rgba32f') {
419
+ radiance.x = input_face[texel * 4 + 0];
420
+ radiance.y = input_face[texel * 4 + 1];
421
+ radiance.z = input_face[texel * 4 + 2];
422
+ } else if (srcTex.format === 'rgba16f') {
423
+ radiance.x = halfToFloat(input_face[texel * 4 + 0]);
424
+ radiance.y = halfToFloat(input_face[texel * 4 + 1]);
425
+ radiance.z = halfToFloat(input_face[texel * 4 + 2]);
426
+ } else if (srcTex.format === 'rg11b10uf') {
427
+ unpackFloat3(input_face[texel], radiance);
428
+ }
429
+ const shBasis = SH.evalBasis(dir, 3);
430
+ for(let c = 0; c < 9; ++c){
431
+ const sh = shBasis[c];
432
+ output[c].x += radiance.x * sh * solidAngle;
433
+ output[c].y += radiance.y * sh * solidAngle;
434
+ output[c].z += radiance.z * sh * solidAngle;
435
+ }
436
+ }
437
+ }
438
+ for (const v of output){
439
+ v.scaleBy(4 * Math.PI / fWt);
440
+ }
441
+ if (srcTex !== input) {
442
+ srcTex.dispose();
443
+ }
444
+ return output;
445
+ }
446
+
447
+ export { projectCubemap, projectCubemapCPU };
448
+ //# sourceMappingURL=shprojection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shprojection.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/values.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Forward render pass type
3
+ * @public
4
+ */ const RENDER_PASS_TYPE_FORWARD = 0;
5
+ /**
6
+ * Shadow map render pass type
7
+ * @public
8
+ */ const RENDER_PASS_TYPE_SHADOWMAP = 1;
9
+ /**
10
+ * Depth only render pass type
11
+ * @public
12
+ */ const RENDER_PASS_TYPE_DEPTH_ONLY = 2;
13
+ /**
14
+ * Max light count for forward rendering
15
+ * @internal
16
+ */ const MAX_FORWARD_LIGHT_COUNT = 8;
17
+ /** @internal */ const DEBUG_CASCADED_SHADOW_MAPS = false;
18
+ /**
19
+ * Builtin texture name for sheen LUT
20
+ * @public
21
+ */ const BUILTIN_ASSET_TEXTURE_SHEEN_LUT = 'LUT_Sheen';
22
+ /**
23
+ * Builtin cube texture for test
24
+ * @public
25
+ */ const BUILTIN_ASSET_TEST_CUBEMAP = 'TEST_Cubemap';
26
+ /**
27
+ * No light
28
+ * @public
29
+ */ const LIGHT_TYPE_NONE = 0;
30
+ /**
31
+ * Directional light type
32
+ * @public
33
+ */ const LIGHT_TYPE_DIRECTIONAL = 1;
34
+ /**
35
+ * Point light type
36
+ * @public
37
+ */ const LIGHT_TYPE_POINT = 2;
38
+ /**
39
+ * Spot light type
40
+ * @public
41
+ */ const LIGHT_TYPE_SPOT = 3;
42
+ /**
43
+ * Max light size for clustered lighting
44
+ * @public
45
+ */ const MAX_CLUSTERED_LIGHTS = 255;
46
+
47
+ export { BUILTIN_ASSET_TEST_CUBEMAP, BUILTIN_ASSET_TEXTURE_SHEEN_LUT, DEBUG_CASCADED_SHADOW_MAPS, LIGHT_TYPE_DIRECTIONAL, LIGHT_TYPE_NONE, LIGHT_TYPE_POINT, LIGHT_TYPE_SPOT, MAX_CLUSTERED_LIGHTS, MAX_FORWARD_LIGHT_COUNT, RENDER_PASS_TYPE_DEPTH_ONLY, RENDER_PASS_TYPE_FORWARD, RENDER_PASS_TYPE_SHADOWMAP };
48
+ //# sourceMappingURL=values.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"values.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@zephyr3d/scene",
3
+ "version": "0.1.0",
4
+ "description": "scene system for zephyr3d",
5
+ "homepage": "https://github.com/gavinyork/zephyr3d#readme",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "files": [
11
+ "**/*.js",
12
+ "**/*.d.ts",
13
+ "**/*.js.map"
14
+ ],
15
+ "sideEffects": false,
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/gavinyork/zephyr3d.git"
19
+ },
20
+ "scripts": {
21
+ "clean": "shx rm -rf ./dist .tsbuildinfo",
22
+ "tsc": "tsc -p . --noEmit",
23
+ "build": "cross-env NODE_OPTIONS=--max_old_space_size=8192 rollup -c --silent"
24
+ },
25
+ "keywords": [
26
+ "javascript",
27
+ "3d",
28
+ "rendering",
29
+ "engine",
30
+ "webgl",
31
+ "webgpu",
32
+ "canvas",
33
+ "zephyr3d",
34
+ "device"
35
+ ],
36
+ "license": "MIT",
37
+ "bugs": {
38
+ "url": "https://github.com/gavinyork/zephyr3d/issues"
39
+ },
40
+ "engines": {
41
+ "node": ">=16.18.0"
42
+ },
43
+ "devDependencies": {
44
+ "@babel/core": "^7.20.12",
45
+ "@babel/preset-env": "^7.20.2",
46
+ "@rollup/plugin-babel": "^6.0.3",
47
+ "@rollup/plugin-node-resolve": "^15.0.1",
48
+ "@rollup/plugin-strip": "^3.0.2",
49
+ "@rollup/plugin-terser": "^0.4.0",
50
+ "@rollup/pluginutils": "^5.0.2",
51
+ "@swc/core": "^1.3.62",
52
+ "rollup-plugin-swc3": "^0.8.2",
53
+ "rollup": "^3.15.0",
54
+ "rollup-plugin-dts": "^5.2.0",
55
+ "rollup-plugin-copy": "^3.4.0",
56
+ "rollup-plugin-typescript2": "^0.34.1",
57
+ "shx": "^0.3.4",
58
+ "cross-env": "^7.0.3",
59
+ "typescript": "^5.1.3",
60
+ "@zephyr3d/base": "workspace:^0.1.0",
61
+ "@zephyr3d/device": "workspace:^0.1.0"
62
+ },
63
+ "peerDependencies": {
64
+ "@zephyr3d/base": "workspace:^0.1.0",
65
+ "@zephyr3d/device": "workspace:^0.1.0"
66
+ },
67
+ "dependencies": {
68
+ "@webgpu/types": "^0.1.31"
69
+ }
70
+ }