babylon.standardMaterial.js 32 KB

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