babylon.standardMaterial.js 31 KB

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