standardMaterial.ts 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. import { serialize, SerializationHelper, serializeAsColor3, expandToProperty, serializeAsFresnelParameters, serializeAsTexture } from "../Misc/decorators";
  2. import { Observer } from "../Misc/observable";
  3. import { SmartArray } from "../Misc/smartArray";
  4. import { IAnimatable } from '../Animations/animatable.interface';
  5. import { Nullable } from "../types";
  6. import { Scene } from "../scene";
  7. import { Matrix } from "../Maths/math.vector";
  8. import { Color3 } from '../Maths/math.color';
  9. import { VertexBuffer } from "../Meshes/buffer";
  10. import { SubMesh } from "../Meshes/subMesh";
  11. import { AbstractMesh } from "../Meshes/abstractMesh";
  12. import { Mesh } from "../Meshes/mesh";
  13. import { ImageProcessingConfiguration, IImageProcessingConfigurationDefines } from "./imageProcessingConfiguration";
  14. import { ColorCurves } from "./colorCurves";
  15. import { FresnelParameters } from "./fresnelParameters";
  16. import { Material } from "../Materials/material";
  17. import { MaterialDefines } from "../Materials/materialDefines";
  18. import { PushMaterial } from "./pushMaterial";
  19. import { MaterialHelper } from "./materialHelper";
  20. import { BaseTexture } from "../Materials/Textures/baseTexture";
  21. import { Texture } from "../Materials/Textures/texture";
  22. import { CubeTexture } from "../Materials/Textures/cubeTexture";
  23. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  24. import { _TypeStore } from "../Misc/typeStore";
  25. import { MaterialFlags } from "./materialFlags";
  26. import "../Shaders/default.fragment";
  27. import "../Shaders/default.vertex";
  28. import { Constants } from "../Engines/constants";
  29. import { EffectFallbacks } from './effectFallbacks';
  30. import { IEffectCreationOptions } from './effect';
  31. /** @hidden */
  32. export class StandardMaterialDefines extends MaterialDefines implements IImageProcessingConfigurationDefines {
  33. public MAINUV1 = false;
  34. public MAINUV2 = false;
  35. public DIFFUSE = false;
  36. public DIFFUSEDIRECTUV = 0;
  37. public AMBIENT = false;
  38. public AMBIENTDIRECTUV = 0;
  39. public OPACITY = false;
  40. public OPACITYDIRECTUV = 0;
  41. public OPACITYRGB = false;
  42. public REFLECTION = false;
  43. public EMISSIVE = false;
  44. public EMISSIVEDIRECTUV = 0;
  45. public SPECULAR = false;
  46. public SPECULARDIRECTUV = 0;
  47. public BUMP = false;
  48. public BUMPDIRECTUV = 0;
  49. public PARALLAX = false;
  50. public PARALLAXOCCLUSION = false;
  51. public SPECULAROVERALPHA = false;
  52. public CLIPPLANE = false;
  53. public CLIPPLANE2 = false;
  54. public CLIPPLANE3 = false;
  55. public CLIPPLANE4 = false;
  56. public CLIPPLANE5 = false;
  57. public CLIPPLANE6 = false;
  58. public ALPHATEST = false;
  59. public DEPTHPREPASS = false;
  60. public ALPHAFROMDIFFUSE = false;
  61. public POINTSIZE = false;
  62. public FOG = false;
  63. public SPECULARTERM = false;
  64. public DIFFUSEFRESNEL = false;
  65. public OPACITYFRESNEL = false;
  66. public REFLECTIONFRESNEL = false;
  67. public REFRACTIONFRESNEL = false;
  68. public EMISSIVEFRESNEL = false;
  69. public FRESNEL = false;
  70. public NORMAL = false;
  71. public UV1 = false;
  72. public UV2 = false;
  73. public VERTEXCOLOR = false;
  74. public VERTEXALPHA = false;
  75. public NUM_BONE_INFLUENCERS = 0;
  76. public BonesPerMesh = 0;
  77. public BONETEXTURE = false;
  78. public INSTANCES = false;
  79. public GLOSSINESS = false;
  80. public ROUGHNESS = false;
  81. public EMISSIVEASILLUMINATION = false;
  82. public LINKEMISSIVEWITHDIFFUSE = false;
  83. public REFLECTIONFRESNELFROMSPECULAR = false;
  84. public LIGHTMAP = false;
  85. public LIGHTMAPDIRECTUV = 0;
  86. public OBJECTSPACE_NORMALMAP = false;
  87. public USELIGHTMAPASSHADOWMAP = false;
  88. public REFLECTIONMAP_3D = false;
  89. public REFLECTIONMAP_SPHERICAL = false;
  90. public REFLECTIONMAP_PLANAR = false;
  91. public REFLECTIONMAP_CUBIC = false;
  92. public USE_LOCAL_REFLECTIONMAP_CUBIC = false;
  93. public REFLECTIONMAP_PROJECTION = false;
  94. public REFLECTIONMAP_SKYBOX = false;
  95. public REFLECTIONMAP_EXPLICIT = false;
  96. public REFLECTIONMAP_EQUIRECTANGULAR = false;
  97. public REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  98. public REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  99. public INVERTCUBICMAP = false;
  100. public LOGARITHMICDEPTH = false;
  101. public REFRACTION = false;
  102. public REFRACTIONMAP_3D = false;
  103. public REFLECTIONOVERALPHA = false;
  104. public TWOSIDEDLIGHTING = false;
  105. public SHADOWFLOAT = false;
  106. public MORPHTARGETS = false;
  107. public MORPHTARGETS_NORMAL = false;
  108. public MORPHTARGETS_TANGENT = false;
  109. public MORPHTARGETS_UV = false;
  110. public NUM_MORPH_INFLUENCERS = 0;
  111. public NONUNIFORMSCALING = false; // https://playground.babylonjs.com#V6DWIH
  112. public PREMULTIPLYALPHA = false; // https://playground.babylonjs.com#LNVJJ7
  113. public ALPHATEST_AFTERALLALPHACOMPUTATIONS = false;
  114. public ALPHABLEND = true;
  115. public IMAGEPROCESSING = false;
  116. public VIGNETTE = false;
  117. public VIGNETTEBLENDMODEMULTIPLY = false;
  118. public VIGNETTEBLENDMODEOPAQUE = false;
  119. public TONEMAPPING = false;
  120. public TONEMAPPING_ACES = false;
  121. public CONTRAST = false;
  122. public COLORCURVES = false;
  123. public COLORGRADING = false;
  124. public COLORGRADING3D = false;
  125. public SAMPLER3DGREENDEPTH = false;
  126. public SAMPLER3DBGRMAP = false;
  127. public IMAGEPROCESSINGPOSTPROCESS = false;
  128. public MULTIVIEW = false;
  129. /**
  130. * If the reflection texture on this material is in linear color space
  131. * @hidden
  132. */
  133. public IS_REFLECTION_LINEAR = false;
  134. /**
  135. * If the refraction texture on this material is in linear color space
  136. * @hidden
  137. */
  138. public IS_REFRACTION_LINEAR = false;
  139. public EXPOSURE = false;
  140. constructor() {
  141. super();
  142. this.rebuild();
  143. }
  144. public setReflectionMode(modeToEnable: string) {
  145. var modes = [
  146. "REFLECTIONMAP_CUBIC", "REFLECTIONMAP_EXPLICIT", "REFLECTIONMAP_PLANAR",
  147. "REFLECTIONMAP_PROJECTION", "REFLECTIONMAP_PROJECTION", "REFLECTIONMAP_SKYBOX",
  148. "REFLECTIONMAP_SPHERICAL", "REFLECTIONMAP_EQUIRECTANGULAR", "REFLECTIONMAP_EQUIRECTANGULAR_FIXED",
  149. "REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED"
  150. ];
  151. for (var mode of modes) {
  152. (<any>this)[mode] = (mode === modeToEnable);
  153. }
  154. }
  155. }
  156. /**
  157. * This is the default material used in Babylon. It is the best trade off between quality
  158. * and performances.
  159. * @see http://doc.babylonjs.com/babylon101/materials
  160. */
  161. export class StandardMaterial extends PushMaterial {
  162. @serializeAsTexture("diffuseTexture")
  163. private _diffuseTexture: Nullable<BaseTexture> = null;
  164. /**
  165. * The basic texture of the material as viewed under a light.
  166. */
  167. @expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  168. public diffuseTexture: Nullable<BaseTexture>;
  169. @serializeAsTexture("ambientTexture")
  170. private _ambientTexture: Nullable<BaseTexture> = null;
  171. /**
  172. * AKA Occlusion Texture in other nomenclature, it helps adding baked shadows into your material.
  173. */
  174. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  175. public ambientTexture: Nullable<BaseTexture>;
  176. @serializeAsTexture("opacityTexture")
  177. private _opacityTexture: Nullable<BaseTexture> = null;
  178. /**
  179. * Define the transparency of the material from a texture.
  180. * The final alpha value can be read either from the red channel (if texture.getAlphaFromRGB is false)
  181. * or from the luminance or the current texel (if texture.getAlphaFromRGB is true)
  182. */
  183. @expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  184. public opacityTexture: Nullable<BaseTexture>;
  185. @serializeAsTexture("reflectionTexture")
  186. private _reflectionTexture: Nullable<BaseTexture> = null;
  187. /**
  188. * Define the texture used to display the reflection.
  189. * @see http://doc.babylonjs.com/how_to/reflect#how-to-obtain-reflections-and-refractions
  190. */
  191. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  192. public reflectionTexture: Nullable<BaseTexture>;
  193. @serializeAsTexture("emissiveTexture")
  194. private _emissiveTexture: Nullable<BaseTexture> = null;
  195. /**
  196. * Define texture of the material as if self lit.
  197. * This will be mixed in the final result even in the absence of light.
  198. */
  199. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  200. public emissiveTexture: Nullable<BaseTexture>;
  201. @serializeAsTexture("specularTexture")
  202. private _specularTexture: Nullable<BaseTexture> = null;
  203. /**
  204. * Define how the color and intensity of the highlight given by the light in the material.
  205. */
  206. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  207. public specularTexture: Nullable<BaseTexture>;
  208. @serializeAsTexture("bumpTexture")
  209. private _bumpTexture: Nullable<BaseTexture> = null;
  210. /**
  211. * Bump mapping is a technique to simulate bump and dents on a rendered surface.
  212. * These are made by creating a normal map from an image. The means to do this can be found on the web, a search for 'normal map generator' will bring up free and paid for methods of doing this.
  213. * @see http://doc.babylonjs.com/how_to/more_materials#bump-map
  214. */
  215. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  216. public bumpTexture: Nullable<BaseTexture>;
  217. @serializeAsTexture("lightmapTexture")
  218. private _lightmapTexture: Nullable<BaseTexture> = null;
  219. /**
  220. * Complex lighting can be computationally expensive to compute at runtime.
  221. * To save on computation, lightmaps may be used to store calculated lighting in a texture which will be applied to a given mesh.
  222. * @see http://doc.babylonjs.com/babylon101/lights#lightmaps
  223. */
  224. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  225. public lightmapTexture: Nullable<BaseTexture>;
  226. @serializeAsTexture("refractionTexture")
  227. private _refractionTexture: Nullable<BaseTexture> = null;
  228. /**
  229. * Define the texture used to display the refraction.
  230. * @see http://doc.babylonjs.com/how_to/reflect#how-to-obtain-reflections-and-refractions
  231. */
  232. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  233. public refractionTexture: Nullable<BaseTexture>;
  234. /**
  235. * The color of the material lit by the environmental background lighting.
  236. * @see http://doc.babylonjs.com/babylon101/materials#ambient-color-example
  237. */
  238. @serializeAsColor3("ambient")
  239. public ambientColor = new Color3(0, 0, 0);
  240. /**
  241. * The basic color of the material as viewed under a light.
  242. */
  243. @serializeAsColor3("diffuse")
  244. public diffuseColor = new Color3(1, 1, 1);
  245. /**
  246. * Define how the color and intensity of the highlight given by the light in the material.
  247. */
  248. @serializeAsColor3("specular")
  249. public specularColor = new Color3(1, 1, 1);
  250. /**
  251. * Define the color of the material as if self lit.
  252. * This will be mixed in the final result even in the absence of light.
  253. */
  254. @serializeAsColor3("emissive")
  255. public emissiveColor = new Color3(0, 0, 0);
  256. /**
  257. * Defines how sharp are the highlights in the material.
  258. * The bigger the value the sharper giving a more glossy feeling to the result.
  259. * Reversely, the smaller the value the blurrier giving a more rough feeling to the result.
  260. */
  261. @serialize()
  262. public specularPower = 64;
  263. @serialize("useAlphaFromDiffuseTexture")
  264. private _useAlphaFromDiffuseTexture = false;
  265. /**
  266. * Does the transparency come from the diffuse texture alpha channel.
  267. */
  268. @expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  269. public useAlphaFromDiffuseTexture: boolean;
  270. @serialize("useEmissiveAsIllumination")
  271. private _useEmissiveAsIllumination = false;
  272. /**
  273. * If true, the emissive value is added into the end result, otherwise it is multiplied in.
  274. */
  275. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  276. public useEmissiveAsIllumination: boolean;
  277. @serialize("linkEmissiveWithDiffuse")
  278. private _linkEmissiveWithDiffuse = false;
  279. /**
  280. * If true, some kind of energy conservation will prevent the end result to be more than 1 by reducing
  281. * the emissive level when the final color is close to one.
  282. */
  283. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  284. public linkEmissiveWithDiffuse: boolean;
  285. @serialize("useSpecularOverAlpha")
  286. private _useSpecularOverAlpha = false;
  287. /**
  288. * Specifies that the material will keep the specular highlights over a transparent surface (only the most limunous ones).
  289. * A car glass is a good exemple of that. When sun reflects on it you can not see what is behind.
  290. */
  291. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  292. public useSpecularOverAlpha: boolean;
  293. @serialize("useReflectionOverAlpha")
  294. private _useReflectionOverAlpha = false;
  295. /**
  296. * Specifies that the material will keeps the reflection highlights over a transparent surface (only the most limunous ones).
  297. * A car glass is a good exemple of that. When the street lights reflects on it you can not see what is behind.
  298. */
  299. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  300. public useReflectionOverAlpha: boolean;
  301. @serialize("disableLighting")
  302. private _disableLighting = false;
  303. /**
  304. * Does lights from the scene impacts this material.
  305. * It can be a nice trick for performance to disable lighting on a fully emissive material.
  306. */
  307. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  308. public disableLighting: boolean;
  309. @serialize("useObjectSpaceNormalMap")
  310. private _useObjectSpaceNormalMap = false;
  311. /**
  312. * Allows using an object space normal map (instead of tangent space).
  313. */
  314. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  315. public useObjectSpaceNormalMap: boolean;
  316. @serialize("useParallax")
  317. private _useParallax = false;
  318. /**
  319. * Is parallax enabled or not.
  320. * @see http://doc.babylonjs.com/how_to/using_parallax_mapping
  321. */
  322. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  323. public useParallax: boolean;
  324. @serialize("useParallaxOcclusion")
  325. private _useParallaxOcclusion = false;
  326. /**
  327. * Is parallax occlusion enabled or not.
  328. * If true, the outcome is way more realistic than traditional Parallax but you can expect a performance hit that worthes consideration.
  329. * @see http://doc.babylonjs.com/how_to/using_parallax_mapping
  330. */
  331. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  332. public useParallaxOcclusion: boolean;
  333. /**
  334. * Apply a scaling factor that determine which "depth" the height map should reprensent. A value between 0.05 and 0.1 is reasonnable in Parallax, you can reach 0.2 using Parallax Occlusion.
  335. */
  336. @serialize()
  337. public parallaxScaleBias = 0.05;
  338. @serialize("roughness")
  339. private _roughness = 0;
  340. /**
  341. * Helps to define how blurry the reflections should appears in the material.
  342. */
  343. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  344. public roughness: number;
  345. /**
  346. * In case of refraction, define the value of the index of refraction.
  347. * @see http://doc.babylonjs.com/how_to/reflect#how-to-obtain-reflections-and-refractions
  348. */
  349. @serialize()
  350. public indexOfRefraction = 0.98;
  351. /**
  352. * Invert the refraction texture alongside the y axis.
  353. * It can be useful with procedural textures or probe for instance.
  354. * @see http://doc.babylonjs.com/how_to/reflect#how-to-obtain-reflections-and-refractions
  355. */
  356. @serialize()
  357. public invertRefractionY = true;
  358. /**
  359. * Defines the alpha limits in alpha test mode.
  360. */
  361. @serialize()
  362. public alphaCutOff = 0.4;
  363. @serialize("useLightmapAsShadowmap")
  364. private _useLightmapAsShadowmap = false;
  365. /**
  366. * In case of light mapping, define whether the map contains light or shadow informations.
  367. */
  368. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  369. public useLightmapAsShadowmap: boolean;
  370. // Fresnel
  371. @serializeAsFresnelParameters("diffuseFresnelParameters")
  372. private _diffuseFresnelParameters: FresnelParameters;
  373. /**
  374. * Define the diffuse fresnel parameters of the material.
  375. * @see http://doc.babylonjs.com/how_to/how_to_use_fresnelparameters
  376. */
  377. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  378. public diffuseFresnelParameters: FresnelParameters;
  379. @serializeAsFresnelParameters("opacityFresnelParameters")
  380. private _opacityFresnelParameters: FresnelParameters;
  381. /**
  382. * Define the opacity fresnel parameters of the material.
  383. * @see http://doc.babylonjs.com/how_to/how_to_use_fresnelparameters
  384. */
  385. @expandToProperty("_markAllSubMeshesAsFresnelAndMiscDirty")
  386. public opacityFresnelParameters: FresnelParameters;
  387. @serializeAsFresnelParameters("reflectionFresnelParameters")
  388. private _reflectionFresnelParameters: FresnelParameters;
  389. /**
  390. * Define the reflection fresnel parameters of the material.
  391. * @see http://doc.babylonjs.com/how_to/how_to_use_fresnelparameters
  392. */
  393. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  394. public reflectionFresnelParameters: FresnelParameters;
  395. @serializeAsFresnelParameters("refractionFresnelParameters")
  396. private _refractionFresnelParameters: FresnelParameters;
  397. /**
  398. * Define the refraction fresnel parameters of the material.
  399. * @see http://doc.babylonjs.com/how_to/how_to_use_fresnelparameters
  400. */
  401. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  402. public refractionFresnelParameters: FresnelParameters;
  403. @serializeAsFresnelParameters("emissiveFresnelParameters")
  404. private _emissiveFresnelParameters: FresnelParameters;
  405. /**
  406. * Define the emissive fresnel parameters of the material.
  407. * @see http://doc.babylonjs.com/how_to/how_to_use_fresnelparameters
  408. */
  409. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  410. public emissiveFresnelParameters: FresnelParameters;
  411. @serialize("useReflectionFresnelFromSpecular")
  412. private _useReflectionFresnelFromSpecular = false;
  413. /**
  414. * If true automatically deducts the fresnels values from the material specularity.
  415. * @see http://doc.babylonjs.com/how_to/how_to_use_fresnelparameters
  416. */
  417. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  418. public useReflectionFresnelFromSpecular: boolean;
  419. @serialize("useGlossinessFromSpecularMapAlpha")
  420. private _useGlossinessFromSpecularMapAlpha = false;
  421. /**
  422. * Defines if the glossiness/roughness of the material should be read from the specular map alpha channel
  423. */
  424. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  425. public useGlossinessFromSpecularMapAlpha: boolean;
  426. @serialize("maxSimultaneousLights")
  427. private _maxSimultaneousLights = 4;
  428. /**
  429. * Defines the maximum number of lights that can be used in the material
  430. */
  431. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  432. public maxSimultaneousLights: number;
  433. @serialize("invertNormalMapX")
  434. private _invertNormalMapX = false;
  435. /**
  436. * If sets to true, x component of normal map value will invert (x = 1.0 - x).
  437. */
  438. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  439. public invertNormalMapX: boolean;
  440. @serialize("invertNormalMapY")
  441. private _invertNormalMapY = false;
  442. /**
  443. * If sets to true, y component of normal map value will invert (y = 1.0 - y).
  444. */
  445. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  446. public invertNormalMapY: boolean;
  447. @serialize("twoSidedLighting")
  448. private _twoSidedLighting = false;
  449. /**
  450. * If sets to true and backfaceCulling is false, normals will be flipped on the backside.
  451. */
  452. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  453. public twoSidedLighting: boolean;
  454. /**
  455. * Default configuration related to image processing available in the standard Material.
  456. */
  457. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  458. /**
  459. * Gets the image processing configuration used either in this material.
  460. */
  461. public get imageProcessingConfiguration(): ImageProcessingConfiguration {
  462. return this._imageProcessingConfiguration;
  463. }
  464. /**
  465. * Sets the Default image processing configuration used either in the this material.
  466. *
  467. * If sets to null, the scene one is in use.
  468. */
  469. public set imageProcessingConfiguration(value: ImageProcessingConfiguration) {
  470. this._attachImageProcessingConfiguration(value);
  471. // Ensure the effect will be rebuilt.
  472. this._markAllSubMeshesAsTexturesDirty();
  473. }
  474. /**
  475. * Keep track of the image processing observer to allow dispose and replace.
  476. */
  477. private _imageProcessingObserver: Nullable<Observer<ImageProcessingConfiguration>>;
  478. /**
  479. * Attaches a new image processing configuration to the Standard Material.
  480. * @param configuration
  481. */
  482. protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>): void {
  483. if (configuration === this._imageProcessingConfiguration) {
  484. return;
  485. }
  486. // Detaches observer
  487. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  488. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  489. }
  490. // Pick the scene configuration if needed
  491. if (!configuration) {
  492. this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
  493. }
  494. else {
  495. this._imageProcessingConfiguration = configuration;
  496. }
  497. // Attaches observer
  498. if (this._imageProcessingConfiguration) {
  499. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(() => {
  500. this._markAllSubMeshesAsImageProcessingDirty();
  501. });
  502. }
  503. }
  504. /**
  505. * Gets wether the color curves effect is enabled.
  506. */
  507. public get cameraColorCurvesEnabled(): boolean {
  508. return this.imageProcessingConfiguration.colorCurvesEnabled;
  509. }
  510. /**
  511. * Sets wether the color curves effect is enabled.
  512. */
  513. public set cameraColorCurvesEnabled(value: boolean) {
  514. this.imageProcessingConfiguration.colorCurvesEnabled = value;
  515. }
  516. /**
  517. * Gets wether the color grading effect is enabled.
  518. */
  519. public get cameraColorGradingEnabled(): boolean {
  520. return this.imageProcessingConfiguration.colorGradingEnabled;
  521. }
  522. /**
  523. * Gets wether the color grading effect is enabled.
  524. */
  525. public set cameraColorGradingEnabled(value: boolean) {
  526. this.imageProcessingConfiguration.colorGradingEnabled = value;
  527. }
  528. /**
  529. * Gets wether tonemapping is enabled or not.
  530. */
  531. public get cameraToneMappingEnabled(): boolean {
  532. return this._imageProcessingConfiguration.toneMappingEnabled;
  533. }
  534. /**
  535. * Sets wether tonemapping is enabled or not
  536. */
  537. public set cameraToneMappingEnabled(value: boolean) {
  538. this._imageProcessingConfiguration.toneMappingEnabled = value;
  539. }
  540. /**
  541. * The camera exposure used on this material.
  542. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  543. * This corresponds to a photographic exposure.
  544. */
  545. public get cameraExposure(): number {
  546. return this._imageProcessingConfiguration.exposure;
  547. }
  548. /**
  549. * The camera exposure used on this material.
  550. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  551. * This corresponds to a photographic exposure.
  552. */
  553. public set cameraExposure(value: number) {
  554. this._imageProcessingConfiguration.exposure = value;
  555. }
  556. /**
  557. * Gets The camera contrast used on this material.
  558. */
  559. public get cameraContrast(): number {
  560. return this._imageProcessingConfiguration.contrast;
  561. }
  562. /**
  563. * Sets The camera contrast used on this material.
  564. */
  565. public set cameraContrast(value: number) {
  566. this._imageProcessingConfiguration.contrast = value;
  567. }
  568. /**
  569. * Gets the Color Grading 2D Lookup Texture.
  570. */
  571. public get cameraColorGradingTexture(): Nullable<BaseTexture> {
  572. return this._imageProcessingConfiguration.colorGradingTexture;
  573. }
  574. /**
  575. * Sets the Color Grading 2D Lookup Texture.
  576. */
  577. public set cameraColorGradingTexture(value: Nullable<BaseTexture>) {
  578. this._imageProcessingConfiguration.colorGradingTexture = value;
  579. }
  580. /**
  581. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  582. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  583. * 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;
  584. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  585. */
  586. public get cameraColorCurves(): Nullable<ColorCurves> {
  587. return this._imageProcessingConfiguration.colorCurves;
  588. }
  589. /**
  590. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  591. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  592. * 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;
  593. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  594. */
  595. public set cameraColorCurves(value: Nullable<ColorCurves>) {
  596. this._imageProcessingConfiguration.colorCurves = value;
  597. }
  598. /**
  599. * Custom callback helping to override the default shader used in the material.
  600. */
  601. public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines, attributes?: string[]) => string;
  602. protected _renderTargets = new SmartArray<RenderTargetTexture>(16);
  603. protected _worldViewProjectionMatrix = Matrix.Zero();
  604. protected _globalAmbientColor = new Color3(0, 0, 0);
  605. protected _useLogarithmicDepth: boolean;
  606. protected _rebuildInParallel = false;
  607. /**
  608. * Instantiates a new standard material.
  609. * This is the default material used in Babylon. It is the best trade off between quality
  610. * and performances.
  611. * @see http://doc.babylonjs.com/babylon101/materials
  612. * @param name Define the name of the material in the scene
  613. * @param scene Define the scene the material belong to
  614. */
  615. constructor(name: string, scene: Scene) {
  616. super(name, scene);
  617. // Setup the default processing configuration to the scene.
  618. this._attachImageProcessingConfiguration(null);
  619. this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
  620. this._renderTargets.reset();
  621. if (StandardMaterial.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  622. this._renderTargets.push(<RenderTargetTexture>this._reflectionTexture);
  623. }
  624. if (StandardMaterial.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  625. this._renderTargets.push(<RenderTargetTexture>this._refractionTexture);
  626. }
  627. return this._renderTargets;
  628. };
  629. }
  630. /**
  631. * Gets a boolean indicating that current material needs to register RTT
  632. */
  633. public get hasRenderTargetTextures(): boolean {
  634. if (StandardMaterial.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  635. return true;
  636. }
  637. if (StandardMaterial.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  638. return true;
  639. }
  640. return false;
  641. }
  642. /**
  643. * Gets the current class name of the material e.g. "StandardMaterial"
  644. * Mainly use in serialization.
  645. * @returns the class name
  646. */
  647. public getClassName(): string {
  648. return "StandardMaterial";
  649. }
  650. /**
  651. * In case the depth buffer does not allow enough depth precision for your scene (might be the case in large scenes)
  652. * You can try switching to logarithmic depth.
  653. * @see http://doc.babylonjs.com/how_to/using_logarithmic_depth_buffer
  654. */
  655. @serialize()
  656. public get useLogarithmicDepth(): boolean {
  657. return this._useLogarithmicDepth;
  658. }
  659. public set useLogarithmicDepth(value: boolean) {
  660. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  661. this._markAllSubMeshesAsMiscDirty();
  662. }
  663. /**
  664. * Specifies if the material will require alpha blending
  665. * @returns a boolean specifying if alpha blending is needed
  666. */
  667. public needAlphaBlending(): boolean {
  668. if (this._disableAlphaBlending) {
  669. return false;
  670. }
  671. return (this.alpha < 1.0) || (this._opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled;
  672. }
  673. /**
  674. * Specifies if this material should be rendered in alpha test mode
  675. * @returns a boolean specifying if an alpha test is needed.
  676. */
  677. public needAlphaTesting(): boolean {
  678. if (this._forceAlphaTest) {
  679. return true;
  680. }
  681. return this._diffuseTexture != null && this._diffuseTexture.hasAlpha && (this._transparencyMode == null || this._transparencyMode === Material.MATERIAL_ALPHATEST);
  682. }
  683. protected _shouldUseAlphaFromDiffuseTexture(): boolean {
  684. return this._diffuseTexture != null && this._diffuseTexture.hasAlpha && this._useAlphaFromDiffuseTexture && this._transparencyMode !== Material.MATERIAL_OPAQUE;
  685. }
  686. /**
  687. * Get the texture used for alpha test purpose.
  688. * @returns the diffuse texture in case of the standard material.
  689. */
  690. public getAlphaTestTexture(): Nullable<BaseTexture> {
  691. return this._diffuseTexture;
  692. }
  693. /**
  694. * Get if the submesh is ready to be used and all its information available.
  695. * Child classes can use it to update shaders
  696. * @param mesh defines the mesh to check
  697. * @param subMesh defines which submesh to check
  698. * @param useInstances specifies that instances should be used
  699. * @returns a boolean indicating that the submesh is ready or not
  700. */
  701. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances: boolean = false): boolean {
  702. if (subMesh.effect && this.isFrozen) {
  703. if (subMesh.effect._wasPreviouslyReady) {
  704. return true;
  705. }
  706. }
  707. if (!subMesh._materialDefines) {
  708. subMesh._materialDefines = new StandardMaterialDefines();
  709. }
  710. var scene = this.getScene();
  711. var defines = <StandardMaterialDefines>subMesh._materialDefines;
  712. if (this._isReadyForSubMesh(subMesh)) {
  713. return true;
  714. }
  715. var engine = scene.getEngine();
  716. // Lights
  717. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
  718. // Multiview
  719. MaterialHelper.PrepareDefinesForMultiview(scene, defines);
  720. // Textures
  721. if (defines._areTexturesDirty) {
  722. defines._needUVs = false;
  723. defines.MAINUV1 = false;
  724. defines.MAINUV2 = false;
  725. if (scene.texturesEnabled) {
  726. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  727. if (!this._diffuseTexture.isReadyOrNotBlocking()) {
  728. return false;
  729. } else {
  730. MaterialHelper.PrepareDefinesForMergedUV(this._diffuseTexture, defines, "DIFFUSE");
  731. }
  732. } else {
  733. defines.DIFFUSE = false;
  734. }
  735. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  736. if (!this._ambientTexture.isReadyOrNotBlocking()) {
  737. return false;
  738. } else {
  739. MaterialHelper.PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
  740. }
  741. } else {
  742. defines.AMBIENT = false;
  743. }
  744. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  745. if (!this._opacityTexture.isReadyOrNotBlocking()) {
  746. return false;
  747. } else {
  748. MaterialHelper.PrepareDefinesForMergedUV(this._opacityTexture, defines, "OPACITY");
  749. defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
  750. }
  751. } else {
  752. defines.OPACITY = false;
  753. }
  754. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  755. if (!this._reflectionTexture.isReadyOrNotBlocking()) {
  756. return false;
  757. } else {
  758. defines._needNormals = true;
  759. defines.REFLECTION = true;
  760. defines.ROUGHNESS = (this._roughness > 0);
  761. defines.REFLECTIONOVERALPHA = this._useReflectionOverAlpha;
  762. defines.INVERTCUBICMAP = (this._reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE);
  763. defines.REFLECTIONMAP_3D = this._reflectionTexture.isCube;
  764. switch (this._reflectionTexture.coordinatesMode) {
  765. case Texture.EXPLICIT_MODE:
  766. defines.setReflectionMode("REFLECTIONMAP_EXPLICIT");
  767. break;
  768. case Texture.PLANAR_MODE:
  769. defines.setReflectionMode("REFLECTIONMAP_PLANAR");
  770. break;
  771. case Texture.PROJECTION_MODE:
  772. defines.setReflectionMode("REFLECTIONMAP_PROJECTION");
  773. break;
  774. case Texture.SKYBOX_MODE:
  775. defines.setReflectionMode("REFLECTIONMAP_SKYBOX");
  776. break;
  777. case Texture.SPHERICAL_MODE:
  778. defines.setReflectionMode("REFLECTIONMAP_SPHERICAL");
  779. break;
  780. case Texture.EQUIRECTANGULAR_MODE:
  781. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR");
  782. break;
  783. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  784. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR_FIXED");
  785. break;
  786. case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
  787. defines.setReflectionMode("REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED");
  788. break;
  789. case Texture.CUBIC_MODE:
  790. case Texture.INVCUBIC_MODE:
  791. default:
  792. defines.setReflectionMode("REFLECTIONMAP_CUBIC");
  793. break;
  794. }
  795. defines.USE_LOCAL_REFLECTIONMAP_CUBIC = (<any>this._reflectionTexture).boundingBoxSize ? true : false;
  796. }
  797. } else {
  798. defines.REFLECTION = false;
  799. }
  800. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  801. if (!this._emissiveTexture.isReadyOrNotBlocking()) {
  802. return false;
  803. } else {
  804. MaterialHelper.PrepareDefinesForMergedUV(this._emissiveTexture, defines, "EMISSIVE");
  805. }
  806. } else {
  807. defines.EMISSIVE = false;
  808. }
  809. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  810. if (!this._lightmapTexture.isReadyOrNotBlocking()) {
  811. return false;
  812. } else {
  813. MaterialHelper.PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
  814. defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
  815. }
  816. } else {
  817. defines.LIGHTMAP = false;
  818. }
  819. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  820. if (!this._specularTexture.isReadyOrNotBlocking()) {
  821. return false;
  822. } else {
  823. MaterialHelper.PrepareDefinesForMergedUV(this._specularTexture, defines, "SPECULAR");
  824. defines.GLOSSINESS = this._useGlossinessFromSpecularMapAlpha;
  825. }
  826. } else {
  827. defines.SPECULAR = false;
  828. }
  829. if (scene.getEngine().getCaps().standardDerivatives && this._bumpTexture && StandardMaterial.BumpTextureEnabled) {
  830. // Bump texure can not be not blocking.
  831. if (!this._bumpTexture.isReady()) {
  832. return false;
  833. } else {
  834. MaterialHelper.PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
  835. defines.PARALLAX = this._useParallax;
  836. defines.PARALLAXOCCLUSION = this._useParallaxOcclusion;
  837. }
  838. defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
  839. } else {
  840. defines.BUMP = false;
  841. }
  842. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  843. if (!this._refractionTexture.isReadyOrNotBlocking()) {
  844. return false;
  845. } else {
  846. defines._needUVs = true;
  847. defines.REFRACTION = true;
  848. defines.REFRACTIONMAP_3D = this._refractionTexture.isCube;
  849. }
  850. } else {
  851. defines.REFRACTION = false;
  852. }
  853. defines.TWOSIDEDLIGHTING = !this._backFaceCulling && this._twoSidedLighting;
  854. } else {
  855. defines.DIFFUSE = false;
  856. defines.AMBIENT = false;
  857. defines.OPACITY = false;
  858. defines.REFLECTION = false;
  859. defines.EMISSIVE = false;
  860. defines.LIGHTMAP = false;
  861. defines.BUMP = false;
  862. defines.REFRACTION = false;
  863. }
  864. defines.ALPHAFROMDIFFUSE = this._shouldUseAlphaFromDiffuseTexture();
  865. defines.EMISSIVEASILLUMINATION = this._useEmissiveAsIllumination;
  866. defines.LINKEMISSIVEWITHDIFFUSE = this._linkEmissiveWithDiffuse;
  867. defines.SPECULAROVERALPHA = this._useSpecularOverAlpha;
  868. defines.PREMULTIPLYALPHA = (this.alphaMode === Constants.ALPHA_PREMULTIPLIED || this.alphaMode === Constants.ALPHA_PREMULTIPLIED_PORTERDUFF);
  869. defines.ALPHATEST_AFTERALLALPHACOMPUTATIONS = this.transparencyMode !== null;
  870. defines.ALPHABLEND = this.transparencyMode === null || this.needAlphaBlendingForMesh(mesh); // check on null for backward compatibility
  871. }
  872. if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
  873. if (!this._imageProcessingConfiguration.isReady()) {
  874. return false;
  875. }
  876. this._imageProcessingConfiguration.prepareDefines(defines);
  877. defines.IS_REFLECTION_LINEAR = (this.reflectionTexture != null && !this.reflectionTexture.gammaSpace);
  878. defines.IS_REFRACTION_LINEAR = (this.refractionTexture != null && !this.refractionTexture.gammaSpace);
  879. }
  880. if (defines._areFresnelDirty) {
  881. if (StandardMaterial.FresnelEnabled) {
  882. // Fresnel
  883. if (this._diffuseFresnelParameters || this._opacityFresnelParameters ||
  884. this._emissiveFresnelParameters || this._refractionFresnelParameters ||
  885. this._reflectionFresnelParameters) {
  886. defines.DIFFUSEFRESNEL = (this._diffuseFresnelParameters && this._diffuseFresnelParameters.isEnabled);
  887. defines.OPACITYFRESNEL = (this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled);
  888. defines.REFLECTIONFRESNEL = (this._reflectionFresnelParameters && this._reflectionFresnelParameters.isEnabled);
  889. defines.REFLECTIONFRESNELFROMSPECULAR = this._useReflectionFresnelFromSpecular;
  890. defines.REFRACTIONFRESNEL = (this._refractionFresnelParameters && this._refractionFresnelParameters.isEnabled);
  891. defines.EMISSIVEFRESNEL = (this._emissiveFresnelParameters && this._emissiveFresnelParameters.isEnabled);
  892. defines._needNormals = true;
  893. defines.FRESNEL = true;
  894. }
  895. } else {
  896. defines.FRESNEL = false;
  897. }
  898. }
  899. // Misc.
  900. MaterialHelper.PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh) || this._forceAlphaTest, defines);
  901. // Attribs
  902. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true, true);
  903. // Values that need to be evaluated on every frame
  904. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  905. // Get correct effect
  906. if (defines.isDirty) {
  907. const lightDisposed = defines._areLightsDisposed;
  908. defines.markAsProcessed();
  909. // Fallbacks
  910. var fallbacks = new EffectFallbacks();
  911. if (defines.REFLECTION) {
  912. fallbacks.addFallback(0, "REFLECTION");
  913. }
  914. if (defines.SPECULAR) {
  915. fallbacks.addFallback(0, "SPECULAR");
  916. }
  917. if (defines.BUMP) {
  918. fallbacks.addFallback(0, "BUMP");
  919. }
  920. if (defines.PARALLAX) {
  921. fallbacks.addFallback(1, "PARALLAX");
  922. }
  923. if (defines.PARALLAXOCCLUSION) {
  924. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  925. }
  926. if (defines.SPECULAROVERALPHA) {
  927. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  928. }
  929. if (defines.FOG) {
  930. fallbacks.addFallback(1, "FOG");
  931. }
  932. if (defines.POINTSIZE) {
  933. fallbacks.addFallback(0, "POINTSIZE");
  934. }
  935. if (defines.LOGARITHMICDEPTH) {
  936. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  937. }
  938. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights);
  939. if (defines.SPECULARTERM) {
  940. fallbacks.addFallback(0, "SPECULARTERM");
  941. }
  942. if (defines.DIFFUSEFRESNEL) {
  943. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  944. }
  945. if (defines.OPACITYFRESNEL) {
  946. fallbacks.addFallback(2, "OPACITYFRESNEL");
  947. }
  948. if (defines.REFLECTIONFRESNEL) {
  949. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  950. }
  951. if (defines.EMISSIVEFRESNEL) {
  952. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  953. }
  954. if (defines.FRESNEL) {
  955. fallbacks.addFallback(4, "FRESNEL");
  956. }
  957. if (defines.MULTIVIEW) {
  958. fallbacks.addFallback(0, "MULTIVIEW");
  959. }
  960. //Attributes
  961. var attribs = [VertexBuffer.PositionKind];
  962. if (defines.NORMAL) {
  963. attribs.push(VertexBuffer.NormalKind);
  964. }
  965. if (defines.UV1) {
  966. attribs.push(VertexBuffer.UVKind);
  967. }
  968. if (defines.UV2) {
  969. attribs.push(VertexBuffer.UV2Kind);
  970. }
  971. if (defines.VERTEXCOLOR) {
  972. attribs.push(VertexBuffer.ColorKind);
  973. }
  974. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  975. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  976. MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, defines);
  977. var shaderName = "default";
  978. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor", "visibility",
  979. "vFogInfos", "vFogColor", "pointSize",
  980. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  981. "mBones",
  982. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "vClipPlane5", "vClipPlane6", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "normalMatrix", "lightmapMatrix", "refractionMatrix",
  983. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor", "refractionLeftColor", "refractionRightColor",
  984. "vReflectionPosition", "vReflectionSize",
  985. "logarithmicDepthConstant", "vTangentSpaceParams", "alphaCutOff", "boneTextureWidth"
  986. ];
  987. var samplers = ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler",
  988. "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler",
  989. "refractionCubeSampler", "refraction2DSampler", "boneSampler"];
  990. var uniformBuffers = ["Material", "Scene"];
  991. if (ImageProcessingConfiguration) {
  992. ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
  993. ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
  994. }
  995. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  996. uniformsNames: uniforms,
  997. uniformBuffersNames: uniformBuffers,
  998. samplers: samplers,
  999. defines: defines,
  1000. maxSimultaneousLights: this._maxSimultaneousLights
  1001. });
  1002. if (this.customShaderNameResolve) {
  1003. shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines, attribs);
  1004. }
  1005. var join = defines.toString();
  1006. let previousEffect = subMesh.effect;
  1007. let effect = scene.getEngine().createEffect(shaderName, <IEffectCreationOptions>{
  1008. attributes: attribs,
  1009. uniformsNames: uniforms,
  1010. uniformBuffersNames: uniformBuffers,
  1011. samplers: samplers,
  1012. defines: join,
  1013. fallbacks: fallbacks,
  1014. onCompiled: this.onCompiled,
  1015. onError: this.onError,
  1016. indexParameters: { maxSimultaneousLights: this._maxSimultaneousLights, maxSimultaneousMorphTargets: defines.NUM_MORPH_INFLUENCERS }
  1017. }, engine);
  1018. if (effect) {
  1019. // Use previous effect while new one is compiling
  1020. if (this.allowShaderHotSwapping && previousEffect && !effect.isReady()) {
  1021. effect = previousEffect;
  1022. this._rebuildInParallel = true;
  1023. defines.markAsUnprocessed();
  1024. if (lightDisposed) {
  1025. // re register in case it takes more than one frame.
  1026. defines._areLightsDisposed = true;
  1027. return false;
  1028. }
  1029. } else {
  1030. this._rebuildInParallel = false;
  1031. scene.resetCachedMaterial();
  1032. subMesh.setEffect(effect, defines);
  1033. this.buildUniformLayout();
  1034. }
  1035. }
  1036. }
  1037. if (!subMesh.effect || !subMesh.effect.isReady()) {
  1038. return false;
  1039. }
  1040. defines._renderId = scene.getRenderId();
  1041. subMesh.effect._wasPreviouslyReady = true;
  1042. return true;
  1043. }
  1044. /**
  1045. * Builds the material UBO layouts.
  1046. * Used internally during the effect preparation.
  1047. */
  1048. public buildUniformLayout(): void {
  1049. // Order is important !
  1050. let ubo = this._uniformBuffer;
  1051. ubo.addUniform("diffuseLeftColor", 4);
  1052. ubo.addUniform("diffuseRightColor", 4);
  1053. ubo.addUniform("opacityParts", 4);
  1054. ubo.addUniform("reflectionLeftColor", 4);
  1055. ubo.addUniform("reflectionRightColor", 4);
  1056. ubo.addUniform("refractionLeftColor", 4);
  1057. ubo.addUniform("refractionRightColor", 4);
  1058. ubo.addUniform("emissiveLeftColor", 4);
  1059. ubo.addUniform("emissiveRightColor", 4);
  1060. ubo.addUniform("vDiffuseInfos", 2);
  1061. ubo.addUniform("vAmbientInfos", 2);
  1062. ubo.addUniform("vOpacityInfos", 2);
  1063. ubo.addUniform("vReflectionInfos", 2);
  1064. ubo.addUniform("vReflectionPosition", 3);
  1065. ubo.addUniform("vReflectionSize", 3);
  1066. ubo.addUniform("vEmissiveInfos", 2);
  1067. ubo.addUniform("vLightmapInfos", 2);
  1068. ubo.addUniform("vSpecularInfos", 2);
  1069. ubo.addUniform("vBumpInfos", 3);
  1070. ubo.addUniform("diffuseMatrix", 16);
  1071. ubo.addUniform("ambientMatrix", 16);
  1072. ubo.addUniform("opacityMatrix", 16);
  1073. ubo.addUniform("reflectionMatrix", 16);
  1074. ubo.addUniform("emissiveMatrix", 16);
  1075. ubo.addUniform("lightmapMatrix", 16);
  1076. ubo.addUniform("specularMatrix", 16);
  1077. ubo.addUniform("bumpMatrix", 16);
  1078. ubo.addUniform("vTangentSpaceParams", 2);
  1079. ubo.addUniform("pointSize", 1);
  1080. ubo.addUniform("refractionMatrix", 16);
  1081. ubo.addUniform("vRefractionInfos", 4);
  1082. ubo.addUniform("vSpecularColor", 4);
  1083. ubo.addUniform("vEmissiveColor", 3);
  1084. ubo.addUniform("visibility", 1);
  1085. ubo.addUniform("vDiffuseColor", 4);
  1086. ubo.create();
  1087. }
  1088. /**
  1089. * Unbinds the material from the mesh
  1090. */
  1091. public unbind(): void {
  1092. if (this._activeEffect) {
  1093. let needFlag = false;
  1094. if (this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  1095. this._activeEffect.setTexture("reflection2DSampler", null);
  1096. needFlag = true;
  1097. }
  1098. if (this._refractionTexture && this._refractionTexture.isRenderTarget) {
  1099. this._activeEffect.setTexture("refraction2DSampler", null);
  1100. needFlag = true;
  1101. }
  1102. if (needFlag) {
  1103. this._markAllSubMeshesAsTexturesDirty();
  1104. }
  1105. }
  1106. super.unbind();
  1107. }
  1108. /**
  1109. * Binds the submesh to this material by preparing the effect and shader to draw
  1110. * @param world defines the world transformation matrix
  1111. * @param mesh defines the mesh containing the submesh
  1112. * @param subMesh defines the submesh to bind the material to
  1113. */
  1114. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  1115. var scene = this.getScene();
  1116. var defines = <StandardMaterialDefines>subMesh._materialDefines;
  1117. if (!defines) {
  1118. return;
  1119. }
  1120. var effect = subMesh.effect;
  1121. if (!effect) {
  1122. return;
  1123. }
  1124. this._activeEffect = effect;
  1125. // Matrices
  1126. if (!defines.INSTANCES) {
  1127. this.bindOnlyWorldMatrix(world);
  1128. }
  1129. // Normal Matrix
  1130. if (defines.OBJECTSPACE_NORMALMAP) {
  1131. world.toNormalMatrix(this._normalMatrix);
  1132. this.bindOnlyNormalMatrix(this._normalMatrix);
  1133. }
  1134. let mustRebind = this._mustRebind(scene, effect, mesh.visibility);
  1135. // Bones
  1136. MaterialHelper.BindBonesParameters(mesh, effect);
  1137. let ubo = this._uniformBuffer;
  1138. if (mustRebind) {
  1139. ubo.bindToEffect(effect, "Material");
  1140. this.bindViewProjection(effect);
  1141. if (!ubo.useUbo || !this.isFrozen || !ubo.isSync) {
  1142. if (StandardMaterial.FresnelEnabled && defines.FRESNEL) {
  1143. // Fresnel
  1144. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  1145. ubo.updateColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  1146. ubo.updateColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  1147. }
  1148. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  1149. ubo.updateColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  1150. }
  1151. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  1152. ubo.updateColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  1153. ubo.updateColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  1154. }
  1155. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  1156. ubo.updateColor4("refractionLeftColor", this.refractionFresnelParameters.leftColor, this.refractionFresnelParameters.power);
  1157. ubo.updateColor4("refractionRightColor", this.refractionFresnelParameters.rightColor, this.refractionFresnelParameters.bias);
  1158. }
  1159. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  1160. ubo.updateColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  1161. ubo.updateColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  1162. }
  1163. }
  1164. // Textures
  1165. if (scene.texturesEnabled) {
  1166. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  1167. ubo.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  1168. MaterialHelper.BindTextureMatrix(this._diffuseTexture, ubo, "diffuse");
  1169. if (this._diffuseTexture.hasAlpha) {
  1170. effect.setFloat("alphaCutOff", this.alphaCutOff);
  1171. }
  1172. }
  1173. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  1174. ubo.updateFloat2("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level);
  1175. MaterialHelper.BindTextureMatrix(this._ambientTexture, ubo, "ambient");
  1176. }
  1177. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  1178. ubo.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
  1179. MaterialHelper.BindTextureMatrix(this._opacityTexture, ubo, "opacity");
  1180. }
  1181. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  1182. ubo.updateFloat2("vReflectionInfos", this._reflectionTexture.level, this.roughness);
  1183. ubo.updateMatrix("reflectionMatrix", this._reflectionTexture.getReflectionTextureMatrix());
  1184. if ((<any>this._reflectionTexture).boundingBoxSize) {
  1185. let cubeTexture = <CubeTexture>this._reflectionTexture;
  1186. ubo.updateVector3("vReflectionPosition", cubeTexture.boundingBoxPosition);
  1187. ubo.updateVector3("vReflectionSize", cubeTexture.boundingBoxSize);
  1188. }
  1189. }
  1190. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  1191. ubo.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
  1192. MaterialHelper.BindTextureMatrix(this._emissiveTexture, ubo, "emissive");
  1193. }
  1194. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  1195. ubo.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
  1196. MaterialHelper.BindTextureMatrix(this._lightmapTexture, ubo, "lightmap");
  1197. }
  1198. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  1199. ubo.updateFloat2("vSpecularInfos", this._specularTexture.coordinatesIndex, this._specularTexture.level);
  1200. MaterialHelper.BindTextureMatrix(this._specularTexture, ubo, "specular");
  1201. }
  1202. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  1203. ubo.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, 1.0 / this._bumpTexture.level, this.parallaxScaleBias);
  1204. MaterialHelper.BindTextureMatrix(this._bumpTexture, ubo, "bump");
  1205. if (scene._mirroredCameraPosition) {
  1206. ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? 1.0 : -1.0, this._invertNormalMapY ? 1.0 : -1.0);
  1207. } else {
  1208. ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? -1.0 : 1.0, this._invertNormalMapY ? -1.0 : 1.0);
  1209. }
  1210. }
  1211. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  1212. var depth = 1.0;
  1213. if (!this._refractionTexture.isCube) {
  1214. ubo.updateMatrix("refractionMatrix", this._refractionTexture.getReflectionTextureMatrix());
  1215. if ((<any>this._refractionTexture).depth) {
  1216. depth = (<any>this._refractionTexture).depth;
  1217. }
  1218. }
  1219. ubo.updateFloat4("vRefractionInfos", this._refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  1220. }
  1221. }
  1222. // Point size
  1223. if (this.pointsCloud) {
  1224. ubo.updateFloat("pointSize", this.pointSize);
  1225. }
  1226. if (defines.SPECULARTERM) {
  1227. ubo.updateColor4("vSpecularColor", this.specularColor, this.specularPower);
  1228. }
  1229. ubo.updateColor3("vEmissiveColor", StandardMaterial.EmissiveTextureEnabled ? this.emissiveColor : Color3.BlackReadOnly);
  1230. // Visibility
  1231. ubo.updateFloat("visibility", mesh.visibility);
  1232. // Diffuse
  1233. ubo.updateColor4("vDiffuseColor", this.diffuseColor, this.alpha);
  1234. }
  1235. // Textures
  1236. if (scene.texturesEnabled) {
  1237. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  1238. effect.setTexture("diffuseSampler", this._diffuseTexture);
  1239. }
  1240. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  1241. effect.setTexture("ambientSampler", this._ambientTexture);
  1242. }
  1243. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  1244. effect.setTexture("opacitySampler", this._opacityTexture);
  1245. }
  1246. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  1247. if (this._reflectionTexture.isCube) {
  1248. effect.setTexture("reflectionCubeSampler", this._reflectionTexture);
  1249. } else {
  1250. effect.setTexture("reflection2DSampler", this._reflectionTexture);
  1251. }
  1252. }
  1253. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  1254. effect.setTexture("emissiveSampler", this._emissiveTexture);
  1255. }
  1256. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  1257. effect.setTexture("lightmapSampler", this._lightmapTexture);
  1258. }
  1259. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  1260. effect.setTexture("specularSampler", this._specularTexture);
  1261. }
  1262. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  1263. effect.setTexture("bumpSampler", this._bumpTexture);
  1264. }
  1265. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  1266. var depth = 1.0;
  1267. if (this._refractionTexture.isCube) {
  1268. effect.setTexture("refractionCubeSampler", this._refractionTexture);
  1269. } else {
  1270. effect.setTexture("refraction2DSampler", this._refractionTexture);
  1271. }
  1272. }
  1273. }
  1274. // Clip plane
  1275. MaterialHelper.BindClipPlane(effect, scene);
  1276. // Colors
  1277. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  1278. MaterialHelper.BindEyePosition(effect, scene);
  1279. effect.setColor3("vAmbientColor", this._globalAmbientColor);
  1280. }
  1281. if (mustRebind || !this.isFrozen) {
  1282. // Lights
  1283. if (scene.lightsEnabled && !this._disableLighting) {
  1284. MaterialHelper.BindLights(scene, mesh, effect, defines, this._maxSimultaneousLights, this._rebuildInParallel);
  1285. }
  1286. // View
  1287. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE || this._reflectionTexture || this._refractionTexture) {
  1288. this.bindView(effect);
  1289. }
  1290. // Fog
  1291. MaterialHelper.BindFogParameters(scene, mesh, effect);
  1292. // Morph targets
  1293. if (defines.NUM_MORPH_INFLUENCERS) {
  1294. MaterialHelper.BindMorphTargetParameters(mesh, effect);
  1295. }
  1296. // Log. depth
  1297. if (this.useLogarithmicDepth) {
  1298. MaterialHelper.BindLogDepth(defines, effect, scene);
  1299. }
  1300. // image processing
  1301. if (this._imageProcessingConfiguration && !this._imageProcessingConfiguration.applyByPostProcess) {
  1302. this._imageProcessingConfiguration.bind(this._activeEffect);
  1303. }
  1304. }
  1305. ubo.update();
  1306. this._afterBind(mesh, this._activeEffect);
  1307. }
  1308. /**
  1309. * Get the list of animatables in the material.
  1310. * @returns the list of animatables object used in the material
  1311. */
  1312. public getAnimatables(): IAnimatable[] {
  1313. var results = [];
  1314. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  1315. results.push(this._diffuseTexture);
  1316. }
  1317. if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
  1318. results.push(this._ambientTexture);
  1319. }
  1320. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  1321. results.push(this._opacityTexture);
  1322. }
  1323. if (this._reflectionTexture && this._reflectionTexture.animations && this._reflectionTexture.animations.length > 0) {
  1324. results.push(this._reflectionTexture);
  1325. }
  1326. if (this._emissiveTexture && this._emissiveTexture.animations && this._emissiveTexture.animations.length > 0) {
  1327. results.push(this._emissiveTexture);
  1328. }
  1329. if (this._specularTexture && this._specularTexture.animations && this._specularTexture.animations.length > 0) {
  1330. results.push(this._specularTexture);
  1331. }
  1332. if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
  1333. results.push(this._bumpTexture);
  1334. }
  1335. if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
  1336. results.push(this._lightmapTexture);
  1337. }
  1338. if (this._refractionTexture && this._refractionTexture.animations && this._refractionTexture.animations.length > 0) {
  1339. results.push(this._refractionTexture);
  1340. }
  1341. return results;
  1342. }
  1343. /**
  1344. * Gets the active textures from the material
  1345. * @returns an array of textures
  1346. */
  1347. public getActiveTextures(): BaseTexture[] {
  1348. var activeTextures = super.getActiveTextures();
  1349. if (this._diffuseTexture) {
  1350. activeTextures.push(this._diffuseTexture);
  1351. }
  1352. if (this._ambientTexture) {
  1353. activeTextures.push(this._ambientTexture);
  1354. }
  1355. if (this._opacityTexture) {
  1356. activeTextures.push(this._opacityTexture);
  1357. }
  1358. if (this._reflectionTexture) {
  1359. activeTextures.push(this._reflectionTexture);
  1360. }
  1361. if (this._emissiveTexture) {
  1362. activeTextures.push(this._emissiveTexture);
  1363. }
  1364. if (this._specularTexture) {
  1365. activeTextures.push(this._specularTexture);
  1366. }
  1367. if (this._bumpTexture) {
  1368. activeTextures.push(this._bumpTexture);
  1369. }
  1370. if (this._lightmapTexture) {
  1371. activeTextures.push(this._lightmapTexture);
  1372. }
  1373. if (this._refractionTexture) {
  1374. activeTextures.push(this._refractionTexture);
  1375. }
  1376. return activeTextures;
  1377. }
  1378. /**
  1379. * Specifies if the material uses a texture
  1380. * @param texture defines the texture to check against the material
  1381. * @returns a boolean specifying if the material uses the texture
  1382. */
  1383. public hasTexture(texture: BaseTexture): boolean {
  1384. if (super.hasTexture(texture)) {
  1385. return true;
  1386. }
  1387. if (this._diffuseTexture === texture) {
  1388. return true;
  1389. }
  1390. if (this._ambientTexture === texture) {
  1391. return true;
  1392. }
  1393. if (this._opacityTexture === texture) {
  1394. return true;
  1395. }
  1396. if (this._reflectionTexture === texture) {
  1397. return true;
  1398. }
  1399. if (this._emissiveTexture === texture) {
  1400. return true;
  1401. }
  1402. if (this._specularTexture === texture) {
  1403. return true;
  1404. }
  1405. if (this._bumpTexture === texture) {
  1406. return true;
  1407. }
  1408. if (this._lightmapTexture === texture) {
  1409. return true;
  1410. }
  1411. if (this._refractionTexture === texture) {
  1412. return true;
  1413. }
  1414. return false;
  1415. }
  1416. /**
  1417. * Disposes the material
  1418. * @param forceDisposeEffect specifies if effects should be forcefully disposed
  1419. * @param forceDisposeTextures specifies if textures should be forcefully disposed
  1420. */
  1421. public dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean): void {
  1422. if (forceDisposeTextures) {
  1423. if (this._diffuseTexture) {
  1424. this._diffuseTexture.dispose();
  1425. }
  1426. if (this._ambientTexture) {
  1427. this._ambientTexture.dispose();
  1428. }
  1429. if (this._opacityTexture) {
  1430. this._opacityTexture.dispose();
  1431. }
  1432. if (this._reflectionTexture) {
  1433. this._reflectionTexture.dispose();
  1434. }
  1435. if (this._emissiveTexture) {
  1436. this._emissiveTexture.dispose();
  1437. }
  1438. if (this._specularTexture) {
  1439. this._specularTexture.dispose();
  1440. }
  1441. if (this._bumpTexture) {
  1442. this._bumpTexture.dispose();
  1443. }
  1444. if (this._lightmapTexture) {
  1445. this._lightmapTexture.dispose();
  1446. }
  1447. if (this._refractionTexture) {
  1448. this._refractionTexture.dispose();
  1449. }
  1450. }
  1451. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  1452. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  1453. }
  1454. super.dispose(forceDisposeEffect, forceDisposeTextures);
  1455. }
  1456. /**
  1457. * Makes a duplicate of the material, and gives it a new name
  1458. * @param name defines the new name for the duplicated material
  1459. * @returns the cloned material
  1460. */
  1461. public clone(name: string): StandardMaterial {
  1462. var result = SerializationHelper.Clone(() => new StandardMaterial(name, this.getScene()), this);
  1463. result.name = name;
  1464. result.id = name;
  1465. return result;
  1466. }
  1467. /**
  1468. * Serializes this material in a JSON representation
  1469. * @returns the serialized material object
  1470. */
  1471. public serialize(): any {
  1472. return SerializationHelper.Serialize(this);
  1473. }
  1474. /**
  1475. * Creates a standard material from parsed material data
  1476. * @param source defines the JSON representation of the material
  1477. * @param scene defines the hosting scene
  1478. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  1479. * @returns a new standard material
  1480. */
  1481. public static Parse(source: any, scene: Scene, rootUrl: string): StandardMaterial {
  1482. return SerializationHelper.Parse(() => new StandardMaterial(source.name, scene), source, scene, rootUrl);
  1483. }
  1484. // Flags used to enable or disable a type of texture for all Standard Materials
  1485. /**
  1486. * Are diffuse textures enabled in the application.
  1487. */
  1488. public static get DiffuseTextureEnabled(): boolean {
  1489. return MaterialFlags.DiffuseTextureEnabled;
  1490. }
  1491. public static set DiffuseTextureEnabled(value: boolean) {
  1492. MaterialFlags.DiffuseTextureEnabled = value;
  1493. }
  1494. /**
  1495. * Are ambient textures enabled in the application.
  1496. */
  1497. public static get AmbientTextureEnabled(): boolean {
  1498. return MaterialFlags.AmbientTextureEnabled;
  1499. }
  1500. public static set AmbientTextureEnabled(value: boolean) {
  1501. MaterialFlags.AmbientTextureEnabled = value;
  1502. }
  1503. /**
  1504. * Are opacity textures enabled in the application.
  1505. */
  1506. public static get OpacityTextureEnabled(): boolean {
  1507. return MaterialFlags.OpacityTextureEnabled;
  1508. }
  1509. public static set OpacityTextureEnabled(value: boolean) {
  1510. MaterialFlags.OpacityTextureEnabled = value;
  1511. }
  1512. /**
  1513. * Are reflection textures enabled in the application.
  1514. */
  1515. public static get ReflectionTextureEnabled(): boolean {
  1516. return MaterialFlags.ReflectionTextureEnabled;
  1517. }
  1518. public static set ReflectionTextureEnabled(value: boolean) {
  1519. MaterialFlags.ReflectionTextureEnabled = value;
  1520. }
  1521. /**
  1522. * Are emissive textures enabled in the application.
  1523. */
  1524. public static get EmissiveTextureEnabled(): boolean {
  1525. return MaterialFlags.EmissiveTextureEnabled;
  1526. }
  1527. public static set EmissiveTextureEnabled(value: boolean) {
  1528. MaterialFlags.EmissiveTextureEnabled = value;
  1529. }
  1530. /**
  1531. * Are specular textures enabled in the application.
  1532. */
  1533. public static get SpecularTextureEnabled(): boolean {
  1534. return MaterialFlags.SpecularTextureEnabled;
  1535. }
  1536. public static set SpecularTextureEnabled(value: boolean) {
  1537. MaterialFlags.SpecularTextureEnabled = value;
  1538. }
  1539. /**
  1540. * Are bump textures enabled in the application.
  1541. */
  1542. public static get BumpTextureEnabled(): boolean {
  1543. return MaterialFlags.BumpTextureEnabled;
  1544. }
  1545. public static set BumpTextureEnabled(value: boolean) {
  1546. MaterialFlags.BumpTextureEnabled = value;
  1547. }
  1548. /**
  1549. * Are lightmap textures enabled in the application.
  1550. */
  1551. public static get LightmapTextureEnabled(): boolean {
  1552. return MaterialFlags.LightmapTextureEnabled;
  1553. }
  1554. public static set LightmapTextureEnabled(value: boolean) {
  1555. MaterialFlags.LightmapTextureEnabled = value;
  1556. }
  1557. /**
  1558. * Are refraction textures enabled in the application.
  1559. */
  1560. public static get RefractionTextureEnabled(): boolean {
  1561. return MaterialFlags.RefractionTextureEnabled;
  1562. }
  1563. public static set RefractionTextureEnabled(value: boolean) {
  1564. MaterialFlags.RefractionTextureEnabled = value;
  1565. }
  1566. /**
  1567. * Are color grading textures enabled in the application.
  1568. */
  1569. public static get ColorGradingTextureEnabled(): boolean {
  1570. return MaterialFlags.ColorGradingTextureEnabled;
  1571. }
  1572. public static set ColorGradingTextureEnabled(value: boolean) {
  1573. MaterialFlags.ColorGradingTextureEnabled = value;
  1574. }
  1575. /**
  1576. * Are fresnels enabled in the application.
  1577. */
  1578. public static get FresnelEnabled(): boolean {
  1579. return MaterialFlags.FresnelEnabled;
  1580. }
  1581. public static set FresnelEnabled(value: boolean) {
  1582. MaterialFlags.FresnelEnabled = value;
  1583. }
  1584. }
  1585. _TypeStore.RegisteredTypes["BABYLON.StandardMaterial"] = StandardMaterial;
  1586. Scene.DefaultMaterialFactory = (scene: Scene) => {
  1587. return new StandardMaterial("default material", scene);
  1588. };