babylon.standardMaterial.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var StandardMaterialDefines = (function (_super) {
  15. __extends(StandardMaterialDefines, _super);
  16. function StandardMaterialDefines() {
  17. _super.call(this);
  18. this.DIFFUSE = false;
  19. this.AMBIENT = false;
  20. this.OPACITY = false;
  21. this.OPACITYRGB = false;
  22. this.REFLECTION = false;
  23. this.EMISSIVE = false;
  24. this.SPECULAR = false;
  25. this.BUMP = false;
  26. this.PARALLAX = false;
  27. this.PARALLAXOCCLUSION = false;
  28. this.SPECULAROVERALPHA = false;
  29. this.CLIPPLANE = false;
  30. this.ALPHATEST = false;
  31. this.ALPHAFROMDIFFUSE = false;
  32. this.POINTSIZE = false;
  33. this.FOG = false;
  34. this.SPECULARTERM = false;
  35. this.DIFFUSEFRESNEL = false;
  36. this.OPACITYFRESNEL = false;
  37. this.REFLECTIONFRESNEL = false;
  38. this.REFRACTIONFRESNEL = false;
  39. this.EMISSIVEFRESNEL = false;
  40. this.FRESNEL = false;
  41. this.NORMAL = false;
  42. this.UV1 = false;
  43. this.UV2 = false;
  44. this.VERTEXCOLOR = false;
  45. this.VERTEXALPHA = false;
  46. this.NUM_BONE_INFLUENCERS = 0;
  47. this.BonesPerMesh = 0;
  48. this.INSTANCES = false;
  49. this.GLOSSINESS = false;
  50. this.ROUGHNESS = false;
  51. this.EMISSIVEASILLUMINATION = false;
  52. this.LINKEMISSIVEWITHDIFFUSE = false;
  53. this.REFLECTIONFRESNELFROMSPECULAR = false;
  54. this.LIGHTMAP = false;
  55. this.USELIGHTMAPASSHADOWMAP = false;
  56. this.REFLECTIONMAP_3D = false;
  57. this.REFLECTIONMAP_SPHERICAL = false;
  58. this.REFLECTIONMAP_PLANAR = false;
  59. this.REFLECTIONMAP_CUBIC = false;
  60. this.REFLECTIONMAP_PROJECTION = false;
  61. this.REFLECTIONMAP_SKYBOX = false;
  62. this.REFLECTIONMAP_EXPLICIT = false;
  63. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  64. this.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  65. this.INVERTCUBICMAP = false;
  66. this.LOGARITHMICDEPTH = false;
  67. this.REFRACTION = false;
  68. this.REFRACTIONMAP_3D = false;
  69. this.REFLECTIONOVERALPHA = false;
  70. this.INVERTNORMALMAPX = false;
  71. this.INVERTNORMALMAPY = false;
  72. this.SHADOWFULLFLOAT = false;
  73. this.CAMERACOLORGRADING = false;
  74. this.CAMERACOLORCURVES = false;
  75. this.rebuild();
  76. }
  77. return StandardMaterialDefines;
  78. }(BABYLON.MaterialDefines));
  79. var StandardMaterial = (function (_super) {
  80. __extends(StandardMaterial, _super);
  81. function StandardMaterial(name, scene) {
  82. var _this = this;
  83. _super.call(this, name, scene);
  84. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  85. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  86. this.specularColor = new BABYLON.Color3(1, 1, 1);
  87. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  88. this.specularPower = 64;
  89. this.useAlphaFromDiffuseTexture = false;
  90. this.useEmissiveAsIllumination = false;
  91. this.linkEmissiveWithDiffuse = false;
  92. this.useReflectionFresnelFromSpecular = false;
  93. this.useSpecularOverAlpha = false;
  94. this.useReflectionOverAlpha = false;
  95. this.disableLighting = false;
  96. this.useParallax = false;
  97. this.useParallaxOcclusion = false;
  98. this.parallaxScaleBias = 0.05;
  99. this.roughness = 0;
  100. this.indexOfRefraction = 0.98;
  101. this.invertRefractionY = true;
  102. this.useLightmapAsShadowmap = false;
  103. this.useGlossinessFromSpecularMapAlpha = false;
  104. this.maxSimultaneousLights = 4;
  105. /**
  106. * If sets to true, x component of normal map value will invert (x = 1.0 - x).
  107. */
  108. this.invertNormalMapX = false;
  109. /**
  110. * If sets to true, y component of normal map value will invert (y = 1.0 - y).
  111. */
  112. this.invertNormalMapY = false;
  113. /**
  114. * Color Grading 2D Lookup Texture.
  115. * This allows special effects like sepia, black and white to sixties rendering style.
  116. */
  117. this.cameraColorGradingTexture = null;
  118. /**
  119. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  120. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  121. * These are similar to controls found in many professional imaging or colorist software. The global controls are applied to the entire image. For advanced tuning, extra controls are provided to adjust the shadow, midtone and highlight areas of the image;
  122. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  123. */
  124. this.cameraColorCurves = null;
  125. this._renderTargets = new BABYLON.SmartArray(16);
  126. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  127. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  128. this._defines = new StandardMaterialDefines();
  129. this._cachedDefines = new StandardMaterialDefines();
  130. this._cachedDefines.BonesPerMesh = -1;
  131. this.getRenderTargetTextures = function () {
  132. _this._renderTargets.reset();
  133. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  134. _this._renderTargets.push(_this.reflectionTexture);
  135. }
  136. if (_this.refractionTexture && _this.refractionTexture.isRenderTarget) {
  137. _this._renderTargets.push(_this.refractionTexture);
  138. }
  139. return _this._renderTargets;
  140. };
  141. }
  142. Object.defineProperty(StandardMaterial.prototype, "useLogarithmicDepth", {
  143. get: function () {
  144. return this._useLogarithmicDepth;
  145. },
  146. set: function (value) {
  147. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  148. },
  149. enumerable: true,
  150. configurable: true
  151. });
  152. StandardMaterial.prototype.needAlphaBlending = function () {
  153. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  154. };
  155. StandardMaterial.prototype.needAlphaTesting = function () {
  156. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha;
  157. };
  158. StandardMaterial.prototype._shouldUseAlphaFromDiffuseTexture = function () {
  159. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
  160. };
  161. StandardMaterial.prototype.getAlphaTestTexture = function () {
  162. return this.diffuseTexture;
  163. };
  164. // Methods
  165. StandardMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  166. if (!mesh) {
  167. return true;
  168. }
  169. if (this._defines.INSTANCES !== useInstances) {
  170. return false;
  171. }
  172. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  173. return true;
  174. }
  175. return false;
  176. };
  177. StandardMaterial.prototype.isReady = function (mesh, useInstances) {
  178. if (this.isFrozen) {
  179. if (this._wasPreviouslyReady) {
  180. return true;
  181. }
  182. }
  183. var scene = this.getScene();
  184. if (!this.checkReadyOnEveryCall) {
  185. if (this._renderId === scene.getRenderId()) {
  186. if (this._checkCache(scene, mesh, useInstances)) {
  187. return true;
  188. }
  189. }
  190. }
  191. var engine = scene.getEngine();
  192. var needNormals = false;
  193. var needUVs = false;
  194. this._defines.reset();
  195. // Textures
  196. if (scene.texturesEnabled) {
  197. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  198. if (!this.diffuseTexture.isReady()) {
  199. return false;
  200. }
  201. else {
  202. needUVs = true;
  203. this._defines.DIFFUSE = true;
  204. }
  205. }
  206. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  207. if (!this.ambientTexture.isReady()) {
  208. return false;
  209. }
  210. else {
  211. needUVs = true;
  212. this._defines.AMBIENT = true;
  213. }
  214. }
  215. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  216. if (!this.opacityTexture.isReady()) {
  217. return false;
  218. }
  219. else {
  220. needUVs = true;
  221. this._defines.OPACITY = true;
  222. if (this.opacityTexture.getAlphaFromRGB) {
  223. this._defines.OPACITYRGB = true;
  224. }
  225. }
  226. }
  227. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  228. if (!this.reflectionTexture.isReady()) {
  229. return false;
  230. }
  231. else {
  232. needNormals = true;
  233. this._defines.REFLECTION = true;
  234. if (this.roughness > 0) {
  235. this._defines.ROUGHNESS = true;
  236. }
  237. if (this.useReflectionOverAlpha) {
  238. this._defines.REFLECTIONOVERALPHA = true;
  239. }
  240. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  241. this._defines.INVERTCUBICMAP = true;
  242. }
  243. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  244. switch (this.reflectionTexture.coordinatesMode) {
  245. case BABYLON.Texture.CUBIC_MODE:
  246. case BABYLON.Texture.INVCUBIC_MODE:
  247. this._defines.REFLECTIONMAP_CUBIC = true;
  248. break;
  249. case BABYLON.Texture.EXPLICIT_MODE:
  250. this._defines.REFLECTIONMAP_EXPLICIT = true;
  251. break;
  252. case BABYLON.Texture.PLANAR_MODE:
  253. this._defines.REFLECTIONMAP_PLANAR = true;
  254. break;
  255. case BABYLON.Texture.PROJECTION_MODE:
  256. this._defines.REFLECTIONMAP_PROJECTION = true;
  257. break;
  258. case BABYLON.Texture.SKYBOX_MODE:
  259. this._defines.REFLECTIONMAP_SKYBOX = true;
  260. break;
  261. case BABYLON.Texture.SPHERICAL_MODE:
  262. this._defines.REFLECTIONMAP_SPHERICAL = true;
  263. break;
  264. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  265. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  266. break;
  267. case BABYLON.Texture.FIXED_EQUIRECTANGULAR_MODE:
  268. this._defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
  269. break;
  270. }
  271. }
  272. }
  273. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  274. if (!this.emissiveTexture.isReady()) {
  275. return false;
  276. }
  277. else {
  278. needUVs = true;
  279. this._defines.EMISSIVE = true;
  280. }
  281. }
  282. if (this.lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  283. if (!this.lightmapTexture.isReady()) {
  284. return false;
  285. }
  286. else {
  287. needUVs = true;
  288. this._defines.LIGHTMAP = true;
  289. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  290. }
  291. }
  292. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  293. if (!this.specularTexture.isReady()) {
  294. return false;
  295. }
  296. else {
  297. needUVs = true;
  298. this._defines.SPECULAR = true;
  299. this._defines.GLOSSINESS = this.useGlossinessFromSpecularMapAlpha;
  300. }
  301. }
  302. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && StandardMaterial.BumpTextureEnabled) {
  303. if (!this.bumpTexture.isReady()) {
  304. return false;
  305. }
  306. else {
  307. needUVs = true;
  308. this._defines.BUMP = true;
  309. if (this.useParallax) {
  310. this._defines.PARALLAX = true;
  311. if (this.useParallaxOcclusion) {
  312. this._defines.PARALLAXOCCLUSION = true;
  313. }
  314. }
  315. if (this.invertNormalMapX) {
  316. this._defines.INVERTNORMALMAPX = true;
  317. }
  318. if (this.invertNormalMapY) {
  319. this._defines.INVERTNORMALMAPY = true;
  320. }
  321. }
  322. }
  323. if (this.refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  324. if (!this.refractionTexture.isReady()) {
  325. return false;
  326. }
  327. else {
  328. needUVs = true;
  329. this._defines.REFRACTION = true;
  330. this._defines.REFRACTIONMAP_3D = this.refractionTexture.isCube;
  331. }
  332. }
  333. if (this.cameraColorGradingTexture && StandardMaterial.ColorGradingTextureEnabled) {
  334. if (!this.cameraColorGradingTexture.isReady()) {
  335. return false;
  336. }
  337. else {
  338. this._defines.CAMERACOLORGRADING = true;
  339. }
  340. }
  341. }
  342. // Effect
  343. if (scene.clipPlane) {
  344. this._defines.CLIPPLANE = true;
  345. }
  346. if (engine.getAlphaTesting()) {
  347. this._defines.ALPHATEST = true;
  348. }
  349. if (this._shouldUseAlphaFromDiffuseTexture()) {
  350. this._defines.ALPHAFROMDIFFUSE = true;
  351. }
  352. if (this.useEmissiveAsIllumination) {
  353. this._defines.EMISSIVEASILLUMINATION = true;
  354. }
  355. if (this.linkEmissiveWithDiffuse) {
  356. this._defines.LINKEMISSIVEWITHDIFFUSE = true;
  357. }
  358. if (this.useLogarithmicDepth) {
  359. this._defines.LOGARITHMICDEPTH = true;
  360. }
  361. if (this.cameraColorCurves) {
  362. this._defines.CAMERACOLORCURVES = true;
  363. }
  364. // Point size
  365. if (this.pointsCloud || scene.forcePointsCloud) {
  366. this._defines.POINTSIZE = true;
  367. }
  368. // Fog
  369. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  370. this._defines.FOG = true;
  371. }
  372. if (scene.lightsEnabled && !this.disableLighting) {
  373. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  374. }
  375. if (StandardMaterial.FresnelEnabled) {
  376. // Fresnel
  377. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled ||
  378. this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  379. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled ||
  380. this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled ||
  381. this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  382. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  383. this._defines.DIFFUSEFRESNEL = true;
  384. }
  385. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  386. this._defines.OPACITYFRESNEL = true;
  387. }
  388. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  389. this._defines.REFLECTIONFRESNEL = true;
  390. if (this.useReflectionFresnelFromSpecular) {
  391. this._defines.REFLECTIONFRESNELFROMSPECULAR = true;
  392. }
  393. }
  394. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  395. this._defines.REFRACTIONFRESNEL = true;
  396. }
  397. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  398. this._defines.EMISSIVEFRESNEL = true;
  399. }
  400. needNormals = true;
  401. this._defines.FRESNEL = true;
  402. }
  403. }
  404. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  405. this._defines.SPECULAROVERALPHA = true;
  406. }
  407. // Attribs
  408. if (mesh) {
  409. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  410. this._defines.NORMAL = true;
  411. }
  412. if (needUVs) {
  413. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  414. this._defines.UV1 = true;
  415. }
  416. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  417. this._defines.UV2 = true;
  418. }
  419. }
  420. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  421. this._defines.VERTEXCOLOR = true;
  422. if (mesh.hasVertexAlpha) {
  423. this._defines.VERTEXALPHA = true;
  424. }
  425. }
  426. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  427. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  428. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  429. }
  430. // Instances
  431. if (useInstances) {
  432. this._defines.INSTANCES = true;
  433. }
  434. }
  435. // Get correct effect
  436. if (!this._defines.isEqual(this._cachedDefines)) {
  437. this._defines.cloneTo(this._cachedDefines);
  438. scene.resetCachedMaterial();
  439. // Fallbacks
  440. var fallbacks = new BABYLON.EffectFallbacks();
  441. if (this._defines.REFLECTION) {
  442. fallbacks.addFallback(0, "REFLECTION");
  443. }
  444. if (this._defines.SPECULAR) {
  445. fallbacks.addFallback(0, "SPECULAR");
  446. }
  447. if (this._defines.BUMP) {
  448. fallbacks.addFallback(0, "BUMP");
  449. }
  450. if (this._defines.PARALLAX) {
  451. fallbacks.addFallback(1, "PARALLAX");
  452. }
  453. if (this._defines.PARALLAXOCCLUSION) {
  454. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  455. }
  456. if (this._defines.SPECULAROVERALPHA) {
  457. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  458. }
  459. if (this._defines.FOG) {
  460. fallbacks.addFallback(1, "FOG");
  461. }
  462. if (this._defines.POINTSIZE) {
  463. fallbacks.addFallback(0, "POINTSIZE");
  464. }
  465. if (this._defines.LOGARITHMICDEPTH) {
  466. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  467. }
  468. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  469. if (this._defines.SPECULARTERM) {
  470. fallbacks.addFallback(0, "SPECULARTERM");
  471. }
  472. if (this._defines.DIFFUSEFRESNEL) {
  473. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  474. }
  475. if (this._defines.OPACITYFRESNEL) {
  476. fallbacks.addFallback(2, "OPACITYFRESNEL");
  477. }
  478. if (this._defines.REFLECTIONFRESNEL) {
  479. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  480. }
  481. if (this._defines.EMISSIVEFRESNEL) {
  482. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  483. }
  484. if (this._defines.FRESNEL) {
  485. fallbacks.addFallback(4, "FRESNEL");
  486. }
  487. //Attributes
  488. var attribs = [BABYLON.VertexBuffer.PositionKind];
  489. if (this._defines.NORMAL) {
  490. attribs.push(BABYLON.VertexBuffer.NormalKind);
  491. }
  492. if (this._defines.UV1) {
  493. attribs.push(BABYLON.VertexBuffer.UVKind);
  494. }
  495. if (this._defines.UV2) {
  496. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  497. }
  498. if (this._defines.VERTEXCOLOR) {
  499. attribs.push(BABYLON.VertexBuffer.ColorKind);
  500. }
  501. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  502. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  503. var shaderName = "default";
  504. var join = this._defines.toString();
  505. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  506. "vFogInfos", "vFogColor", "pointSize",
  507. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  508. "mBones",
  509. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  510. "depthValues",
  511. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor", "refractionLeftColor", "refractionRightColor",
  512. "logarithmicDepthConstant"
  513. ];
  514. var samplers = ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler"];
  515. if (this._defines.CAMERACOLORCURVES) {
  516. BABYLON.ColorCurves.PrepareUniforms(uniforms);
  517. }
  518. if (this._defines.CAMERACOLORGRADING) {
  519. BABYLON.ColorGradingTexture.PrepareUniformsAndSamplers(uniforms, samplers);
  520. }
  521. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  522. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, samplers, join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights - 1 });
  523. }
  524. if (!this._effect.isReady()) {
  525. return false;
  526. }
  527. this._renderId = scene.getRenderId();
  528. this._wasPreviouslyReady = true;
  529. if (mesh) {
  530. if (!mesh._materialDefines) {
  531. mesh._materialDefines = new StandardMaterialDefines();
  532. }
  533. this._defines.cloneTo(mesh._materialDefines);
  534. }
  535. return true;
  536. };
  537. StandardMaterial.prototype.unbind = function () {
  538. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  539. this._effect.setTexture("reflection2DSampler", null);
  540. }
  541. if (this.refractionTexture && this.refractionTexture.isRenderTarget) {
  542. this._effect.setTexture("refraction2DSampler", null);
  543. }
  544. _super.prototype.unbind.call(this);
  545. };
  546. StandardMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  547. this._effect.setMatrix("world", world);
  548. };
  549. StandardMaterial.prototype.bind = function (world, mesh) {
  550. var scene = this.getScene();
  551. // Matrices
  552. this.bindOnlyWorldMatrix(world);
  553. // Bones
  554. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  555. if (scene.getCachedMaterial() !== this) {
  556. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  557. if (StandardMaterial.FresnelEnabled) {
  558. // Fresnel
  559. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  560. this._effect.setColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  561. this._effect.setColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  562. }
  563. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  564. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  565. }
  566. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  567. this._effect.setColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  568. this._effect.setColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  569. }
  570. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  571. this._effect.setColor4("refractionLeftColor", this.refractionFresnelParameters.leftColor, this.refractionFresnelParameters.power);
  572. this._effect.setColor4("refractionRightColor", this.refractionFresnelParameters.rightColor, this.refractionFresnelParameters.bias);
  573. }
  574. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  575. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  576. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  577. }
  578. }
  579. // Textures
  580. if (scene.texturesEnabled) {
  581. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  582. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  583. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  584. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  585. }
  586. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  587. this._effect.setTexture("ambientSampler", this.ambientTexture);
  588. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  589. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  590. }
  591. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  592. this._effect.setTexture("opacitySampler", this.opacityTexture);
  593. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  594. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  595. }
  596. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  597. if (this.reflectionTexture.isCube) {
  598. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  599. }
  600. else {
  601. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  602. }
  603. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  604. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, this.roughness);
  605. }
  606. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  607. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  608. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  609. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  610. }
  611. if (this.lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  612. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  613. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  614. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  615. }
  616. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  617. this._effect.setTexture("specularSampler", this.specularTexture);
  618. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  619. this._effect.setMatrix("specularMatrix", this.specularTexture.getTextureMatrix());
  620. }
  621. if (this.bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  622. this._effect.setTexture("bumpSampler", this.bumpTexture);
  623. this._effect.setFloat3("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level, this.parallaxScaleBias);
  624. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  625. }
  626. if (this.refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  627. var depth = 1.0;
  628. if (this.refractionTexture.isCube) {
  629. this._effect.setTexture("refractionCubeSampler", this.refractionTexture);
  630. }
  631. else {
  632. this._effect.setTexture("refraction2DSampler", this.refractionTexture);
  633. this._effect.setMatrix("refractionMatrix", this.refractionTexture.getReflectionTextureMatrix());
  634. if (this.refractionTexture.depth) {
  635. depth = this.refractionTexture.depth;
  636. }
  637. }
  638. this._effect.setFloat4("vRefractionInfos", this.refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  639. }
  640. if (this.cameraColorGradingTexture && StandardMaterial.ColorGradingTextureEnabled) {
  641. BABYLON.ColorGradingTexture.Bind(this.cameraColorGradingTexture, this._effect);
  642. }
  643. }
  644. // Clip plane
  645. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  646. // Point size
  647. if (this.pointsCloud) {
  648. this._effect.setFloat("pointSize", this.pointSize);
  649. }
  650. // Colors
  651. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  652. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  653. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  654. if (this._defines.SPECULARTERM) {
  655. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  656. }
  657. this._effect.setColor3("vEmissiveColor", this.emissiveColor);
  658. }
  659. if (scene.getCachedMaterial() !== this || !this.isFrozen) {
  660. // Diffuse
  661. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  662. // Lights
  663. if (scene.lightsEnabled && !this.disableLighting) {
  664. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  665. }
  666. // View
  667. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture || this.refractionTexture) {
  668. this._effect.setMatrix("view", scene.getViewMatrix());
  669. }
  670. // Fog
  671. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  672. // Log. depth
  673. BABYLON.MaterialHelper.BindLogDepth(this._defines, this._effect, scene);
  674. // Color Curves
  675. if (this.cameraColorCurves) {
  676. BABYLON.ColorCurves.Bind(this.cameraColorCurves, this._effect);
  677. }
  678. }
  679. _super.prototype.bind.call(this, world, mesh);
  680. };
  681. StandardMaterial.prototype.getAnimatables = function () {
  682. var results = [];
  683. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  684. results.push(this.diffuseTexture);
  685. }
  686. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  687. results.push(this.ambientTexture);
  688. }
  689. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  690. results.push(this.opacityTexture);
  691. }
  692. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  693. results.push(this.reflectionTexture);
  694. }
  695. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  696. results.push(this.emissiveTexture);
  697. }
  698. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  699. results.push(this.specularTexture);
  700. }
  701. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  702. results.push(this.bumpTexture);
  703. }
  704. if (this.lightmapTexture && this.lightmapTexture.animations && this.lightmapTexture.animations.length > 0) {
  705. results.push(this.lightmapTexture);
  706. }
  707. if (this.refractionTexture && this.refractionTexture.animations && this.refractionTexture.animations.length > 0) {
  708. results.push(this.refractionTexture);
  709. }
  710. if (this.cameraColorGradingTexture && this.cameraColorGradingTexture.animations && this.cameraColorGradingTexture.animations.length > 0) {
  711. results.push(this.cameraColorGradingTexture);
  712. }
  713. return results;
  714. };
  715. StandardMaterial.prototype.dispose = function (forceDisposeEffect, forceDisposeTextures) {
  716. if (forceDisposeTextures) {
  717. if (this.diffuseTexture) {
  718. this.diffuseTexture.dispose();
  719. }
  720. if (this.ambientTexture) {
  721. this.ambientTexture.dispose();
  722. }
  723. if (this.opacityTexture) {
  724. this.opacityTexture.dispose();
  725. }
  726. if (this.reflectionTexture) {
  727. this.reflectionTexture.dispose();
  728. }
  729. if (this.emissiveTexture) {
  730. this.emissiveTexture.dispose();
  731. }
  732. if (this.specularTexture) {
  733. this.specularTexture.dispose();
  734. }
  735. if (this.bumpTexture) {
  736. this.bumpTexture.dispose();
  737. }
  738. if (this.lightmapTexture) {
  739. this.lightmapTexture.dispose();
  740. }
  741. if (this.refractionTexture) {
  742. this.refractionTexture.dispose();
  743. }
  744. if (this.cameraColorGradingTexture) {
  745. this.cameraColorGradingTexture.dispose();
  746. }
  747. }
  748. _super.prototype.dispose.call(this, forceDisposeEffect, forceDisposeTextures);
  749. };
  750. StandardMaterial.prototype.clone = function (name) {
  751. var _this = this;
  752. return BABYLON.SerializationHelper.Clone(function () { return new StandardMaterial(name, _this.getScene()); }, this);
  753. };
  754. StandardMaterial.prototype.serialize = function () {
  755. return BABYLON.SerializationHelper.Serialize(this);
  756. };
  757. // Statics
  758. StandardMaterial.Parse = function (source, scene, rootUrl) {
  759. return BABYLON.SerializationHelper.Parse(function () { return new StandardMaterial(source.name, scene); }, source, scene, rootUrl);
  760. };
  761. // Flags used to enable or disable a type of texture for all Standard Materials
  762. StandardMaterial.DiffuseTextureEnabled = true;
  763. StandardMaterial.AmbientTextureEnabled = true;
  764. StandardMaterial.OpacityTextureEnabled = true;
  765. StandardMaterial.ReflectionTextureEnabled = true;
  766. StandardMaterial.EmissiveTextureEnabled = true;
  767. StandardMaterial.SpecularTextureEnabled = true;
  768. StandardMaterial.BumpTextureEnabled = true;
  769. StandardMaterial.FresnelEnabled = true;
  770. StandardMaterial.LightmapTextureEnabled = true;
  771. StandardMaterial.RefractionTextureEnabled = true;
  772. StandardMaterial.ColorGradingTextureEnabled = true;
  773. __decorate([
  774. BABYLON.serializeAsTexture()
  775. ], StandardMaterial.prototype, "diffuseTexture", void 0);
  776. __decorate([
  777. BABYLON.serializeAsTexture()
  778. ], StandardMaterial.prototype, "ambientTexture", void 0);
  779. __decorate([
  780. BABYLON.serializeAsTexture()
  781. ], StandardMaterial.prototype, "opacityTexture", void 0);
  782. __decorate([
  783. BABYLON.serializeAsTexture()
  784. ], StandardMaterial.prototype, "reflectionTexture", void 0);
  785. __decorate([
  786. BABYLON.serializeAsTexture()
  787. ], StandardMaterial.prototype, "emissiveTexture", void 0);
  788. __decorate([
  789. BABYLON.serializeAsTexture()
  790. ], StandardMaterial.prototype, "specularTexture", void 0);
  791. __decorate([
  792. BABYLON.serializeAsTexture()
  793. ], StandardMaterial.prototype, "bumpTexture", void 0);
  794. __decorate([
  795. BABYLON.serializeAsTexture()
  796. ], StandardMaterial.prototype, "lightmapTexture", void 0);
  797. __decorate([
  798. BABYLON.serializeAsTexture()
  799. ], StandardMaterial.prototype, "refractionTexture", void 0);
  800. __decorate([
  801. BABYLON.serializeAsColor3("ambient")
  802. ], StandardMaterial.prototype, "ambientColor", void 0);
  803. __decorate([
  804. BABYLON.serializeAsColor3("diffuse")
  805. ], StandardMaterial.prototype, "diffuseColor", void 0);
  806. __decorate([
  807. BABYLON.serializeAsColor3("specular")
  808. ], StandardMaterial.prototype, "specularColor", void 0);
  809. __decorate([
  810. BABYLON.serializeAsColor3("emissive")
  811. ], StandardMaterial.prototype, "emissiveColor", void 0);
  812. __decorate([
  813. BABYLON.serialize()
  814. ], StandardMaterial.prototype, "specularPower", void 0);
  815. __decorate([
  816. BABYLON.serialize()
  817. ], StandardMaterial.prototype, "useAlphaFromDiffuseTexture", void 0);
  818. __decorate([
  819. BABYLON.serialize()
  820. ], StandardMaterial.prototype, "useEmissiveAsIllumination", void 0);
  821. __decorate([
  822. BABYLON.serialize()
  823. ], StandardMaterial.prototype, "linkEmissiveWithDiffuse", void 0);
  824. __decorate([
  825. BABYLON.serialize()
  826. ], StandardMaterial.prototype, "useReflectionFresnelFromSpecular", void 0);
  827. __decorate([
  828. BABYLON.serialize()
  829. ], StandardMaterial.prototype, "useSpecularOverAlpha", void 0);
  830. __decorate([
  831. BABYLON.serialize()
  832. ], StandardMaterial.prototype, "useReflectionOverAlpha", void 0);
  833. __decorate([
  834. BABYLON.serialize()
  835. ], StandardMaterial.prototype, "disableLighting", void 0);
  836. __decorate([
  837. BABYLON.serialize()
  838. ], StandardMaterial.prototype, "useParallax", void 0);
  839. __decorate([
  840. BABYLON.serialize()
  841. ], StandardMaterial.prototype, "useParallaxOcclusion", void 0);
  842. __decorate([
  843. BABYLON.serialize()
  844. ], StandardMaterial.prototype, "parallaxScaleBias", void 0);
  845. __decorate([
  846. BABYLON.serialize()
  847. ], StandardMaterial.prototype, "roughness", void 0);
  848. __decorate([
  849. BABYLON.serialize()
  850. ], StandardMaterial.prototype, "indexOfRefraction", void 0);
  851. __decorate([
  852. BABYLON.serialize()
  853. ], StandardMaterial.prototype, "invertRefractionY", void 0);
  854. __decorate([
  855. BABYLON.serialize()
  856. ], StandardMaterial.prototype, "useLightmapAsShadowmap", void 0);
  857. __decorate([
  858. BABYLON.serializeAsFresnelParameters()
  859. ], StandardMaterial.prototype, "diffuseFresnelParameters", void 0);
  860. __decorate([
  861. BABYLON.serializeAsFresnelParameters()
  862. ], StandardMaterial.prototype, "opacityFresnelParameters", void 0);
  863. __decorate([
  864. BABYLON.serializeAsFresnelParameters()
  865. ], StandardMaterial.prototype, "reflectionFresnelParameters", void 0);
  866. __decorate([
  867. BABYLON.serializeAsFresnelParameters()
  868. ], StandardMaterial.prototype, "refractionFresnelParameters", void 0);
  869. __decorate([
  870. BABYLON.serializeAsFresnelParameters()
  871. ], StandardMaterial.prototype, "emissiveFresnelParameters", void 0);
  872. __decorate([
  873. BABYLON.serialize()
  874. ], StandardMaterial.prototype, "useGlossinessFromSpecularMapAlpha", void 0);
  875. __decorate([
  876. BABYLON.serialize()
  877. ], StandardMaterial.prototype, "maxSimultaneousLights", void 0);
  878. __decorate([
  879. BABYLON.serialize()
  880. ], StandardMaterial.prototype, "invertNormalMapX", void 0);
  881. __decorate([
  882. BABYLON.serialize()
  883. ], StandardMaterial.prototype, "invertNormalMapY", void 0);
  884. __decorate([
  885. BABYLON.serializeAsTexture()
  886. ], StandardMaterial.prototype, "cameraColorGradingTexture", void 0);
  887. __decorate([
  888. BABYLON.serializeAsColorCurves()
  889. ], StandardMaterial.prototype, "cameraColorCurves", void 0);
  890. __decorate([
  891. BABYLON.serialize()
  892. ], StandardMaterial.prototype, "useLogarithmicDepth", null);
  893. return StandardMaterial;
  894. }(BABYLON.Material));
  895. BABYLON.StandardMaterial = StandardMaterial;
  896. })(BABYLON || (BABYLON = {}));