@zephyr3d/scene 0.9.1 → 0.9.3

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 (522) hide show
  1. package/dist/animation/animationset.js +2 -0
  2. package/dist/animation/animationset.js.map +1 -1
  3. package/dist/animation/cloth/gpu_cloth_system.js +9 -1
  4. package/dist/animation/cloth/gpu_cloth_system.js.map +1 -1
  5. package/dist/animation/fixed_geometry_cache_track.js +5 -1
  6. package/dist/animation/fixed_geometry_cache_track.js.map +1 -1
  7. package/dist/animation/geometry_cache_utils.js +20 -4
  8. package/dist/animation/geometry_cache_utils.js.map +1 -1
  9. package/dist/animation/ik/ik_angle_constraint.js +2 -6
  10. package/dist/animation/ik/ik_angle_constraint.js.map +1 -1
  11. package/dist/animation/ik/ik_chain_builder.js +68 -0
  12. package/dist/animation/ik/ik_chain_builder.js.map +1 -0
  13. package/dist/animation/ik/ik_pole_constraint.js +2 -7
  14. package/dist/animation/ik/ik_pole_constraint.js.map +1 -1
  15. package/dist/animation/ik/two_bone_ik_solver.js +2 -6
  16. package/dist/animation/ik/two_bone_ik_solver.js.map +1 -1
  17. package/dist/animation/joint_dynamics/collision.js +60 -12
  18. package/dist/animation/joint_dynamics/collision.js.map +1 -1
  19. package/dist/animation/joint_dynamics/constraints.js +20 -4
  20. package/dist/animation/joint_dynamics/constraints.js.map +1 -1
  21. package/dist/animation/joint_dynamics/controller.js +94 -46
  22. package/dist/animation/joint_dynamics/controller.js.map +1 -1
  23. package/dist/animation/joint_dynamics/math.js +16 -0
  24. package/dist/animation/joint_dynamics/math.js.map +1 -0
  25. package/dist/animation/joint_dynamics/solver.js +21 -11
  26. package/dist/animation/joint_dynamics/solver.js.map +1 -1
  27. package/dist/animation/joint_dynamics/types.js +15 -3
  28. package/dist/animation/joint_dynamics/types.js.map +1 -1
  29. package/dist/animation/pca_geometry_cache_track.js +5 -1
  30. package/dist/animation/pca_geometry_cache_track.js.map +1 -1
  31. package/dist/animation/skeleton.js +2 -2
  32. package/dist/animation/skeleton.js.map +1 -1
  33. package/dist/animation/skeleton_modifier.js +2 -6
  34. package/dist/animation/skeleton_modifier.js.map +1 -1
  35. package/dist/animation/spring/spring_particle.js +4 -4
  36. package/dist/animation/spring/spring_particle.js.map +1 -1
  37. package/dist/animation/spring/spring_system.js +13 -40
  38. package/dist/animation/spring/spring_system.js.map +1 -1
  39. package/dist/animation/spring2/constraints.js.map +1 -1
  40. package/dist/animation/spring2/controller.js +64 -64
  41. package/dist/animation/spring2/controller.js.map +1 -1
  42. package/dist/animation/spring2/math.js.map +1 -1
  43. package/dist/animation/spring2/spring_system.js +8 -8
  44. package/dist/animation/spring2/spring_system.js.map +1 -1
  45. package/dist/app/app.js.map +1 -1
  46. package/dist/app/engine.js +1 -1
  47. package/dist/app/engine.js.map +1 -1
  48. package/dist/app/screen.js +4 -4
  49. package/dist/app/screen.js.map +1 -1
  50. package/dist/app/scriptingsystem.js +24 -1
  51. package/dist/app/scriptingsystem.js.map +1 -1
  52. package/dist/app/scriptregistry.js +22 -1
  53. package/dist/app/scriptregistry.js.map +1 -1
  54. package/dist/asset/assetmanager.js +2 -2
  55. package/dist/asset/assetmanager.js.map +1 -1
  56. package/dist/asset/model.js +60 -60
  57. package/dist/asset/model.js.map +1 -1
  58. package/dist/camera/camera.js +1 -1
  59. package/dist/camera/camera.js.map +1 -1
  60. package/dist/camera/orbit.js.map +1 -1
  61. package/dist/index.d.ts +795 -266
  62. package/dist/index.js +3 -2
  63. package/dist/index.js.map +1 -1
  64. package/dist/material/material.js +3 -1
  65. package/dist/material/material.js.map +1 -1
  66. package/dist/material/mixins/lightmodel/pbrmetallicroughness.js.map +1 -1
  67. package/dist/material/shader/helper.js +281 -278
  68. package/dist/material/shader/helper.js.map +1 -1
  69. package/dist/material/sprite.js +6 -6
  70. package/dist/material/sprite.js.map +1 -1
  71. package/dist/material/terrain-cm.js +2 -2
  72. package/dist/material/terrain-cm.js.map +1 -1
  73. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimemgr.js +38 -0
  74. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimemgr.js.map +1 -0
  75. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimescript.js +10 -0
  76. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimescript.js.map +1 -0
  77. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptingsystem.js +127 -0
  78. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptingsystem.js.map +1 -0
  79. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptregistry.js +263 -0
  80. package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptregistry.js.map +1 -0
  81. package/dist/posteffect/taa.js +31 -39
  82. package/dist/posteffect/taa.js.map +1 -1
  83. package/dist/render/cluster_light.js +3 -4
  84. package/dist/render/cluster_light.js.map +1 -1
  85. package/dist/render/drawable.js +5 -1
  86. package/dist/render/drawable.js.map +1 -1
  87. package/dist/render/rendergraph/device_pool_allocator.js +29 -3
  88. package/dist/render/rendergraph/device_pool_allocator.js.map +1 -1
  89. package/dist/render/rendergraph/executor.js +283 -33
  90. package/dist/render/rendergraph/executor.js.map +1 -1
  91. package/dist/render/rendergraph/forward_plus_builder.js +338 -108
  92. package/dist/render/rendergraph/forward_plus_builder.js.map +1 -1
  93. package/dist/render/rendergraph/history_resource_manager.js +195 -115
  94. package/dist/render/rendergraph/history_resource_manager.js.map +1 -1
  95. package/dist/render/rendergraph/history_resources.js +10 -0
  96. package/dist/render/rendergraph/history_resources.js.map +1 -0
  97. package/dist/render/rendergraph/rendergraph.js +162 -35
  98. package/dist/render/rendergraph/rendergraph.js.map +1 -1
  99. package/dist/render/rendergraph/types.js +25 -5
  100. package/dist/render/rendergraph/types.js.map +1 -1
  101. package/dist/render/rendertarget.js +5 -1
  102. package/dist/render/rendertarget.js.map +1 -1
  103. package/dist/render/screenrendertarget.js +5 -1
  104. package/dist/render/screenrendertarget.js.map +1 -1
  105. package/dist/render/sky.js +4 -4
  106. package/dist/render/sky.js.map +1 -1
  107. package/dist/scene/basesprite.js +2 -2
  108. package/dist/scene/basesprite.js.map +1 -1
  109. package/dist/scene/graph_node.js +1 -1
  110. package/dist/scene/graph_node.js.map +1 -1
  111. package/dist/scene/mesh.js +81 -97
  112. package/dist/scene/mesh.js.map +1 -1
  113. package/dist/scene/particlesys.js +1 -1
  114. package/dist/scene/particlesys.js.map +1 -1
  115. package/dist/scene/scene_node.js +228 -228
  116. package/dist/scene/scene_node.js.map +1 -1
  117. package/dist/scene/script_attachment.js +15 -3
  118. package/dist/scene/script_attachment.js.map +1 -1
  119. package/dist/scene/terrain-cm/terrain-cm.js +3 -3
  120. package/dist/scene/terrain-cm/terrain-cm.js.map +1 -1
  121. package/dist/shadow/shadowmapper.js.map +1 -1
  122. package/dist/shapes/box.js +1 -1
  123. package/dist/shapes/box.js.map +1 -1
  124. package/dist/shapes/capsule.js +0 -3
  125. package/dist/shapes/capsule.js.map +1 -1
  126. package/dist/shapes/cylinder.js +0 -3
  127. package/dist/shapes/cylinder.js.map +1 -1
  128. package/dist/shapes/plane.js +0 -3
  129. package/dist/shapes/plane.js.map +1 -1
  130. package/dist/shapes/tetrahedron.js +3 -9
  131. package/dist/shapes/tetrahedron.js.map +1 -1
  132. package/dist/shapes/torus.js +94 -66
  133. package/dist/shapes/torus.js.map +1 -1
  134. package/dist/src/animation/animation.js +127 -0
  135. package/dist/src/animation/animation.js.map +1 -0
  136. package/dist/src/animation/animationset.js +255 -0
  137. package/dist/src/animation/animationset.js.map +1 -0
  138. package/dist/src/animation/animationtrack.js +34 -0
  139. package/dist/src/animation/animationtrack.js.map +1 -0
  140. package/dist/src/animation/eulerrotationtrack.js +52 -0
  141. package/dist/src/animation/eulerrotationtrack.js.map +1 -0
  142. package/dist/src/animation/morphtarget.js +93 -0
  143. package/dist/src/animation/morphtarget.js.map +1 -0
  144. package/dist/src/animation/morphtrack.js +70 -0
  145. package/dist/src/animation/morphtrack.js.map +1 -0
  146. package/dist/src/animation/proptrack.js +161 -0
  147. package/dist/src/animation/proptrack.js.map +1 -0
  148. package/dist/src/animation/rotationtrack.js +51 -0
  149. package/dist/src/animation/rotationtrack.js.map +1 -0
  150. package/dist/src/animation/scaletrack.js +50 -0
  151. package/dist/src/animation/scaletrack.js.map +1 -0
  152. package/dist/src/animation/skeleton.js +204 -0
  153. package/dist/src/animation/skeleton.js.map +1 -0
  154. package/dist/src/animation/translationtrack.js +50 -0
  155. package/dist/src/animation/translationtrack.js.map +1 -0
  156. package/dist/src/app/app.js +129 -0
  157. package/dist/src/app/app.js.map +1 -0
  158. package/dist/src/app/inputmgr.js +267 -0
  159. package/dist/src/app/inputmgr.js.map +1 -0
  160. package/dist/src/asset/assetmanager.js +404 -0
  161. package/dist/src/asset/assetmanager.js.map +1 -0
  162. package/dist/src/asset/builtin.js +337 -0
  163. package/dist/src/asset/builtin.js.map +1 -0
  164. package/dist/src/asset/loaders/dds/dds.js +470 -0
  165. package/dist/src/asset/loaders/dds/dds.js.map +1 -0
  166. package/dist/src/asset/loaders/dds/dds_loader.js +28 -0
  167. package/dist/src/asset/loaders/dds/dds_loader.js.map +1 -0
  168. package/dist/src/asset/loaders/gltf/gltf_loader.js +1265 -0
  169. package/dist/src/asset/loaders/gltf/gltf_loader.js.map +1 -0
  170. package/dist/src/asset/loaders/gltf/helpers.js +327 -0
  171. package/dist/src/asset/loaders/gltf/helpers.js.map +1 -0
  172. package/dist/src/asset/loaders/hdr/hdr.js +180 -0
  173. package/dist/src/asset/loaders/hdr/hdr.js.map +1 -0
  174. package/dist/src/asset/loaders/image/tga_Loader.js +116 -0
  175. package/dist/src/asset/loaders/image/tga_Loader.js.map +1 -0
  176. package/dist/src/asset/loaders/image/webimage_loader.js +63 -0
  177. package/dist/src/asset/loaders/image/webimage_loader.js.map +1 -0
  178. package/dist/src/asset/loaders/loader.js +45 -0
  179. package/dist/src/asset/loaders/loader.js.map +1 -0
  180. package/dist/src/asset/model.js +414 -0
  181. package/dist/src/asset/model.js.map +1 -0
  182. package/dist/src/blitter/bilateralblur.js +221 -0
  183. package/dist/src/blitter/bilateralblur.js.map +1 -0
  184. package/dist/src/blitter/blitter.js +390 -0
  185. package/dist/src/blitter/blitter.js.map +1 -0
  186. package/dist/src/blitter/box.js +118 -0
  187. package/dist/src/blitter/box.js.map +1 -0
  188. package/dist/src/blitter/copy.js +22 -0
  189. package/dist/src/blitter/copy.js.map +1 -0
  190. package/dist/src/blitter/gaussianblur.js +228 -0
  191. package/dist/src/blitter/gaussianblur.js.map +1 -0
  192. package/dist/src/camera/base.js +92 -0
  193. package/dist/src/camera/base.js.map +1 -0
  194. package/dist/src/camera/camera.js +1005 -0
  195. package/dist/src/camera/camera.js.map +1 -0
  196. package/dist/src/camera/fps.js +238 -0
  197. package/dist/src/camera/fps.js.map +1 -0
  198. package/dist/src/camera/orbit.js +245 -0
  199. package/dist/src/camera/orbit.js.map +1 -0
  200. package/dist/src/camera/orthocamera.js +167 -0
  201. package/dist/src/camera/orthocamera.js.map +1 -0
  202. package/dist/src/camera/perspectivecamera.js +141 -0
  203. package/dist/src/camera/perspectivecamera.js.map +1 -0
  204. package/dist/src/index.js +120 -0
  205. package/dist/src/index.js.map +1 -0
  206. package/dist/src/material/blinn.js +81 -0
  207. package/dist/src/material/blinn.js.map +1 -0
  208. package/dist/src/material/grassmaterial.js +113 -0
  209. package/dist/src/material/grassmaterial.js.map +1 -0
  210. package/dist/src/material/lambert.js +92 -0
  211. package/dist/src/material/lambert.js.map +1 -0
  212. package/dist/src/material/material.js +301 -0
  213. package/dist/src/material/material.js.map +1 -0
  214. package/dist/src/material/meshmaterial.js +704 -0
  215. package/dist/src/material/meshmaterial.js.map +1 -0
  216. package/dist/src/material/mixins/albedocolor.js +76 -0
  217. package/dist/src/material/mixins/albedocolor.js.map +1 -0
  218. package/dist/src/material/mixins/foliage.js +47 -0
  219. package/dist/src/material/mixins/foliage.js.map +1 -0
  220. package/dist/src/material/mixins/lightmodel/blinnphong.js +112 -0
  221. package/dist/src/material/mixins/lightmodel/blinnphong.js.map +1 -0
  222. package/dist/src/material/mixins/lightmodel/lambert.js +58 -0
  223. package/dist/src/material/mixins/lightmodel/lambert.js.map +1 -0
  224. package/dist/src/material/mixins/lightmodel/pbrmetallicroughness.js +178 -0
  225. package/dist/src/material/mixins/lightmodel/pbrmetallicroughness.js.map +1 -0
  226. package/dist/src/material/mixins/lightmodel/pbrspecularglossness.js +139 -0
  227. package/dist/src/material/mixins/lightmodel/pbrspecularglossness.js.map +1 -0
  228. package/dist/src/material/mixins/lit.js +476 -0
  229. package/dist/src/material/mixins/lit.js.map +1 -0
  230. package/dist/src/material/mixins/pbr/common.js +918 -0
  231. package/dist/src/material/mixins/pbr/common.js.map +1 -0
  232. package/dist/src/material/mixins/texture.js +172 -0
  233. package/dist/src/material/mixins/texture.js.map +1 -0
  234. package/dist/src/material/mixins/vertexcolor.js +56 -0
  235. package/dist/src/material/mixins/vertexcolor.js.map +1 -0
  236. package/dist/src/material/particle.js +178 -0
  237. package/dist/src/material/particle.js.map +1 -0
  238. package/dist/src/material/pbrmr.js +97 -0
  239. package/dist/src/material/pbrmr.js.map +1 -0
  240. package/dist/src/material/pbrsg.js +97 -0
  241. package/dist/src/material/pbrsg.js.map +1 -0
  242. package/dist/src/material/shader/helper.js +1209 -0
  243. package/dist/src/material/shader/helper.js.map +1 -0
  244. package/dist/src/material/terrain-cm.js +606 -0
  245. package/dist/src/material/terrain-cm.js.map +1 -0
  246. package/dist/src/material/terrainmaterial.js +375 -0
  247. package/dist/src/material/terrainmaterial.js.map +1 -0
  248. package/dist/src/material/unlit.js +41 -0
  249. package/dist/src/material/unlit.js.map +1 -0
  250. package/dist/src/material/water.js +417 -0
  251. package/dist/src/material/water.js.map +1 -0
  252. package/dist/src/posteffect/bloom.js +361 -0
  253. package/dist/src/posteffect/bloom.js.map +1 -0
  254. package/dist/src/posteffect/compositor.js +226 -0
  255. package/dist/src/posteffect/compositor.js.map +1 -0
  256. package/dist/src/posteffect/fxaa.js +273 -0
  257. package/dist/src/posteffect/fxaa.js.map +1 -0
  258. package/dist/src/posteffect/grayscale.js +69 -0
  259. package/dist/src/posteffect/grayscale.js.map +1 -0
  260. package/dist/src/posteffect/motionblur.js +96 -0
  261. package/dist/src/posteffect/motionblur.js.map +1 -0
  262. package/dist/src/posteffect/posteffect.js +126 -0
  263. package/dist/src/posteffect/posteffect.js.map +1 -0
  264. package/dist/src/posteffect/sao.js +324 -0
  265. package/dist/src/posteffect/sao.js.map +1 -0
  266. package/dist/src/posteffect/ssr.js +489 -0
  267. package/dist/src/posteffect/ssr.js.map +1 -0
  268. package/dist/src/posteffect/taa.js +172 -0
  269. package/dist/src/posteffect/taa.js.map +1 -0
  270. package/dist/src/posteffect/tonemap.js +94 -0
  271. package/dist/src/posteffect/tonemap.js.map +1 -0
  272. package/dist/src/render/abuffer_oit.js +361 -0
  273. package/dist/src/render/abuffer_oit.js.map +1 -0
  274. package/dist/src/render/clipmap.js +851 -0
  275. package/dist/src/render/clipmap.js.map +1 -0
  276. package/dist/src/render/cluster_light.js +333 -0
  277. package/dist/src/render/cluster_light.js.map +1 -0
  278. package/dist/src/render/cull_visitor.js +187 -0
  279. package/dist/src/render/cull_visitor.js.map +1 -0
  280. package/dist/src/render/depthpass.js +68 -0
  281. package/dist/src/render/depthpass.js.map +1 -0
  282. package/dist/src/render/drawable_mixin.js +227 -0
  283. package/dist/src/render/drawable_mixin.js.map +1 -0
  284. package/dist/src/render/envlight.js +463 -0
  285. package/dist/src/render/envlight.js.map +1 -0
  286. package/dist/src/render/fbm_wavegenerator.js +251 -0
  287. package/dist/src/render/fbm_wavegenerator.js.map +1 -0
  288. package/dist/src/render/fft_wavegenerator.js +1006 -0
  289. package/dist/src/render/fft_wavegenerator.js.map +1 -0
  290. package/dist/src/render/fullscreenquad.js +38 -0
  291. package/dist/src/render/fullscreenquad.js.map +1 -0
  292. package/dist/src/render/gerstner_wavegenerator.js +314 -0
  293. package/dist/src/render/gerstner_wavegenerator.js.map +1 -0
  294. package/dist/src/render/globalbindgroup_allocator.js +60 -0
  295. package/dist/src/render/globalbindgroup_allocator.js.map +1 -0
  296. package/dist/src/render/hzb.js +273 -0
  297. package/dist/src/render/hzb.js.map +1 -0
  298. package/dist/src/render/lightpass.js +172 -0
  299. package/dist/src/render/lightpass.js.map +1 -0
  300. package/dist/src/render/objectcolorpass.js +51 -0
  301. package/dist/src/render/objectcolorpass.js.map +1 -0
  302. package/dist/src/render/primitive.js +364 -0
  303. package/dist/src/render/primitive.js.map +1 -0
  304. package/dist/src/render/render_queue.js +467 -0
  305. package/dist/src/render/render_queue.js.map +1 -0
  306. package/dist/src/render/renderbundle_wrapper.js +152 -0
  307. package/dist/src/render/renderbundle_wrapper.js.map +1 -0
  308. package/dist/src/render/renderer.js +455 -0
  309. package/dist/src/render/renderer.js.map +1 -0
  310. package/dist/src/render/renderpass.js +200 -0
  311. package/dist/src/render/renderpass.js.map +1 -0
  312. package/dist/src/render/shadowmap_pass.js +56 -0
  313. package/dist/src/render/shadowmap_pass.js.map +1 -0
  314. package/dist/src/render/sky.js +1103 -0
  315. package/dist/src/render/sky.js.map +1 -0
  316. package/dist/src/render/weightedblended_oit.js +168 -0
  317. package/dist/src/render/weightedblended_oit.js.map +1 -0
  318. package/dist/src/scene/batchgroup.js +162 -0
  319. package/dist/src/scene/batchgroup.js.map +1 -0
  320. package/dist/src/scene/environment.js +209 -0
  321. package/dist/src/scene/environment.js.map +1 -0
  322. package/dist/src/scene/graph_node.js +72 -0
  323. package/dist/src/scene/graph_node.js.map +1 -0
  324. package/dist/src/scene/light.js +416 -0
  325. package/dist/src/scene/light.js.map +1 -0
  326. package/dist/src/scene/mesh.js +341 -0
  327. package/dist/src/scene/mesh.js.map +1 -0
  328. package/dist/src/scene/octree.js +649 -0
  329. package/dist/src/scene/octree.js.map +1 -0
  330. package/dist/src/scene/particlesys.js +738 -0
  331. package/dist/src/scene/particlesys.js.map +1 -0
  332. package/dist/src/scene/raycast_visitor.js +103 -0
  333. package/dist/src/scene/raycast_visitor.js.map +1 -0
  334. package/dist/src/scene/scene.js +284 -0
  335. package/dist/src/scene/scene.js.map +1 -0
  336. package/dist/src/scene/scene_node.js +732 -0
  337. package/dist/src/scene/scene_node.js.map +1 -0
  338. package/dist/src/scene/terrain/grass.js +278 -0
  339. package/dist/src/scene/terrain/grass.js.map +1 -0
  340. package/dist/src/scene/terrain/heightfield.js +475 -0
  341. package/dist/src/scene/terrain/heightfield.js.map +1 -0
  342. package/dist/src/scene/terrain/patch.js +530 -0
  343. package/dist/src/scene/terrain/patch.js.map +1 -0
  344. package/dist/src/scene/terrain/quadtree.js +461 -0
  345. package/dist/src/scene/terrain/quadtree.js.map +1 -0
  346. package/dist/src/scene/terrain/terrain.js +246 -0
  347. package/dist/src/scene/terrain/terrain.js.map +1 -0
  348. package/dist/src/scene/terrain-cm/grass.js +594 -0
  349. package/dist/src/scene/terrain-cm/grass.js.map +1 -0
  350. package/dist/src/scene/terrain-cm/grassmaterial.js +159 -0
  351. package/dist/src/scene/terrain-cm/grassmaterial.js.map +1 -0
  352. package/dist/src/scene/terrain-cm/terrain-cm.js +538 -0
  353. package/dist/src/scene/terrain-cm/terrain-cm.js.map +1 -0
  354. package/dist/src/scene/water.js +374 -0
  355. package/dist/src/scene/water.js.map +1 -0
  356. package/dist/src/shaders/atmosphere.js +957 -0
  357. package/dist/src/shaders/atmosphere.js.map +1 -0
  358. package/dist/src/shaders/fog.js +112 -0
  359. package/dist/src/shaders/fog.js.map +1 -0
  360. package/dist/src/shaders/misc.js +266 -0
  361. package/dist/src/shaders/misc.js.map +1 -0
  362. package/dist/src/shaders/noise.js +222 -0
  363. package/dist/src/shaders/noise.js.map +1 -0
  364. package/dist/src/shaders/pbr.js +51 -0
  365. package/dist/src/shaders/pbr.js.map +1 -0
  366. package/dist/src/shaders/shadow.js +636 -0
  367. package/dist/src/shaders/shadow.js.map +1 -0
  368. package/dist/src/shaders/ssr.js +490 -0
  369. package/dist/src/shaders/ssr.js.map +1 -0
  370. package/dist/src/shaders/temporal.js +215 -0
  371. package/dist/src/shaders/temporal.js.map +1 -0
  372. package/dist/src/shaders/water.js +756 -0
  373. package/dist/src/shaders/water.js.map +1 -0
  374. package/dist/src/shadow/esm.js +237 -0
  375. package/dist/src/shadow/esm.js.map +1 -0
  376. package/dist/src/shadow/pcf_opt.js +181 -0
  377. package/dist/src/shadow/pcf_opt.js.map +1 -0
  378. package/dist/src/shadow/pcf_pd.js +189 -0
  379. package/dist/src/shadow/pcf_pd.js.map +1 -0
  380. package/dist/src/shadow/shader.js +37 -0
  381. package/dist/src/shadow/shader.js.map +1 -0
  382. package/dist/src/shadow/shadow_impl.js +15 -0
  383. package/dist/src/shadow/shadow_impl.js.map +1 -0
  384. package/dist/src/shadow/shadowmapper.js +790 -0
  385. package/dist/src/shadow/shadowmapper.js.map +1 -0
  386. package/dist/src/shadow/ssm.js +159 -0
  387. package/dist/src/shadow/ssm.js.map +1 -0
  388. package/dist/src/shadow/vsm.js +297 -0
  389. package/dist/src/shadow/vsm.js.map +1 -0
  390. package/dist/src/shapes/box.js +386 -0
  391. package/dist/src/shapes/box.js.map +1 -0
  392. package/dist/src/shapes/cylinder.js +125 -0
  393. package/dist/src/shapes/cylinder.js.map +1 -0
  394. package/dist/src/shapes/plane.js +88 -0
  395. package/dist/src/shapes/plane.js.map +1 -0
  396. package/dist/src/shapes/shape.js +87 -0
  397. package/dist/src/shapes/shape.js.map +1 -0
  398. package/dist/src/shapes/sphere.js +114 -0
  399. package/dist/src/shapes/sphere.js.map +1 -0
  400. package/dist/src/shapes/tetrahedron.js +188 -0
  401. package/dist/src/shapes/tetrahedron.js.map +1 -0
  402. package/dist/src/shapes/torus.js +111 -0
  403. package/dist/src/shapes/torus.js.map +1 -0
  404. package/dist/src/utility/aabbtree.js +400 -0
  405. package/dist/src/utility/aabbtree.js.map +1 -0
  406. package/dist/src/utility/bounding_volume.js +29 -0
  407. package/dist/src/utility/bounding_volume.js.map +1 -0
  408. package/dist/src/utility/debug.js +28 -0
  409. package/dist/src/utility/debug.js.map +1 -0
  410. package/dist/src/utility/draco/decoder.js +116 -0
  411. package/dist/src/utility/draco/decoder.js.map +1 -0
  412. package/dist/src/utility/misc.js +105 -0
  413. package/dist/src/utility/misc.js.map +1 -0
  414. package/dist/src/utility/panorama.js +163 -0
  415. package/dist/src/utility/panorama.js.map +1 -0
  416. package/dist/src/utility/pmrem.js +354 -0
  417. package/dist/src/utility/pmrem.js.map +1 -0
  418. package/dist/src/utility/rendermipmap.js +115 -0
  419. package/dist/src/utility/rendermipmap.js.map +1 -0
  420. package/dist/src/utility/serialization/json.js +402 -0
  421. package/dist/src/utility/serialization/json.js.map +1 -0
  422. package/dist/src/utility/serialization/manager.js +623 -0
  423. package/dist/src/utility/serialization/manager.js.map +1 -0
  424. package/dist/src/utility/serialization/scene/animation.js +248 -0
  425. package/dist/src/utility/serialization/scene/animation.js.map +1 -0
  426. package/dist/src/utility/serialization/scene/batch.js +59 -0
  427. package/dist/src/utility/serialization/scene/batch.js.map +1 -0
  428. package/dist/src/utility/serialization/scene/camera.js +790 -0
  429. package/dist/src/utility/serialization/scene/camera.js.map +1 -0
  430. package/dist/src/utility/serialization/scene/common.js +222 -0
  431. package/dist/src/utility/serialization/scene/common.js.map +1 -0
  432. package/dist/src/utility/serialization/scene/light.js +575 -0
  433. package/dist/src/utility/serialization/scene/light.js.map +1 -0
  434. package/dist/src/utility/serialization/scene/material.js +1111 -0
  435. package/dist/src/utility/serialization/scene/material.js.map +1 -0
  436. package/dist/src/utility/serialization/scene/mesh.js +148 -0
  437. package/dist/src/utility/serialization/scene/mesh.js.map +1 -0
  438. package/dist/src/utility/serialization/scene/misc.js +39 -0
  439. package/dist/src/utility/serialization/scene/misc.js.map +1 -0
  440. package/dist/src/utility/serialization/scene/node.js +451 -0
  441. package/dist/src/utility/serialization/scene/node.js.map +1 -0
  442. package/dist/src/utility/serialization/scene/particle.js +425 -0
  443. package/dist/src/utility/serialization/scene/particle.js.map +1 -0
  444. package/dist/src/utility/serialization/scene/primitive.js +692 -0
  445. package/dist/src/utility/serialization/scene/primitive.js.map +1 -0
  446. package/dist/src/utility/serialization/scene/scene.js +704 -0
  447. package/dist/src/utility/serialization/scene/scene.js.map +1 -0
  448. package/dist/src/utility/serialization/scene/terrain.js +488 -0
  449. package/dist/src/utility/serialization/scene/terrain.js.map +1 -0
  450. package/dist/src/utility/serialization/scene/water.js +465 -0
  451. package/dist/src/utility/serialization/scene/water.js.map +1 -0
  452. package/dist/src/utility/shprojector.js +297 -0
  453. package/dist/src/utility/shprojector.js.map +1 -0
  454. package/dist/src/utility/textures/ggxlut.js +213 -0
  455. package/dist/src/utility/textures/ggxlut.js.map +1 -0
  456. package/dist/src/utility/textures/gradientnoise.js +62 -0
  457. package/dist/src/utility/textures/gradientnoise.js.map +1 -0
  458. package/dist/src/utility/textures/randomnoise.js +41 -0
  459. package/dist/src/utility/textures/randomnoise.js.map +1 -0
  460. package/dist/src/values.js +162 -0
  461. package/dist/src/values.js.map +1 -0
  462. package/dist/utility/blueprint/material/common.js +7 -0
  463. package/dist/utility/blueprint/material/common.js.map +1 -0
  464. package/dist/utility/blueprint/material/inputs.js +3 -3
  465. package/dist/utility/blueprint/material/inputs.js.map +1 -1
  466. package/dist/utility/blueprint/material/ir.js +4 -4
  467. package/dist/utility/blueprint/material/ir.js.map +1 -1
  468. package/dist/utility/serialization/blueprint/constants.js +255 -0
  469. package/dist/utility/serialization/blueprint/constants.js.map +1 -0
  470. package/dist/utility/serialization/blueprint/material/constants.js +203 -0
  471. package/dist/utility/serialization/blueprint/material/constants.js.map +1 -0
  472. package/dist/utility/serialization/blueprint/material/texture.js +165 -0
  473. package/dist/utility/serialization/blueprint/material/texture.js.map +1 -0
  474. package/dist/utility/serialization/manager.js +299 -268
  475. package/dist/utility/serialization/manager.js.map +1 -1
  476. package/dist/utility/serialization/scene/animation.js +45 -0
  477. package/dist/utility/serialization/scene/animation.js.map +1 -1
  478. package/dist/utility/serialization/scene/camera.js +53 -0
  479. package/dist/utility/serialization/scene/camera.js.map +1 -1
  480. package/dist/utility/serialization/scene/common.js +10 -0
  481. package/dist/utility/serialization/scene/common.js.map +1 -1
  482. package/dist/utility/serialization/scene/light.js +30 -0
  483. package/dist/utility/serialization/scene/light.js.map +1 -1
  484. package/dist/utility/serialization/scene/material.js +63 -0
  485. package/dist/utility/serialization/scene/material.js.map +1 -1
  486. package/dist/utility/serialization/scene/mesh.js +11 -0
  487. package/dist/utility/serialization/scene/mesh.js.map +1 -1
  488. package/dist/utility/serialization/scene/misc.js +2 -0
  489. package/dist/utility/serialization/scene/misc.js.map +1 -1
  490. package/dist/utility/serialization/scene/node.js +19 -0
  491. package/dist/utility/serialization/scene/node.js.map +1 -1
  492. package/dist/utility/serialization/scene/particle.js +22 -0
  493. package/dist/utility/serialization/scene/particle.js.map +1 -1
  494. package/dist/utility/serialization/scene/primitive.js +33 -0
  495. package/dist/utility/serialization/scene/primitive.js.map +1 -1
  496. package/dist/utility/serialization/scene/scene.js +39 -0
  497. package/dist/utility/serialization/scene/scene.js.map +1 -1
  498. package/dist/utility/serialization/scene/script.js +2 -0
  499. package/dist/utility/serialization/scene/script.js.map +1 -1
  500. package/dist/utility/serialization/scene/sprite.js +6 -0
  501. package/dist/utility/serialization/scene/sprite.js.map +1 -1
  502. package/dist/utility/serialization/scene/terrain.js +9 -0
  503. package/dist/utility/serialization/scene/terrain.js.map +1 -1
  504. package/dist/utility/serialization/scene/water.js +21 -0
  505. package/dist/utility/serialization/scene/water.js.map +1 -1
  506. package/dist/utility/serialization/types.js +5 -1
  507. package/dist/utility/serialization/types.js.map +1 -1
  508. package/dist/values.js +49 -53
  509. package/dist/values.js.map +1 -1
  510. package/package.json +2 -2
  511. package/dist/animation/joint_dynamics/vrm_spring_bone.js +0 -46
  512. package/dist/animation/joint_dynamics/vrm_spring_bone.js.map +0 -1
  513. package/dist/animation/spring/spring_bone.js +0 -240
  514. package/dist/animation/spring/spring_bone.js.map +0 -1
  515. package/dist/animation/spring/spring_bone_collider.js +0 -198
  516. package/dist/animation/spring/spring_bone_collider.js.map +0 -1
  517. package/dist/animation/spring/spring_bone_constraint.js +0 -170
  518. package/dist/animation/spring/spring_bone_constraint.js.map +0 -1
  519. package/dist/animation/spring/spring_bone_system.js +0 -328
  520. package/dist/animation/spring/spring_bone_system.js.map +0 -1
  521. package/dist/render/sky_motion_vector.js +0 -85
  522. package/dist/render/sky_motion_vector.js.map +0 -1
