babylon.materialHelper.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. var type;
  193. if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
  194. type = "SPOTLIGHT" + lightIndex;
  195. let spotLight = light as SpotLight;
  196. defines["PROJECTEDLIGHTTEXTURE" + lightIndex] = spotLight.projectionTexture ? true : false;
  197. } else if (light.getTypeID() === Light.LIGHTTYPEID_HEMISPHERICLIGHT) {
  198. type = "HEMILIGHT" + lightIndex;
  199. } else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
  200. type = "POINTLIGHT" + lightIndex;
  201. } else {
  202. type = "DIRLIGHT" + lightIndex;
  203. }
  204. defines[type] = true;
  205. // Specular
  206. if (specularSupported && !light.specular.equalsFloats(0, 0, 0)) {
  207. specularEnabled = true;
  208. }
  209. // Shadows
  210. defines["SHADOW" + lightIndex] = false;
  211. defines["SHADOWPCF" + lightIndex] = false;
  212. defines["SHADOWPCSS" + lightIndex] = false;
  213. defines["SHADOWPOISSON" + lightIndex] = false;
  214. defines["SHADOWESM" + lightIndex] = false;
  215. defines["SHADOWCUBE" + lightIndex] = false;
  216. defines["SHADOWLOWQUALITY" + lightIndex] = false;
  217. defines["SHADOWMEDIUMQUALITY" + lightIndex] = false;
  218. if (mesh && mesh.receiveShadows && scene.shadowsEnabled && light.shadowEnabled) {
  219. var shadowGenerator = light.getShadowGenerator();
  220. if (shadowGenerator) {
  221. shadowEnabled = true;
  222. shadowGenerator.prepareDefines(defines, lightIndex);
  223. }
  224. }
  225. if (light.lightmapMode != Light.LIGHTMAP_DEFAULT) {
  226. lightmapMode = true;
  227. defines["LIGHTMAPEXCLUDED" + lightIndex] = true;
  228. defines["LIGHTMAPNOSPECULAR" + lightIndex] = (light.lightmapMode == Light.LIGHTMAP_SHADOWSONLY);
  229. } else {
  230. defines["LIGHTMAPEXCLUDED" + lightIndex] = false;
  231. defines["LIGHTMAPNOSPECULAR" + lightIndex] = false;
  232. }
  233. lightIndex++;
  234. if (lightIndex === maxSimultaneousLights)
  235. break;
  236. }
  237. }
  238. defines["SPECULARTERM"] = specularEnabled;
  239. defines["SHADOWS"] = shadowEnabled;
  240. // Resetting all other lights if any
  241. for (var index = lightIndex; index < maxSimultaneousLights; index++) {
  242. if (defines["LIGHT" + index] !== undefined) {
  243. defines["LIGHT" + index] = false;
  244. defines["HEMILIGHT" + lightIndex] = false;
  245. defines["POINTLIGHT" + lightIndex] = false;
  246. defines["DIRLIGHT" + lightIndex] = false;
  247. defines["SPOTLIGHT" + lightIndex] = false;
  248. defines["SHADOW" + lightIndex] = false;
  249. }
  250. }
  251. let caps = scene.getEngine().getCaps();
  252. if (defines["SHADOWFLOAT"] === undefined) {
  253. needRebuild = true;
  254. }
  255. defines["SHADOWFLOAT"] = shadowEnabled &&
  256. ((caps.textureFloatRender && caps.textureFloatLinearFiltering) ||
  257. (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering));
  258. defines["LIGHTMAPEXCLUDED"] = lightmapMode;
  259. if (needRebuild) {
  260. defines.rebuild();
  261. }
  262. return needNormals;
  263. }
  264. /**
  265. * Prepares the uniforms and samplers list to be used in the effect. This can automatically remove from the list uniforms
  266. * that won t be acctive due to defines being turned off.
  267. * @param uniformsListOrOptions The uniform names to prepare or an EffectCreationOptions containing the liist and extra information
  268. * @param samplersList The samplers list
  269. * @param defines The defines helping in the list generation
  270. * @param maxSimultaneousLights The maximum number of simultanous light allowed in the effect
  271. */
  272. public static PrepareUniformsAndSamplersList(uniformsListOrOptions: string[] | EffectCreationOptions, samplersList?: string[], defines?: any, maxSimultaneousLights = 4): void {
  273. let uniformsList: string[];
  274. let uniformBuffersList: Nullable<string[]> = null;
  275. if ((<EffectCreationOptions>uniformsListOrOptions).uniformsNames) {
  276. var options = <EffectCreationOptions>uniformsListOrOptions;
  277. uniformsList = options.uniformsNames;
  278. uniformBuffersList = options.uniformBuffersNames;
  279. samplersList = options.samplers;
  280. defines = options.defines;
  281. maxSimultaneousLights = options.maxSimultaneousLights;
  282. } else {
  283. uniformsList = <string[]>uniformsListOrOptions;
  284. if (!samplersList) {
  285. samplersList = [];
  286. }
  287. }
  288. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  289. if (!defines["LIGHT" + lightIndex]) {
  290. break;
  291. }
  292. uniformsList.push(
  293. "vLightData" + lightIndex,
  294. "vLightDiffuse" + lightIndex,
  295. "vLightSpecular" + lightIndex,
  296. "vLightDirection" + lightIndex,
  297. "vLightGround" + lightIndex,
  298. "lightMatrix" + lightIndex,
  299. "shadowsInfo" + lightIndex,
  300. "depthValues" + lightIndex,
  301. );
  302. if (uniformBuffersList) {
  303. uniformBuffersList.push("Light" + lightIndex);
  304. }
  305. samplersList.push("shadowSampler" + lightIndex);
  306. samplersList.push("depthSampler" + lightIndex);
  307. if (defines["PROJECTEDLIGHTTEXTURE" + lightIndex]){
  308. samplersList.push("projectionLightSampler" + lightIndex,);
  309. uniformsList.push(
  310. "textureProjectionMatrix" + lightIndex,
  311. );
  312. }
  313. }
  314. if (defines["NUM_MORPH_INFLUENCERS"]) {
  315. uniformsList.push("morphTargetInfluences");
  316. }
  317. }
  318. /**
  319. * This helps decreasing rank by rank the shadow quality (0 being the highest rank and quality)
  320. * @param defines The defines to update while falling back
  321. * @param fallbacks The authorized effect fallbacks
  322. * @param maxSimultaneousLights The maximum number of lights allowed
  323. * @param rank the current rank of the Effect
  324. * @returns The newly affected rank
  325. */
  326. public static HandleFallbacksForShadows(defines: any, fallbacks: EffectFallbacks, maxSimultaneousLights = 4, rank = 0): number {
  327. let lightFallbackRank = 0;
  328. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  329. if (!defines["LIGHT" + lightIndex]) {
  330. break;
  331. }
  332. if (lightIndex > 0) {
  333. lightFallbackRank = rank + lightIndex;
  334. fallbacks.addFallback(lightFallbackRank, "LIGHT" + lightIndex);
  335. }
  336. if (!defines["SHADOWS"]) {
  337. if (defines["SHADOW" + lightIndex]) {
  338. fallbacks.addFallback(rank, "SHADOW" + lightIndex);
  339. }
  340. if (defines["SHADOWPCF" + lightIndex]) {
  341. fallbacks.addFallback(rank, "SHADOWPCF" + lightIndex);
  342. }
  343. if (defines["SHADOWPCSS" + lightIndex]) {
  344. fallbacks.addFallback(rank, "SHADOWPCSS" + lightIndex);
  345. }
  346. if (defines["SHADOWPOISSON" + lightIndex]) {
  347. fallbacks.addFallback(rank, "SHADOWPOISSON" + lightIndex);
  348. }
  349. if (defines["SHADOWESM" + lightIndex]) {
  350. fallbacks.addFallback(rank, "SHADOWESM" + lightIndex);
  351. }
  352. }
  353. }
  354. return lightFallbackRank++;
  355. }
  356. /**
  357. * Prepares the list of attributes required for morph targets according to the effect defines.
  358. * @param attribs The current list of supported attribs
  359. * @param mesh The mesh to prepare the morph targets attributes for
  360. * @param defines The current Defines of the effect
  361. */
  362. public static PrepareAttributesForMorphTargets(attribs: string[], mesh: AbstractMesh, defines: any): void {
  363. var influencers = defines["NUM_MORPH_INFLUENCERS"];
  364. if (influencers > 0 && Engine.LastCreatedEngine) {
  365. var maxAttributesCount = Engine.LastCreatedEngine.getCaps().maxVertexAttribs;
  366. var manager = (<Mesh>mesh).morphTargetManager;
  367. var normal = manager && manager.supportsNormals && defines["NORMAL"];
  368. var tangent = manager && manager.supportsTangents && defines["TANGENT"];
  369. for (var index = 0; index < influencers; index++) {
  370. attribs.push(VertexBuffer.PositionKind + index);
  371. if (normal) {
  372. attribs.push(VertexBuffer.NormalKind + index);
  373. }
  374. if (tangent) {
  375. attribs.push(VertexBuffer.TangentKind + index);
  376. }
  377. if (attribs.length > maxAttributesCount) {
  378. Tools.Error("Cannot add more vertex attributes for mesh " + mesh.name);
  379. }
  380. }
  381. }
  382. }
  383. /**
  384. * Prepares the list of attributes required for bones according to the effect defines.
  385. * @param attribs The current list of supported attribs
  386. * @param mesh The mesh to prepare the bones attributes for
  387. * @param defines The current Defines of the effect
  388. * @param fallbacks The current efffect fallback strategy
  389. */
  390. public static PrepareAttributesForBones(attribs: string[], mesh: AbstractMesh, defines: any, fallbacks: EffectFallbacks): void {
  391. if (defines["NUM_BONE_INFLUENCERS"] > 0) {
  392. fallbacks.addCPUSkinningFallback(0, mesh);
  393. attribs.push(VertexBuffer.MatricesIndicesKind);
  394. attribs.push(VertexBuffer.MatricesWeightsKind);
  395. if (defines["NUM_BONE_INFLUENCERS"] > 4) {
  396. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  397. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  398. }
  399. }
  400. }
  401. /**
  402. * Prepares the list of attributes required for instances according to the effect defines.
  403. * @param attribs The current list of supported attribs
  404. * @param defines The current Defines of the effect
  405. */
  406. public static PrepareAttributesForInstances(attribs: string[], defines: any): void {
  407. if (defines["INSTANCES"]) {
  408. attribs.push("world0");
  409. attribs.push("world1");
  410. attribs.push("world2");
  411. attribs.push("world3");
  412. }
  413. }
  414. /**
  415. * Binds the light shadow information to the effect for the given mesh.
  416. * @param light The light containing the generator
  417. * @param scene The scene the lights belongs to
  418. * @param mesh The mesh we are binding the information to render
  419. * @param lightIndex The light index in the effect used to render the mesh
  420. * @param effect The effect we are binding the data to
  421. */
  422. public static BindLightShadow(light: Light, scene: Scene, mesh: AbstractMesh, lightIndex: string, effect: Effect): void {
  423. if (light.shadowEnabled && mesh.receiveShadows) {
  424. var shadowGenerator = light.getShadowGenerator();
  425. if (shadowGenerator) {
  426. shadowGenerator.bindShadowLight(lightIndex, effect);
  427. }
  428. }
  429. }
  430. /**
  431. * Binds the light information to the effect.
  432. * @param light The light containing the generator
  433. * @param effect The effect we are binding the data to
  434. * @param lightIndex The light index in the effect used to render
  435. */
  436. public static BindLightProperties(light: Light, effect: Effect, lightIndex: number): void {
  437. light.transferToEffect(effect, lightIndex + "");
  438. }
  439. /**
  440. * Binds the lights information from the scene to the effect for the given mesh.
  441. * @param scene The scene the lights belongs to
  442. * @param mesh The mesh we are binding the information to render
  443. * @param effect The effect we are binding the data to
  444. * @param defines The generated defines for the effect
  445. * @param maxSimultaneousLights The maximum number of light that can be bound to the effect
  446. * @param usePhysicalLightFalloff Specifies whether the light falloff is defined physically or not
  447. */
  448. public static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: any, maxSimultaneousLights = 4, usePhysicalLightFalloff = false): void {
  449. let len = Math.min(mesh._lightSources.length, maxSimultaneousLights);
  450. for (var i = 0; i < len; i++) {
  451. let light = mesh._lightSources[i];
  452. let iAsString = i.toString();
  453. let scaledIntensity = light.getScaledIntensity();
  454. light._uniformBuffer.bindToEffect(effect, "Light" + i);
  455. MaterialHelper.BindLightProperties(light, effect, i);
  456. light.diffuse.scaleToRef(scaledIntensity, Tmp.Color3[0]);
  457. light._uniformBuffer.updateColor4("vLightDiffuse", Tmp.Color3[0], usePhysicalLightFalloff ? light.radius : light.range, iAsString);
  458. if (defines["SPECULARTERM"]) {
  459. light.specular.scaleToRef(scaledIntensity, Tmp.Color3[1]);
  460. light._uniformBuffer.updateColor3("vLightSpecular", Tmp.Color3[1], iAsString);
  461. }
  462. // Shadows
  463. if (scene.shadowsEnabled) {
  464. this.BindLightShadow(light, scene, mesh, iAsString, effect);
  465. }
  466. light._uniformBuffer.update();
  467. }
  468. }
  469. /**
  470. * Binds the fog information from the scene to the effect for the given mesh.
  471. * @param scene The scene the lights belongs to
  472. * @param mesh The mesh we are binding the information to render
  473. * @param effect The effect we are binding the data to
  474. */
  475. public static BindFogParameters(scene: Scene, mesh: AbstractMesh, effect: Effect): void {
  476. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  477. effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  478. effect.setColor3("vFogColor", scene.fogColor);
  479. }
  480. }
  481. /**
  482. * Binds the bones information from the mesh to the effect.
  483. * @param mesh The mesh we are binding the information to render
  484. * @param effect The effect we are binding the data to
  485. */
  486. public static BindBonesParameters(mesh?: AbstractMesh, effect?: Effect): void {
  487. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  488. var matrices = mesh.skeleton.getTransformMatrices(mesh);
  489. if (matrices && effect) {
  490. effect.setMatrices("mBones", matrices);
  491. }
  492. }
  493. }
  494. /**
  495. * Binds the morph targets information from the mesh to the effect.
  496. * @param abstractMesh The mesh we are binding the information to render
  497. * @param effect The effect we are binding the data to
  498. */
  499. public static BindMorphTargetParameters(abstractMesh: AbstractMesh, effect: Effect): void {
  500. let manager = (<Mesh>abstractMesh).morphTargetManager;
  501. if (!abstractMesh || !manager) {
  502. return;
  503. }
  504. effect.setFloatArray("morphTargetInfluences", manager.influences);
  505. }
  506. /**
  507. * Binds the logarithmic depth information from the scene to the effect for the given defines.
  508. * @param defines The generated defines used in the effect
  509. * @param effect The effect we are binding the data to
  510. * @param scene The scene we are willing to render with logarithmic scale for
  511. */
  512. public static BindLogDepth(defines: any, effect: Effect, scene: Scene): void {
  513. if (defines["LOGARITHMICDEPTH"]) {
  514. effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log((<Camera>scene.activeCamera).maxZ + 1.0) / Math.LN2));
  515. }
  516. }
  517. /**
  518. * Binds the clip plane information from the scene to the effect.
  519. * @param scene The scene the clip plane information are extracted from
  520. * @param effect The effect we are binding the data to
  521. */
  522. public static BindClipPlane(effect: Effect, scene: Scene): void {
  523. if (scene.clipPlane) {
  524. var clipPlane = scene.clipPlane;
  525. effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  526. }
  527. }
  528. }
  529. }