babylon.standardMaterial.js 43 KB

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