@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,373 @@
1
+ import { Vector4, Vector3, Vector2 } from '@zephyr3d/base';
2
+ import { BUILTIN_ASSET_TEXTURE_SHEEN_LUT } from '../values.js';
3
+ import { Application } from '../app.js';
4
+
5
+ /*
6
+ interface MicrofacetDistributionSample {
7
+ pdf?: number;
8
+ cosTheta?: number;
9
+ sinTheta?: number;
10
+ phi?: number;
11
+ }
12
+ */ /** @internal */ function getTestCubemapLoader() {
13
+ return async function(assetManager) {
14
+ const tex = Application.instance.device.createCubeTexture('rgba8unorm', 32, {
15
+ samplerOptions: {
16
+ mipFilter: 'none'
17
+ }
18
+ });
19
+ const fb = Application.instance.device.createFrameBuffer([
20
+ tex
21
+ ], null);
22
+ Application.instance.device.pushDeviceStates();
23
+ Application.instance.device.setFramebuffer(fb);
24
+ const colors = [
25
+ new Vector4(1, 0, 0, 1),
26
+ new Vector4(0.2, 0, 0, 1),
27
+ new Vector4(0, 1, 0, 1),
28
+ new Vector4(0, 0.2, 0, 1),
29
+ new Vector4(0, 0, 1, 1),
30
+ new Vector4(0, 0, 0.2, 1)
31
+ ];
32
+ for(let i = 0; i < 6; i++){
33
+ fb.setColorAttachmentCubeFace(0, i);
34
+ Application.instance.device.clearFrameBuffer(colors[i], null, null);
35
+ }
36
+ Application.instance.device.popDeviceStates();
37
+ fb.dispose();
38
+ return tex;
39
+ };
40
+ }
41
+ /** @internal */ function getSheenLutLoader(textureSize) {
42
+ const bits = new Uint32Array(1);
43
+ //Van der Corput radical inverse
44
+ function radicalInverse_VdC(i) {
45
+ bits[0] = i;
46
+ bits[0] = (bits[0] << 16 | bits[0] >> 16) >>> 0;
47
+ bits[0] = (bits[0] & 0x55555555) << 1 | (bits[0] & 0xaaaaaaaa) >>> 1 >>> 0;
48
+ bits[0] = (bits[0] & 0x33333333) << 2 | (bits[0] & 0xcccccccc) >>> 2 >>> 0;
49
+ bits[0] = (bits[0] & 0x0f0f0f0f) << 4 | (bits[0] & 0xf0f0f0f0) >>> 4 >>> 0;
50
+ bits[0] = (bits[0] & 0x00ff00ff) << 8 | (bits[0] & 0xff00ff00) >>> 8 >>> 0;
51
+ return bits[0] * 2.3283064365386963e-10; // / 0x100000000 or / 4294967296
52
+ }
53
+ function hammersley(i, iN, out) {
54
+ out.setXY(i * iN, radicalInverse_VdC(i));
55
+ }
56
+ /*
57
+ function hammersley(i: number, iN: number, out: Vector2) {
58
+ const tof = 0.5 / 0x80000000;
59
+ let bits = i;
60
+ bits = (bits << 16) | (bits >>> 16);
61
+ bits = ((bits & 0x55555555) << 1) | ((bits & 0xAAAAAAAA) >>> 1);
62
+ bits = ((bits & 0x33333333) << 2) | ((bits & 0xCCCCCCCC) >>> 2);
63
+ bits = ((bits & 0x0F0F0F0F) << 4) | ((bits & 0xF0F0F0F0) >>> 4);
64
+ bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >>> 8);
65
+ out.setXY(i * iN, (bits >>> 0) * tof);
66
+ }
67
+ */ /*
68
+ function generateTBN(normal: Vector3, out: Matrix3x3) {
69
+ bitangent.setXYZ(0.0, 1.0, 0.0);
70
+ const NdotUp = Vector3.dot(normal, up);
71
+ const epsilon = 0.0000001;
72
+ if (1.0 - Math.abs(NdotUp) <= epsilon) {
73
+ // Sampling +Y or -Y, so we need a more robust bitangent.
74
+ if (NdotUp > 0.0) {
75
+ bitangent.setXYZ(0.0, 0.0, 1.0);
76
+ } else {
77
+ bitangent.setXYZ(0.0, 0.0, -1.0);
78
+ }
79
+ }
80
+ Vector3.cross(bitangent, normal, tangent).inplaceNormalize();
81
+ Vector3.cross(normal, tangent, bitangent);
82
+ out.setCol(0, tangent);
83
+ out.setCol(1, bitangent);
84
+ out.setCol(2, normal);
85
+ }
86
+ function mix(x: number, y: number, a: number): number {
87
+ return x * (1 - a) + y * a;
88
+ }
89
+ function l(x: number, alphaG: number): number {
90
+ const oneMinusAlphaSq = (1 - alphaG) * (1 - alphaG);
91
+ const a = mix(21.5473, 25.3245, oneMinusAlphaSq);
92
+ const b = mix(3.82987, 3.32435, oneMinusAlphaSq);
93
+ const c = mix(0.19823, 0.16801, oneMinusAlphaSq);
94
+ const d = mix(-1.9776, -1.27393, oneMinusAlphaSq);
95
+ const e = mix(-4.32054, -4.85967, oneMinusAlphaSq);
96
+ return a / (1 + b * Math.pow(x, c)) + d * x + e;
97
+ }
98
+ function lambdaSheen(cosTheta: number, alphaG: number): number {
99
+ return Math.abs(cosTheta) < 0.5
100
+ ? Math.exp(l(Math.abs(cosTheta), alphaG))
101
+ : Math.exp(2 * l(0.5, alphaG) - l(1 - Math.abs(cosTheta), alphaG));
102
+ }
103
+ function visibilityCharlie(NdotV: number, NdotL: number, a: number): number {
104
+ const alphaG = a;
105
+ return 1 / ((1 + lambdaSheen(NdotV, alphaG) + lambdaSheen(NdotL, alphaG)) * (4 * NdotV * NdotL));
106
+ }
107
+ */ function distributionCharlie(NdotH, roughness) {
108
+ // roughness = Math.max(roughness, 0.000001);
109
+ const invAlpha = 1 / roughness;
110
+ const cos2h = NdotH * NdotH;
111
+ const sin2h = 1 - cos2h;
112
+ return (2 + invAlpha) * Math.pow(sin2h, invAlpha * 0.5) / (2 * Math.PI);
113
+ }
114
+ /*
115
+ function charlie(xi: Vector2, roughness: number, sample: MicrofacetDistributionSample) {
116
+ const alpha = roughness * roughness;
117
+ sample.sinTheta = Math.pow(xi.y, alpha / (2 * alpha + 1));
118
+ sample.cosTheta = Math.sqrt(1 - sample.sinTheta * sample.sinTheta);
119
+ sample.phi = 2 * Math.PI * xi.x;
120
+ sample.pdf = distributionCharlie(sample.cosTheta, Math.max(alpha, 0.000001)) / 4;
121
+ }
122
+ function getImportanceSample(
123
+ sampleIndex: number,
124
+ sampleCount: number,
125
+ N: Vector3,
126
+ roughness: number,
127
+ out: Vector4
128
+ ) {
129
+ // generate a quasi monte carlo point in the unit square [0.1)^2
130
+ hammersley(sampleIndex, 1 / sampleCount, xi);
131
+ // generate the points on the hemisphere with a fitting mapping for
132
+ // the distribution (e.g. lambertian uses a cosine importance)
133
+ charlie(xi, roughness, importanceSample);
134
+
135
+ // transform the hemisphere sample to the normal coordinate frame
136
+ // i.e. rotate the hemisphere to the normal direction
137
+ localSpaceDirection
138
+ .setXYZ(
139
+ importanceSample.sinTheta * Math.cos(importanceSample.phi),
140
+ importanceSample.sinTheta * Math.sin(importanceSample.phi),
141
+ importanceSample.cosTheta
142
+ )
143
+ .inplaceNormalize();
144
+ generateTBN(N, TBN);
145
+ TBN.transform(localSpaceDirection, direction);
146
+ out.setXYZW(direction.x, direction.y, direction.z, importanceSample.pdf);
147
+ }
148
+ function lut(NdotV: number, roughness: number, numSamples: number, out: Vector4) {
149
+ V.setXYZ(Math.sqrt(1 - NdotV * NdotV), 0, NdotV);
150
+ N.setXYZ(0, 0, 1);
151
+ const A = 0;
152
+ const B = 0;
153
+ let C = 0;
154
+ const importanceSample = new Vector4();
155
+ for (let i = 0; i < numSamples; i++) {
156
+ getImportanceSample(i, numSamples, N, roughness, importanceSample);
157
+ H.setXYZ(importanceSample.x, importanceSample.y, importanceSample.z);
158
+ // do reflect L = normalize(reflect(-V, H)) = normalize(-V - 2.0 * dot(H, -V) * H) = normalize(2 * dot(H, V) * H - V)
159
+ Vector3.scale(H, Vector3.dot(V, H) * 2, L)
160
+ .subBy(V)
161
+ .inplaceNormalize();
162
+ const NdotL = Math.min(Math.max(L.z, 0), 1);
163
+ const NdotH = Math.min(Math.max(H.z, 0), 1);
164
+ const VdotH = Math.min(Math.max(Vector3.dot(V, H), 0), 1);
165
+ if (NdotL > 0) {
166
+ const sheenDistribution = distributionCharlie(NdotH, roughness);
167
+ // const sheenVisibility = visibilityAshikhmin(NdotV, NdotL);
168
+ const sheenVisibility = visibilityCharlie(NdotV, NdotL, roughness);
169
+ C += sheenVisibility * sheenDistribution * NdotL * VdotH;
170
+ }
171
+ }
172
+ out.setXYZW(4 * A, 4 * B, 4 * 2 * Math.PI * C, 0).scaleBy(1 / numSamples);
173
+ }
174
+
175
+ async function createSheenLUT(): Promise<Texture2D> {
176
+ const tex = Application.instance.device.createTexture2D('rgba8unorm', textureSize, textureSize);
177
+ const image = new Uint8Array(textureSize * textureSize * 4);
178
+ let p = 0;
179
+ const c = new Vector4();
180
+ for (let y = 0; y < textureSize; y++) {
181
+ const coord = Math.min(Math.max((y + 0.5) / textureSize, 0), 1);
182
+ const roughness = coord;
183
+ for (let x = 0; x < textureSize; x++) {
184
+ const NdotV = Math.min(Math.max((x + 0.5) / textureSize, 0), 1);
185
+ // const c = dfvCharlieUniform(NdotV, roughness, 1024);
186
+ // const c = Math.min(Math.max(Math.round(t * 255), 0), 255);
187
+ lut(NdotV, roughness, 1024, c);
188
+ image[p++] = Math.min(Math.max(Math.round(c.x * 255), 0), 255);
189
+ image[p++] = Math.min(Math.max(Math.round(c.y * 255), 0), 255);
190
+ image[p++] = Math.min(Math.max(Math.round(c.z * 255), 0), 255);
191
+ image[p++] = 255;
192
+ }
193
+ }
194
+ tex.update(image, 0, 0, textureSize, textureSize);
195
+ tex.name = `builtin:${BUILTIN_ASSET_TEXTURE_SHEEN_LUT}`;
196
+ return tex;
197
+ }
198
+ */ //////////////////////////////////////////////////////////////////////////////////////////////////////
199
+ function visibilityAshikhmin(NdotV, NdotL) {
200
+ return Math.min(Math.max(1 / (4 * (NdotL + NdotV - NdotL * NdotV)), 0), 1);
201
+ }
202
+ function hemisphereUniformSample(u, out) {
203
+ const phi = 2 * Math.PI * u.x;
204
+ const cosTheta = 1 - u.y;
205
+ const sinTheta = Math.sqrt(1 - cosTheta * cosTheta);
206
+ out.setXYZ(sinTheta * Math.cos(phi), sinTheta * Math.sin(phi), cosTheta);
207
+ }
208
+ function dfvCharlieUniform(NdotV, roughness, numSamples) {
209
+ let r = 0;
210
+ const V = new Vector3(Math.sqrt(1 - NdotV * NdotV), 0, NdotV);
211
+ const u = new Vector2();
212
+ const H = new Vector3();
213
+ const L = new Vector3();
214
+ for(let i = 0; i < numSamples; i++){
215
+ hammersley(i, 1 / numSamples, u);
216
+ hemisphereUniformSample(u, H);
217
+ Vector3.scale(H, Vector3.dot(V, H) * 2, L).subBy(V);
218
+ const VdotH = Math.min(Math.max(Vector3.dot(V, H), 0), 1);
219
+ const NdotL = Math.min(Math.max(L.z, 0), 1);
220
+ const NdotH = Math.min(Math.max(H.z, 0), 1);
221
+ if (NdotL > 0) {
222
+ const v = visibilityAshikhmin(NdotV, NdotL);
223
+ // const v = visibilityCharlie(NdotV, NdotL, roughness);
224
+ const d = distributionCharlie(NdotH, roughness);
225
+ r += v * d * NdotL * VdotH;
226
+ }
227
+ }
228
+ return r * (4 * 2 * Math.PI / numSamples);
229
+ }
230
+ const _tables = function _generateTables() {
231
+ // float32 to float16 helpers
232
+ const buffer = new ArrayBuffer(4);
233
+ const floatView = new Float32Array(buffer);
234
+ const uint32View = new Uint32Array(buffer);
235
+ const baseTable = new Uint32Array(512);
236
+ const shiftTable = new Uint32Array(512);
237
+ for(let i = 0; i < 256; ++i){
238
+ const e = i - 127;
239
+ // very small number (0, -0)
240
+ if (e < -27) {
241
+ baseTable[i] = 0x0000;
242
+ baseTable[i | 0x100] = 0x8000;
243
+ shiftTable[i] = 24;
244
+ shiftTable[i | 0x100] = 24;
245
+ // small number (denorm)
246
+ } else if (e < -14) {
247
+ baseTable[i] = 0x0400 >> -e - 14;
248
+ baseTable[i | 0x100] = 0x0400 >> -e - 14 | 0x8000;
249
+ shiftTable[i] = -e - 1;
250
+ shiftTable[i | 0x100] = -e - 1;
251
+ // normal number
252
+ } else if (e <= 15) {
253
+ baseTable[i] = e + 15 << 10;
254
+ baseTable[i | 0x100] = e + 15 << 10 | 0x8000;
255
+ shiftTable[i] = 13;
256
+ shiftTable[i | 0x100] = 13;
257
+ // large number (Infinity, -Infinity)
258
+ } else if (e < 128) {
259
+ baseTable[i] = 0x7c00;
260
+ baseTable[i | 0x100] = 0xfc00;
261
+ shiftTable[i] = 24;
262
+ shiftTable[i | 0x100] = 24;
263
+ // stay (NaN, Infinity, -Infinity)
264
+ } else {
265
+ baseTable[i] = 0x7c00;
266
+ baseTable[i | 0x100] = 0xfc00;
267
+ shiftTable[i] = 13;
268
+ shiftTable[i | 0x100] = 13;
269
+ }
270
+ }
271
+ // float16 to float32 helpers
272
+ const mantissaTable = new Uint32Array(2048);
273
+ const exponentTable = new Uint32Array(64);
274
+ const offsetTable = new Uint32Array(64);
275
+ for(let i = 1; i < 1024; ++i){
276
+ let m = i << 13; // zero pad mantissa bits
277
+ let e = 0; // zero exponent
278
+ // normalized
279
+ while((m & 0x00800000) === 0){
280
+ m <<= 1;
281
+ e -= 0x00800000; // decrement exponent
282
+ }
283
+ m &= ~0x00800000; // clear leading 1 bit
284
+ e += 0x38800000; // adjust bias
285
+ mantissaTable[i] = m | e;
286
+ }
287
+ for(let i = 1024; i < 2048; ++i){
288
+ mantissaTable[i] = 0x38000000 + (i - 1024 << 13);
289
+ }
290
+ for(let i = 1; i < 31; ++i){
291
+ exponentTable[i] = i << 23;
292
+ }
293
+ exponentTable[31] = 0x47800000;
294
+ exponentTable[32] = 0x80000000;
295
+ for(let i = 33; i < 63; ++i){
296
+ exponentTable[i] = 0x80000000 + (i - 32 << 23);
297
+ }
298
+ exponentTable[63] = 0xc7800000;
299
+ for(let i = 1; i < 64; ++i){
300
+ if (i !== 32) {
301
+ offsetTable[i] = 1024;
302
+ }
303
+ }
304
+ return {
305
+ floatView: floatView,
306
+ uint32View: uint32View,
307
+ baseTable: baseTable,
308
+ shiftTable: shiftTable,
309
+ mantissaTable: mantissaTable,
310
+ exponentTable: exponentTable,
311
+ offsetTable: offsetTable
312
+ };
313
+ }();
314
+ function encodeF16(val) {
315
+ val = Math.min(Math.max(val, -65504), 65504);
316
+ _tables.floatView[0] = val;
317
+ const f = _tables.uint32View[0];
318
+ const e = f >> 23 & 0x1ff;
319
+ return _tables.baseTable[e] + ((f & 0x007fffff) >> _tables.shiftTable[e]);
320
+ }
321
+ /*
322
+ function decodeF16(val: number) {
323
+ const exponent = (val & 0x7c00) >> 10;
324
+ const fraction = val & 0x03ff;
325
+ return (
326
+ (val >> 15 ? -1 : 1) *
327
+ (exponent
328
+ ? exponent === 0x1f
329
+ ? fraction
330
+ ? NaN
331
+ : Infinity
332
+ : Math.pow(2, exponent - 15) * (1 + fraction / 0x400)
333
+ : 6.103515625e-5 * (fraction / 0x400))
334
+ );
335
+ }
336
+ */ async function createSheenLUTFilament(assetManager, texture) {
337
+ if (texture) {
338
+ if (!texture.isTexture2D()) {
339
+ throw new Error('can not reload sheen lut texture: invalid texture type');
340
+ }
341
+ if (texture.format !== 'rgba16f') {
342
+ throw new Error('can not reload sheen lut texture: invalid texture format');
343
+ }
344
+ if (texture.width !== textureSize || texture.height !== textureSize) {
345
+ throw new Error('can not reload sheen lut texture: invalid texture size');
346
+ }
347
+ }
348
+ const tex = texture || Application.instance.device.createTexture2D('rgba16f', textureSize, textureSize);
349
+ const image = new Uint16Array(textureSize * textureSize * 4);
350
+ let p = 0;
351
+ const one = encodeF16(1);
352
+ for(let y = 0; y < textureSize; y++){
353
+ const coord = Math.min(Math.max((y + 0.5) / textureSize, 0), 1);
354
+ const roughness = coord * coord;
355
+ for(let x = 0; x < textureSize; x++){
356
+ const NdotV = Math.min(Math.max((x + 0.5) / textureSize, 0), 1);
357
+ const c = dfvCharlieUniform(NdotV, roughness, 4096);
358
+ const f16 = encodeF16(c);
359
+ image[p++] = 0;
360
+ image[p++] = 0;
361
+ image[p++] = f16;
362
+ image[p++] = one;
363
+ }
364
+ }
365
+ tex.update(image, 0, 0, textureSize, textureSize);
366
+ tex.name = `builtin:${BUILTIN_ASSET_TEXTURE_SHEEN_LUT}`;
367
+ return tex;
368
+ }
369
+ return createSheenLUTFilament;
370
+ }
371
+
372
+ export { getSheenLutLoader, getTestCubemapLoader };
373
+ //# sourceMappingURL=builtin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builtin.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}