standardMaterial.ts 74 KB

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