@@ -0,0 +1,1209 @@
1
+ import { Vector3, Vector4, Vector2 } from '@zephyr3d/base';
2
+ import { RENDER_PASS_TYPE_DEPTH, RENDER_PASS_TYPE_SHADOWMAP, RENDER_PASS_TYPE_OBJECT_COLOR, RENDER_PASS_TYPE_LIGHT, MaterialVaryingFlags, MAX_CLUSTERED_LIGHTS, MORPH_WEIGHTS_VECTOR_COUNT, MORPH_TARGET_POSITION, MORPH_TARGET_NORMAL, MORPH_TARGET_TANGENT, MORPH_ATTRIBUTE_VECTOR_COUNT } from '../../values.js';
3
+ import { ProgramBuilder } from '@zephyr3d/device';
4
+ import { decodeNormalizedFloatFromRGBA, linearToGamma } from '../../shaders/misc.js';
5
+ import { Application } from '../../app/app.js';
6
+ import { fetchSampler } from '../../utility/misc.js';
7
+ import { getDefaultAtmosphereParams, getAtmosphereParamsStruct } from '../../shaders/atmosphere.js';
8
+ import { getDefaultHeightFogParams, getHeightFogParamsStruct, calculateFog } from '../../shaders/fog.js';
9
+
10
+ const UNIFORM_NAME_LIGHT_BUFFER = 'Z_UniformLightBuffer';
11
+ const UNIFORM_NAME_LIGHT_INDEX_TEXTURE = 'Z_UniformLightIndexTex';
12
+ const UNIFORM_NAME_BAKED_SKY_MAP = 'Z_UniformBakedSky';
13
+ const UNIFORM_NAME_AERIALPERSPECTIVE_LUT = 'Z_UniformAerialPerspectiveLUT';
14
+ const UNIFORM_NAME_SKYDISTANTLIGHT_LUT = 'Z_UniformSkyDistantLightLUT';
15
+ const UNIFORM_NAME_SHADOW_MAP = 'Z_UniformShadowMap';
16
+ const UNIFORM_NAME_LINEAR_DEPTH_MAP = 'Z_UniformLinearDepth';
17
+ const UNIFORM_NAME_LINEAR_DEPTH_MAP_SIZE = 'Z_UniformLinearDepthSize';
18
+ const UNIFORM_NAME_SCENE_COLOR_MAP = 'Z_UniformSceneColor';
19
+ const UNIFORM_NAME_SCENE_COLOR_MAP_SIZE = 'Z_UniformSceneColorSize';
20
+ const UNIFORM_NAME_HIZ_DEPTH_MAP = 'Z_UniformHiZDepth';
21
+ const UNIFORM_NAME_HIZ_DEPTH_MAP_INFO = 'Z_UniformHiZDepthInfo';
22
+ const UNIFORM_NAME_WORLD_MATRIX = 'Z_UniformWorldMatrix';
23
+ const UNIFORM_NAME_PREV_WORLD_MATRIX = 'Z_UniformPrevWorldMatrix';
24
+ const UNIFORM_NAME_PREV_WORLD_MATRXI_FRAME = 'Z_UniformPrevWorldMatrixFrame';
25
+ const UNIFORM_NAME_INSTANCE_DATA_STRIDE = 'Z_UniformInstanceDataStride';
26
+ const UNIFORM_NAME_INSTANCE_DATA = 'Z_UniformInstanceData';
27
+ const UNIFORM_NAME_INSTANCE_DATA_OFFSET = 'Z_UniformInstanceDataOffset';
28
+ const UNIFORM_NAME_BONE_MATRICES = 'Z_UniformBoneMatrices';
29
+ const UNIFORM_NAME_BONE_TEXTURE_SIZE = 'Z_UniformBoneTexSize';
30
+ const UNIFORM_NAME_BONE_INV_BIND_MATRIX = 'Z_UniformBoneInvBindMatrix';
31
+ const UNIFORM_NAME_MORPH_DATA = 'Z_UniformMorphData';
32
+ const UNIFORM_NAME_MORPH_INFO = 'Z_UniformMorphInfo';
33
+ /**
34
+ * Helper shader functions for the builtin material system
35
+ * @public
36
+ */ class ShaderHelper {
37
+ static BILLBOARD_SPHERICAL = 1;
38
+ static BILLBOARD_SYLINDRAL = 2;
39
+ static MATERIAL_INSTANCE_DATA_OFFSET = 9;
40
+ /** @internal */ static defaultSunDir = Vector3.one().inplaceNormalize();
41
+ /** @internal */ static SKIN_MATRIX_NAME = 'Z_SkinMatrix';
42
+ static SKIN_PREV_MATRIX_NAME = 'Z_PrevSkinMatrix';
43
+ static SKIN_BONE_OFFSET = 'Z_boneOffset';
44
+ /** @internal */ static _drawableBindGroupLayouts = {};
45
+ /** @internal */ static _lightUniformShadow = {
46
+ sunDir: new Vector3(),
47
+ envLightStrength: 1,
48
+ shadowCascades: 1,
49
+ positionAndRange: new Vector4(),
50
+ directionAndCutoff: new Vector4(),
51
+ diffuseAndIntensity: new Vector4(),
52
+ cascadeDistances: new Vector4(),
53
+ depthBiasValues: new Vector4(),
54
+ shadowCameraParams: new Vector4(),
55
+ depthBiasScales: new Vector4(),
56
+ shadowMatrices: new Float32Array(16 * 4)
57
+ };
58
+ /** @internal */ static _fogUniforms = {
59
+ withAerialPerspective: 0,
60
+ fogType: 0,
61
+ additive: 0,
62
+ atmosphereParams: getDefaultAtmosphereParams(),
63
+ heightFogParams: getDefaultHeightFogParams()
64
+ };
65
+ static getWorldMatrixUniformName() {
66
+ return UNIFORM_NAME_WORLD_MATRIX;
67
+ }
68
+ static getPrevWorldMatrixUniformName() {
69
+ return UNIFORM_NAME_PREV_WORLD_MATRIX;
70
+ }
71
+ static getPrevWorldMatrixFrameUniformName() {
72
+ return UNIFORM_NAME_PREV_WORLD_MATRXI_FRAME;
73
+ }
74
+ static getInstanceDataUniformName() {
75
+ return UNIFORM_NAME_INSTANCE_DATA;
76
+ }
77
+ static getInstanceDataOffsetUniformName() {
78
+ return UNIFORM_NAME_INSTANCE_DATA_OFFSET;
79
+ }
80
+ static getInstanceDataStrideUniformName() {
81
+ return UNIFORM_NAME_INSTANCE_DATA_STRIDE;
82
+ }
83
+ static getBoneMatricesUniformName() {
84
+ return UNIFORM_NAME_BONE_MATRICES;
85
+ }
86
+ static getBoneTextureSizeUniformName() {
87
+ return UNIFORM_NAME_BONE_TEXTURE_SIZE;
88
+ }
89
+ static getBoneInvBindMatrixUniformName() {
90
+ return UNIFORM_NAME_BONE_INV_BIND_MATRIX;
91
+ }
92
+ static getMorphDataUniformName() {
93
+ return UNIFORM_NAME_MORPH_DATA;
94
+ }
95
+ static getMorphInfoUniformName() {
96
+ return UNIFORM_NAME_MORPH_INFO;
97
+ }
98
+ static getLightBufferUniformName() {
99
+ return UNIFORM_NAME_LIGHT_BUFFER;
100
+ }
101
+ static getDrawableBindGroupLayout(skinning, morphing, instancing) {
102
+ const hash = `${skinning ? 1 : 0}${morphing ? 1 : 0}${instancing ? 1 : 0}`;
103
+ let bindGroupLayout = this._drawableBindGroupLayouts[hash];
104
+ if (!bindGroupLayout) {
105
+ const device = Application.instance.device;
106
+ const buildInfo = new ProgramBuilder(device).buildRender({
107
+ vertex (pb) {
108
+ ShaderHelper.vertexShaderDrawableStuff(this, skinning, morphing, instancing);
109
+ pb.main(function() {});
110
+ },
111
+ fragment (pb) {
112
+ pb.main(function() {});
113
+ }
114
+ });
115
+ bindGroupLayout = buildInfo[2][1];
116
+ this._drawableBindGroupLayouts[hash] = bindGroupLayout;
117
+ }
118
+ return bindGroupLayout;
119
+ }
120
+ /**
121
+ * Prepares the fragment shader which is going to be used in our material system
122
+ *
123
+ * @remarks
124
+ * This function will setup all nessesary uniforms acoording to the drawing context
125
+ *
126
+ * @param pb - The program builder
127
+ * @param ctx - The drawing context
128
+ */ static prepareFragmentShader(pb, ctx) {
129
+ this.setupGlobalUniforms(pb, ctx);
130
+ }
131
+ /**
132
+ * Prepares the vertex shader which is going to be used in our material system
133
+ *
134
+ * @remarks
135
+ * This function will setup all nessesary uniforms according to the drawing context
136
+ *
137
+ * @param pb - The program builder
138
+ * @param ctx - The drawing context
139
+ */ static prepareVertexShader(pb, ctx) {
140
+ this.setupGlobalUniforms(pb, ctx);
141
+ this.prepareVertexShaderCommon(pb, ctx);
142
+ }
143
+ /** @internal */ static setupGlobalUniforms(pb, ctx) {
144
+ const scope = pb.getGlobalScope();
145
+ const cameraStruct = pb.defineStruct([
146
+ pb.vec4('position'),
147
+ pb.vec4('clipPlane'),
148
+ pb.mat4('viewProjectionMatrix'),
149
+ pb.mat4('invViewProjectionMatrix'),
150
+ pb.mat4('unjitteredVPMatrix'),
151
+ pb.mat4('jitteredInvVPMatrix'),
152
+ pb.mat4('viewMatrix'),
153
+ pb.mat4('projectionMatrix'),
154
+ pb.mat4('invProjectionMatrix'),
155
+ pb.vec4('params'),
156
+ ...ctx.motionVectors && ctx.renderPass.type === RENDER_PASS_TYPE_DEPTH ? [
157
+ pb.mat4('prevUnjitteredVPMatrix')
158
+ ] : [],
159
+ pb.vec2('renderSize'),
160
+ pb.vec2('jitterValue'),
161
+ pb.float('roughnessFactor'),
162
+ pb.float('frameDeltaTime'),
163
+ pb.float('elapsedTime'),
164
+ pb.int('framestamp')
165
+ ]);
166
+ if (ctx.renderPass.type === RENDER_PASS_TYPE_SHADOWMAP) {
167
+ const lightStruct = pb.defineStruct([
168
+ pb.vec4('positionAndRange'),
169
+ pb.vec4('directionCutoff'),
170
+ pb.mat4('viewMatrix'),
171
+ pb.vec4('depthBias'),
172
+ pb.int('lightType')
173
+ ]);
174
+ scope.camera = cameraStruct().uniform(0);
175
+ scope.light = lightStruct().uniform(0);
176
+ } else if (ctx.renderPass.type === RENDER_PASS_TYPE_DEPTH || ctx.renderPass.type === RENDER_PASS_TYPE_OBJECT_COLOR) {
177
+ scope.camera = cameraStruct().uniform(0);
178
+ } else if (ctx.renderPass.type === RENDER_PASS_TYPE_LIGHT) {
179
+ const useClusteredLighting = !ctx.currentShadowLight;
180
+ if (ctx.materialFlags & MaterialVaryingFlags.APPLY_FOG) {
181
+ const fogStructMembers = [
182
+ pb.int('withAerialPerspective'),
183
+ pb.int('fogType'),
184
+ pb.int('additive'),
185
+ getAtmosphereParamsStruct(pb)('atmosphereParams'),
186
+ getHeightFogParamsStruct(pb)('heightFogParams')
187
+ ];
188
+ const fogStruct = pb.defineStruct(fogStructMembers);
189
+ scope.fog = fogStruct().uniform(0);
190
+ scope[UNIFORM_NAME_AERIALPERSPECTIVE_LUT] = pb.tex2D().uniform(0);
191
+ scope[UNIFORM_NAME_SKYDISTANTLIGHT_LUT] = pb.tex2D().uniform(0);
192
+ }
193
+ const lightStruct = ctx.currentShadowLight ? pb.defineStruct([
194
+ pb.vec3('sunDir'),
195
+ pb.int('shadowCascades'),
196
+ pb.vec4('positionAndRange'),
197
+ pb.vec4('directionAndCutoff'),
198
+ pb.vec4('diffuseAndIntensity'),
199
+ pb.vec4('cascadeDistances'),
200
+ pb.vec4('depthBiasValues'),
201
+ pb.vec4('shadowCameraParams'),
202
+ pb.vec4('depthBiasScales'),
203
+ pb.vec4[16]('shadowMatrices'),
204
+ pb.float('envLightStrength')
205
+ ]) : pb.defineStruct([
206
+ pb.vec3('sunDir'),
207
+ pb.float('envLightStrength'),
208
+ pb.vec4('clusterParams'),
209
+ pb.ivec4('countParams'),
210
+ pb.ivec2('lightIndexTexSize')
211
+ ]);
212
+ scope.camera = cameraStruct().uniform(0);
213
+ scope.light = lightStruct().uniform(0);
214
+ if (useClusteredLighting) {
215
+ scope[UNIFORM_NAME_LIGHT_BUFFER] = pb.vec4[(MAX_CLUSTERED_LIGHTS + 1) * 3]().uniformBuffer(0);
216
+ scope[UNIFORM_NAME_LIGHT_INDEX_TEXTURE] = (pb.getDevice().type === 'webgl' ? pb.tex2D() : pb.utex2D()).uniform(0);
217
+ }
218
+ scope[UNIFORM_NAME_BAKED_SKY_MAP] = pb.texCube().uniform(0);
219
+ if (ctx.currentShadowLight) {
220
+ const scope = pb.getGlobalScope();
221
+ const shadowMapParams = ctx.shadowMapInfo.get(ctx.currentShadowLight);
222
+ const tex = shadowMapParams.shadowMap.isTextureCube() ? shadowMapParams.shadowMap.isDepth() ? scope.$builder.texCubeShadow() : scope.$builder.texCube() : shadowMapParams.shadowMap.isTexture2D() ? shadowMapParams.shadowMap.isDepth() ? scope.$builder.tex2DShadow() : scope.$builder.tex2D() : shadowMapParams.shadowMap.isDepth() ? scope.$builder.tex2DArrayShadow() : scope.$builder.tex2DArray();
223
+ if (!shadowMapParams.shadowMap.isDepth() && !ctx.device.getDeviceCaps().textureCaps.getTextureFormatInfo(shadowMapParams.shadowMap.format).filterable) {
224
+ tex.sampleType('unfilterable-float');
225
+ }
226
+ scope[UNIFORM_NAME_SHADOW_MAP] = tex.uniform(0);
227
+ }
228
+ if (ctx.drawEnvLight) {
229
+ ctx.env.light.envLight.initShaderBindings(pb);
230
+ }
231
+ if (ctx.linearDepthTexture) {
232
+ scope[UNIFORM_NAME_LINEAR_DEPTH_MAP] = pb.tex2D().uniform(0);
233
+ scope[UNIFORM_NAME_LINEAR_DEPTH_MAP_SIZE] = pb.vec2().uniform(0);
234
+ }
235
+ if (ctx.sceneColorTexture) {
236
+ scope[UNIFORM_NAME_SCENE_COLOR_MAP] = pb.tex2D().uniform(0);
237
+ scope[UNIFORM_NAME_SCENE_COLOR_MAP_SIZE] = pb.vec2().uniform(0);
238
+ }
239
+ if (ctx.HiZTexture) {
240
+ scope[UNIFORM_NAME_HIZ_DEPTH_MAP] = pb.tex2D().uniform(0);
241
+ scope[UNIFORM_NAME_HIZ_DEPTH_MAP_INFO] = pb.vec4().uniform(0);
242
+ }
243
+ }
244
+ }
245
+ /**
246
+ * This function checks if the shader needs to process skeletal animation.
247
+ *
248
+ * @param scope - Current shader scope
249
+ *
250
+ * @returns true if the shader needs to process skeletal animation, otherwise false.
251
+ */ static hasSkinning(scope) {
252
+ return !!scope[UNIFORM_NAME_BONE_MATRICES];
253
+ }
254
+ /**
255
+ * This function checks if the shader needs to process morph target animation.
256
+ *
257
+ * @param scope - Current shader scope
258
+ *
259
+ * @returns true if the shader needs to process morph target animation, otherwise false.
260
+ */ static hasMorphing(scope) {
261
+ return !!scope[UNIFORM_NAME_MORPH_DATA];
262
+ }
263
+ /**
264
+ * Calculate skinning matrix for current vertex
265
+ *
266
+ * @param scope - Current shader scope
267
+ *
268
+ * @returns Skinning matrix for current vertex, or null if there is not skeletal animation
269
+ */ static calculateSkinMatrix(scope) {
270
+ if (!this.hasSkinning(scope)) {
271
+ return null;
272
+ }
273
+ const pb = scope.$builder;
274
+ const funcNameGetBoneMatrixFromTexture = 'Z_getBoneMatrixFromTexture';
275
+ pb.func(funcNameGetBoneMatrixFromTexture, [
276
+ pb.int('boneIndex')
277
+ ], function() {
278
+ const boneTexture = this[UNIFORM_NAME_BONE_MATRICES];
279
+ this.$l.uvOffsets = pb.textureSampleLevel(boneTexture, pb.div(pb.vec2(0.5), this[UNIFORM_NAME_BONE_TEXTURE_SIZE]), 0);
280
+ this.$l.currentOffset = pb.int(this.uvOffsets.x);
281
+ this.$l.w = this[UNIFORM_NAME_BONE_TEXTURE_SIZE].x;
282
+ this.$l.pixelIndex = pb.float(pb.mul(pb.add(this.boneIndex, this.currentOffset), 4));
283
+ this.$l.xIndex = pb.mod(this.pixelIndex, this.w);
284
+ this.$l.yIndex = pb.floor(pb.div(this.pixelIndex, this.w));
285
+ this.$l.u1 = pb.div(pb.add(this.xIndex, 0.5), this.w);
286
+ this.$l.u2 = pb.div(pb.add(this.xIndex, 1.5), this.w);
287
+ this.$l.u3 = pb.div(pb.add(this.xIndex, 2.5), this.w);
288
+ this.$l.u4 = pb.div(pb.add(this.xIndex, 3.5), this.w);
289
+ this.$l.v = pb.div(pb.add(this.yIndex, 0.5), this.w);
290
+ this.$l.row1 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u1, this.v), 0);
291
+ this.$l.row2 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u2, this.v), 0);
292
+ this.$l.row3 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u3, this.v), 0);
293
+ this.$l.row4 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u4, this.v), 0);
294
+ this.$return(pb.mat4(this.row1, this.row2, this.row3, this.row4));
295
+ });
296
+ const funcNameGetSkinningMatrix = 'Z_getSkinningMatrix';
297
+ pb.func(funcNameGetSkinningMatrix, [], function() {
298
+ const invBindMatrix = this[UNIFORM_NAME_BONE_INV_BIND_MATRIX];
299
+ const blendIndices = scope.$getVertexAttrib('blendIndices');
300
+ const blendWeights = scope.$getVertexAttrib('blendWeights');
301
+ this.$l.m0 = scope.$g[funcNameGetBoneMatrixFromTexture](pb.int(blendIndices[0]));
302
+ this.$l.m1 = scope.$g[funcNameGetBoneMatrixFromTexture](pb.int(blendIndices[1]));
303
+ this.$l.m2 = scope.$g[funcNameGetBoneMatrixFromTexture](pb.int(blendIndices[2]));
304
+ this.$l.m3 = scope.$g[funcNameGetBoneMatrixFromTexture](pb.int(blendIndices[3]));
305
+ this.$l.m = pb.add(pb.mul(this.m0, blendWeights.x), pb.mul(this.m1, blendWeights.y), pb.mul(this.m2, blendWeights.z), pb.mul(this.m3, blendWeights.w));
306
+ this.$return(pb.mul(invBindMatrix, this.m));
307
+ });
308
+ return scope.$g[funcNameGetSkinningMatrix]();
309
+ }
310
+ static calculateMorphDelta(scope, attrib) {
311
+ const pb = scope.$builder;
312
+ const isWebGL1 = pb.getDevice().type === 'webgl';
313
+ if (pb.shaderKind !== 'vertex') {
314
+ throw new Error(`ShaderHelper.calculateMorphDelta(): must be called at vertex stage`);
315
+ }
316
+ const funcName = 'Z_calculateMorph';
317
+ const that = this;
318
+ pb.func(funcName, [
319
+ pb.int('offset')
320
+ ], function() {
321
+ this.$if(pb.lessThan(this.offset, 0), function() {
322
+ this.$return(pb.vec4(0));
323
+ });
324
+ this.$l.vertexIndex = isWebGL1 ? pb.int(scope.$inputs.zFakeVertexID) : pb.int(scope.$builtins.vertexIndex);
325
+ const morphInfo = scope[that.getMorphInfoUniformName()];
326
+ this.$l.metaData = pb.ivec4(morphInfo[0]);
327
+ this.$l.texWidth = pb.float(this.metaData.x);
328
+ this.$l.texHeight = pb.float(this.metaData.y);
329
+ this.$l.numVertices = this.metaData.z;
330
+ this.$l.numTargets = this.metaData.w;
331
+ this.$l.value = pb.vec4(0);
332
+ if (isWebGL1) {
333
+ this.$for(pb.int('i'), 0, MORPH_WEIGHTS_VECTOR_COUNT, function() {
334
+ this.$for(pb.int('j'), 0, 4, function() {
335
+ this.$l.index = pb.add(pb.mul(this.i, 4), this.j);
336
+ this.$if(pb.greaterThanEqual(this.index, this.numTargets), function() {
337
+ this.$return(this.value);
338
+ });
339
+ this.$l.weight = morphInfo.at(pb.add(1, this.i)).at(this.j);
340
+ this.$l.pixelIndex = pb.float(pb.add(this.offset, pb.mul(this.index, this.numVertices), this.vertexIndex));
341
+ this.$l.xIndex = pb.mod(this.pixelIndex, this.texWidth);
342
+ this.$l.yIndex = pb.floor(pb.div(this.pixelIndex, this.texWidth));
343
+ this.$l.u = pb.div(pb.add(this.xIndex, 0.5), this.texWidth);
344
+ this.$l.v = pb.div(pb.add(this.yIndex, 0.5), this.texHeight);
345
+ this.$l.morphValue = pb.textureSampleLevel(this[that.getMorphDataUniformName()], pb.vec2(this.u, this.v), 0);
346
+ this.value = pb.add(this.value, pb.mul(this.morphValue, this.weight));
347
+ });
348
+ });
349
+ } else {
350
+ this.$for(pb.int('t'), 0, this.numTargets, function() {
351
+ this.$l.i = pb.sar(this.t, 2);
352
+ this.$l.j = pb.compAnd(this.t, 3);
353
+ this.$l.weight = morphInfo.at(pb.add(1, this.i)).at(this.j);
354
+ this.$l.pixelIndex = pb.float(pb.add(this.offset, pb.mul(this.t, this.numVertices), this.vertexIndex));
355
+ this.$l.xIndex = pb.mod(this.pixelIndex, this.texWidth);
356
+ this.$l.yIndex = pb.floor(pb.div(this.pixelIndex, this.texWidth));
357
+ this.$l.u = pb.div(pb.add(this.xIndex, 0.5), this.texWidth);
358
+ this.$l.v = pb.div(pb.add(this.yIndex, 0.5), this.texHeight);
359
+ this.$l.morphValue = pb.textureSampleLevel(this[that.getMorphDataUniformName()], pb.vec2(this.u, this.v), 0);
360
+ this.value = pb.add(this.value, pb.mul(this.morphValue, this.weight));
361
+ });
362
+ }
363
+ this.$return(this.value);
364
+ });
365
+ const pos = 1 + MORPH_WEIGHTS_VECTOR_COUNT + (attrib >> 2);
366
+ const comp = attrib & 3;
367
+ const offset = scope[this.getMorphInfoUniformName()][pos][comp];
368
+ return scope[funcName](pb.int(offset));
369
+ }
370
+ /** @internal */ static prepareSkinAnimation(scope) {
371
+ if (!this.hasSkinning(scope)) {
372
+ return;
373
+ }
374
+ const that = this;
375
+ const pb = scope.$builder;
376
+ const funcNameGetBoneMatrixFromTexture = 'Z_getBoneMatrixFromTexture';
377
+ pb.func(funcNameGetBoneMatrixFromTexture, [
378
+ pb.float('boneIndex'),
379
+ pb.float('boneOffset')
380
+ ], function() {
381
+ const boneTexture = this[UNIFORM_NAME_BONE_MATRICES];
382
+ this.$l.w = this[UNIFORM_NAME_BONE_TEXTURE_SIZE].x;
383
+ this.$l.pixelIndex = pb.mul(pb.add(this.boneIndex, this.boneOffset), 4);
384
+ this.$l.xIndex = pb.mod(this.pixelIndex, this.w);
385
+ this.$l.yIndex = pb.floor(pb.div(this.pixelIndex, this.w));
386
+ this.$l.u1 = pb.div(pb.add(this.xIndex, 0.5), this.w);
387
+ this.$l.u2 = pb.div(pb.add(this.xIndex, 1.5), this.w);
388
+ this.$l.u3 = pb.div(pb.add(this.xIndex, 2.5), this.w);
389
+ this.$l.u4 = pb.div(pb.add(this.xIndex, 3.5), this.w);
390
+ this.$l.v = pb.div(pb.add(this.yIndex, 0.5), this.w);
391
+ this.$l.row1 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u1, this.v), 0);
392
+ this.$l.row2 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u2, this.v), 0);
393
+ this.$l.row3 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u3, this.v), 0);
394
+ this.$l.row4 = pb.textureSampleLevel(boneTexture, pb.vec2(this.u4, this.v), 0);
395
+ this.$return(pb.mat4(this.row1, this.row2, this.row3, this.row4));
396
+ });
397
+ const funcNameGetSkinningMatrix = 'Z_getSkinningMatrix';
398
+ pb.func(funcNameGetSkinningMatrix, [
399
+ pb.float('boneOffset')
400
+ ], function() {
401
+ const invBindMatrix = this[UNIFORM_NAME_BONE_INV_BIND_MATRIX];
402
+ const blendIndices = scope.$getVertexAttrib('blendIndices');
403
+ const blendWeights = scope.$getVertexAttrib('blendWeights');
404
+ this.$l.m0 = scope.$g[funcNameGetBoneMatrixFromTexture](blendIndices[0], this.boneOffset);
405
+ this.$l.m1 = scope.$g[funcNameGetBoneMatrixFromTexture](blendIndices[1], this.boneOffset);
406
+ this.$l.m2 = scope.$g[funcNameGetBoneMatrixFromTexture](blendIndices[2], this.boneOffset);
407
+ this.$l.m3 = scope.$g[funcNameGetBoneMatrixFromTexture](blendIndices[3], this.boneOffset);
408
+ this.$l.m = pb.add(pb.mul(this.m0, blendWeights.x), pb.mul(this.m1, blendWeights.y), pb.mul(this.m2, blendWeights.z), pb.mul(this.m3, blendWeights.w));
409
+ this.$return(pb.mul(invBindMatrix, this.m));
410
+ });
411
+ const motionVector = !!this.getUnjitteredViewProjectionMatrix(scope);
412
+ const boneTexture = scope[UNIFORM_NAME_BONE_MATRICES];
413
+ scope.$l[that.SKIN_BONE_OFFSET] = pb.textureSampleLevel(boneTexture, pb.div(pb.vec2(0.5), scope[UNIFORM_NAME_BONE_TEXTURE_SIZE]), 0).xy;
414
+ scope.$l[that.SKIN_MATRIX_NAME] = scope[funcNameGetSkinningMatrix](scope[that.SKIN_BONE_OFFSET].x);
415
+ if (motionVector) {
416
+ scope.$l[that.SKIN_PREV_MATRIX_NAME] = scope[funcNameGetSkinningMatrix](scope[that.SKIN_BONE_OFFSET].y);
417
+ }
418
+ }
419
+ /**
420
+ * Calculates the vertex position of type vec3 in object space
421
+ *
422
+ * @param scope - Current shader scope
423
+ * @param skinMatrix - The skinning matrix if there is skeletal animation, otherwise null
424
+ * @returns The calculated vertex position in object space, or null if pos is null
425
+ */ static resolveVertexPosition(scope) {
426
+ const pb = scope.$builder;
427
+ if (pb.shaderKind !== 'vertex') {
428
+ throw new Error(`ShaderHelper.resolveVertexPosition(): must be called at vertex stage`);
429
+ }
430
+ if (!pb.getGlobalScope().$getVertexAttrib('position')) {
431
+ pb.getGlobalScope().$inputs.Z_pos = pb.vec3().attrib('position');
432
+ }
433
+ const that = this;
434
+ const params = scope[that.SKIN_MATRIX_NAME] && scope[that.SKIN_PREV_MATRIX_NAME] ? [
435
+ pb.mat4('skinMatrix'),
436
+ pb.mat4('prevSkinMatrix')
437
+ ] : scope[that.SKIN_MATRIX_NAME] ? [
438
+ pb.mat4('skinMatrix')
439
+ ] : [];
440
+ pb.func('Z_resolveVertexPosition', params, function() {
441
+ this.$l.opos = this.$getVertexAttrib('position').xyz;
442
+ if (that.hasMorphing(scope)) {
443
+ this.opos = pb.add(this.opos, that.calculateMorphDelta(this, MORPH_TARGET_POSITION).xyz);
444
+ }
445
+ if (this.skinMatrix) {
446
+ this.$l.pos = pb.mul(this.skinMatrix, pb.vec4(this.opos, 1)).xyz;
447
+ } else {
448
+ this.$l.pos = this.opos;
449
+ }
450
+ const prevUnjitteredVPMatrix = that.getPrevUnjitteredViewProjectionMatrix(this);
451
+ if (prevUnjitteredVPMatrix) {
452
+ this.$l.unjitteredVPMatrix = that.getUnjitteredViewProjectionMatrix(this);
453
+ this.$l.worldPos = pb.mul(that.getWorldMatrix(this), pb.vec4(this.pos, 1));
454
+ if (this.prevSkinMatrix) {
455
+ this.$l.prevWorldPos = pb.mul(that.getPrevWorldMatrix(this), pb.mul(this.prevSkinMatrix, pb.vec4(this.opos, 1)));
456
+ } else {
457
+ this.$l.prevWorldPos = pb.mul(that.getPrevWorldMatrix(this), pb.vec4(this.pos, 1));
458
+ }
459
+ this.$outputs.zMotionVectorPosCurrent = pb.mul(this.unjitteredVPMatrix, this.worldPos);
460
+ this.$outputs.zMotionVectorPosPrev = pb.mul(prevUnjitteredVPMatrix, this.prevWorldPos);
461
+ }
462
+ this.$return(this.pos);
463
+ });
464
+ return scope[that.SKIN_MATRIX_NAME] && scope[that.SKIN_PREV_MATRIX_NAME] ? scope.Z_resolveVertexPosition(scope[that.SKIN_MATRIX_NAME], scope[that.SKIN_PREV_MATRIX_NAME]) : scope[that.SKIN_MATRIX_NAME] ? scope.Z_resolveVertexPosition(scope[that.SKIN_MATRIX_NAME]) : scope.Z_resolveVertexPosition();
465
+ }
466
+ /**
467
+ * Resolve motion vector
468
+ *
469
+ * @param scope - Current shader scope
470
+ * @param worldPos - Current object position in world space
471
+ * @param prevWorldPos - Previous object position in world space
472
+ */ static resolveMotionVector(scope, worldPos, prevWorldPos) {
473
+ const that = this;
474
+ const pb = scope.$builder;
475
+ const prevUnjitteredVPMatrix = that.getPrevUnjitteredViewProjectionMatrix(scope);
476
+ if (prevUnjitteredVPMatrix) {
477
+ const unjitteredVPMatrix = that.getUnjitteredViewProjectionMatrix(scope);
478
+ scope.$outputs.zMotionVectorPosCurrent = pb.mul(unjitteredVPMatrix, pb.vec4(worldPos.xyz, 1));
479
+ scope.$outputs.zMotionVectorPosPrev = pb.mul(prevUnjitteredVPMatrix, pb.vec4(prevWorldPos.xyz, 1));
480
+ }
481
+ }
482
+ /**
483
+ * Calculates the normal vector of type vec3 in object space
484
+ *
485
+ * @param scope - Current shader scope
486
+ * @param normal - Vertex normal input, must be type of vec3, null if no vertex normal input
487
+ * @param skinMatrix - The skinning matrix if there is skeletal animation, otherwise null
488
+ * @returns The calculated normal vector in object space, or null if normal is null
489
+ */ static resolveVertexNormal(scope, normal) {
490
+ const pb = scope.$builder;
491
+ if (pb.shaderKind !== 'vertex') {
492
+ throw new Error(`ShaderHelper.resolveVertexNormal(): must be called in vertex stage`);
493
+ }
494
+ const funcScope = pb.getCurrentFunctionScope();
495
+ if (!funcScope || !funcScope.$isMain()) {
496
+ throw new Error(`ShaderHelper.resolveVertexNormal(): must be called at entry function`);
497
+ }
498
+ if (!normal) {
499
+ if (!scope.$getVertexAttrib('normal')) {
500
+ scope.$inputs.Z_normal = pb.vec3().attrib('normal');
501
+ }
502
+ normal = scope.$getVertexAttrib('normal');
503
+ }
504
+ if (this.hasMorphing(scope)) {
505
+ normal = pb.normalize(pb.add(normal, this.calculateMorphDelta(scope, MORPH_TARGET_NORMAL).xyz));
506
+ }
507
+ if (scope[this.SKIN_MATRIX_NAME]) {
508
+ return pb.mul(scope[this.SKIN_MATRIX_NAME], pb.vec4(normal, 0)).xyz;
509
+ } else {
510
+ return normal;
511
+ }
512
+ }
513
+ /**
514
+ * Calculates the tangent vector of type vec3 in object space
515
+ *
516
+ * @param scope - Current shader scope
517
+ * @param tangent - Vertex tangent input, must be type of vec4, null if no vertex tangent input
518
+ * @param skinMatrix - The skinning matrix if there is skeletal animation, otherwise null
519
+ * @returns The calculated tangent vector of type vec4 in object space, or null if tangent is null
520
+ */ static resolveVertexTangent(scope, tangent) {
521
+ const pb = scope.$builder;
522
+ if (pb.shaderKind !== 'vertex') {
523
+ throw new Error(`ShaderHelper.resolveVertexTangent(): must be called in vertex stage`);
524
+ }
525
+ const funcScope = pb.getCurrentFunctionScope();
526
+ if (!funcScope || !funcScope.$isMain()) {
527
+ throw new Error(`ShaderHelper.resolveVertexTangent(): must be called at entry function`);
528
+ }
529
+ if (!tangent) {
530
+ if (!scope.$getVertexAttrib('tangent')) {
531
+ scope.$inputs.Z_tangent = pb.vec4().attrib('tangent');
532
+ }
533
+ tangent = scope.$getVertexAttrib('tangent');
534
+ }
535
+ if (this.hasMorphing(scope)) {
536
+ tangent = pb.normalize(pb.add(tangent, pb.vec4(this.calculateMorphDelta(scope, MORPH_TARGET_TANGENT).xyz, 0)));
537
+ }
538
+ if (scope[this.SKIN_MATRIX_NAME]) {
539
+ return pb.vec4(pb.mul(scope[this.SKIN_MATRIX_NAME], pb.vec4(tangent.xyz, 0)).xyz, tangent.w);
540
+ } else {
541
+ return tangent;
542
+ }
543
+ }
544
+ /**
545
+ * Gets the uniform variable of type mat4 which holds the world matrix of current object to be drawn
546
+ * @param scope - Current shader scope
547
+ * @returns The world matrix of current object to be drawn
548
+ */ static getWorldMatrix(scope) {
549
+ const pb = scope.$builder;
550
+ return scope[UNIFORM_NAME_WORLD_MATRIX] ?? pb.mat4(this.getInstancedUniform(scope, 0), this.getInstancedUniform(scope, 1), this.getInstancedUniform(scope, 2), this.getInstancedUniform(scope, 3));
551
+ }
552
+ /**
553
+ * Gets the uniform variable of type mat4 which holds the world matrix at previous frame of current object to be drawn
554
+ * @param scope - Current shader scope
555
+ * @returns The world matrix at previous frame of current object to be drawn
556
+ */ static getPrevWorldMatrix(scope) {
557
+ const pb = scope.$builder;
558
+ const that = this;
559
+ const framestamp = this.getFramestamp(scope);
560
+ if (scope[UNIFORM_NAME_WORLD_MATRIX]) {
561
+ if (pb.getDevice().type === 'webgpu') {
562
+ pb.func('Z_getPrevWorldMatrix', [
563
+ pb.int('framestamp')
564
+ ], function() {
565
+ this.$if(pb.equal(this.framestamp, this[UNIFORM_NAME_PREV_WORLD_MATRXI_FRAME]), function() {
566
+ this.$return(this[UNIFORM_NAME_PREV_WORLD_MATRIX]);
567
+ }).$else(function() {
568
+ this.$return(this[UNIFORM_NAME_WORLD_MATRIX]);
569
+ });
570
+ });
571
+ return scope.Z_getPrevWorldMatrix(framestamp);
572
+ } else {
573
+ return scope.$choice(pb.equal(framestamp, scope[UNIFORM_NAME_PREV_WORLD_MATRXI_FRAME]), scope[UNIFORM_NAME_PREV_WORLD_MATRIX], scope[UNIFORM_NAME_WORLD_MATRIX]);
574
+ }
575
+ } else {
576
+ pb.func('Z_getPrevWorldMatrix', [
577
+ pb.int('framestamp')
578
+ ], function() {
579
+ this.$l.prevFrame = pb.floatBitsToInt(that.getInstancedUniform(this, 4).x);
580
+ this.$l.index = this.$choice(pb.equal(this.framestamp, this.prevFrame), pb.int(5), pb.int(0));
581
+ this.$return(pb.mat4(that.getInstancedUniform(scope, this.index), that.getInstancedUniform(scope, pb.add(this.index, 1)), that.getInstancedUniform(scope, pb.add(this.index, 2)), that.getInstancedUniform(scope, pb.add(this.index, 3))));
582
+ });
583
+ return scope.Z_getPrevWorldMatrix(framestamp);
584
+ }
585
+ }
586
+ /**
587
+ * Gets the instance uniform value of type vec4 by uniform index
588
+ * @param scope - Current shader scope
589
+ * @returns instance uniform value
590
+ */ static getInstancedUniform(scope, uniformIndex) {
591
+ const pb = scope.$builder;
592
+ return scope[UNIFORM_NAME_INSTANCE_DATA].at(pb.add(pb.mul(scope[UNIFORM_NAME_INSTANCE_DATA_STRIDE], pb.uint(scope.$builtins.instanceIndex)), scope[UNIFORM_NAME_INSTANCE_DATA_OFFSET], pb.uint(uniformIndex)));
593
+ }
594
+ /**
595
+ * Gets the uniform variable of type mat4 which holds the normal matrix of current object to be drawn
596
+ *
597
+ * @param scope - Current shader scope
598
+ * @returns The normal matrix of current object to be drawn
599
+ */ static getNormalMatrix(scope) {
600
+ return this.getWorldMatrix(scope);
601
+ }
602
+ /**
603
+ * Vertex shader drawable stuff
604
+ *
605
+ * @param scope - Current shader scope
606
+ * @param skinning - true if skinning is used, otherwise false.
607
+ * @param instanced - true if instancing is used, otherwise false.
608
+ */ static vertexShaderDrawableStuff(scope, skinning, morphing, instanced) {
609
+ const pb = scope.$builder;
610
+ if (instanced) {
611
+ scope[UNIFORM_NAME_INSTANCE_DATA_STRIDE] = pb.uint().uniform(1);
612
+ scope[UNIFORM_NAME_INSTANCE_DATA_OFFSET] = pb.uint().uniform(1);
613
+ scope[UNIFORM_NAME_INSTANCE_DATA] = pb.vec4[65536 >> 4]().uniformBuffer(3);
614
+ } else {
615
+ scope[UNIFORM_NAME_WORLD_MATRIX] = pb.mat4().uniform(1);
616
+ scope[UNIFORM_NAME_PREV_WORLD_MATRIX] = pb.mat4().uniform(1);
617
+ scope[UNIFORM_NAME_PREV_WORLD_MATRXI_FRAME] = pb.int().uniform(1);
618
+ }
619
+ if (skinning) {
620
+ scope[UNIFORM_NAME_BONE_MATRICES] = pb.tex2D().uniform(1).sampleType('unfilterable-float');
621
+ scope[UNIFORM_NAME_BONE_INV_BIND_MATRIX] = pb.mat4().uniform(1);
622
+ scope[UNIFORM_NAME_BONE_TEXTURE_SIZE] = pb.vec2().uniform(1);
623
+ }
624
+ if (morphing) {
625
+ scope[UNIFORM_NAME_MORPH_DATA] = pb.tex2D().uniform(1).sampleType('unfilterable-float');
626
+ scope[UNIFORM_NAME_MORPH_INFO] = pb.vec4[1 + MORPH_WEIGHTS_VECTOR_COUNT + MORPH_ATTRIBUTE_VECTOR_COUNT]().uniformBuffer(1);
627
+ }
628
+ }
629
+ /** @internal */ static prepareVertexShaderCommon(pb, ctx) {
630
+ this.vertexShaderDrawableStuff(pb.getGlobalScope(), !!(ctx.materialFlags & MaterialVaryingFlags.SKIN_ANIMATION), !!(ctx.materialFlags & MaterialVaryingFlags.MORPH_ANIMATION), !!(ctx.materialFlags & MaterialVaryingFlags.INSTANCING));
631
+ }
632
+ /** @internal */ static setCameraUniforms(bindGroup, ctx, linear) {
633
+ const pos = ctx.camera.getWorldPosition();
634
+ const useJitter = ctx.motionVectors && ctx.renderPass.type !== RENDER_PASS_TYPE_SHADOWMAP && ctx.renderPass.type !== RENDER_PASS_TYPE_OBJECT_COLOR;
635
+ const cameraStruct = {
636
+ position: new Vector4(pos.x, pos.y, pos.z, ctx.camera.clipPlane ? 1 : 0),
637
+ clipPlane: ctx.camera.clipPlane ?? Vector4.zero(),
638
+ renderSize: new Vector2(ctx.renderWidth, ctx.renderHeight),
639
+ viewProjectionMatrix: useJitter ? ctx.camera.jitteredVPMatrix : ctx.camera.viewProjectionMatrix,
640
+ unjitteredVPMatrix: ctx.camera.viewProjectionMatrix,
641
+ jitteredInvVPMatrix: useJitter ? ctx.camera.jitteredInvVPMatrix : ctx.camera.invViewProjectionMatrix,
642
+ jitterValue: ctx.camera.jitterValue,
643
+ invViewProjectionMatrix: ctx.camera.invViewProjectionMatrix,
644
+ projectionMatrix: ctx.camera.getProjectionMatrix(),
645
+ invProjectionMatrix: ctx.camera.getInvProjectionMatrix(),
646
+ viewMatrix: ctx.camera.viewMatrix,
647
+ params: new Vector4(ctx.camera.getNearPlane(), ctx.camera.getFarPlane(), ctx.flip ? -1 : 1, linear ? 0 : 1),
648
+ roughnessFactor: ctx.camera.SSR ? ctx.camera.ssrRoughnessFactor : 1,
649
+ frameDeltaTime: Application.instance.device.frameInfo.elapsedFrame * 0.001,
650
+ elapsedTime: Application.instance.device.frameInfo.elapsedOverall * 0.001,
651
+ framestamp: Application.instance.device.frameInfo.frameCounter
652
+ };
653
+ if (ctx.motionVectors && ctx.renderPass.type === RENDER_PASS_TYPE_DEPTH) {
654
+ cameraStruct.prevUnjitteredVPMatrix = ctx.camera.prevVPMatrix;
655
+ }
656
+ if (ctx.renderPass.type === RENDER_PASS_TYPE_LIGHT) {
657
+ if (ctx.linearDepthTexture) {
658
+ bindGroup.setTexture(UNIFORM_NAME_LINEAR_DEPTH_MAP, ctx.linearDepthTexture);
659
+ bindGroup.setValue(UNIFORM_NAME_LINEAR_DEPTH_MAP_SIZE, new Vector2(ctx.linearDepthTexture.width, ctx.linearDepthTexture.height));
660
+ }
661
+ if (ctx.sceneColorTexture) {
662
+ bindGroup.setTexture(UNIFORM_NAME_SCENE_COLOR_MAP, ctx.sceneColorTexture);
663
+ bindGroup.setValue(UNIFORM_NAME_SCENE_COLOR_MAP_SIZE, new Vector2(ctx.sceneColorTexture.width, ctx.sceneColorTexture.height));
664
+ }
665
+ if (ctx.HiZTexture) {
666
+ bindGroup.setTexture(UNIFORM_NAME_HIZ_DEPTH_MAP, ctx.HiZTexture, fetchSampler('clamp_nearest'));
667
+ bindGroup.setValue(UNIFORM_NAME_HIZ_DEPTH_MAP_INFO, new Vector4(ctx.HiZTexture.width, ctx.HiZTexture.height, ctx.HiZTexture.mipLevelCount, 0));
668
+ }
669
+ }
670
+ bindGroup.setValue('camera', cameraStruct);
671
+ }
672
+ /** @internal */ static setLightUniformsShadowMap(bindGroup, ctx, light) {
673
+ if (light) {
674
+ const shadowMapParams = ctx.shadowMapInfo.get(light);
675
+ bindGroup.setValue('light', {
676
+ positionAndRange: light.positionAndRange,
677
+ directionCutoff: light.directionAndCutoff,
678
+ viewMatrix: light.viewMatrix,
679
+ depthBias: shadowMapParams.depthBiasValues[0],
680
+ lightType: light.lightType
681
+ });
682
+ }
683
+ }
684
+ /** @internal */ static setFogUniforms(bindGroup, withAerialPerspective, fogType, additive, atmosphereParams, heightFogParams, aerialPerspectiveLUT, skyDistantLightLUT) {
685
+ this._fogUniforms.withAerialPerspective = withAerialPerspective;
686
+ this._fogUniforms.fogType = fogType;
687
+ this._fogUniforms.additive = additive;
688
+ this._fogUniforms.atmosphereParams = atmosphereParams;
689
+ this._fogUniforms.heightFogParams = heightFogParams;
690
+ bindGroup.setValue('fog', this._fogUniforms);
691
+ bindGroup.setTexture(UNIFORM_NAME_AERIALPERSPECTIVE_LUT, aerialPerspectiveLUT);
692
+ bindGroup.setTexture(UNIFORM_NAME_SKYDISTANTLIGHT_LUT, skyDistantLightLUT);
693
+ }
694
+ /** @internal */ static setLightUniforms(bindGroup, ctx, clusterParams, countParams, lightBuffer, lightIndexTexture) {
695
+ bindGroup.setValue('light', {
696
+ sunDir: ctx.sunLight ? ctx.sunLight.directionAndCutoff.xyz().scaleBy(-1) : this.defaultSunDir,
697
+ clusterParams: clusterParams,
698
+ countParams: countParams,
699
+ envLightStrength: ctx.env.light.strength ?? 0,
700
+ lightIndexTexSize: new Int32Array([
701
+ lightIndexTexture.width,
702
+ lightIndexTexture.height
703
+ ])
704
+ });
705
+ bindGroup.setBuffer(UNIFORM_NAME_LIGHT_BUFFER, lightBuffer);
706
+ bindGroup.setTexture(UNIFORM_NAME_LIGHT_INDEX_TEXTURE, lightIndexTexture);
707
+ bindGroup.setTexture(UNIFORM_NAME_BAKED_SKY_MAP, ctx.scene.env.sky.getBakedSkyTexture(ctx));
708
+ if (ctx.drawEnvLight) {
709
+ ctx.env.light.envLight.updateBindGroup(bindGroup);
710
+ }
711
+ }
712
+ /** @internal */ static setLightUniformsShadow(bindGroup, ctx, light) {
713
+ const shadowMapParams = ctx.shadowMapInfo.get(light);
714
+ this._lightUniformShadow.sunDir = ctx.sunLight ? ctx.sunLight.directionAndCutoff.xyz().scaleBy(-1) : this.defaultSunDir;
715
+ this._lightUniformShadow.envLightStrength = ctx.env?.light.strength ?? 0;
716
+ this._lightUniformShadow.shadowCascades = shadowMapParams.numShadowCascades;
717
+ this._lightUniformShadow.positionAndRange.set(light.positionAndRange);
718
+ this._lightUniformShadow.directionAndCutoff.set(light.directionAndCutoff);
719
+ this._lightUniformShadow.diffuseAndIntensity.set(light.diffuseAndIntensity);
720
+ this._lightUniformShadow.cascadeDistances.set(shadowMapParams.cascadeDistances);
721
+ this._lightUniformShadow.depthBiasValues.set(shadowMapParams.depthBiasValues[0]);
722
+ this._lightUniformShadow.shadowCameraParams.set(shadowMapParams.cameraParams);
723
+ this._lightUniformShadow.depthBiasScales.set(shadowMapParams.depthBiasScales);
724
+ this._lightUniformShadow.shadowMatrices.set(shadowMapParams.shadowMatrices);
725
+ bindGroup.setValue('light', this._lightUniformShadow);
726
+ bindGroup.setTexture(UNIFORM_NAME_SHADOW_MAP, shadowMapParams.shadowMap, shadowMapParams.shadowMapSampler);
727
+ bindGroup.setTexture(UNIFORM_NAME_BAKED_SKY_MAP, ctx.scene.env.sky.getBakedSkyTexture(ctx));
728
+ if (ctx.drawEnvLight) {
729
+ ctx.env.light.envLight.updateBindGroup(bindGroup);
730
+ }
731
+ }
732
+ /**
733
+ * Gets the uniform variable of type float which holds the strength of the environment light
734
+ *
735
+ * @remarks
736
+ * This function can only be used in the fragment shader
737
+ *
738
+ * @param scope - Current shader scope
739
+ * @returns The uniform variable of which presents the strength of the environment light
740
+ */ static getEnvLightStrength(scope) {
741
+ return scope.light.envLightStrength;
742
+ }
743
+ /**
744
+ * Gets current scene color texture
745
+ * @param scope - Current shader scope
746
+ * @returns current scene color texture
747
+ */ static getSceneColorTexture(scope) {
748
+ return scope[UNIFORM_NAME_SCENE_COLOR_MAP];
749
+ }
750
+ /**
751
+ * Gets the size of current scene color texture
752
+ * @param scope - Current shader scope
753
+ * @returns The size of current scene color texture
754
+ */ static getSceneColorTextureSize(scope) {
755
+ return scope[UNIFORM_NAME_SCENE_COLOR_MAP_SIZE];
756
+ }
757
+ /**
758
+ * Gets current linear depth texture
759
+ * @param scope - Current shader scope
760
+ * @returns current linear depth texture
761
+ */ static getLinearDepthTexture(scope) {
762
+ return scope[UNIFORM_NAME_LINEAR_DEPTH_MAP];
763
+ }
764
+ /**
765
+ * Gets the size of current linear depth texture
766
+ * @param scope - Current shader scope
767
+ * @returns The size of current linear depth texture
768
+ */ static getLinearDepthTextureSize(scope) {
769
+ return scope[UNIFORM_NAME_LINEAR_DEPTH_MAP_SIZE];
770
+ }
771
+ /**
772
+ * Gets current HiZ depth texture
773
+ * @param scope - Current shader scope
774
+ * @returns current HiZ depth texture
775
+ */ static getHiZDepthTexture(scope) {
776
+ return scope[UNIFORM_NAME_HIZ_DEPTH_MAP];
777
+ }
778
+ /**
779
+ * Gets the size of current HiZ depth texture
780
+ * @param scope - Current shader scope
781
+ * @returns The size of current HiZ depth texture
782
+ */ static getHiZDepthTextureSize(scope) {
783
+ return scope[UNIFORM_NAME_HIZ_DEPTH_MAP_INFO].xy;
784
+ }
785
+ /**
786
+ * Gets the mipmap levels count of current HiZ depth texture
787
+ * @param scope - Current shader scope
788
+ * @returns The mipmap levels count of current HiZ depth texture
789
+ */ static getHiZDepthTextureMipLevelCount(scope) {
790
+ return scope[UNIFORM_NAME_HIZ_DEPTH_MAP_INFO].z;
791
+ }
792
+ /**
793
+ * Gets current baked skybox texture
794
+ * @param scope - Current shader scope
795
+ * @returns current baked skybox texture
796
+ */ static getBakedSkyTexture(scope) {
797
+ return scope[UNIFORM_NAME_BAKED_SKY_MAP];
798
+ }
799
+ /**
800
+ * Gets the elapsed time in seconds
801
+ * @param scope - Current shader scope
802
+ * @returns The elapsed time in seconds
803
+ */ static getElapsedTime(scope) {
804
+ return scope.camera.elapsedTime;
805
+ }
806
+ /**
807
+ * Gets the elapsed time since last frame in seconds
808
+ * @param scope - Current shader scope
809
+ * @returns The elapsed time since last frame in seconds
810
+ */ static getElapsedTimeFrame(scope) {
811
+ return scope.camera.frameDeltaTime;
812
+ }
813
+ /**
814
+ * Gets the uniform variable of type vec3 which holds the camera position
815
+ * @param scope - Current shader scope
816
+ * @returns The camera position
817
+ */ static getCameraPosition(scope) {
818
+ return scope.camera.position.xyz;
819
+ }
820
+ /**
821
+ * Gets the uniform variable of type float which holds the roughness factor
822
+ * @param scope - Current shader scope
823
+ * @returns The roughness factor
824
+ */ static getCameraRoughnessFactor(scope) {
825
+ return scope.camera.roughnessFactor;
826
+ }
827
+ /**
828
+ * Gets framebuffer size for rendering
829
+ * @param scope - Current shader scope
830
+ * @returns The roughness factor
831
+ */ static getRenderSize(scope) {
832
+ return scope.camera.renderSize;
833
+ }
834
+ /**
835
+ * Gets the uniform variable of type uint which holds the framestamp
836
+ * @param scope - Current shader scope
837
+ * @returns The framestamp
838
+ */ static getFramestamp(scope) {
839
+ return scope.camera.framestamp;
840
+ }
841
+ /**
842
+ * Discard the fragment if it was clipped by the clip plane
843
+ * @param scope - Current shader scope
844
+ */ static discardIfClipped(scope, worldPos) {
845
+ const funcName = 'Z_discardIfClippped';
846
+ const pb = scope.$builder;
847
+ const that = this;
848
+ pb.func(funcName, [
849
+ pb.vec3('worldPos')
850
+ ], function() {
851
+ this.$if(pb.notEqual(that.getCameraClipPlaneFlag(this), 0), function() {
852
+ this.$l.clipPlane = that.getCameraClipPlane(this);
853
+ this.$if(pb.greaterThan(pb.add(pb.dot(this.worldPos.xyz, this.clipPlane.xyz), this.clipPlane.w), 0), function() {
854
+ pb.discard();
855
+ });
856
+ });
857
+ });
858
+ pb.getGlobalScope()[funcName](worldPos);
859
+ }
860
+ /**
861
+ * Gets the clip plane flag
862
+ * @param scope - Current shader scope
863
+ * @returns A float value of 1 indices the clip plane presents, otherwise 0
864
+ */ static getCameraClipPlaneFlag(scope) {
865
+ return scope.camera.position.w;
866
+ }
867
+ /**
868
+ * Gets the clip plane
869
+ * @param scope - Current shader scope
870
+ * @returns A vec4 presents the clip plane
871
+ */ static getCameraClipPlane(scope) {
872
+ return scope.camera.clipPlane;
873
+ }
874
+ /**
875
+ * Gets the uniform variable of type vec4 which holds the camera parameters
876
+ * @param scope - Current shader scope
877
+ * @returns The camera parameters
878
+ */ static getCameraParams(scope) {
879
+ return scope.camera.params;
880
+ }
881
+ /** @internal */ static getClusterParams(scope) {
882
+ return scope.light.clusterParams;
883
+ }
884
+ /** @internal */ static getCountParams(scope) {
885
+ return scope.light.countParams;
886
+ }
887
+ /** @internal */ static getClusteredLightIndexTexture(scope) {
888
+ return scope[UNIFORM_NAME_LIGHT_INDEX_TEXTURE];
889
+ }
890
+ /**
891
+ * Gets the uniform variable that contains atmosphere parameters
892
+ * @param scope - Current shader scope
893
+ * @returns The atmosphere parameters
894
+ */ static getAtmosphereParams(scope) {
895
+ return scope.fog.atmosphereParams;
896
+ }
897
+ /**
898
+ * Gets the aerial perspective LUT
899
+ * @param scope - Current shader scope
900
+ * @returns The aerial perspective LUT texture
901
+ */ static getAerialPerspectiveLUT(scope) {
902
+ return scope[UNIFORM_NAME_AERIALPERSPECTIVE_LUT];
903
+ }
904
+ /**
905
+ * Gets the uniform variable of type mat4 which holds the view projection matrix of current camera
906
+ * @param scope - Current shader scope
907
+ * @returns The view projection matrix of current camera
908
+ */ static getViewProjectionMatrix(scope) {
909
+ return scope.camera.viewProjectionMatrix;
910
+ }
911
+ /**
912
+ * Gets the uniform variable of type mat4 which holds the inversed view projection matrix of current camera
913
+ * @param scope - Current shader scope
914
+ * @returns The inversed view projection matrix of current camera
915
+ */ static getInvViewProjectionMatrix(scope) {
916
+ return scope.camera.invViewProjectionMatrix;
917
+ }
918
+ /**
919
+ * Gets the uniform variable of type mat4 which holds the projection matrix of current camera
920
+ * @param scope - Current shader scope
921
+ * @returns The projection matrix of current camera
922
+ */ static getProjectionMatrix(scope) {
923
+ return scope.camera.projectionMatrix;
924
+ }
925
+ /**
926
+ * Gets the uniform variable of type mat4 which holds the inversed projection matrix of current camera
927
+ * @param scope - Current shader scope
928
+ * @returns The inversed projection matrix of current camera
929
+ */ static getInvProjectionMatrix(scope) {
930
+ return scope.camera.invProjectionMatrix;
931
+ }
932
+ /**
933
+ * Gets the uniform variable of type mat4 which holds the unjittered view projection matrix of current camera
934
+ * @param scope - Current shader scope
935
+ * @returns The unjittered view projection matrix of current camera
936
+ */ static getUnjitteredViewProjectionMatrix(scope) {
937
+ return scope.camera.unjitteredVPMatrix;
938
+ }
939
+ /**
940
+ * Gets the uniform variable of type vec2 which holds the jitter value of the projection matrix of current camera
941
+ * @param scope - Current shader scope
942
+ * @returns The jitter value of projection matrix of current camera
943
+ */ static getProjectionMatrixJitterValue(scope) {
944
+ return scope.camera.jitterValue;
945
+ }
946
+ /**
947
+ * Gets the uniform variable of type mat4 which holds the jittered inversed view-projection matrix
948
+ * @param scope - Current shader scope
949
+ * @returns The jittered inversed view-projection matrix
950
+ */ static getJitteredInvVPMatrix(scope) {
951
+ return scope.camera.jitteredInvVPMatrix;
952
+ }
953
+ /**
954
+ * Gets the uniform variable of type mat4 which holds the unjittered view projection at previous frame matrix of current camera
955
+ * @param scope - Current shader scope
956
+ * @returns The unjittered view projection matrix at previous frame of current camera
957
+ */ static getPrevUnjitteredViewProjectionMatrix(scope) {
958
+ return scope.camera.prevUnjitteredVPMatrix;
959
+ }
960
+ /**
961
+ * Gets the uniform variable of type mat4 which holds the view matrix of current camera
962
+ * @param scope - Current shader scope
963
+ * @returns The view matrix of current camera
964
+ */ static getViewMatrix(scope) {
965
+ return scope.camera.viewMatrix;
966
+ }
967
+ /** @internal */ static getCascadeDistances(scope) {
968
+ return scope.light.cascadeDistances;
969
+ }
970
+ /** @internal */ static getDepthBiasValues(scope) {
971
+ return scope.light.depthBiasValues;
972
+ }
973
+ /** @internal */ static getShadowCameraParams(scope) {
974
+ return scope.light.shadowCameraParams;
975
+ }
976
+ /** @internal */ static getDepthBiasScales(scope) {
977
+ return scope.light.depthBiasScales;
978
+ }
979
+ /** @internal */ static getNumLights(scope) {
980
+ return scope.light.numLights;
981
+ }
982
+ /** @internal */ static getSunLightDir(scope) {
983
+ return scope.light.sunDir;
984
+ }
985
+ /** @internal */ static getLightTypeForShadow(scope) {
986
+ return scope.light.lightType;
987
+ }
988
+ /** @internal */ static getLightPositionAndRangeForShadow(scope) {
989
+ return scope.light.positionAndRange;
990
+ }
991
+ /** @internal */ static getLightViewMatrixForShadow(scope) {
992
+ return scope.light.viewMatrix;
993
+ }
994
+ /** @internal */ static calculateShadowSpaceVertex(scope, worldPos, cascade = 0) {
995
+ const pb = scope.$builder;
996
+ if (typeof cascade === 'number') {
997
+ return pb.vec4(pb.dot(scope.light.shadowMatrices.at(cascade * 4 + 0), worldPos), pb.dot(scope.light.shadowMatrices.at(cascade * 4 + 1), worldPos), pb.dot(scope.light.shadowMatrices.at(cascade * 4 + 2), worldPos), pb.dot(scope.light.shadowMatrices.at(cascade * 4 + 3), worldPos));
998
+ } else {
999
+ return pb.vec4(pb.dot(scope.light.shadowMatrices.at(pb.add(pb.mul(cascade, 4), 0)), worldPos), pb.dot(scope.light.shadowMatrices.at(pb.add(pb.mul(cascade, 4), 1)), worldPos), pb.dot(scope.light.shadowMatrices.at(pb.add(pb.mul(cascade, 4), 2)), worldPos), pb.dot(scope.light.shadowMatrices.at(pb.add(pb.mul(cascade, 4), 3)), worldPos));
1000
+ }
1001
+ }
1002
+ /** @internal */ static getLightPositionAndRange(scope, lightIndex) {
1003
+ return scope[UNIFORM_NAME_LIGHT_BUFFER].at(scope.$builder.mul(lightIndex, 3));
1004
+ }
1005
+ /** @internal */ static getLightDirectionAndCutoff(scope, lightIndex) {
1006
+ return scope[UNIFORM_NAME_LIGHT_BUFFER].at(scope.$builder.add(scope.$builder.mul(lightIndex, 3), 1));
1007
+ }
1008
+ /** @internal */ static getLightColorAndIntensity(scope, lightIndex) {
1009
+ return scope[UNIFORM_NAME_LIGHT_BUFFER].at(scope.$builder.add(scope.$builder.mul(lightIndex, 3), 2));
1010
+ }
1011
+ /**
1012
+ * Sets the clip space position in vertex shader
1013
+ *
1014
+ * @remarks
1015
+ * Use this function instead of using
1016
+ * <pre>
1017
+ * // Do not use this
1018
+ * this.$builtins.position = some_value;
1019
+ * // Use this
1020
+ * ShaderFramework.setClipSpacePosition(some_value);
1021
+ * </pre>,
1022
+ *
1023
+ * @param scope - Current shader scope
1024
+ * @param pos - The clip space position to be set
1025
+ */ static setClipSpacePosition(scope, pos) {
1026
+ const pb = scope.$builder;
1027
+ const cameraParams = this.getCameraParams(scope);
1028
+ if (cameraParams) {
1029
+ scope.$builtins.position = pb.mul(pos, pb.vec4(1, cameraParams.z, 1, 1));
1030
+ } else {
1031
+ scope.$builtins.position = pos;
1032
+ }
1033
+ }
1034
+ /**
1035
+ * Get shadow map uniform value
1036
+ *
1037
+ * @param scope - Shader scope
1038
+ * @returns The shadow map texture uniform
1039
+ */ static getShadowMap(scope) {
1040
+ return scope[UNIFORM_NAME_SHADOW_MAP];
1041
+ }
1042
+ /**
1043
+ * Calculates shadow of current fragment
1044
+ *
1045
+ * @param scope - Shader scope
1046
+ * @param NoL - NdotL vector
1047
+ * @returns Shadow of current fragment, 1 means no shadow and 0 means full shadowed.
1048
+ */ static calculateShadow(scope, worldPos, NoL, ctx) {
1049
+ const pb = scope.$builder;
1050
+ const that = this;
1051
+ const shadowMapParams = ctx.shadowMapInfo.get(ctx.currentShadowLight);
1052
+ const funcName = 'Z_calculateShadow';
1053
+ pb.func(funcName, [
1054
+ pb.vec3('worldPos'),
1055
+ pb.float('NoL')
1056
+ ], function() {
1057
+ if (shadowMapParams.numShadowCascades > 1) {
1058
+ this.$l.shadowCascades = this.light.shadowCascades;
1059
+ this.$l.shadowBound = pb.vec4(0, 0, 1, 1);
1060
+ this.$l.linearDepth = that.nonLinearDepthToLinear(this, this.$builtins.fragCoord.z);
1061
+ this.$l.splitDistances = that.getCascadeDistances(this);
1062
+ this.$l.comparison = pb.vec4(pb.greaterThan(pb.vec4(this.linearDepth), this.splitDistances));
1063
+ this.$l.cascadeFlags = pb.vec4(pb.float(pb.greaterThan(this.shadowCascades, 0)), pb.float(pb.greaterThan(this.shadowCascades, 1)), pb.float(pb.greaterThan(this.shadowCascades, 2)), pb.float(pb.greaterThan(this.shadowCascades, 3)));
1064
+ this.$l.split = pb.int(pb.dot(this.comparison, this.cascadeFlags));
1065
+ if (ctx.device.type === 'webgl') {
1066
+ this.$l.shadowVertex = pb.vec4();
1067
+ this.$for(pb.int('cascade'), 0, 4, function() {
1068
+ this.$if(pb.equal(this.cascade, this.split), function() {
1069
+ this.shadowVertex = that.calculateShadowSpaceVertex(this, pb.vec4(this.worldPos, 1), this.cascade);
1070
+ this.$break();
1071
+ });
1072
+ });
1073
+ } else {
1074
+ this.$l.shadowVertex = that.calculateShadowSpaceVertex(this, pb.vec4(this.worldPos, 1), this.split);
1075
+ }
1076
+ const shadowMapParams = ctx.shadowMapInfo.get(ctx.currentShadowLight);
1077
+ this.$l.shadow = shadowMapParams.impl.computeShadowCSM(shadowMapParams, this, this.shadowVertex, this.NoL, this.split);
1078
+ this.$l.shadowDistance = that.getShadowCameraParams(scope).w;
1079
+ this.shadow = pb.mix(this.shadow, 1, pb.smoothStep(pb.mul(this.shadowDistance, 0.8), this.shadowDistance, pb.distance(that.getCameraPosition(this), this.worldPos)));
1080
+ this.$return(this.shadow);
1081
+ } else {
1082
+ this.$l.shadowVertex = that.calculateShadowSpaceVertex(this, pb.vec4(this.worldPos, 1));
1083
+ const shadowMapParams = ctx.shadowMapInfo.get(ctx.currentShadowLight);
1084
+ this.$l.shadow = shadowMapParams.impl.computeShadow(shadowMapParams, this, this.shadowVertex, this.NoL);
1085
+ this.$l.shadowDistance = that.getShadowCameraParams(scope).w;
1086
+ this.shadow = pb.mix(this.shadow, 1, pb.smoothStep(pb.mul(this.shadowDistance, 0.8), this.shadowDistance, pb.distance(that.getCameraPosition(this), this.worldPos)));
1087
+ this.$return(this.shadow);
1088
+ }
1089
+ });
1090
+ return pb.getGlobalScope()[funcName](worldPos, NoL);
1091
+ }
1092
+ static applyFog(scope, worldPos, color, ctx) {
1093
+ const pb = scope.$builder;
1094
+ const that = this;
1095
+ if (ctx.materialFlags & MaterialVaryingFlags.APPLY_FOG) {
1096
+ const funcName = 'Z_applyFog';
1097
+ pb.func(funcName, [
1098
+ pb.vec3('worldPos'),
1099
+ pb.vec4('color').inout()
1100
+ ], function() {
1101
+ this.$l.uv = pb.div(pb.vec2(this.$builtins.fragCoord.xy), that.getRenderSize(this));
1102
+ this.$l.fogging = calculateFog(this, this.fog.withAerialPerspective, this.fog.fogType, this.fog.atmosphereParams, this.fog.heightFogParams, this.uv, false, that.getCameraPosition(this).xyz, this.worldPos, this.fog.additive, this[UNIFORM_NAME_AERIALPERSPECTIVE_LUT], this[UNIFORM_NAME_SKYDISTANTLIGHT_LUT]);
1103
+ this.color = pb.vec4(pb.add(pb.mul(this.color.rgb, this.fogging.a), this.fogging.rgb), this.color.a);
1104
+ //this.color = pb.vec4(pb.vec3(pb.mix(this.u0, this.u1, this.factor)), this.color.a);
1105
+ });
1106
+ scope[funcName](worldPos, color);
1107
+ }
1108
+ }
1109
+ /**
1110
+ * Calculates the non-linear depth from linear depth
1111
+ *
1112
+ * @param scope - Current shader scope
1113
+ * @param depth - The linear depth
1114
+ * @param nearFar - A vector that contains the near clip plane in x component and the far clip plane in y component
1115
+ * @returns The calculated non-linear depth
1116
+ */ static linearDepthToNonLinear(scope, depth, nearFar) {
1117
+ const pb = scope.$builder;
1118
+ nearFar = nearFar ?? this.getCameraParams(scope);
1119
+ return pb.div(pb.sub(nearFar.y, pb.div(pb.mul(nearFar.x, nearFar.y), depth)), pb.sub(nearFar.y, nearFar.x));
1120
+ }
1121
+ /**
1122
+ * Calculates the linear depth from non-linear depth
1123
+ *
1124
+ * @param scope - Current shader scope
1125
+ * @param depth - The non-linear depth
1126
+ * @param nearFar - A vector that contains the near clip plane in x component and the far clip plane in y component
1127
+ * @returns The calculated linear depth
1128
+ */ static nonLinearDepthToLinear(scope, depth, nearFar) {
1129
+ const pb = scope.$builder;
1130
+ nearFar = nearFar ?? this.getCameraParams(scope);
1131
+ return pb.div(pb.mul(nearFar.x, nearFar.y), pb.mix(nearFar.y, nearFar.x, depth));
1132
+ }
1133
+ /**
1134
+ * Calculates the normalized linear depth from non-linear depth
1135
+ *
1136
+ * @param scope - Current shader scope
1137
+ * @param depth - The non-linear depth
1138
+ * @param nearFar - A vector that contains the near clip plane in x component and the far clip plane in y component
1139
+ * @returns The calculated normalized linear depth
1140
+ */ static nonLinearDepthToLinearNormalized(scope, depth, nearFar) {
1141
+ const pb = scope.$builder;
1142
+ nearFar = nearFar ?? this.getCameraParams(scope);
1143
+ return pb.div(nearFar.x, pb.mix(nearFar.y, nearFar.x, depth));
1144
+ }
1145
+ /**
1146
+ * Sample linear depth from linear depth texture
1147
+ * @param scope - Current shader scope
1148
+ * @param tex - The linear depth texture
1149
+ * @param uv - The uv coordinates
1150
+ * @param level - The mipmap level to sample
1151
+ * @returns Linear depth value
1152
+ */ static sampleLinearDepth(scope, tex, uv, level) {
1153
+ const pb = scope.$builder;
1154
+ const depth = pb.textureSampleLevel(tex, uv, level);
1155
+ return pb.getDevice().type === 'webgl' ? decodeNormalizedFloatFromRGBA(scope, depth) : depth.r;
1156
+ }
1157
+ static samplePositionFromDepth(scope, depthTex, uv, mat, cameraNearFar) {
1158
+ const pb = scope.$builder;
1159
+ const that = this;
1160
+ pb.func('zSamplePositionFromDepth', [
1161
+ pb.vec2('uv'),
1162
+ pb.vec2('cameraNearFar'),
1163
+ pb.mat4('mat')
1164
+ ], function() {
1165
+ this.$l.linearDepth = that.sampleLinearDepth(this, depthTex, this.uv, 0);
1166
+ this.$l.nonLinearDepth = pb.div(pb.sub(pb.div(this.cameraNearFar.x, this.linearDepth), this.cameraNearFar.y), pb.sub(this.cameraNearFar.x, this.cameraNearFar.y));
1167
+ this.$l.clipSpacePos = pb.vec4(pb.sub(pb.mul(this.uv, 2), pb.vec2(1)), pb.sub(pb.mul(pb.clamp(this.nonLinearDepth, 0, 1), 2), 1), 1);
1168
+ this.$l.wPos = pb.mul(this.mat, this.clipSpacePos);
1169
+ this.$return(pb.vec4(pb.div(this.wPos.xyz, this.wPos.w), this.linearDepth));
1170
+ });
1171
+ return scope.zSamplePositionFromDepth(uv, cameraNearFar, mat);
1172
+ }
1173
+ /**
1174
+ * Sample linear depth from linear depth texture with backface
1175
+ * @param scope - Current shader scope
1176
+ * @param tex - The linear depth texture
1177
+ * @param uv - The uv coordinates
1178
+ * @param level - The mipmap level to sample
1179
+ * @returns Linear depth value
1180
+ */ static sampleLinearDepthWithBackface(scope, tex, uv, level) {
1181
+ const pb = scope.$builder;
1182
+ return pb.textureSampleLevel(tex, uv, level).rg;
1183
+ }
1184
+ /**
1185
+ * Transform color to sRGB color space if nessesary
1186
+ *
1187
+ * @param scope - Current shader scope
1188
+ * @param outputColor - The color to be transformed
1189
+ * @returns The transformed color
1190
+ */ static encodeColorOutput(scope, outputColor) {
1191
+ const pb = scope.$builder;
1192
+ const that = this;
1193
+ const funcName = 'Z_encodeColorOutput';
1194
+ pb.func(funcName, [
1195
+ pb.vec4('outputColor')
1196
+ ], function() {
1197
+ const params = that.getCameraParams(this);
1198
+ this.$if(pb.notEqual(params.w, 0), function() {
1199
+ this.$return(pb.vec4(linearToGamma(this, this.outputColor.rgb), this.outputColor.w));
1200
+ }).$else(function() {
1201
+ this.$return(this.outputColor);
1202
+ });
1203
+ });
1204
+ return pb.getGlobalScope()[funcName](outputColor);
1205
+ }
1206
+ }
1207
+
1208
+ export { ShaderHelper };
1209
+ //# sourceMappingURL=helper.js.map