babylon.materialHelper.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. module BABYLON {
  2. export class MaterialHelper {
  3. public static PrepareDefinesForLights(scene: Scene, mesh: AbstractMesh, defines: MaterialDefines, maxSimultaneousLights = 4): boolean {
  4. var lightIndex = 0;
  5. var needNormals = false;
  6. var needRebuild = false;
  7. var needShadows = false;
  8. var lightmapMode = false;
  9. for (var index = 0; index < scene.lights.length; index++) {
  10. var light = scene.lights[index];
  11. if (!light.isEnabled()) {
  12. continue;
  13. }
  14. // Excluded check
  15. if (light._excludedMeshesIds.length > 0) {
  16. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  17. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  18. if (excludedMesh) {
  19. light.excludedMeshes.push(excludedMesh);
  20. }
  21. }
  22. light._excludedMeshesIds = [];
  23. }
  24. // Included check
  25. if (light._includedOnlyMeshesIds.length > 0) {
  26. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  27. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  28. if (includedOnlyMesh) {
  29. light.includedOnlyMeshes.push(includedOnlyMesh);
  30. }
  31. }
  32. light._includedOnlyMeshesIds = [];
  33. }
  34. if (!light.canAffectMesh(mesh)) {
  35. continue;
  36. }
  37. needNormals = true;
  38. if (defines["LIGHT" + lightIndex] === undefined) {
  39. needRebuild = true;
  40. }
  41. defines["LIGHT" + lightIndex] = true;
  42. var type;
  43. if (light instanceof SpotLight) {
  44. type = "SPOTLIGHT" + lightIndex;
  45. } else if (light instanceof HemisphericLight) {
  46. type = "HEMILIGHT" + lightIndex;
  47. } else if (light instanceof PointLight) {
  48. type = "POINTLIGHT" + lightIndex;
  49. } else {
  50. type = "DIRLIGHT" + lightIndex;
  51. }
  52. if (defines[type] === undefined) {
  53. needRebuild = true;
  54. }
  55. defines[type] = true;
  56. // Specular
  57. if (!light.specular.equalsFloats(0, 0, 0) && defines["SPECULARTERM"] !== undefined) {
  58. defines["SPECULARTERM"] = true;
  59. }
  60. // Shadows
  61. if (scene.shadowsEnabled) {
  62. var shadowGenerator = <ShadowGenerator>light.getShadowGenerator();
  63. if (mesh && mesh.receiveShadows && shadowGenerator) {
  64. if (defines["SHADOW" + lightIndex] === undefined) {
  65. needRebuild = true;
  66. }
  67. defines["SHADOW" + lightIndex] = true;
  68. defines["SHADOWS"] = true;
  69. if (shadowGenerator.usePoissonSampling) {
  70. if (defines["SHADOWPCF" + lightIndex] === undefined) {
  71. needRebuild = true;
  72. }
  73. defines["SHADOWPCF" + lightIndex] = true;
  74. }
  75. else if (shadowGenerator.useExponentialShadowMap || shadowGenerator.useBlurExponentialShadowMap) {
  76. if (defines["SHADOWESM" + lightIndex] === undefined) {
  77. needRebuild = true;
  78. }
  79. defines["SHADOWESM" + lightIndex] = true;
  80. }
  81. needShadows = true;
  82. }
  83. }
  84. if (light.lightmapMode != Light.LIGHTMAP_DEFAULT ) {
  85. lightmapMode = true;
  86. if (defines["LIGHTMAPEXCLUDED" + lightIndex] === undefined) {
  87. needRebuild = true;
  88. }
  89. if (defines["LIGHTMAPNOSPECULAR" + lightIndex] === undefined) {
  90. needRebuild = true;
  91. }
  92. defines["LIGHTMAPEXCLUDED" + lightIndex] = true;
  93. if (light.lightmapMode == Light.LIGHTMAP_SHADOWSONLY) {
  94. defines["LIGHTMAPNOSPECULAR" + lightIndex] = true;
  95. }
  96. }
  97. lightIndex++;
  98. if (lightIndex === maxSimultaneousLights)
  99. break;
  100. }
  101. let caps = scene.getEngine().getCaps();
  102. if (needShadows && caps.textureFloat && caps.textureFloatLinearFiltering && caps.textureFloatRender) {
  103. if (defines["SHADOWFULLFLOAT"] === undefined) {
  104. needRebuild = true;
  105. }
  106. defines["SHADOWFULLFLOAT"] = true;
  107. }
  108. if (defines["LIGHTMAPEXCLUDED"] === undefined) {
  109. needRebuild = true;
  110. }
  111. if (lightmapMode) {
  112. defines["LIGHTMAPEXCLUDED"] = true;
  113. }
  114. if (needRebuild) {
  115. defines.rebuild();
  116. }
  117. return needNormals;
  118. }
  119. public static PrepareUniformsAndSamplersList(uniformsList: string[], samplersList: string[], defines: MaterialDefines, maxSimultaneousLights = 4): void {
  120. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  121. if (!defines["LIGHT" + lightIndex]) {
  122. break;
  123. }
  124. uniformsList.push(
  125. "vLightData" + lightIndex,
  126. "vLightDiffuse" + lightIndex,
  127. "vLightSpecular" + lightIndex,
  128. "vLightDirection" + lightIndex,
  129. "vLightGround" + lightIndex,
  130. "lightMatrix" + lightIndex,
  131. "shadowsInfo" + lightIndex
  132. );
  133. samplersList.push("shadowSampler" + lightIndex);
  134. }
  135. }
  136. public static HandleFallbacksForShadows(defines: MaterialDefines, fallbacks: EffectFallbacks, maxSimultaneousLights = 4): void {
  137. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  138. if (!defines["LIGHT" + lightIndex]) {
  139. break;
  140. }
  141. if (lightIndex > 0) {
  142. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  143. }
  144. if (defines["SHADOW" + lightIndex]) {
  145. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  146. }
  147. if (defines["SHADOWPCF" + lightIndex]) {
  148. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  149. }
  150. if (defines["SHADOWESM" + lightIndex]) {
  151. fallbacks.addFallback(0, "SHADOWESM" + lightIndex);
  152. }
  153. }
  154. }
  155. public static PrepareAttributesForBones(attribs: string[], mesh: AbstractMesh, defines: MaterialDefines, fallbacks: EffectFallbacks): void {
  156. if (defines["NUM_BONE_INFLUENCERS"] > 0) {
  157. fallbacks.addCPUSkinningFallback(0, mesh);
  158. attribs.push(VertexBuffer.MatricesIndicesKind);
  159. attribs.push(VertexBuffer.MatricesWeightsKind);
  160. if (defines["NUM_BONE_INFLUENCERS"] > 4) {
  161. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  162. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  163. }
  164. }
  165. }
  166. public static PrepareAttributesForInstances(attribs: string[], defines: MaterialDefines): void {
  167. if (defines["INSTANCES"]) {
  168. attribs.push("world0");
  169. attribs.push("world1");
  170. attribs.push("world2");
  171. attribs.push("world3");
  172. }
  173. }
  174. // Bindings
  175. public static BindLightShadow(light: Light, scene: Scene, mesh: AbstractMesh, lightIndex: number, effect: Effect, depthValuesAlreadySet: boolean): boolean {
  176. var shadowGenerator = <ShadowGenerator>light.getShadowGenerator();
  177. if (mesh.receiveShadows && shadowGenerator) {
  178. if (!(<any>light).needCube()) {
  179. effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  180. } else {
  181. if (!depthValuesAlreadySet) {
  182. depthValuesAlreadySet = true;
  183. effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
  184. }
  185. }
  186. effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  187. effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.depthScale);
  188. }
  189. return depthValuesAlreadySet;
  190. }
  191. public static BindLightProperties(light: Light, effect: Effect, lightIndex: number): void {
  192. if (light instanceof PointLight) {
  193. // Point Light
  194. light.transferToEffect(effect, "vLightData" + lightIndex);
  195. } else if (light instanceof DirectionalLight) {
  196. // Directional Light
  197. light.transferToEffect(effect, "vLightData" + lightIndex);
  198. } else if (light instanceof SpotLight) {
  199. // Spot Light
  200. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  201. } else if (light instanceof HemisphericLight) {
  202. // Hemispheric Light
  203. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  204. }
  205. }
  206. public static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: MaterialDefines, maxSimultaneousLights = 4) {
  207. var lightIndex = 0;
  208. var depthValuesAlreadySet = false;
  209. for (var index = 0; index < scene.lights.length; index++) {
  210. var light = scene.lights[index];
  211. if (!light.isEnabled()) {
  212. continue;
  213. }
  214. if (!light.canAffectMesh(mesh)) {
  215. continue;
  216. }
  217. MaterialHelper.BindLightProperties(light, effect, lightIndex);
  218. light.diffuse.scaleToRef(light.intensity, Tmp.Color3[0]);
  219. effect.setColor4("vLightDiffuse" + lightIndex, Tmp.Color3[0], light.range);
  220. if (defines["SPECULARTERM"]) {
  221. light.specular.scaleToRef(light.intensity, Tmp.Color3[1]);
  222. effect.setColor3("vLightSpecular" + lightIndex, Tmp.Color3[1]);
  223. }
  224. // Shadows
  225. if (scene.shadowsEnabled) {
  226. depthValuesAlreadySet = this.BindLightShadow(light, scene, mesh, lightIndex, effect, depthValuesAlreadySet);
  227. }
  228. lightIndex++;
  229. if (lightIndex === maxSimultaneousLights)
  230. break;
  231. }
  232. }
  233. public static BindFogParameters(scene: Scene, mesh: AbstractMesh, effect: Effect): void {
  234. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  235. effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  236. effect.setColor3("vFogColor", scene.fogColor);
  237. }
  238. }
  239. public static BindBonesParameters(mesh: AbstractMesh, effect: Effect): void {
  240. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  241. var matrices = mesh.skeleton.getTransformMatrices(mesh);
  242. if (matrices) {
  243. effect.setMatrices("mBones", matrices);
  244. }
  245. }
  246. }
  247. public static BindLogDepth(defines: MaterialDefines, effect: Effect, scene: Scene): void {
  248. if (defines["LOGARITHMICDEPTH"]) {
  249. effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log(scene.activeCamera.maxZ + 1.0) / Math.LN2));
  250. }
  251. }
  252. public static BindClipPlane(effect: Effect, scene: Scene): void {
  253. if (scene.clipPlane) {
  254. var clipPlane = scene.clipPlane;
  255. effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  256. }
  257. }
  258. }
  259. }