materialHelper.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { BindBonesParameters, BindFogParameters, BindLight, BindLightProperties, BindLights, BindLogDepth, BindMorphTargetParameters, BindSceneUniformBuffer, BindTextureMatrix, GetFogState, HandleFallbacksForShadows, PrepareAttributesForBakedVertexAnimation, PrepareAttributesForBones, PrepareAttributesForInstances, PrepareAttributesForMorphTargets, PrepareAttributesForMorphTargetsInfluencers, PrepareDefinesForAttributes, PrepareDefinesForBakedVertexAnimation, PrepareDefinesForBones, PrepareDefinesForCamera, PrepareDefinesForFrameBoundValues, PrepareDefinesForLight, PrepareDefinesForLights, PrepareDefinesForMergedUV, PrepareDefinesForMisc, PrepareDefinesForMorphTargets, PrepareDefinesForMultiview, PrepareDefinesForOIT, PrepareDefinesForPrePass, PrepareUniformsAndSamplersForLight, PrepareUniformsAndSamplersList, PushAttributesForInstances, } from "./materialHelper.functions.js";
  2. /**
  3. * "Static Class" containing the most commonly used helper while dealing with material for rendering purpose.
  4. *
  5. * It contains the basic tools to help defining defines, binding uniform for the common part of the materials.
  6. *
  7. * This works by convention in BabylonJS but is meant to be use only with shader following the in place naming rules and conventions.
  8. */
  9. export class MaterialHelper {
  10. }
  11. /**
  12. * Binds the scene's uniform buffer to the effect.
  13. * @param effect defines the effect to bind to the scene uniform buffer
  14. * @param sceneUbo defines the uniform buffer storing scene data
  15. */
  16. MaterialHelper.BindSceneUniformBuffer = BindSceneUniformBuffer;
  17. /**
  18. * Helps preparing the defines values about the UVs in used in the effect.
  19. * UVs are shared as much as we can across channels in the shaders.
  20. * @param texture The texture we are preparing the UVs for
  21. * @param defines The defines to update
  22. * @param key The channel key "diffuse", "specular"... used in the shader
  23. */
  24. MaterialHelper.PrepareDefinesForMergedUV = PrepareDefinesForMergedUV;
  25. /**
  26. * Binds a texture matrix value to its corresponding uniform
  27. * @param texture The texture to bind the matrix for
  28. * @param uniformBuffer The uniform buffer receiving the data
  29. * @param key The channel key "diffuse", "specular"... used in the shader
  30. */
  31. MaterialHelper.BindTextureMatrix = BindTextureMatrix;
  32. /**
  33. * Gets the current status of the fog (should it be enabled?)
  34. * @param mesh defines the mesh to evaluate for fog support
  35. * @param scene defines the hosting scene
  36. * @returns true if fog must be enabled
  37. */
  38. MaterialHelper.GetFogState = GetFogState;
  39. /**
  40. * Helper used to prepare the list of defines associated with misc. values for shader compilation
  41. * @param mesh defines the current mesh
  42. * @param scene defines the current scene
  43. * @param useLogarithmicDepth defines if logarithmic depth has to be turned on
  44. * @param pointsCloud defines if point cloud rendering has to be turned on
  45. * @param fogEnabled defines if fog has to be turned on
  46. * @param alphaTest defines if alpha testing has to be turned on
  47. * @param defines defines the current list of defines
  48. * @param applyDecalAfterDetail Defines if the decal is applied after or before the detail
  49. */
  50. MaterialHelper.PrepareDefinesForMisc = PrepareDefinesForMisc;
  51. /**
  52. * Helper used to prepare the defines relative to the active camera
  53. * @param scene defines the current scene
  54. * @param defines specifies the list of active defines
  55. * @returns true if the defines have been updated, else false
  56. */
  57. MaterialHelper.PrepareDefinesForCamera = PrepareDefinesForCamera;
  58. /**
  59. * Helper used to prepare the list of defines associated with frame values for shader compilation
  60. * @param scene defines the current scene
  61. * @param engine defines the current engine
  62. * @param material defines the material we are compiling the shader for
  63. * @param defines specifies the list of active defines
  64. * @param useInstances defines if instances have to be turned on
  65. * @param useClipPlane defines if clip plane have to be turned on
  66. * @param useThinInstances defines if thin instances have to be turned on
  67. */
  68. MaterialHelper.PrepareDefinesForFrameBoundValues = PrepareDefinesForFrameBoundValues;
  69. /**
  70. * Prepares the defines for bones
  71. * @param mesh The mesh containing the geometry data we will draw
  72. * @param defines The defines to update
  73. */
  74. MaterialHelper.PrepareDefinesForBones = PrepareDefinesForBones;
  75. /**
  76. * Prepares the defines for morph targets
  77. * @param mesh The mesh containing the geometry data we will draw
  78. * @param defines The defines to update
  79. */
  80. MaterialHelper.PrepareDefinesForMorphTargets = PrepareDefinesForMorphTargets;
  81. /**
  82. * Prepares the defines for baked vertex animation
  83. * @param mesh The mesh containing the geometry data we will draw
  84. * @param defines The defines to update
  85. */
  86. MaterialHelper.PrepareDefinesForBakedVertexAnimation = PrepareDefinesForBakedVertexAnimation;
  87. /**
  88. * Prepares the defines used in the shader depending on the attributes data available in the mesh
  89. * @param mesh The mesh containing the geometry data we will draw
  90. * @param defines The defines to update
  91. * @param useVertexColor Precise whether vertex colors should be used or not (override mesh info)
  92. * @param useBones Precise whether bones should be used or not (override mesh info)
  93. * @param useMorphTargets Precise whether morph targets should be used or not (override mesh info)
  94. * @param useVertexAlpha Precise whether vertex alpha should be used or not (override mesh info)
  95. * @param useBakedVertexAnimation Precise whether baked vertex animation should be used or not (override mesh info)
  96. * @returns false if defines are considered not dirty and have not been checked
  97. */
  98. MaterialHelper.PrepareDefinesForAttributes = PrepareDefinesForAttributes;
  99. /**
  100. * Prepares the defines related to multiview
  101. * @param scene The scene we are intending to draw
  102. * @param defines The defines to update
  103. */
  104. MaterialHelper.PrepareDefinesForMultiview = PrepareDefinesForMultiview;
  105. /**
  106. * Prepares the defines related to order independant transparency
  107. * @param scene The scene we are intending to draw
  108. * @param defines The defines to update
  109. * @param needAlphaBlending Determines if the material needs alpha blending
  110. */
  111. MaterialHelper.PrepareDefinesForOIT = PrepareDefinesForOIT;
  112. /**
  113. * Prepares the defines related to the prepass
  114. * @param scene The scene we are intending to draw
  115. * @param defines The defines to update
  116. * @param canRenderToMRT Indicates if this material renders to several textures in the prepass
  117. */
  118. MaterialHelper.PrepareDefinesForPrePass = PrepareDefinesForPrePass;
  119. /**
  120. * Prepares the defines related to the light information passed in parameter
  121. * @param scene The scene we are intending to draw
  122. * @param mesh The mesh the effect is compiling for
  123. * @param light The light the effect is compiling for
  124. * @param lightIndex The index of the light
  125. * @param defines The defines to update
  126. * @param specularSupported Specifies whether specular is supported or not (override lights data)
  127. * @param state Defines the current state regarding what is needed (normals, etc...)
  128. * @param state.needNormals
  129. * @param state.needRebuild
  130. * @param state.shadowEnabled
  131. * @param state.specularEnabled
  132. * @param state.lightmapMode
  133. */
  134. MaterialHelper.PrepareDefinesForLight = PrepareDefinesForLight;
  135. /**
  136. * Prepares the defines related to the light information passed in parameter
  137. * @param scene The scene we are intending to draw
  138. * @param mesh The mesh the effect is compiling for
  139. * @param defines The defines to update
  140. * @param specularSupported Specifies whether specular is supported or not (override lights data)
  141. * @param maxSimultaneousLights Specifies how manuy lights can be added to the effect at max
  142. * @param disableLighting Specifies whether the lighting is disabled (override scene and light)
  143. * @returns true if normals will be required for the rest of the effect
  144. */
  145. MaterialHelper.PrepareDefinesForLights = PrepareDefinesForLights;
  146. /**
  147. * Prepares the uniforms and samplers list to be used in the effect (for a specific light)
  148. * @param lightIndex defines the light index
  149. * @param uniformsList The uniform list
  150. * @param samplersList The sampler list
  151. * @param projectedLightTexture defines if projected texture must be used
  152. * @param uniformBuffersList defines an optional list of uniform buffers
  153. * @param updateOnlyBuffersList True to only update the uniformBuffersList array
  154. */
  155. MaterialHelper.PrepareUniformsAndSamplersForLight = PrepareUniformsAndSamplersForLight;
  156. /**
  157. * Prepares the uniforms and samplers list to be used in the effect
  158. * @param uniformsListOrOptions The uniform names to prepare or an EffectCreationOptions containing the list and extra information
  159. * @param samplersList The sampler list
  160. * @param defines The defines helping in the list generation
  161. * @param maxSimultaneousLights The maximum number of simultaneous light allowed in the effect
  162. */
  163. MaterialHelper.PrepareUniformsAndSamplersList = PrepareUniformsAndSamplersList;
  164. /**
  165. * This helps decreasing rank by rank the shadow quality (0 being the highest rank and quality)
  166. * @param defines The defines to update while falling back
  167. * @param fallbacks The authorized effect fallbacks
  168. * @param maxSimultaneousLights The maximum number of lights allowed
  169. * @param rank the current rank of the Effect
  170. * @returns The newly affected rank
  171. */
  172. MaterialHelper.HandleFallbacksForShadows = HandleFallbacksForShadows;
  173. /**
  174. * Prepares the list of attributes required for morph targets according to the effect defines.
  175. * @param attribs The current list of supported attribs
  176. * @param mesh The mesh to prepare the morph targets attributes for
  177. * @param influencers The number of influencers
  178. */
  179. MaterialHelper.PrepareAttributesForMorphTargetsInfluencers = PrepareAttributesForMorphTargetsInfluencers;
  180. /**
  181. * Prepares the list of attributes required for morph targets according to the effect defines.
  182. * @param attribs The current list of supported attribs
  183. * @param mesh The mesh to prepare the morph targets attributes for
  184. * @param defines The current Defines of the effect
  185. */
  186. MaterialHelper.PrepareAttributesForMorphTargets = PrepareAttributesForMorphTargets;
  187. /**
  188. * Prepares the list of attributes required for baked vertex animations according to the effect defines.
  189. * @param attribs The current list of supported attribs
  190. * @param mesh The mesh to prepare for baked vertex animations
  191. * @param defines The current Defines of the effect
  192. */
  193. MaterialHelper.PrepareAttributesForBakedVertexAnimation = PrepareAttributesForBakedVertexAnimation;
  194. /**
  195. * Prepares the list of attributes required for bones according to the effect defines.
  196. * @param attribs The current list of supported attribs
  197. * @param mesh The mesh to prepare the bones attributes for
  198. * @param defines The current Defines of the effect
  199. * @param fallbacks The current effect fallback strategy
  200. */
  201. MaterialHelper.PrepareAttributesForBones = PrepareAttributesForBones;
  202. /**
  203. * Check and prepare the list of attributes required for instances according to the effect defines.
  204. * @param attribs The current list of supported attribs
  205. * @param defines The current MaterialDefines of the effect
  206. */
  207. MaterialHelper.PrepareAttributesForInstances = PrepareAttributesForInstances;
  208. /**
  209. * Add the list of attributes required for instances to the attribs array.
  210. * @param attribs The current list of supported attribs
  211. * @param needsPreviousMatrices If the shader needs previous matrices
  212. */
  213. MaterialHelper.PushAttributesForInstances = PushAttributesForInstances;
  214. /**
  215. * Binds the light information to the effect.
  216. * @param light The light containing the generator
  217. * @param effect The effect we are binding the data to
  218. * @param lightIndex The light index in the effect used to render
  219. */
  220. MaterialHelper.BindLightProperties = BindLightProperties;
  221. /**
  222. * Binds the lights information from the scene to the effect for the given mesh.
  223. * @param light Light to bind
  224. * @param lightIndex Light index
  225. * @param scene The scene where the light belongs to
  226. * @param effect The effect we are binding the data to
  227. * @param useSpecular Defines if specular is supported
  228. * @param receiveShadows Defines if the effect (mesh) we bind the light for receives shadows
  229. */
  230. MaterialHelper.BindLight = BindLight;
  231. /**
  232. * Binds the lights information from the scene to the effect for the given mesh.
  233. * @param scene The scene the lights belongs to
  234. * @param mesh The mesh we are binding the information to render
  235. * @param effect The effect we are binding the data to
  236. * @param defines The generated defines for the effect
  237. * @param maxSimultaneousLights The maximum number of light that can be bound to the effect
  238. */
  239. MaterialHelper.BindLights = BindLights;
  240. /**
  241. * Binds the fog information from the scene to the effect for the given mesh.
  242. * @param scene The scene the lights belongs to
  243. * @param mesh The mesh we are binding the information to render
  244. * @param effect The effect we are binding the data to
  245. * @param linearSpace Defines if the fog effect is applied in linear space
  246. */
  247. MaterialHelper.BindFogParameters = BindFogParameters;
  248. /**
  249. * Binds the bones information from the mesh to the effect.
  250. * @param mesh The mesh we are binding the information to render
  251. * @param effect The effect we are binding the data to
  252. * @param prePassConfiguration Configuration for the prepass, in case prepass is activated
  253. */
  254. MaterialHelper.BindBonesParameters = BindBonesParameters;
  255. /**
  256. * Binds the morph targets information from the mesh to the effect.
  257. * @param abstractMesh The mesh we are binding the information to render
  258. * @param effect The effect we are binding the data to
  259. */
  260. MaterialHelper.BindMorphTargetParameters = BindMorphTargetParameters;
  261. /**
  262. * Binds the logarithmic depth information from the scene to the effect for the given defines.
  263. * @param defines The generated defines used in the effect
  264. * @param effect The effect we are binding the data to
  265. * @param scene The scene we are willing to render with logarithmic scale for
  266. */
  267. MaterialHelper.BindLogDepth = BindLogDepth;
  268. //# sourceMappingURL=materialHelper.js.map