standardMaterial.ts 75 KB

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