babylon.standardMaterial.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. // Textures
  77. if (scene.texturesEnabled) {
  78. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  79. if (!this.diffuseTexture.isReady()) {
  80. return false;
  81. } else {
  82. defines.push("#define DIFFUSE");
  83. }
  84. }
  85. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  86. if (!this.ambientTexture.isReady()) {
  87. return false;
  88. } else {
  89. defines.push("#define AMBIENT");
  90. }
  91. }
  92. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  93. if (!this.opacityTexture.isReady()) {
  94. return false;
  95. } else {
  96. defines.push("#define OPACITY");
  97. if (this.opacityTexture.getAlphaFromRGB) {
  98. defines.push("#define OPACITYRGB");
  99. }
  100. }
  101. }
  102. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  103. if (!this.reflectionTexture.isReady()) {
  104. return false;
  105. } else {
  106. defines.push("#define REFLECTION");
  107. fallbacks.addFallback(0, "REFLECTION");
  108. }
  109. }
  110. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  111. if (!this.emissiveTexture.isReady()) {
  112. return false;
  113. } else {
  114. defines.push("#define EMISSIVE");
  115. }
  116. }
  117. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  118. if (!this.specularTexture.isReady()) {
  119. return false;
  120. } else {
  121. defines.push("#define SPECULAR");
  122. fallbacks.addFallback(0, "SPECULAR");
  123. }
  124. }
  125. }
  126. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && StandardMaterial.BumpTextureEnabled) {
  127. if (!this.bumpTexture.isReady()) {
  128. return false;
  129. } else {
  130. defines.push("#define BUMP");
  131. fallbacks.addFallback(0, "BUMP");
  132. }
  133. }
  134. // Effect
  135. if (this.useSpecularOverAlpha) {
  136. defines.push("#define SPECULAROVERALPHA");
  137. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  138. }
  139. if (scene.clipPlane) {
  140. defines.push("#define CLIPPLANE");
  141. }
  142. if (engine.getAlphaTesting()) {
  143. defines.push("#define ALPHATEST");
  144. }
  145. if (this._shouldUseAlphaFromDiffuseTexture()) {
  146. defines.push("#define ALPHAFROMDIFFUSE");
  147. }
  148. // Point size
  149. if (this.pointsCloud || scene.forcePointsCloud) {
  150. defines.push("#define POINTSIZE");
  151. }
  152. // Fog
  153. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  154. defines.push("#define FOG");
  155. fallbacks.addFallback(1, "FOG");
  156. }
  157. var shadowsActivated = false;
  158. var lightIndex = 0;
  159. if (scene.lightsEnabled) {
  160. for (var index = 0; index < scene.lights.length; index++) {
  161. var light = scene.lights[index];
  162. if (!light.isEnabled()) {
  163. continue;
  164. }
  165. // Excluded check
  166. if (light._excludedMeshesIds.length > 0) {
  167. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  168. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  169. if (excludedMesh) {
  170. light.excludedMeshes.push(excludedMesh);
  171. }
  172. }
  173. light._excludedMeshesIds = [];
  174. }
  175. // Included check
  176. if (light._includedOnlyMeshesIds.length > 0) {
  177. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  178. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  179. if (includedOnlyMesh) {
  180. light.includedOnlyMeshes.push(includedOnlyMesh);
  181. }
  182. }
  183. light._includedOnlyMeshesIds = [];
  184. }
  185. if (!light.canAffectMesh(mesh)) {
  186. continue;
  187. }
  188. defines.push("#define LIGHT" + lightIndex);
  189. if (lightIndex > 0) {
  190. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  191. }
  192. var type;
  193. if (light instanceof BABYLON.SpotLight) {
  194. type = "#define SPOTLIGHT" + lightIndex;
  195. } else if (light instanceof BABYLON.HemisphericLight) {
  196. type = "#define HEMILIGHT" + lightIndex;
  197. } else {
  198. type = "#define POINTDIRLIGHT" + lightIndex;
  199. }
  200. defines.push(type);
  201. if (lightIndex > 0) {
  202. fallbacks.addFallback(lightIndex, type.replace("#define ", ""));
  203. }
  204. // Shadows
  205. if (scene.shadowsEnabled) {
  206. var shadowGenerator = light.getShadowGenerator();
  207. if (mesh && mesh.receiveShadows && shadowGenerator) {
  208. defines.push("#define SHADOW" + lightIndex);
  209. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  210. if (!shadowsActivated) {
  211. defines.push("#define SHADOWS");
  212. shadowsActivated = true;
  213. }
  214. if (shadowGenerator.useVarianceShadowMap) {
  215. defines.push("#define SHADOWVSM" + lightIndex);
  216. if (lightIndex > 0) {
  217. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  218. }
  219. }
  220. if (shadowGenerator.usePoissonSampling) {
  221. defines.push("#define SHADOWPCF" + lightIndex);
  222. if (lightIndex > 0) {
  223. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  224. }
  225. }
  226. }
  227. }
  228. lightIndex++;
  229. if (lightIndex === maxSimultaneousLights)
  230. break;
  231. }
  232. }
  233. if (StandardMaterial.FresnelEnabled) {
  234. // Fresnel
  235. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled || this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled || this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  236. var fresnelRank = 1;
  237. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  238. defines.push("#define DIFFUSEFRESNEL");
  239. fallbacks.addFallback(fresnelRank, "DIFFUSEFRESNEL");
  240. fresnelRank++;
  241. }
  242. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  243. defines.push("#define OPACITYFRESNEL");
  244. fallbacks.addFallback(fresnelRank, "OPACITYFRESNEL");
  245. fresnelRank++;
  246. }
  247. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  248. defines.push("#define REFLECTIONFRESNEL");
  249. fallbacks.addFallback(fresnelRank, "REFLECTIONFRESNEL");
  250. fresnelRank++;
  251. }
  252. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  253. defines.push("#define EMISSIVEFRESNEL");
  254. fallbacks.addFallback(fresnelRank, "EMISSIVEFRESNEL");
  255. fresnelRank++;
  256. }
  257. defines.push("#define FRESNEL");
  258. fallbacks.addFallback(fresnelRank - 1, "FRESNEL");
  259. }
  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.useVertexColors && 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 && scene.skeletonsEnabled && 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. scene.resetCachedMaterial();
  301. // Legacy browser patch
  302. var shaderName = "default";
  303. if (!scene.getEngine().getCaps().standardDerivatives) {
  304. shaderName = "legacydefault";
  305. }
  306. this._effect = scene.getEngine().createEffect(shaderName, 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. ], join, fallbacks, this.onCompiled, this.onError);
  322. }
  323. if (!this._effect.isReady()) {
  324. return false;
  325. }
  326. this._renderId = scene.getRenderId();
  327. this._wasPreviouslyReady = true;
  328. return true;
  329. };
  330. StandardMaterial.prototype.unbind = function () {
  331. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  332. this._effect.setTexture("reflection2DSampler", null);
  333. }
  334. };
  335. StandardMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  336. this._effect.setMatrix("world", world);
  337. };
  338. StandardMaterial.prototype.bind = function (world, mesh) {
  339. var scene = this.getScene();
  340. // Matrices
  341. this.bindOnlyWorldMatrix(world);
  342. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  343. // Bones
  344. if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  345. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  346. }
  347. if (scene.getCachedMaterial() !== this) {
  348. if (StandardMaterial.FresnelEnabled) {
  349. // Fresnel
  350. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  351. this._effect.setColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  352. this._effect.setColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  353. }
  354. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  355. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  356. }
  357. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  358. this._effect.setColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  359. this._effect.setColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  360. }
  361. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  362. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  363. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  364. }
  365. }
  366. // Textures
  367. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  368. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  369. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  370. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  371. }
  372. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  373. this._effect.setTexture("ambientSampler", this.ambientTexture);
  374. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  375. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  376. }
  377. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  378. this._effect.setTexture("opacitySampler", this.opacityTexture);
  379. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  380. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  381. }
  382. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  383. if (this.reflectionTexture.isCube) {
  384. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  385. } else {
  386. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  387. }
  388. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  389. this._effect.setFloat3("vReflectionInfos", this.reflectionTexture.coordinatesMode, this.reflectionTexture.level, this.reflectionTexture.isCube ? 1 : 0);
  390. }
  391. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  392. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  393. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  394. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  395. }
  396. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  397. this._effect.setTexture("specularSampler", this.specularTexture);
  398. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  399. this._effect.setMatrix("specularMatrix", this.specularTexture.getTextureMatrix());
  400. }
  401. if (this.bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  402. this._effect.setTexture("bumpSampler", this.bumpTexture);
  403. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  404. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  405. }
  406. // Clip plane
  407. if (scene.clipPlane) {
  408. var clipPlane = scene.clipPlane;
  409. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  410. }
  411. // Point size
  412. if (this.pointsCloud) {
  413. this._effect.setFloat("pointSize", this.pointSize);
  414. }
  415. // Colors
  416. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  417. // Scaling down color according to emissive
  418. this._scaledSpecular.r = this.specularColor.r * BABYLON.Tools.Clamp(1.0 - this.emissiveColor.r);
  419. this._scaledSpecular.g = this.specularColor.g * BABYLON.Tools.Clamp(1.0 - this.emissiveColor.g);
  420. this._scaledSpecular.b = this.specularColor.b * BABYLON.Tools.Clamp(1.0 - this.emissiveColor.b);
  421. this._effect.setVector3("vEyePosition", scene.activeCamera.position);
  422. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  423. this._effect.setColor4("vSpecularColor", this._scaledSpecular, this.specularPower);
  424. this._effect.setColor3("vEmissiveColor", this.emissiveColor);
  425. }
  426. // Scaling down color according to emissive
  427. this._scaledDiffuse.r = this.diffuseColor.r * BABYLON.Tools.Clamp(1.0 - this.emissiveColor.r);
  428. this._scaledDiffuse.g = this.diffuseColor.g * BABYLON.Tools.Clamp(1.0 - this.emissiveColor.g);
  429. this._scaledDiffuse.b = this.diffuseColor.b * BABYLON.Tools.Clamp(1.0 - this.emissiveColor.b);
  430. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  431. if (scene.lightsEnabled) {
  432. var lightIndex = 0;
  433. for (var index = 0; index < scene.lights.length; index++) {
  434. var light = scene.lights[index];
  435. if (!light.isEnabled()) {
  436. continue;
  437. }
  438. if (!light.canAffectMesh(mesh)) {
  439. continue;
  440. }
  441. if (light instanceof BABYLON.PointLight) {
  442. // Point Light
  443. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  444. } else if (light instanceof BABYLON.DirectionalLight) {
  445. // Directional Light
  446. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  447. } else if (light instanceof BABYLON.SpotLight) {
  448. // Spot Light
  449. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  450. } else if (light instanceof BABYLON.HemisphericLight) {
  451. // Hemispheric Light
  452. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  453. }
  454. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  455. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  456. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  457. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  458. // Shadows
  459. if (scene.shadowsEnabled) {
  460. var shadowGenerator = light.getShadowGenerator();
  461. if (mesh.receiveShadows && shadowGenerator) {
  462. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  463. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMap());
  464. this._effect.setFloat("darkness" + lightIndex, shadowGenerator.getDarkness());
  465. }
  466. }
  467. lightIndex++;
  468. if (lightIndex === maxSimultaneousLights)
  469. break;
  470. }
  471. }
  472. // View
  473. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  474. this._effect.setMatrix("view", scene.getViewMatrix());
  475. }
  476. // Fog
  477. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  478. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  479. this._effect.setColor3("vFogColor", scene.fogColor);
  480. }
  481. _super.prototype.bind.call(this, world, mesh);
  482. };
  483. StandardMaterial.prototype.getAnimatables = function () {
  484. var results = [];
  485. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  486. results.push(this.diffuseTexture);
  487. }
  488. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  489. results.push(this.ambientTexture);
  490. }
  491. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  492. results.push(this.opacityTexture);
  493. }
  494. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  495. results.push(this.reflectionTexture);
  496. }
  497. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  498. results.push(this.emissiveTexture);
  499. }
  500. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  501. results.push(this.specularTexture);
  502. }
  503. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  504. results.push(this.bumpTexture);
  505. }
  506. return results;
  507. };
  508. StandardMaterial.prototype.dispose = function (forceDisposeEffect) {
  509. if (this.diffuseTexture) {
  510. this.diffuseTexture.dispose();
  511. }
  512. if (this.ambientTexture) {
  513. this.ambientTexture.dispose();
  514. }
  515. if (this.opacityTexture) {
  516. this.opacityTexture.dispose();
  517. }
  518. if (this.reflectionTexture) {
  519. this.reflectionTexture.dispose();
  520. }
  521. if (this.emissiveTexture) {
  522. this.emissiveTexture.dispose();
  523. }
  524. if (this.specularTexture) {
  525. this.specularTexture.dispose();
  526. }
  527. if (this.bumpTexture) {
  528. this.bumpTexture.dispose();
  529. }
  530. _super.prototype.dispose.call(this, forceDisposeEffect);
  531. };
  532. StandardMaterial.prototype.clone = function (name) {
  533. var newStandardMaterial = new StandardMaterial(name, this.getScene());
  534. // Base material
  535. newStandardMaterial.checkReadyOnEveryCall = this.checkReadyOnEveryCall;
  536. newStandardMaterial.alpha = this.alpha;
  537. newStandardMaterial.fillMode = this.fillMode;
  538. newStandardMaterial.backFaceCulling = this.backFaceCulling;
  539. // Standard material
  540. if (this.diffuseTexture && this.diffuseTexture.clone) {
  541. newStandardMaterial.diffuseTexture = this.diffuseTexture.clone();
  542. }
  543. if (this.ambientTexture && this.ambientTexture.clone) {
  544. newStandardMaterial.ambientTexture = this.ambientTexture.clone();
  545. }
  546. if (this.opacityTexture && this.opacityTexture.clone) {
  547. newStandardMaterial.opacityTexture = this.opacityTexture.clone();
  548. }
  549. if (this.reflectionTexture && this.reflectionTexture.clone) {
  550. newStandardMaterial.reflectionTexture = this.reflectionTexture.clone();
  551. }
  552. if (this.emissiveTexture && this.emissiveTexture.clone) {
  553. newStandardMaterial.emissiveTexture = this.emissiveTexture.clone();
  554. }
  555. if (this.specularTexture && this.specularTexture.clone) {
  556. newStandardMaterial.specularTexture = this.specularTexture.clone();
  557. }
  558. if (this.bumpTexture && this.bumpTexture.clone) {
  559. newStandardMaterial.bumpTexture = this.bumpTexture.clone();
  560. }
  561. newStandardMaterial.ambientColor = this.ambientColor.clone();
  562. newStandardMaterial.diffuseColor = this.diffuseColor.clone();
  563. newStandardMaterial.specularColor = this.specularColor.clone();
  564. newStandardMaterial.specularPower = this.specularPower;
  565. newStandardMaterial.emissiveColor = this.emissiveColor.clone();
  566. return newStandardMaterial;
  567. };
  568. StandardMaterial.DiffuseTextureEnabled = true;
  569. StandardMaterial.AmbientTextureEnabled = true;
  570. StandardMaterial.OpacityTextureEnabled = true;
  571. StandardMaterial.ReflectionTextureEnabled = true;
  572. StandardMaterial.EmissiveTextureEnabled = true;
  573. StandardMaterial.SpecularTextureEnabled = true;
  574. StandardMaterial.BumpTextureEnabled = true;
  575. StandardMaterial.FresnelEnabled = true;
  576. return StandardMaterial;
  577. })(BABYLON.Material);
  578. BABYLON.StandardMaterial = StandardMaterial;
  579. })(BABYLON || (BABYLON = {}));
  580. //# sourceMappingURL=babylon.standardMaterial.js.map