babylon.standardMaterial.js 33 KB

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