babylon.standardMaterial.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 Color3(0, 0, 0);
  19. public diffuseColor = new Color3(1, 1, 1);
  20. public specularColor = new Color3(1, 1, 1);
  21. public specularPower = 64;
  22. public emissiveColor = new 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 SmartArray<RenderTargetTexture>(16);
  32. private _worldViewProjectionMatrix = Matrix.Zero();
  33. private _globalAmbientColor = new Color3(0, 0, 0);
  34. private _scaledDiffuse = new Color3();
  35. private _scaledSpecular = new 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 && StandardMaterial.DiffuseTextureEnabled) {
  78. if (!this.diffuseTexture.isReady()) {
  79. return false;
  80. } else {
  81. defines.push("#define DIFFUSE");
  82. }
  83. }
  84. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  85. if (!this.ambientTexture.isReady()) {
  86. return false;
  87. } else {
  88. defines.push("#define AMBIENT");
  89. }
  90. }
  91. if (this.opacityTexture && 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 && 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 && StandardMaterial.EmissiveTextureEnabled) {
  110. if (!this.emissiveTexture.isReady()) {
  111. return false;
  112. } else {
  113. defines.push("#define EMISSIVE");
  114. }
  115. }
  116. if (this.specularTexture && 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 && 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 || scene.forcePointsCloud) {
  149. defines.push("#define POINTSIZE");
  150. }
  151. // Fog
  152. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== 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 SpotLight) {
  193. type = "#define SPOTLIGHT" + lightIndex;
  194. } else if (light instanceof 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. if (StandardMaterial.FresnelEnabled) {
  233. // Fresnel
  234. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled ||
  235. this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  236. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled ||
  237. this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  238. var fresnelRank = 1;
  239. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  240. defines.push("#define DIFFUSEFRESNEL");
  241. fallbacks.addFallback(fresnelRank, "DIFFUSEFRESNEL");
  242. fresnelRank++;
  243. }
  244. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  245. defines.push("#define OPACITYFRESNEL");
  246. fallbacks.addFallback(fresnelRank, "OPACITYFRESNEL");
  247. fresnelRank++;
  248. }
  249. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  250. defines.push("#define REFLECTIONFRESNEL");
  251. fallbacks.addFallback(fresnelRank, "REFLECTIONFRESNEL");
  252. fresnelRank++;
  253. }
  254. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  255. defines.push("#define EMISSIVEFRESNEL");
  256. fallbacks.addFallback(fresnelRank, "EMISSIVEFRESNEL");
  257. fresnelRank++;
  258. }
  259. defines.push("#define FRESNEL");
  260. fallbacks.addFallback(fresnelRank - 1, "FRESNEL");
  261. }
  262. }
  263. // Attribs
  264. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  265. if (mesh) {
  266. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  267. attribs.push(VertexBuffer.UVKind);
  268. defines.push("#define UV1");
  269. }
  270. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  271. attribs.push(VertexBuffer.UV2Kind);
  272. defines.push("#define UV2");
  273. }
  274. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  275. attribs.push(VertexBuffer.ColorKind);
  276. defines.push("#define VERTEXCOLOR");
  277. if (mesh.hasVertexAlpha) {
  278. defines.push("#define VERTEXALPHA");
  279. }
  280. }
  281. if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  282. attribs.push(VertexBuffer.MatricesIndicesKind);
  283. attribs.push(VertexBuffer.MatricesWeightsKind);
  284. defines.push("#define BONES");
  285. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  286. defines.push("#define BONES4");
  287. fallbacks.addFallback(0, "BONES4");
  288. }
  289. // Instances
  290. if (useInstances) {
  291. defines.push("#define INSTANCES");
  292. attribs.push("world0");
  293. attribs.push("world1");
  294. attribs.push("world2");
  295. attribs.push("world3");
  296. }
  297. }
  298. // Get correct effect
  299. var join = defines.join("\n");
  300. if (this._cachedDefines !== join) {
  301. this._cachedDefines = join;
  302. scene.resetCachedMaterial();
  303. // Legacy browser patch
  304. var shaderName = "default";
  305. if (!scene.getEngine().getCaps().standardDerivatives) {
  306. shaderName = "legacydefault";
  307. }
  308. this._effect = scene.getEngine().createEffect(shaderName,
  309. attribs,
  310. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  311. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  312. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  313. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  314. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  315. "vFogInfos", "vFogColor", "pointSize",
  316. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos",
  317. "mBones",
  318. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix",
  319. "darkness0", "darkness1", "darkness2", "darkness3",
  320. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor"
  321. ],
  322. ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler",
  323. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  324. ],
  325. join, fallbacks, this.onCompiled, this.onError);
  326. }
  327. if (!this._effect.isReady()) {
  328. return false;
  329. }
  330. this._renderId = scene.getRenderId();
  331. this._wasPreviouslyReady = true;
  332. return true;
  333. }
  334. public unbind(): void {
  335. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  336. this._effect.setTexture("reflection2DSampler", null);
  337. }
  338. }
  339. public bindOnlyWorldMatrix(world: Matrix): void {
  340. this._effect.setMatrix("world", world);
  341. }
  342. public bind(world: Matrix, mesh: Mesh): void {
  343. var scene = this.getScene();
  344. // Matrices
  345. this.bindOnlyWorldMatrix(world);
  346. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  347. // Bones
  348. if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  349. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  350. }
  351. if (scene.getCachedMaterial() !== this) {
  352. if (StandardMaterial.FresnelEnabled) {
  353. // Fresnel
  354. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  355. this._effect.setColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  356. this._effect.setColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  357. }
  358. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  359. this._effect.setColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  360. }
  361. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  362. this._effect.setColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  363. this._effect.setColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  364. }
  365. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  366. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  367. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  368. }
  369. }
  370. // Textures
  371. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  372. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  373. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  374. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  375. }
  376. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  377. this._effect.setTexture("ambientSampler", this.ambientTexture);
  378. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  379. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  380. }
  381. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  382. this._effect.setTexture("opacitySampler", this.opacityTexture);
  383. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  384. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  385. }
  386. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  387. if (this.reflectionTexture.isCube) {
  388. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  389. } else {
  390. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  391. }
  392. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  393. this._effect.setFloat3("vReflectionInfos", this.reflectionTexture.coordinatesMode, this.reflectionTexture.level, this.reflectionTexture.isCube ? 1 : 0);
  394. }
  395. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  396. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  397. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  398. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  399. }
  400. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  401. this._effect.setTexture("specularSampler", this.specularTexture);
  402. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  403. this._effect.setMatrix("specularMatrix", this.specularTexture.getTextureMatrix());
  404. }
  405. if (this.bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  406. this._effect.setTexture("bumpSampler", this.bumpTexture);
  407. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  408. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  409. }
  410. // Clip plane
  411. if (scene.clipPlane) {
  412. var clipPlane = scene.clipPlane;
  413. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  414. }
  415. // Point size
  416. if (this.pointsCloud) {
  417. this._effect.setFloat("pointSize", this.pointSize);
  418. }
  419. // Colors
  420. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  421. // Scaling down color according to emissive
  422. this._scaledSpecular.r = this.specularColor.r * Tools.Clamp(1.0 - this.emissiveColor.r);
  423. this._scaledSpecular.g = this.specularColor.g * Tools.Clamp(1.0 - this.emissiveColor.g);
  424. this._scaledSpecular.b = this.specularColor.b * Tools.Clamp(1.0 - this.emissiveColor.b);
  425. this._effect.setVector3("vEyePosition", scene.activeCamera.position);
  426. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  427. this._effect.setColor4("vSpecularColor", this._scaledSpecular, this.specularPower);
  428. this._effect.setColor3("vEmissiveColor", this.emissiveColor);
  429. }
  430. // Scaling down color according to emissive
  431. this._scaledDiffuse.r = this.diffuseColor.r * Tools.Clamp(1.0 - this.emissiveColor.r);
  432. this._scaledDiffuse.g = this.diffuseColor.g * Tools.Clamp(1.0 - this.emissiveColor.g);
  433. this._scaledDiffuse.b = this.diffuseColor.b * Tools.Clamp(1.0 - this.emissiveColor.b);
  434. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  435. if (scene.lightsEnabled) {
  436. var lightIndex = 0;
  437. for (var index = 0; index < scene.lights.length; index++) {
  438. var light = scene.lights[index];
  439. if (!light.isEnabled()) {
  440. continue;
  441. }
  442. if (!light.canAffectMesh(mesh)) {
  443. continue;
  444. }
  445. if (light instanceof PointLight) {
  446. // Point Light
  447. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  448. } else if (light instanceof DirectionalLight) {
  449. // Directional Light
  450. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  451. } else if (light instanceof SpotLight) {
  452. // Spot Light
  453. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  454. } else if (light instanceof HemisphericLight) {
  455. // Hemispheric Light
  456. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  457. }
  458. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  459. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  460. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  461. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  462. // Shadows
  463. if (scene.shadowsEnabled) {
  464. var shadowGenerator = light.getShadowGenerator();
  465. if (mesh.receiveShadows && shadowGenerator) {
  466. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  467. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMap());
  468. this._effect.setFloat("darkness" + lightIndex, shadowGenerator.getDarkness());
  469. }
  470. }
  471. lightIndex++;
  472. if (lightIndex === maxSimultaneousLights)
  473. break;
  474. }
  475. }
  476. // View
  477. if (scene.fogEnabled && mesh.applyFog &&scene.fogMode !== Scene.FOGMODE_NONE || this.reflectionTexture) {
  478. this._effect.setMatrix("view", scene.getViewMatrix());
  479. }
  480. // Fog
  481. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  482. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  483. this._effect.setColor3("vFogColor", scene.fogColor);
  484. }
  485. super.bind(world, mesh);
  486. }
  487. public getAnimatables(): IAnimatable[] {
  488. var results = [];
  489. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  490. results.push(this.diffuseTexture);
  491. }
  492. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  493. results.push(this.ambientTexture);
  494. }
  495. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  496. results.push(this.opacityTexture);
  497. }
  498. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  499. results.push(this.reflectionTexture);
  500. }
  501. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  502. results.push(this.emissiveTexture);
  503. }
  504. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  505. results.push(this.specularTexture);
  506. }
  507. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  508. results.push(this.bumpTexture);
  509. }
  510. return results;
  511. }
  512. public dispose(forceDisposeEffect?: boolean): void {
  513. if (this.diffuseTexture) {
  514. this.diffuseTexture.dispose();
  515. }
  516. if (this.ambientTexture) {
  517. this.ambientTexture.dispose();
  518. }
  519. if (this.opacityTexture) {
  520. this.opacityTexture.dispose();
  521. }
  522. if (this.reflectionTexture) {
  523. this.reflectionTexture.dispose();
  524. }
  525. if (this.emissiveTexture) {
  526. this.emissiveTexture.dispose();
  527. }
  528. if (this.specularTexture) {
  529. this.specularTexture.dispose();
  530. }
  531. if (this.bumpTexture) {
  532. this.bumpTexture.dispose();
  533. }
  534. super.dispose(forceDisposeEffect);
  535. }
  536. public clone(name: string): StandardMaterial {
  537. var newStandardMaterial = new StandardMaterial(name, this.getScene());
  538. // Base material
  539. newStandardMaterial.checkReadyOnEveryCall = this.checkReadyOnEveryCall;
  540. newStandardMaterial.alpha = this.alpha;
  541. newStandardMaterial.fillMode = this.fillMode;
  542. newStandardMaterial.backFaceCulling = this.backFaceCulling;
  543. // Standard material
  544. if (this.diffuseTexture && this.diffuseTexture.clone) {
  545. newStandardMaterial.diffuseTexture = this.diffuseTexture.clone();
  546. }
  547. if (this.ambientTexture && this.ambientTexture.clone) {
  548. newStandardMaterial.ambientTexture = this.ambientTexture.clone();
  549. }
  550. if (this.opacityTexture && this.opacityTexture.clone) {
  551. newStandardMaterial.opacityTexture = this.opacityTexture.clone();
  552. }
  553. if (this.reflectionTexture && this.reflectionTexture.clone) {
  554. newStandardMaterial.reflectionTexture = this.reflectionTexture.clone();
  555. }
  556. if (this.emissiveTexture && this.emissiveTexture.clone) {
  557. newStandardMaterial.emissiveTexture = this.emissiveTexture.clone();
  558. }
  559. if (this.specularTexture && this.specularTexture.clone) {
  560. newStandardMaterial.specularTexture = this.specularTexture.clone();
  561. }
  562. if (this.bumpTexture && this.bumpTexture.clone) {
  563. newStandardMaterial.bumpTexture = this.bumpTexture.clone();
  564. }
  565. newStandardMaterial.ambientColor = this.ambientColor.clone();
  566. newStandardMaterial.diffuseColor = this.diffuseColor.clone();
  567. newStandardMaterial.specularColor = this.specularColor.clone();
  568. newStandardMaterial.specularPower = this.specularPower;
  569. newStandardMaterial.emissiveColor = this.emissiveColor.clone();
  570. return newStandardMaterial;
  571. }
  572. // Statics
  573. // Flags used to enable or disable a type of texture for all Standard Materials
  574. public static DiffuseTextureEnabled = true;
  575. public static AmbientTextureEnabled = true;
  576. public static OpacityTextureEnabled = true;
  577. public static ReflectionTextureEnabled = true;
  578. public static EmissiveTextureEnabled = true;
  579. public static SpecularTextureEnabled = true;
  580. public static BumpTextureEnabled = true;
  581. public static FresnelEnabled = true;
  582. }
  583. }