babylon.standardMaterial.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. var __extends = (this && 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. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var maxSimultaneousLights = 4;
  9. var FresnelParameters = (function () {
  10. function FresnelParameters() {
  11. this.isEnabled = true;
  12. this.leftColor = BABYLON.Color3.White();
  13. this.rightColor = BABYLON.Color3.Black();
  14. this.bias = 0;
  15. this.power = 1;
  16. }
  17. FresnelParameters.prototype.clone = function () {
  18. var newFresnelParameters = new FresnelParameters();
  19. BABYLON.Tools.DeepCopy(this, newFresnelParameters);
  20. return new FresnelParameters;
  21. };
  22. return FresnelParameters;
  23. })();
  24. BABYLON.FresnelParameters = FresnelParameters;
  25. var StandardMaterialDefines = (function (_super) {
  26. __extends(StandardMaterialDefines, _super);
  27. function StandardMaterialDefines() {
  28. _super.call(this);
  29. this.DIFFUSE = false;
  30. this.AMBIENT = false;
  31. this.OPACITY = false;
  32. this.OPACITYRGB = false;
  33. this.REFLECTION = false;
  34. this.EMISSIVE = false;
  35. this.SPECULAR = false;
  36. this.BUMP = false;
  37. this.SPECULAROVERALPHA = false;
  38. this.CLIPPLANE = false;
  39. this.ALPHATEST = false;
  40. this.ALPHAFROMDIFFUSE = false;
  41. this.POINTSIZE = false;
  42. this.FOG = false;
  43. this.LIGHT0 = false;
  44. this.LIGHT1 = false;
  45. this.LIGHT2 = false;
  46. this.LIGHT3 = false;
  47. this.SPOTLIGHT0 = false;
  48. this.SPOTLIGHT1 = false;
  49. this.SPOTLIGHT2 = false;
  50. this.SPOTLIGHT3 = false;
  51. this.HEMILIGHT0 = false;
  52. this.HEMILIGHT1 = false;
  53. this.HEMILIGHT2 = false;
  54. this.HEMILIGHT3 = false;
  55. this.POINTLIGHT0 = false;
  56. this.POINTLIGHT1 = false;
  57. this.POINTLIGHT2 = false;
  58. this.POINTLIGHT3 = false;
  59. this.DIRLIGHT0 = false;
  60. this.DIRLIGHT1 = false;
  61. this.DIRLIGHT2 = false;
  62. this.DIRLIGHT3 = false;
  63. this.SPECULARTERM = false;
  64. this.SHADOW0 = false;
  65. this.SHADOW1 = false;
  66. this.SHADOW2 = false;
  67. this.SHADOW3 = false;
  68. this.SHADOWS = false;
  69. this.SHADOWVSM0 = false;
  70. this.SHADOWVSM1 = false;
  71. this.SHADOWVSM2 = false;
  72. this.SHADOWVSM3 = false;
  73. this.SHADOWPCF0 = false;
  74. this.SHADOWPCF1 = false;
  75. this.SHADOWPCF2 = false;
  76. this.SHADOWPCF3 = false;
  77. this.DIFFUSEFRESNEL = false;
  78. this.OPACITYFRESNEL = false;
  79. this.REFLECTIONFRESNEL = false;
  80. this.EMISSIVEFRESNEL = false;
  81. this.FRESNEL = false;
  82. this.NORMAL = false;
  83. this.UV1 = false;
  84. this.UV2 = false;
  85. this.VERTEXCOLOR = false;
  86. this.VERTEXALPHA = false;
  87. this.NUM_BONE_INFLUENCERS = 0;
  88. this.BonesPerMesh = 0;
  89. this.INSTANCES = false;
  90. this.GLOSSINESS = false;
  91. this.ROUGHNESS = false;
  92. this.EMISSIVEASILLUMINATION = false;
  93. this.LINKEMISSIVEWITHDIFFUSE = false;
  94. this.REFLECTIONFRESNELFROMSPECULAR = false;
  95. this.LIGHTMAP = false;
  96. this.USELIGHTMAPASSHADOWMAP = false;
  97. this.REFLECTIONMAP_3D = false;
  98. this.REFLECTIONMAP_SPHERICAL = false;
  99. this.REFLECTIONMAP_PLANAR = false;
  100. this.REFLECTIONMAP_CUBIC = false;
  101. this.REFLECTIONMAP_PROJECTION = false;
  102. this.REFLECTIONMAP_SKYBOX = false;
  103. this.REFLECTIONMAP_EXPLICIT = false;
  104. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  105. this.INVERTCUBICMAP = false;
  106. this._keys = Object.keys(this);
  107. }
  108. return StandardMaterialDefines;
  109. })(BABYLON.MaterialDefines);
  110. var StandardMaterial = (function (_super) {
  111. __extends(StandardMaterial, _super);
  112. function StandardMaterial(name, scene) {
  113. var _this = this;
  114. _super.call(this, name, scene);
  115. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  116. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  117. this.specularColor = new BABYLON.Color3(1, 1, 1);
  118. this.specularPower = 64;
  119. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  120. this.useAlphaFromDiffuseTexture = false;
  121. this.useEmissiveAsIllumination = false;
  122. this.linkEmissiveWithDiffuse = false;
  123. this.useReflectionFresnelFromSpecular = false;
  124. this.useSpecularOverAlpha = true;
  125. this.disableLighting = false;
  126. this.roughness = 0;
  127. this.useLightmapAsShadowmap = false;
  128. this.useGlossinessFromSpecularMapAlpha = false;
  129. this._renderTargets = new BABYLON.SmartArray(16);
  130. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  131. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  132. this._defines = new StandardMaterialDefines();
  133. this._cachedDefines = new StandardMaterialDefines();
  134. this._cachedDefines.BonesPerMesh = -1;
  135. this.getRenderTargetTextures = function () {
  136. _this._renderTargets.reset();
  137. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  138. _this._renderTargets.push(_this.reflectionTexture);
  139. }
  140. return _this._renderTargets;
  141. };
  142. }
  143. StandardMaterial.prototype.needAlphaBlending = function () {
  144. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  145. };
  146. StandardMaterial.prototype.needAlphaTesting = function () {
  147. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha;
  148. };
  149. StandardMaterial.prototype._shouldUseAlphaFromDiffuseTexture = function () {
  150. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
  151. };
  152. StandardMaterial.prototype.getAlphaTestTexture = function () {
  153. return this.diffuseTexture;
  154. };
  155. // Methods
  156. StandardMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  157. if (!mesh) {
  158. return true;
  159. }
  160. if (this._defines.INSTANCES !== useInstances) {
  161. return false;
  162. }
  163. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  164. return true;
  165. }
  166. return false;
  167. };
  168. StandardMaterial.PrepareDefinesForLights = function (scene, mesh, defines) {
  169. var lightIndex = 0;
  170. var needNormals = false;
  171. for (var index = 0; index < scene.lights.length; index++) {
  172. var light = scene.lights[index];
  173. if (!light.isEnabled()) {
  174. continue;
  175. }
  176. // Excluded check
  177. if (light._excludedMeshesIds.length > 0) {
  178. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  179. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  180. if (excludedMesh) {
  181. light.excludedMeshes.push(excludedMesh);
  182. }
  183. }
  184. light._excludedMeshesIds = [];
  185. }
  186. // Included check
  187. if (light._includedOnlyMeshesIds.length > 0) {
  188. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  189. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  190. if (includedOnlyMesh) {
  191. light.includedOnlyMeshes.push(includedOnlyMesh);
  192. }
  193. }
  194. light._includedOnlyMeshesIds = [];
  195. }
  196. if (!light.canAffectMesh(mesh)) {
  197. continue;
  198. }
  199. needNormals = true;
  200. defines["LIGHT" + lightIndex] = true;
  201. var type;
  202. if (light instanceof BABYLON.SpotLight) {
  203. type = "SPOTLIGHT" + lightIndex;
  204. }
  205. else if (light instanceof BABYLON.HemisphericLight) {
  206. type = "HEMILIGHT" + lightIndex;
  207. }
  208. else if (light instanceof BABYLON.PointLight) {
  209. type = "POINTLIGHT" + lightIndex;
  210. }
  211. else {
  212. type = "DIRLIGHT" + lightIndex;
  213. }
  214. defines[type] = true;
  215. // Specular
  216. if (!light.specular.equalsFloats(0, 0, 0)) {
  217. defines["SPECULARTERM"] = true;
  218. }
  219. // Shadows
  220. if (scene.shadowsEnabled) {
  221. var shadowGenerator = light.getShadowGenerator();
  222. if (mesh && mesh.receiveShadows && shadowGenerator) {
  223. defines["SHADOW" + lightIndex] = true;
  224. defines["SHADOWS"] = true;
  225. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  226. defines["SHADOWVSM" + lightIndex] = true;
  227. }
  228. if (shadowGenerator.usePoissonSampling) {
  229. defines["SHADOWPCF" + lightIndex] = true;
  230. }
  231. }
  232. }
  233. lightIndex++;
  234. if (lightIndex === maxSimultaneousLights)
  235. break;
  236. }
  237. return needNormals;
  238. };
  239. StandardMaterial.BindLights = function (scene, mesh, effect, defines) {
  240. var lightIndex = 0;
  241. for (var index = 0; index < scene.lights.length; index++) {
  242. var light = scene.lights[index];
  243. if (!light.isEnabled()) {
  244. continue;
  245. }
  246. if (!light.canAffectMesh(mesh)) {
  247. continue;
  248. }
  249. if (light instanceof BABYLON.PointLight) {
  250. // Point Light
  251. light.transferToEffect(effect, "vLightData" + lightIndex);
  252. }
  253. else if (light instanceof BABYLON.DirectionalLight) {
  254. // Directional Light
  255. light.transferToEffect(effect, "vLightData" + lightIndex);
  256. }
  257. else if (light instanceof BABYLON.SpotLight) {
  258. // Spot Light
  259. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  260. }
  261. else if (light instanceof BABYLON.HemisphericLight) {
  262. // Hemispheric Light
  263. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  264. }
  265. light.diffuse.scaleToRef(light.intensity, StandardMaterial._scaledDiffuse);
  266. effect.setColor4("vLightDiffuse" + lightIndex, StandardMaterial._scaledDiffuse, light.range);
  267. if (defines["SPECULARTERM"]) {
  268. light.specular.scaleToRef(light.intensity, StandardMaterial._scaledSpecular);
  269. effect.setColor3("vLightSpecular" + lightIndex, StandardMaterial._scaledSpecular);
  270. }
  271. // Shadows
  272. if (scene.shadowsEnabled) {
  273. var shadowGenerator = light.getShadowGenerator();
  274. if (mesh.receiveShadows && shadowGenerator) {
  275. if (!light.needCube()) {
  276. effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  277. }
  278. effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  279. effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  280. }
  281. }
  282. lightIndex++;
  283. if (lightIndex === maxSimultaneousLights)
  284. break;
  285. }
  286. };
  287. StandardMaterial.prototype.isReady = function (mesh, useInstances) {
  288. if (this.checkReadyOnlyOnce) {
  289. if (this._wasPreviouslyReady) {
  290. return true;
  291. }
  292. }
  293. var scene = this.getScene();
  294. if (!this.checkReadyOnEveryCall) {
  295. if (this._renderId === scene.getRenderId()) {
  296. if (this._checkCache(scene, mesh, useInstances)) {
  297. return true;
  298. }
  299. }
  300. }
  301. var engine = scene.getEngine();
  302. var needNormals = false;
  303. var needUVs = false;
  304. this._defines.reset();
  305. // Textures
  306. if (scene.texturesEnabled) {
  307. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  308. if (!this.diffuseTexture.isReady()) {
  309. return false;
  310. }
  311. else {
  312. needUVs = true;
  313. this._defines.DIFFUSE = true;
  314. }
  315. }
  316. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  317. if (!this.ambientTexture.isReady()) {
  318. return false;
  319. }
  320. else {
  321. needUVs = true;
  322. this._defines.AMBIENT = true;
  323. }
  324. }
  325. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  326. if (!this.opacityTexture.isReady()) {
  327. return false;
  328. }
  329. else {
  330. needUVs = true;
  331. this._defines.OPACITY = true;
  332. if (this.opacityTexture.getAlphaFromRGB) {
  333. this._defines.OPACITYRGB = true;
  334. }
  335. }
  336. }
  337. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  338. if (!this.reflectionTexture.isReady()) {
  339. return false;
  340. }
  341. else {
  342. needNormals = true;
  343. this._defines.REFLECTION = true;
  344. if (this.roughness > 0) {
  345. this._defines.ROUGHNESS = true;
  346. }
  347. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  348. this._defines.INVERTCUBICMAP = true;
  349. }
  350. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  351. switch (this.reflectionTexture.coordinatesMode) {
  352. case BABYLON.Texture.CUBIC_MODE:
  353. case BABYLON.Texture.INVCUBIC_MODE:
  354. this._defines.REFLECTIONMAP_CUBIC = true;
  355. break;
  356. case BABYLON.Texture.EXPLICIT_MODE:
  357. this._defines.REFLECTIONMAP_EXPLICIT = true;
  358. break;
  359. case BABYLON.Texture.PLANAR_MODE:
  360. this._defines.REFLECTIONMAP_PLANAR = true;
  361. break;
  362. case BABYLON.Texture.PROJECTION_MODE:
  363. this._defines.REFLECTIONMAP_PROJECTION = true;
  364. break;
  365. case BABYLON.Texture.SKYBOX_MODE:
  366. this._defines.REFLECTIONMAP_SKYBOX = true;
  367. break;
  368. case BABYLON.Texture.SPHERICAL_MODE:
  369. this._defines.REFLECTIONMAP_SPHERICAL = true;
  370. break;
  371. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  372. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  373. break;
  374. }
  375. }
  376. }
  377. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  378. if (!this.emissiveTexture.isReady()) {
  379. return false;
  380. }
  381. else {
  382. needUVs = true;
  383. this._defines.EMISSIVE = true;
  384. }
  385. }
  386. if (this.lightmapTexture && StandardMaterial.LightmapEnabled) {
  387. if (!this.lightmapTexture.isReady()) {
  388. return false;
  389. }
  390. else {
  391. needUVs = true;
  392. this._defines.LIGHTMAP = true;
  393. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  394. }
  395. }
  396. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  397. if (!this.specularTexture.isReady()) {
  398. return false;
  399. }
  400. else {
  401. needUVs = true;
  402. this._defines.SPECULAR = true;
  403. this._defines.GLOSSINESS = this.useGlossinessFromSpecularMapAlpha;
  404. }
  405. }
  406. }
  407. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && StandardMaterial.BumpTextureEnabled) {
  408. if (!this.bumpTexture.isReady()) {
  409. return false;
  410. }
  411. else {
  412. needUVs = true;
  413. this._defines.BUMP = true;
  414. }
  415. }
  416. // Effect
  417. if (scene.clipPlane) {
  418. this._defines.CLIPPLANE = true;
  419. }
  420. if (engine.getAlphaTesting()) {
  421. this._defines.ALPHATEST = true;
  422. }
  423. if (this._shouldUseAlphaFromDiffuseTexture()) {
  424. this._defines.ALPHAFROMDIFFUSE = true;
  425. }
  426. if (this.useEmissiveAsIllumination) {
  427. this._defines.EMISSIVEASILLUMINATION = true;
  428. }
  429. if (this.linkEmissiveWithDiffuse) {
  430. this._defines.LINKEMISSIVEWITHDIFFUSE = true;
  431. }
  432. if (this.useReflectionFresnelFromSpecular) {
  433. this._defines.REFLECTIONFRESNELFROMSPECULAR = true;
  434. }
  435. // Point size
  436. if (this.pointsCloud || scene.forcePointsCloud) {
  437. this._defines.POINTSIZE = true;
  438. }
  439. // Fog
  440. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  441. this._defines.FOG = true;
  442. }
  443. if (scene.lightsEnabled && !this.disableLighting) {
  444. needNormals = StandardMaterial.PrepareDefinesForLights(scene, mesh, this._defines);
  445. }
  446. if (StandardMaterial.FresnelEnabled) {
  447. // Fresnel
  448. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled ||
  449. this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  450. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled ||
  451. this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  452. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  453. this._defines.DIFFUSEFRESNEL = true;
  454. }
  455. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  456. this._defines.OPACITYFRESNEL = true;
  457. }
  458. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  459. this._defines.REFLECTIONFRESNEL = true;
  460. }
  461. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  462. this._defines.EMISSIVEFRESNEL = true;
  463. }
  464. needNormals = true;
  465. this._defines.FRESNEL = true;
  466. }
  467. }
  468. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  469. this._defines.SPECULAROVERALPHA = true;
  470. }
  471. // Attribs
  472. if (mesh) {
  473. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  474. this._defines.NORMAL = true;
  475. }
  476. if (needUVs) {
  477. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  478. this._defines.UV1 = true;
  479. }
  480. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  481. this._defines.UV2 = true;
  482. }
  483. }
  484. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  485. this._defines.VERTEXCOLOR = true;
  486. if (mesh.hasVertexAlpha) {
  487. this._defines.VERTEXALPHA = true;
  488. }
  489. }
  490. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  491. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  492. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  493. }
  494. // Instances
  495. if (useInstances) {
  496. this._defines.INSTANCES = true;
  497. }
  498. }
  499. // Get correct effect
  500. if (!this._defines.isEqual(this._cachedDefines)) {
  501. this._defines.cloneTo(this._cachedDefines);
  502. scene.resetCachedMaterial();
  503. // Fallbacks
  504. var fallbacks = new BABYLON.EffectFallbacks();
  505. if (this._defines.REFLECTION) {
  506. fallbacks.addFallback(0, "REFLECTION");
  507. }
  508. if (this._defines.SPECULAR) {
  509. fallbacks.addFallback(0, "SPECULAR");
  510. }
  511. if (this._defines.BUMP) {
  512. fallbacks.addFallback(0, "BUMP");
  513. }
  514. if (this._defines.SPECULAROVERALPHA) {
  515. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  516. }
  517. if (this._defines.FOG) {
  518. fallbacks.addFallback(1, "FOG");
  519. }
  520. if (this._defines.POINTSIZE) {
  521. fallbacks.addFallback(0, "POINTSIZE");
  522. }
  523. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  524. if (!this._defines["LIGHT" + lightIndex]) {
  525. continue;
  526. }
  527. if (lightIndex > 0) {
  528. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  529. }
  530. if (this._defines["SHADOW" + lightIndex]) {
  531. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  532. }
  533. if (this._defines["SHADOWPCF" + lightIndex]) {
  534. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  535. }
  536. if (this._defines["SHADOWVSM" + lightIndex]) {
  537. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  538. }
  539. }
  540. if (this._defines.SPECULARTERM) {
  541. fallbacks.addFallback(0, "SPECULARTERM");
  542. }
  543. if (this._defines.DIFFUSEFRESNEL) {
  544. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  545. }
  546. if (this._defines.OPACITYFRESNEL) {
  547. fallbacks.addFallback(2, "OPACITYFRESNEL");
  548. }
  549. if (this._defines.REFLECTIONFRESNEL) {
  550. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  551. }
  552. if (this._defines.EMISSIVEFRESNEL) {
  553. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  554. }
  555. if (this._defines.FRESNEL) {
  556. fallbacks.addFallback(4, "FRESNEL");
  557. }
  558. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  559. fallbacks.addCPUSkinningFallback(0, mesh);
  560. }
  561. //Attributes
  562. var attribs = [BABYLON.VertexBuffer.PositionKind];
  563. if (this._defines.NORMAL) {
  564. attribs.push(BABYLON.VertexBuffer.NormalKind);
  565. }
  566. if (this._defines.UV1) {
  567. attribs.push(BABYLON.VertexBuffer.UVKind);
  568. }
  569. if (this._defines.UV2) {
  570. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  571. }
  572. if (this._defines.VERTEXCOLOR) {
  573. attribs.push(BABYLON.VertexBuffer.ColorKind);
  574. }
  575. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  576. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  577. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  578. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  579. attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
  580. attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
  581. }
  582. }
  583. if (this._defines.INSTANCES) {
  584. attribs.push("world0");
  585. attribs.push("world1");
  586. attribs.push("world2");
  587. attribs.push("world3");
  588. }
  589. // Legacy browser patch
  590. var shaderName = "default";
  591. if (!scene.getEngine().getCaps().standardDerivatives) {
  592. shaderName = "legacydefault";
  593. }
  594. var join = this._defines.toString();
  595. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  596. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  597. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  598. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  599. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  600. "vFogInfos", "vFogColor", "pointSize",
  601. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos",
  602. "mBones",
  603. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix",
  604. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  605. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor"
  606. ], ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler",
  607. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  608. ], join, fallbacks, this.onCompiled, this.onError);
  609. }
  610. if (!this._effect.isReady()) {
  611. return false;
  612. }
  613. this._renderId = scene.getRenderId();
  614. this._wasPreviouslyReady = true;
  615. if (mesh) {
  616. if (!mesh._materialDefines) {
  617. mesh._materialDefines = new StandardMaterialDefines();
  618. }
  619. this._defines.cloneTo(mesh._materialDefines);
  620. }
  621. return true;
  622. };
  623. StandardMaterial.prototype.unbind = function () {
  624. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  625. this._effect.setTexture("reflection2DSampler", null);
  626. }
  627. _super.prototype.unbind.call(this);
  628. };
  629. StandardMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  630. this._effect.setMatrix("world", world);
  631. };
  632. StandardMaterial.prototype.bind = function (world, mesh) {
  633. var scene = this.getScene();
  634. // Matrices
  635. this.bindOnlyWorldMatrix(world);
  636. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  637. // Bones
  638. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  639. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  640. }
  641. if (scene.getCachedMaterial() !== this) {
  642. if (StandardMaterial.FresnelEnabled) {
  643. // Fresnel
  644. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  645. this._effect.setColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  646. this._effect.setColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  647. }
  648. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  649. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  650. }
  651. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  652. this._effect.setColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  653. this._effect.setColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  654. }
  655. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  656. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  657. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  658. }
  659. }
  660. // Textures
  661. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  662. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  663. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  664. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  665. }
  666. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  667. this._effect.setTexture("ambientSampler", this.ambientTexture);
  668. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  669. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  670. }
  671. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  672. this._effect.setTexture("opacitySampler", this.opacityTexture);
  673. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  674. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  675. }
  676. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  677. if (this.reflectionTexture.isCube) {
  678. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  679. }
  680. else {
  681. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  682. }
  683. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  684. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, this.roughness);
  685. }
  686. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  687. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  688. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  689. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  690. }
  691. if (this.lightmapTexture && StandardMaterial.LightmapEnabled) {
  692. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  693. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  694. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  695. }
  696. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  697. this._effect.setTexture("specularSampler", this.specularTexture);
  698. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  699. this._effect.setMatrix("specularMatrix", this.specularTexture.getTextureMatrix());
  700. }
  701. if (this.bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  702. this._effect.setTexture("bumpSampler", this.bumpTexture);
  703. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  704. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  705. }
  706. // Clip plane
  707. if (scene.clipPlane) {
  708. var clipPlane = scene.clipPlane;
  709. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  710. }
  711. // Point size
  712. if (this.pointsCloud) {
  713. this._effect.setFloat("pointSize", this.pointSize);
  714. }
  715. // Colors
  716. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  717. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  718. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  719. if (this._defines.SPECULARTERM) {
  720. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  721. }
  722. this._effect.setColor3("vEmissiveColor", this.emissiveColor);
  723. }
  724. // Diffuse
  725. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  726. // Lights
  727. if (scene.lightsEnabled && !this.disableLighting) {
  728. StandardMaterial.BindLights(scene, mesh, this._effect, this._defines);
  729. }
  730. // View
  731. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  732. this._effect.setMatrix("view", scene.getViewMatrix());
  733. }
  734. // Fog
  735. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  736. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  737. this._effect.setColor3("vFogColor", scene.fogColor);
  738. }
  739. _super.prototype.bind.call(this, world, mesh);
  740. };
  741. StandardMaterial.prototype.getAnimatables = function () {
  742. var results = [];
  743. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  744. results.push(this.diffuseTexture);
  745. }
  746. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  747. results.push(this.ambientTexture);
  748. }
  749. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  750. results.push(this.opacityTexture);
  751. }
  752. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  753. results.push(this.reflectionTexture);
  754. }
  755. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  756. results.push(this.emissiveTexture);
  757. }
  758. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  759. results.push(this.specularTexture);
  760. }
  761. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  762. results.push(this.bumpTexture);
  763. }
  764. return results;
  765. };
  766. StandardMaterial.prototype.dispose = function (forceDisposeEffect) {
  767. if (this.diffuseTexture) {
  768. this.diffuseTexture.dispose();
  769. }
  770. if (this.ambientTexture) {
  771. this.ambientTexture.dispose();
  772. }
  773. if (this.opacityTexture) {
  774. this.opacityTexture.dispose();
  775. }
  776. if (this.reflectionTexture) {
  777. this.reflectionTexture.dispose();
  778. }
  779. if (this.emissiveTexture) {
  780. this.emissiveTexture.dispose();
  781. }
  782. if (this.specularTexture) {
  783. this.specularTexture.dispose();
  784. }
  785. if (this.bumpTexture) {
  786. this.bumpTexture.dispose();
  787. }
  788. _super.prototype.dispose.call(this, forceDisposeEffect);
  789. };
  790. StandardMaterial.prototype.clone = function (name) {
  791. var newStandardMaterial = new StandardMaterial(name, this.getScene());
  792. // Base material
  793. this.copyTo(newStandardMaterial);
  794. // Standard material
  795. if (this.diffuseTexture && this.diffuseTexture.clone) {
  796. newStandardMaterial.diffuseTexture = this.diffuseTexture.clone();
  797. }
  798. if (this.ambientTexture && this.ambientTexture.clone) {
  799. newStandardMaterial.ambientTexture = this.ambientTexture.clone();
  800. }
  801. if (this.opacityTexture && this.opacityTexture.clone) {
  802. newStandardMaterial.opacityTexture = this.opacityTexture.clone();
  803. }
  804. if (this.reflectionTexture && this.reflectionTexture.clone) {
  805. newStandardMaterial.reflectionTexture = this.reflectionTexture.clone();
  806. }
  807. if (this.emissiveTexture && this.emissiveTexture.clone) {
  808. newStandardMaterial.emissiveTexture = this.emissiveTexture.clone();
  809. }
  810. if (this.specularTexture && this.specularTexture.clone) {
  811. newStandardMaterial.specularTexture = this.specularTexture.clone();
  812. }
  813. if (this.bumpTexture && this.bumpTexture.clone) {
  814. newStandardMaterial.bumpTexture = this.bumpTexture.clone();
  815. }
  816. if (this.lightmapTexture && this.lightmapTexture.clone) {
  817. newStandardMaterial.lightmapTexture = this.lightmapTexture.clone();
  818. newStandardMaterial.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  819. }
  820. newStandardMaterial.ambientColor = this.ambientColor.clone();
  821. newStandardMaterial.diffuseColor = this.diffuseColor.clone();
  822. newStandardMaterial.specularColor = this.specularColor.clone();
  823. newStandardMaterial.specularPower = this.specularPower;
  824. newStandardMaterial.emissiveColor = this.emissiveColor.clone();
  825. newStandardMaterial.useAlphaFromDiffuseTexture = this.useAlphaFromDiffuseTexture;
  826. newStandardMaterial.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  827. newStandardMaterial.useGlossinessFromSpecularMapAlpha = this.useGlossinessFromSpecularMapAlpha;
  828. newStandardMaterial.useReflectionFresnelFromSpecular = this.useReflectionFresnelFromSpecular;
  829. newStandardMaterial.useSpecularOverAlpha = this.useSpecularOverAlpha;
  830. newStandardMaterial.roughness = this.roughness;
  831. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.clone) {
  832. newStandardMaterial.diffuseFresnelParameters = this.diffuseFresnelParameters.clone();
  833. }
  834. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.clone) {
  835. newStandardMaterial.emissiveFresnelParameters = this.emissiveFresnelParameters.clone();
  836. }
  837. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.clone) {
  838. newStandardMaterial.reflectionFresnelParameters = this.reflectionFresnelParameters.clone();
  839. }
  840. if (this.opacityFresnelParameters && this.opacityFresnelParameters.clone) {
  841. newStandardMaterial.opacityFresnelParameters = this.opacityFresnelParameters.clone();
  842. }
  843. return newStandardMaterial;
  844. };
  845. StandardMaterial._scaledDiffuse = new BABYLON.Color3();
  846. StandardMaterial._scaledSpecular = new BABYLON.Color3();
  847. // Statics
  848. // Flags used to enable or disable a type of texture for all Standard Materials
  849. StandardMaterial.DiffuseTextureEnabled = true;
  850. StandardMaterial.AmbientTextureEnabled = true;
  851. StandardMaterial.OpacityTextureEnabled = true;
  852. StandardMaterial.ReflectionTextureEnabled = true;
  853. StandardMaterial.EmissiveTextureEnabled = true;
  854. StandardMaterial.SpecularTextureEnabled = true;
  855. StandardMaterial.BumpTextureEnabled = true;
  856. StandardMaterial.FresnelEnabled = true;
  857. StandardMaterial.LightmapEnabled = true;
  858. return StandardMaterial;
  859. })(BABYLON.Material);
  860. BABYLON.StandardMaterial = StandardMaterial;
  861. })(BABYLON || (BABYLON = {}));