babylon.standardMaterial.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. module BABYLON {
  2. var maxSimultaneousLights = 4;
  3. export class FresnelParameters {
  4. public isEnabled = true;
  5. public leftColor = Color3.White();
  6. public rightColor = Color3.Black();
  7. public bias = 0;
  8. public power = 1;
  9. }
  10. export class StandardMaterial extends Material {
  11. public diffuseTexture: BaseTexture;
  12. public ambientTexture: BaseTexture;
  13. public opacityTexture: BaseTexture;
  14. public reflectionTexture: BaseTexture;
  15. public emissiveTexture: BaseTexture;
  16. public specularTexture: BaseTexture;
  17. public bumpTexture: BaseTexture;
  18. public ambientColor = new BABYLON.Color3(0, 0, 0);
  19. public diffuseColor = new BABYLON.Color3(1, 1, 1);
  20. public specularColor = new BABYLON.Color3(1, 1, 1);
  21. public specularPower = 64;
  22. public emissiveColor = new BABYLON.Color3(0, 0, 0);
  23. public useAlphaFromDiffuseTexture = false;
  24. public useSpecularOverAlpha = true;
  25. public fogEnabled = true;
  26. public diffuseFresnelParameters: FresnelParameters;
  27. public opacityFresnelParameters: FresnelParameters;
  28. public reflectionFresnelParameters: FresnelParameters;
  29. public emissiveFresnelParameters: FresnelParameters;
  30. private _cachedDefines = null;
  31. private _renderTargets = new BABYLON.SmartArray<RenderTargetTexture>(16);
  32. private _worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  33. private _globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  34. private _scaledDiffuse = new BABYLON.Color3();
  35. private _scaledSpecular = new BABYLON.Color3();
  36. private _renderId: number;
  37. constructor(name: string, scene: Scene) {
  38. super(name, scene);
  39. this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
  40. this._renderTargets.reset();
  41. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  42. this._renderTargets.push(this.reflectionTexture);
  43. }
  44. return this._renderTargets;
  45. }
  46. }
  47. public needAlphaBlending(): boolean {
  48. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  49. }
  50. public needAlphaTesting(): boolean {
  51. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && !this.diffuseTexture.getAlphaFromRGB;
  52. }
  53. private _shouldUseAlphaFromDiffuseTexture(): boolean {
  54. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
  55. }
  56. public getAlphaTestTexture(): BaseTexture {
  57. return this.diffuseTexture;
  58. }
  59. // Methods
  60. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  61. if (this.checkReadyOnlyOnce) {
  62. if (this._wasPreviouslyReady) {
  63. return true;
  64. }
  65. }
  66. var scene = this.getScene();
  67. if (!this.checkReadyOnEveryCall) {
  68. if (this._renderId === scene.getRenderId()) {
  69. return true;
  70. }
  71. }
  72. var engine = scene.getEngine();
  73. var defines = [];
  74. var fallbacks = new EffectFallbacks();
  75. // Textures
  76. if (scene.texturesEnabled) {
  77. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  78. if (!this.diffuseTexture.isReady()) {
  79. return false;
  80. } else {
  81. defines.push("#define DIFFUSE");
  82. }
  83. }
  84. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  85. if (!this.ambientTexture.isReady()) {
  86. return false;
  87. } else {
  88. defines.push("#define AMBIENT");
  89. }
  90. }
  91. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  92. if (!this.opacityTexture.isReady()) {
  93. return false;
  94. } else {
  95. defines.push("#define OPACITY");
  96. if (this.opacityTexture.getAlphaFromRGB) {
  97. defines.push("#define OPACITYRGB");
  98. }
  99. }
  100. }
  101. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  102. if (!this.reflectionTexture.isReady()) {
  103. return false;
  104. } else {
  105. defines.push("#define REFLECTION");
  106. fallbacks.addFallback(0, "REFLECTION");
  107. }
  108. }
  109. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  110. if (!this.emissiveTexture.isReady()) {
  111. return false;
  112. } else {
  113. defines.push("#define EMISSIVE");
  114. }
  115. }
  116. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  117. if (!this.specularTexture.isReady()) {
  118. return false;
  119. } else {
  120. defines.push("#define SPECULAR");
  121. fallbacks.addFallback(0, "SPECULAR");
  122. }
  123. }
  124. }
  125. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  126. if (!this.bumpTexture.isReady()) {
  127. return false;
  128. } else {
  129. defines.push("#define BUMP");
  130. fallbacks.addFallback(0, "BUMP");
  131. }
  132. }
  133. // Effect
  134. if (this.useSpecularOverAlpha) {
  135. defines.push("#define SPECULAROVERALPHA");
  136. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  137. }
  138. if (scene.clipPlane) {
  139. defines.push("#define CLIPPLANE");
  140. }
  141. if (engine.getAlphaTesting()) {
  142. defines.push("#define ALPHATEST");
  143. }
  144. if (this._shouldUseAlphaFromDiffuseTexture()) {
  145. defines.push("#define ALPHAFROMDIFFUSE");
  146. }
  147. // Point size
  148. if (this.pointsCloud) {
  149. defines.push("#define POINTSIZE");
  150. }
  151. // Fog
  152. if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  153. defines.push("#define FOG");
  154. fallbacks.addFallback(1, "FOG");
  155. }
  156. var shadowsActivated = false;
  157. var lightIndex = 0;
  158. if (scene.lightsEnabled) {
  159. for (var index = 0; index < scene.lights.length; index++) {
  160. var light = scene.lights[index];
  161. if (!light.isEnabled()) {
  162. continue;
  163. }
  164. // Excluded check
  165. if (light._excludedMeshesIds.length > 0) {
  166. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  167. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  168. if (excludedMesh) {
  169. light.excludedMeshes.push(excludedMesh);
  170. }
  171. }
  172. light._excludedMeshesIds = [];
  173. }
  174. // Included check
  175. if (light._includedOnlyMeshesIds.length > 0) {
  176. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  177. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  178. if (includedOnlyMesh) {
  179. light.includedOnlyMeshes.push(includedOnlyMesh);
  180. }
  181. }
  182. light._includedOnlyMeshesIds = [];
  183. }
  184. if (!light.canAffectMesh(mesh)) {
  185. continue;
  186. }
  187. defines.push("#define LIGHT" + lightIndex);
  188. if (lightIndex > 0) {
  189. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  190. }
  191. var type;
  192. if (light instanceof BABYLON.SpotLight) {
  193. type = "#define SPOTLIGHT" + lightIndex;
  194. } else if (light instanceof BABYLON.HemisphericLight) {
  195. type = "#define HEMILIGHT" + lightIndex;
  196. } else {
  197. type = "#define POINTDIRLIGHT" + lightIndex;
  198. }
  199. defines.push(type);
  200. if (lightIndex > 0) {
  201. fallbacks.addFallback(lightIndex, type.replace("#define ", ""));
  202. }
  203. // Shadows
  204. if (scene.shadowsEnabled) {
  205. var shadowGenerator = light.getShadowGenerator();
  206. if (mesh && mesh.receiveShadows && shadowGenerator) {
  207. defines.push("#define SHADOW" + lightIndex);
  208. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  209. if (!shadowsActivated) {
  210. defines.push("#define SHADOWS");
  211. shadowsActivated = true;
  212. }
  213. if (shadowGenerator.useVarianceShadowMap) {
  214. defines.push("#define SHADOWVSM" + lightIndex);
  215. if (lightIndex > 0) {
  216. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  217. }
  218. }
  219. if (shadowGenerator.usePoissonSampling) {
  220. defines.push("#define SHADOWPCF" + lightIndex);
  221. if (lightIndex > 0) {
  222. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  223. }
  224. }
  225. }
  226. }
  227. lightIndex++;
  228. if (lightIndex == maxSimultaneousLights)
  229. break;
  230. }
  231. }
  232. // Fresnel
  233. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled ||
  234. this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  235. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled ||
  236. this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  237. var fresnelRank = 1;
  238. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  239. defines.push("#define DIFFUSEFRESNEL");
  240. fallbacks.addFallback(fresnelRank, "DIFFUSEFRESNEL");
  241. fresnelRank++;
  242. }
  243. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  244. defines.push("#define OPACITYFRESNEL");
  245. fallbacks.addFallback(fresnelRank, "OPACITYFRESNEL");
  246. fresnelRank++;
  247. }
  248. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  249. defines.push("#define REFLECTIONFRESNEL");
  250. fallbacks.addFallback(fresnelRank, "REFLECTIONFRESNEL");
  251. fresnelRank++;
  252. }
  253. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  254. defines.push("#define EMISSIVEFRESNEL");
  255. fallbacks.addFallback(fresnelRank, "EMISSIVEFRESNEL");
  256. fresnelRank++;
  257. }
  258. defines.push("#define FRESNEL");
  259. fallbacks.addFallback(fresnelRank - 1, "FRESNEL");
  260. }
  261. // Attribs
  262. var attribs = [BABYLON.VertexBuffer.PositionKind, BABYLON.VertexBuffer.NormalKind];
  263. if (mesh) {
  264. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  265. attribs.push(BABYLON.VertexBuffer.UVKind);
  266. defines.push("#define UV1");
  267. }
  268. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  269. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  270. defines.push("#define UV2");
  271. }
  272. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  273. attribs.push(BABYLON.VertexBuffer.ColorKind);
  274. defines.push("#define VERTEXCOLOR");
  275. if (mesh.hasVertexAlpha) {
  276. defines.push("#define VERTEXALPHA");
  277. }
  278. }
  279. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  280. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  281. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  282. defines.push("#define BONES");
  283. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  284. defines.push("#define BONES4");
  285. fallbacks.addFallback(0, "BONES4");
  286. }
  287. // Instances
  288. if (useInstances) {
  289. defines.push("#define INSTANCES");
  290. attribs.push("world0");
  291. attribs.push("world1");
  292. attribs.push("world2");
  293. attribs.push("world3");
  294. }
  295. }
  296. // Get correct effect
  297. var join = defines.join("\n");
  298. if (this._cachedDefines != join) {
  299. this._cachedDefines = join;
  300. // Legacy browser patch
  301. var shaderName = "default";
  302. if (!scene.getEngine().getCaps().standardDerivatives) {
  303. shaderName = "legacydefault";
  304. }
  305. this._effect = scene.getEngine().createEffect(shaderName,
  306. attribs,
  307. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  308. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  309. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  310. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  311. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  312. "vFogInfos", "vFogColor", "pointSize",
  313. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos",
  314. "mBones",
  315. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix",
  316. "darkness0", "darkness1", "darkness2", "darkness3",
  317. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor"
  318. ],
  319. ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler",
  320. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  321. ],
  322. join, fallbacks, this.onCompiled, this.onError);
  323. }
  324. if (!this._effect.isReady()) {
  325. return false;
  326. }
  327. this._renderId = scene.getRenderId();
  328. this._wasPreviouslyReady = true;
  329. return true;
  330. }
  331. public unbind(): void {
  332. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  333. this._effect.setTexture("reflection2DSampler", null);
  334. }
  335. }
  336. public bindOnlyWorldMatrix(world: Matrix): void {
  337. this._effect.setMatrix("world", world);
  338. }
  339. public bind(world: Matrix, mesh: Mesh): void {
  340. var scene = this.getScene();
  341. // Matrices
  342. this.bindOnlyWorldMatrix(world);
  343. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  344. // Bones
  345. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  346. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  347. }
  348. // Fresnel
  349. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  350. this._effect.setColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  351. this._effect.setColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  352. }
  353. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  354. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  355. }
  356. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  357. this._effect.setColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  358. this._effect.setColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  359. }
  360. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  361. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  362. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  363. }
  364. // Textures
  365. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  366. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  367. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  368. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  369. }
  370. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  371. this._effect.setTexture("ambientSampler", this.ambientTexture);
  372. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  373. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  374. }
  375. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  376. this._effect.setTexture("opacitySampler", this.opacityTexture);
  377. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  378. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  379. }
  380. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  381. if (this.reflectionTexture.isCube) {
  382. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  383. } else {
  384. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  385. }
  386. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  387. this._effect.setFloat3("vReflectionInfos", this.reflectionTexture.coordinatesMode, this.reflectionTexture.level, this.reflectionTexture.isCube ? 1 : 0);
  388. }
  389. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  390. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  391. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  392. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  393. }
  394. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  395. this._effect.setTexture("specularSampler", this.specularTexture);
  396. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  397. this._effect.setMatrix("specularMatrix", this.specularTexture.getTextureMatrix());
  398. }
  399. if (this.bumpTexture && scene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled) {
  400. this._effect.setTexture("bumpSampler", this.bumpTexture);
  401. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, this.bumpTexture.level);
  402. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  403. }
  404. // Colors
  405. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  406. this._effect.setVector3("vEyePosition", scene.activeCamera.position);
  407. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  408. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  409. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  410. this._effect.setColor3("vEmissiveColor", this.emissiveColor);
  411. if (scene.lightsEnabled) {
  412. var lightIndex = 0;
  413. for (var index = 0; index < scene.lights.length; index++) {
  414. var light = scene.lights[index];
  415. if (!light.isEnabled()) {
  416. continue;
  417. }
  418. if (!light.canAffectMesh(mesh)) {
  419. continue;
  420. }
  421. if (light instanceof BABYLON.PointLight) {
  422. // Point Light
  423. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  424. } else if (light instanceof BABYLON.DirectionalLight) {
  425. // Directional Light
  426. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  427. } else if (light instanceof BABYLON.SpotLight) {
  428. // Spot Light
  429. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  430. } else if (light instanceof BABYLON.HemisphericLight) {
  431. // Hemispheric Light
  432. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  433. }
  434. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  435. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  436. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  437. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  438. // Shadows
  439. if (scene.shadowsEnabled) {
  440. var shadowGenerator = light.getShadowGenerator();
  441. if (mesh.receiveShadows && shadowGenerator) {
  442. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  443. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMap());
  444. this._effect.setFloat("darkness" + lightIndex, shadowGenerator.getDarkness());
  445. }
  446. }
  447. lightIndex++;
  448. if (lightIndex == maxSimultaneousLights)
  449. break;
  450. }
  451. }
  452. if (scene.clipPlane) {
  453. var clipPlane = scene.clipPlane;
  454. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  455. }
  456. // View
  457. if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  458. this._effect.setMatrix("view", scene.getViewMatrix());
  459. }
  460. // Fog
  461. if (scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  462. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  463. this._effect.setColor3("vFogColor", scene.fogColor);
  464. }
  465. // Point size
  466. if (this.pointsCloud) {
  467. this._effect.setFloat("pointSize", this.pointSize);
  468. }
  469. }
  470. public getAnimatables(): IAnimatable[] {
  471. var results = [];
  472. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  473. results.push(this.diffuseTexture);
  474. }
  475. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  476. results.push(this.ambientTexture);
  477. }
  478. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  479. results.push(this.opacityTexture);
  480. }
  481. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  482. results.push(this.reflectionTexture);
  483. }
  484. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  485. results.push(this.emissiveTexture);
  486. }
  487. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  488. results.push(this.specularTexture);
  489. }
  490. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  491. results.push(this.bumpTexture);
  492. }
  493. return results;
  494. }
  495. public dispose(forceDisposeEffect?: boolean): void {
  496. if (this.diffuseTexture) {
  497. this.diffuseTexture.dispose();
  498. }
  499. if (this.ambientTexture) {
  500. this.ambientTexture.dispose();
  501. }
  502. if (this.opacityTexture) {
  503. this.opacityTexture.dispose();
  504. }
  505. if (this.reflectionTexture) {
  506. this.reflectionTexture.dispose();
  507. }
  508. if (this.emissiveTexture) {
  509. this.emissiveTexture.dispose();
  510. }
  511. if (this.specularTexture) {
  512. this.specularTexture.dispose();
  513. }
  514. if (this.bumpTexture) {
  515. this.bumpTexture.dispose();
  516. }
  517. super.dispose(forceDisposeEffect);
  518. }
  519. public clone(name: string): StandardMaterial {
  520. var newStandardMaterial = new BABYLON.StandardMaterial(name, this.getScene());
  521. // Base material
  522. newStandardMaterial.checkReadyOnEveryCall = this.checkReadyOnEveryCall;
  523. newStandardMaterial.alpha = this.alpha;
  524. newStandardMaterial.fillMode = this.fillMode;
  525. newStandardMaterial.backFaceCulling = this.backFaceCulling;
  526. // Standard material
  527. if (this.diffuseTexture && this.diffuseTexture.clone) {
  528. newStandardMaterial.diffuseTexture = this.diffuseTexture.clone();
  529. }
  530. if (this.ambientTexture && this.ambientTexture.clone) {
  531. newStandardMaterial.ambientTexture = this.ambientTexture.clone();
  532. }
  533. if (this.opacityTexture && this.opacityTexture.clone) {
  534. newStandardMaterial.opacityTexture = this.opacityTexture.clone();
  535. }
  536. if (this.reflectionTexture && this.reflectionTexture.clone) {
  537. newStandardMaterial.reflectionTexture = this.reflectionTexture.clone();
  538. }
  539. if (this.emissiveTexture && this.emissiveTexture.clone) {
  540. newStandardMaterial.emissiveTexture = this.emissiveTexture.clone();
  541. }
  542. if (this.specularTexture && this.specularTexture.clone) {
  543. newStandardMaterial.specularTexture = this.specularTexture.clone();
  544. }
  545. if (this.bumpTexture && this.bumpTexture.clone) {
  546. newStandardMaterial.bumpTexture = this.bumpTexture.clone();
  547. }
  548. newStandardMaterial.ambientColor = this.ambientColor.clone();
  549. newStandardMaterial.diffuseColor = this.diffuseColor.clone();
  550. newStandardMaterial.specularColor = this.specularColor.clone();
  551. newStandardMaterial.specularPower = this.specularPower;
  552. newStandardMaterial.emissiveColor = this.emissiveColor.clone();
  553. return newStandardMaterial;
  554. }
  555. // Statics
  556. // Flags used to enable or disable a type of texture for all Standard Materials
  557. public static DiffuseTextureEnabled = true;
  558. public static AmbientTextureEnabled = true;
  559. public static OpacityTextureEnabled = true;
  560. public static ReflectionTextureEnabled = true;
  561. public static EmissiveTextureEnabled = true;
  562. public static SpecularTextureEnabled = true;
  563. public static BumpTextureEnabled = true;
  564. }
  565. }