babylon.materialHelper.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. module BABYLON {
  2. /**
  3. * "Static Class" containing the most commonly used helper while dealing with material for
  4. * rendering purpose.
  5. *
  6. * It contains the basic tools to help defining defines, binding uniform for the common part of the materials.
  7. *
  8. * This works by convention in BabylonJS but is meant to be use only with shader following the in place naming rules and conventions.
  9. */
  10. export class MaterialHelper {
  11. /**
  12. * Bind the current view position to an effect.
  13. * @param effect The effect to be bound
  14. * @param scene The scene the eyes position is used from
  15. */
  16. public static BindEyePosition(effect: Effect, scene: Scene): void {
  17. if (scene._forcedViewPosition) {
  18. effect.setVector3("vEyePosition", scene._forcedViewPosition);
  19. return;
  20. }
  21. effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera!.globalPosition);
  22. }
  23. /**
  24. * Helps preparing the defines values about the UVs in used in the effect.
  25. * UVs are shared as much as we can accross chanels in the shaders.
  26. * @param texture The texture we are preparing the UVs for
  27. * @param defines The defines to update
  28. * @param key The chanel key "diffuse", "specular"... used in the shader
  29. */
  30. public static PrepareDefinesForMergedUV(texture: BaseTexture, defines: any, key: string): void {
  31. defines._needUVs = true;
  32. defines[key] = true;
  33. if (texture.getTextureMatrix().isIdentity(true)) {
  34. defines[key + "DIRECTUV"] = texture.coordinatesIndex + 1;
  35. if (texture.coordinatesIndex === 0) {
  36. defines["MAINUV1"] = true;
  37. } else {
  38. defines["MAINUV2"] = true;
  39. }
  40. } else {
  41. defines[key + "DIRECTUV"] = 0;
  42. }
  43. }
  44. /**
  45. * Binds a texture matrix value to its corrsponding uniform
  46. * @param texture The texture to bind the matrix for
  47. * @param uniformBuffer The uniform buffer receivin the data
  48. * @param key The chanel key "diffuse", "specular"... used in the shader
  49. */
  50. public static BindTextureMatrix(texture: BaseTexture, uniformBuffer: UniformBuffer, key: string): void {
  51. var matrix = texture.getTextureMatrix();
  52. if (!matrix.isIdentity(true)) {
  53. uniformBuffer.updateMatrix(key + "Matrix", matrix);
  54. }
  55. }
  56. /**
  57. * Helper used to prepare the list of defines associated with misc. values for shader compilation
  58. * @param mesh defines the current mesh
  59. * @param scene defines the current scene
  60. * @param useLogarithmicDepth defines if logarithmic depth has to be turned on
  61. * @param pointsCloud defines if point cloud rendering has to be turned on
  62. * @param fogEnabled defines if fog has to be turned on
  63. * @param alphaTest defines if alpha testing has to be turned on
  64. * @param defines defines the current list of defines
  65. */
  66. public static PrepareDefinesForMisc(mesh: AbstractMesh, scene: Scene, useLogarithmicDepth: boolean, pointsCloud: boolean, fogEnabled: boolean, alphaTest: boolean, defines: any): void {
  67. if (defines._areMiscDirty) {
  68. defines["LOGARITHMICDEPTH"] = useLogarithmicDepth;
  69. defines["POINTSIZE"] = pointsCloud;
  70. defines["FOG"] = (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && fogEnabled);
  71. defines["NONUNIFORMSCALING"] = mesh.nonUniformScaling;
  72. defines["ALPHATEST"] = alphaTest;
  73. }
  74. }
  75. /**
  76. * Helper used to prepare the list of defines associated with frame values for shader compilation
  77. * @param scene defines the current scene
  78. * @param engine defines the current engine
  79. * @param defines specifies the list of active defines
  80. * @param useInstances defines if instances have to be turned on
  81. * @param useClipPlane defines if clip plane have to be turned on
  82. */
  83. public static PrepareDefinesForFrameBoundValues(scene: Scene, engine: Engine, defines: any, useInstances: boolean, useClipPlane: Nullable<boolean> = null): void {
  84. var changed = false;
  85. if (useClipPlane == null) {
  86. useClipPlane = (scene.clipPlane !== undefined && scene.clipPlane !== null);
  87. }
  88. if (defines["CLIPPLANE"] !== useClipPlane) {
  89. defines["CLIPPLANE"] = useClipPlane;
  90. changed = true;
  91. }
  92. if (defines["DEPTHPREPASS"] !== !engine.getColorWrite()) {
  93. defines["DEPTHPREPASS"] = !defines["DEPTHPREPASS"];
  94. changed = true;
  95. }
  96. if (defines["INSTANCES"] !== useInstances) {
  97. defines["INSTANCES"] = useInstances;
  98. changed = true;
  99. }
  100. if (changed) {
  101. defines.markAsUnprocessed();
  102. }
  103. }
  104. /**
  105. * Prepares the defines used in the shader depending on the attributes data available in the mesh
  106. * @param mesh The mesh containing the geometry data we will draw
  107. * @param defines The defines to update
  108. * @param useVertexColor Precise whether vertex colors should be used or not (override mesh info)
  109. * @param useBones Precise whether bones should be used or not (override mesh info)
  110. * @param useMorphTargets Precise whether morph targets should be used or not (override mesh info)
  111. * @param useVertexAlpha Precise whether vertex alpha should be used or not (override mesh info)
  112. * @returns false if defines are considered not dirty and have not been checked
  113. */
  114. public static PrepareDefinesForAttributes(mesh: AbstractMesh, defines: any, useVertexColor: boolean, useBones: boolean, useMorphTargets = false, useVertexAlpha = true): boolean {
  115. if (!defines._areAttributesDirty && defines._needNormals === defines._normals && defines._needUVs === defines._uvs) {
  116. return false;
  117. }
  118. defines._normals = defines._needNormals;
  119. defines._uvs = defines._needUVs;
  120. defines["NORMAL"] = (defines._needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind));
  121. if (defines._needNormals && mesh.isVerticesDataPresent(VertexBuffer.TangentKind)) {
  122. defines["TANGENT"] = true;
  123. }
  124. if (defines._needUVs) {
  125. defines["UV1"] = mesh.isVerticesDataPresent(VertexBuffer.UVKind);
  126. defines["UV2"] = mesh.isVerticesDataPresent(VertexBuffer.UV2Kind);
  127. } else {
  128. defines["UV1"] = false;
  129. defines["UV2"] = false;
  130. }
  131. if (useVertexColor) {
  132. var hasVertexColors = mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind);
  133. defines["VERTEXCOLOR"] = hasVertexColors;
  134. defines["VERTEXALPHA"] = mesh.hasVertexAlpha && hasVertexColors && useVertexAlpha;
  135. }
  136. if (useBones) {
  137. if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  138. defines["NUM_BONE_INFLUENCERS"] = mesh.numBoneInfluencers;
  139. defines["BonesPerMesh"] = (mesh.skeleton.bones.length + 1);
  140. } else {
  141. defines["NUM_BONE_INFLUENCERS"] = 0;
  142. defines["BonesPerMesh"] = 0;
  143. }
  144. }
  145. if (useMorphTargets) {
  146. var manager = (<Mesh>mesh).morphTargetManager;
  147. if (manager) {
  148. defines["MORPHTARGETS_TANGENT"] = manager.supportsTangents && defines["TANGENT"];
  149. defines["MORPHTARGETS_NORMAL"] = manager.supportsNormals && defines["NORMAL"];
  150. defines["MORPHTARGETS"] = (manager.numInfluencers > 0);
  151. defines["NUM_MORPH_INFLUENCERS"] = manager.numInfluencers;
  152. } else {
  153. defines["MORPHTARGETS_TANGENT"] = false;
  154. defines["MORPHTARGETS_NORMAL"] = false;
  155. defines["MORPHTARGETS"] = false;
  156. defines["NUM_MORPH_INFLUENCERS"] = 0;
  157. }
  158. }
  159. return true;
  160. }
  161. /**
  162. * Prepares the defines related to the light information passed in parameter
  163. * @param scene The scene we are intending to draw
  164. * @param mesh The mesh the effect is compiling for
  165. * @param defines The defines to update
  166. * @param specularSupported Specifies whether specular is supported or not (override lights data)
  167. * @param maxSimultaneousLights Specfies how manuy lights can be added to the effect at max
  168. * @param disableLighting Specifies whether the lighting is disabled (override scene and light)
  169. * @returns true if normals will be required for the rest of the effect
  170. */
  171. public static PrepareDefinesForLights(scene: Scene, mesh: AbstractMesh, defines: any, specularSupported: boolean, maxSimultaneousLights = 4, disableLighting = false): boolean {
  172. if (!defines._areLightsDirty) {
  173. return defines._needNormals;
  174. }
  175. var lightIndex = 0;
  176. var needNormals = false;
  177. var needRebuild = false;
  178. var lightmapMode = false;
  179. var shadowEnabled = false;
  180. var specularEnabled = false;
  181. if (scene.lightsEnabled && !disableLighting) {
  182. for (var light of mesh._lightSources) {
  183. needNormals = true;
  184. if (defines["LIGHT" + lightIndex] === undefined) {
  185. needRebuild = true;
  186. }
  187. defines["LIGHT" + lightIndex] = true;
  188. defines["SPOTLIGHT" + lightIndex] = false;
  189. defines["HEMILIGHT" + lightIndex] = false;
  190. defines["POINTLIGHT" + lightIndex] = false;
  191. defines["DIRLIGHT" + lightIndex] = false;
  192. light.prepareLightSpecificDefines(defines, lightIndex);
  193. // FallOff.
  194. defines["LIGHT_FALLOFF_PHYSICAL" + lightIndex] = false;
  195. defines["LIGHT_FALLOFF_GLTF" + lightIndex] = false;
  196. defines["LIGHT_FALLOFF_STANDARD" + lightIndex] = false;
  197. switch (light.falloffType) {
  198. case Light.FALLOFF_GLTF:
  199. defines["LIGHT_FALLOFF_GLTF" + lightIndex] = true;
  200. break;
  201. case Light.FALLOFF_PHYSICAL:
  202. defines["LIGHT_FALLOFF_PHYSICAL" + lightIndex] = true;
  203. break;
  204. case Light.FALLOFF_STANDARD:
  205. defines["LIGHT_FALLOFF_STANDARD" + lightIndex] = true;
  206. break;
  207. }
  208. // Specular
  209. if (specularSupported && !light.specular.equalsFloats(0, 0, 0)) {
  210. specularEnabled = true;
  211. }
  212. // Shadows
  213. defines["SHADOW" + lightIndex] = false;
  214. defines["SHADOWPCF" + lightIndex] = false;
  215. defines["SHADOWPCSS" + lightIndex] = false;
  216. defines["SHADOWPOISSON" + lightIndex] = false;
  217. defines["SHADOWESM" + lightIndex] = false;
  218. defines["SHADOWCUBE" + lightIndex] = false;
  219. defines["SHADOWLOWQUALITY" + lightIndex] = false;
  220. defines["SHADOWMEDIUMQUALITY" + lightIndex] = false;
  221. if (mesh && mesh.receiveShadows && scene.shadowsEnabled && light.shadowEnabled) {
  222. var shadowGenerator = light.getShadowGenerator();
  223. if (shadowGenerator) {
  224. const shadowMap = shadowGenerator.getShadowMap();
  225. if (shadowMap) {
  226. if (shadowMap.renderList && shadowMap.renderList.length > 0) {
  227. shadowEnabled = true;
  228. shadowGenerator.prepareDefines(defines, lightIndex);
  229. }
  230. }
  231. }
  232. }
  233. if (light.lightmapMode != Light.LIGHTMAP_DEFAULT) {
  234. lightmapMode = true;
  235. defines["LIGHTMAPEXCLUDED" + lightIndex] = true;
  236. defines["LIGHTMAPNOSPECULAR" + lightIndex] = (light.lightmapMode == Light.LIGHTMAP_SHADOWSONLY);
  237. } else {
  238. defines["LIGHTMAPEXCLUDED" + lightIndex] = false;
  239. defines["LIGHTMAPNOSPECULAR" + lightIndex] = false;
  240. }
  241. lightIndex++;
  242. if (lightIndex === maxSimultaneousLights)
  243. break;
  244. }
  245. }
  246. defines["SPECULARTERM"] = specularEnabled;
  247. defines["SHADOWS"] = shadowEnabled;
  248. // Resetting all other lights if any
  249. for (var index = lightIndex; index < maxSimultaneousLights; index++) {
  250. if (defines["LIGHT" + index] !== undefined) {
  251. defines["LIGHT" + index] = false;
  252. defines["HEMILIGHT" + lightIndex] = false;
  253. defines["POINTLIGHT" + lightIndex] = false;
  254. defines["DIRLIGHT" + lightIndex] = false;
  255. defines["SPOTLIGHT" + lightIndex] = false;
  256. defines["SHADOW" + lightIndex] = false;
  257. }
  258. }
  259. let caps = scene.getEngine().getCaps();
  260. if (defines["SHADOWFLOAT"] === undefined) {
  261. needRebuild = true;
  262. }
  263. defines["SHADOWFLOAT"] = shadowEnabled &&
  264. ((caps.textureFloatRender && caps.textureFloatLinearFiltering) ||
  265. (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering));
  266. defines["LIGHTMAPEXCLUDED"] = lightmapMode;
  267. if (needRebuild) {
  268. defines.rebuild();
  269. }
  270. return needNormals;
  271. }
  272. /**
  273. * Prepares the uniforms and samplers list to be used in the effect. This can automatically remove from the list uniforms
  274. * that won t be acctive due to defines being turned off.
  275. * @param uniformsListOrOptions The uniform names to prepare or an EffectCreationOptions containing the liist and extra information
  276. * @param samplersList The samplers list
  277. * @param defines The defines helping in the list generation
  278. * @param maxSimultaneousLights The maximum number of simultanous light allowed in the effect
  279. */
  280. public static PrepareUniformsAndSamplersList(uniformsListOrOptions: string[] | EffectCreationOptions, samplersList?: string[], defines?: any, maxSimultaneousLights = 4): void {
  281. let uniformsList: string[];
  282. let uniformBuffersList: Nullable<string[]> = null;
  283. if ((<EffectCreationOptions>uniformsListOrOptions).uniformsNames) {
  284. var options = <EffectCreationOptions>uniformsListOrOptions;
  285. uniformsList = options.uniformsNames;
  286. uniformBuffersList = options.uniformBuffersNames;
  287. samplersList = options.samplers;
  288. defines = options.defines;
  289. maxSimultaneousLights = options.maxSimultaneousLights;
  290. } else {
  291. uniformsList = <string[]>uniformsListOrOptions;
  292. if (!samplersList) {
  293. samplersList = [];
  294. }
  295. }
  296. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  297. if (!defines["LIGHT" + lightIndex]) {
  298. break;
  299. }
  300. uniformsList.push(
  301. "vLightData" + lightIndex,
  302. "vLightDiffuse" + lightIndex,
  303. "vLightSpecular" + lightIndex,
  304. "vLightDirection" + lightIndex,
  305. "vLightFalloff" + lightIndex,
  306. "vLightGround" + lightIndex,
  307. "lightMatrix" + lightIndex,
  308. "shadowsInfo" + lightIndex,
  309. "depthValues" + lightIndex,
  310. );
  311. if (uniformBuffersList) {
  312. uniformBuffersList.push("Light" + lightIndex);
  313. }
  314. samplersList.push("shadowSampler" + lightIndex);
  315. samplersList.push("depthSampler" + lightIndex);
  316. if (defines["PROJECTEDLIGHTTEXTURE" + lightIndex]){
  317. samplersList.push("projectionLightSampler" + lightIndex,);
  318. uniformsList.push(
  319. "textureProjectionMatrix" + lightIndex,
  320. );
  321. }
  322. }
  323. if (defines["NUM_MORPH_INFLUENCERS"]) {
  324. uniformsList.push("morphTargetInfluences");
  325. }
  326. }
  327. /**
  328. * This helps decreasing rank by rank the shadow quality (0 being the highest rank and quality)
  329. * @param defines The defines to update while falling back
  330. * @param fallbacks The authorized effect fallbacks
  331. * @param maxSimultaneousLights The maximum number of lights allowed
  332. * @param rank the current rank of the Effect
  333. * @returns The newly affected rank
  334. */
  335. public static HandleFallbacksForShadows(defines: any, fallbacks: EffectFallbacks, maxSimultaneousLights = 4, rank = 0): number {
  336. let lightFallbackRank = 0;
  337. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  338. if (!defines["LIGHT" + lightIndex]) {
  339. break;
  340. }
  341. if (lightIndex > 0) {
  342. lightFallbackRank = rank + lightIndex;
  343. fallbacks.addFallback(lightFallbackRank, "LIGHT" + lightIndex);
  344. }
  345. if (!defines["SHADOWS"]) {
  346. if (defines["SHADOW" + lightIndex]) {
  347. fallbacks.addFallback(rank, "SHADOW" + lightIndex);
  348. }
  349. if (defines["SHADOWPCF" + lightIndex]) {
  350. fallbacks.addFallback(rank, "SHADOWPCF" + lightIndex);
  351. }
  352. if (defines["SHADOWPCSS" + lightIndex]) {
  353. fallbacks.addFallback(rank, "SHADOWPCSS" + lightIndex);
  354. }
  355. if (defines["SHADOWPOISSON" + lightIndex]) {
  356. fallbacks.addFallback(rank, "SHADOWPOISSON" + lightIndex);
  357. }
  358. if (defines["SHADOWESM" + lightIndex]) {
  359. fallbacks.addFallback(rank, "SHADOWESM" + lightIndex);
  360. }
  361. }
  362. }
  363. return lightFallbackRank++;
  364. }
  365. /**
  366. * Prepares the list of attributes required for morph targets according to the effect defines.
  367. * @param attribs The current list of supported attribs
  368. * @param mesh The mesh to prepare the morph targets attributes for
  369. * @param defines The current Defines of the effect
  370. */
  371. public static PrepareAttributesForMorphTargets(attribs: string[], mesh: AbstractMesh, defines: any): void {
  372. var influencers = defines["NUM_MORPH_INFLUENCERS"];
  373. if (influencers > 0 && Engine.LastCreatedEngine) {
  374. var maxAttributesCount = Engine.LastCreatedEngine.getCaps().maxVertexAttribs;
  375. var manager = (<Mesh>mesh).morphTargetManager;
  376. var normal = manager && manager.supportsNormals && defines["NORMAL"];
  377. var tangent = manager && manager.supportsTangents && defines["TANGENT"];
  378. for (var index = 0; index < influencers; index++) {
  379. attribs.push(VertexBuffer.PositionKind + index);
  380. if (normal) {
  381. attribs.push(VertexBuffer.NormalKind + index);
  382. }
  383. if (tangent) {
  384. attribs.push(VertexBuffer.TangentKind + index);
  385. }
  386. if (attribs.length > maxAttributesCount) {
  387. Tools.Error("Cannot add more vertex attributes for mesh " + mesh.name);
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * Prepares the list of attributes required for bones according to the effect defines.
  394. * @param attribs The current list of supported attribs
  395. * @param mesh The mesh to prepare the bones attributes for
  396. * @param defines The current Defines of the effect
  397. * @param fallbacks The current efffect fallback strategy
  398. */
  399. public static PrepareAttributesForBones(attribs: string[], mesh: AbstractMesh, defines: any, fallbacks: EffectFallbacks): void {
  400. if (defines["NUM_BONE_INFLUENCERS"] > 0) {
  401. fallbacks.addCPUSkinningFallback(0, mesh);
  402. attribs.push(VertexBuffer.MatricesIndicesKind);
  403. attribs.push(VertexBuffer.MatricesWeightsKind);
  404. if (defines["NUM_BONE_INFLUENCERS"] > 4) {
  405. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  406. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  407. }
  408. }
  409. }
  410. /**
  411. * Prepares the list of attributes required for instances according to the effect defines.
  412. * @param attribs The current list of supported attribs
  413. * @param defines The current Defines of the effect
  414. */
  415. public static PrepareAttributesForInstances(attribs: string[], defines: any): void {
  416. if (defines["INSTANCES"]) {
  417. attribs.push("world0");
  418. attribs.push("world1");
  419. attribs.push("world2");
  420. attribs.push("world3");
  421. }
  422. }
  423. /**
  424. * Binds the light shadow information to the effect for the given mesh.
  425. * @param light The light containing the generator
  426. * @param scene The scene the lights belongs to
  427. * @param mesh The mesh we are binding the information to render
  428. * @param lightIndex The light index in the effect used to render the mesh
  429. * @param effect The effect we are binding the data to
  430. */
  431. public static BindLightShadow(light: Light, scene: Scene, mesh: AbstractMesh, lightIndex: string, effect: Effect): void {
  432. if (light.shadowEnabled && mesh.receiveShadows) {
  433. var shadowGenerator = light.getShadowGenerator();
  434. if (shadowGenerator) {
  435. shadowGenerator.bindShadowLight(lightIndex, effect);
  436. }
  437. }
  438. }
  439. /**
  440. * Binds the light information to the effect.
  441. * @param light The light containing the generator
  442. * @param effect The effect we are binding the data to
  443. * @param lightIndex The light index in the effect used to render
  444. */
  445. public static BindLightProperties(light: Light, effect: Effect, lightIndex: number): void {
  446. light.transferToEffect(effect, lightIndex + "");
  447. }
  448. /**
  449. * Binds the lights information from the scene to the effect for the given mesh.
  450. * @param scene The scene the lights belongs to
  451. * @param mesh The mesh we are binding the information to render
  452. * @param effect The effect we are binding the data to
  453. * @param defines The generated defines for the effect
  454. * @param maxSimultaneousLights The maximum number of light that can be bound to the effect
  455. * @param usePhysicalLightFalloff Specifies whether the light falloff is defined physically or not
  456. */
  457. public static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: any, maxSimultaneousLights = 4, usePhysicalLightFalloff = false): void {
  458. let len = Math.min(mesh._lightSources.length, maxSimultaneousLights);
  459. for (var i = 0; i < len; i++) {
  460. let light = mesh._lightSources[i];
  461. let iAsString = i.toString();
  462. let scaledIntensity = light.getScaledIntensity();
  463. light._uniformBuffer.bindToEffect(effect, "Light" + i);
  464. MaterialHelper.BindLightProperties(light, effect, i);
  465. light.diffuse.scaleToRef(scaledIntensity, Tmp.Color3[0]);
  466. light._uniformBuffer.updateColor4("vLightDiffuse", Tmp.Color3[0], usePhysicalLightFalloff ? light.radius : light.range, iAsString);
  467. if (defines["SPECULARTERM"]) {
  468. light.specular.scaleToRef(scaledIntensity, Tmp.Color3[1]);
  469. light._uniformBuffer.updateColor3("vLightSpecular", Tmp.Color3[1], iAsString);
  470. }
  471. // Shadows
  472. if (scene.shadowsEnabled) {
  473. this.BindLightShadow(light, scene, mesh, iAsString, effect);
  474. }
  475. light._uniformBuffer.update();
  476. }
  477. }
  478. /**
  479. * Binds the fog information from the scene to the effect for the given mesh.
  480. * @param scene The scene the lights belongs to
  481. * @param mesh The mesh we are binding the information to render
  482. * @param effect The effect we are binding the data to
  483. */
  484. public static BindFogParameters(scene: Scene, mesh: AbstractMesh, effect: Effect): void {
  485. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  486. effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  487. effect.setColor3("vFogColor", scene.fogColor);
  488. }
  489. }
  490. /**
  491. * Binds the bones information from the mesh to the effect.
  492. * @param mesh The mesh we are binding the information to render
  493. * @param effect The effect we are binding the data to
  494. */
  495. public static BindBonesParameters(mesh?: AbstractMesh, effect?: Effect): void {
  496. if (!effect || !mesh) {
  497. return;
  498. }
  499. if (mesh.computeBonesUsingShaders && effect._bonesComputationForcedToCPU) {
  500. mesh.computeBonesUsingShaders = false;
  501. }
  502. if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  503. var matrices = mesh.skeleton.getTransformMatrices(mesh);
  504. if (matrices) {
  505. effect.setMatrices("mBones", matrices);
  506. }
  507. }
  508. }
  509. /**
  510. * Binds the morph targets information from the mesh to the effect.
  511. * @param abstractMesh The mesh we are binding the information to render
  512. * @param effect The effect we are binding the data to
  513. */
  514. public static BindMorphTargetParameters(abstractMesh: AbstractMesh, effect: Effect): void {
  515. let manager = (<Mesh>abstractMesh).morphTargetManager;
  516. if (!abstractMesh || !manager) {
  517. return;
  518. }
  519. effect.setFloatArray("morphTargetInfluences", manager.influences);
  520. }
  521. /**
  522. * Binds the logarithmic depth information from the scene to the effect for the given defines.
  523. * @param defines The generated defines used in the effect
  524. * @param effect The effect we are binding the data to
  525. * @param scene The scene we are willing to render with logarithmic scale for
  526. */
  527. public static BindLogDepth(defines: any, effect: Effect, scene: Scene): void {
  528. if (defines["LOGARITHMICDEPTH"]) {
  529. effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log((<Camera>scene.activeCamera).maxZ + 1.0) / Math.LN2));
  530. }
  531. }
  532. /**
  533. * Binds the clip plane information from the scene to the effect.
  534. * @param scene The scene the clip plane information are extracted from
  535. * @param effect The effect we are binding the data to
  536. */
  537. public static BindClipPlane(effect: Effect, scene: Scene): void {
  538. if (scene.clipPlane) {
  539. var clipPlane = scene.clipPlane;
  540. effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  541. }
  542. }
  543. }
  544. }