babylon.standardMaterial.ts 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. module BABYLON {
  2. /** @hidden */
  3. export class StandardMaterialDefines extends MaterialDefines implements IImageProcessingConfigurationDefines {
  4. public MAINUV1 = false;
  5. public MAINUV2 = false;
  6. public DIFFUSE = false;
  7. public DIFFUSEDIRECTUV = 0;
  8. public AMBIENT = false;
  9. public AMBIENTDIRECTUV = 0;
  10. public OPACITY = false;
  11. public OPACITYDIRECTUV = 0;
  12. public OPACITYRGB = false;
  13. public REFLECTION = false;
  14. public EMISSIVE = false;
  15. public EMISSIVEDIRECTUV = 0;
  16. public SPECULAR = false;
  17. public SPECULARDIRECTUV = 0;
  18. public BUMP = false;
  19. public BUMPDIRECTUV = 0;
  20. public PARALLAX = false;
  21. public PARALLAXOCCLUSION = false;
  22. public SPECULAROVERALPHA = false;
  23. public CLIPPLANE = false;
  24. public CLIPPLANE2 = false;
  25. public CLIPPLANE3 = false;
  26. public CLIPPLANE4 = false;
  27. public ALPHATEST = false;
  28. public DEPTHPREPASS = false;
  29. public ALPHAFROMDIFFUSE = false;
  30. public POINTSIZE = false;
  31. public FOG = false;
  32. public SPECULARTERM = false;
  33. public DIFFUSEFRESNEL = false;
  34. public OPACITYFRESNEL = false;
  35. public REFLECTIONFRESNEL = false;
  36. public REFRACTIONFRESNEL = false;
  37. public EMISSIVEFRESNEL = false;
  38. public FRESNEL = false;
  39. public NORMAL = false;
  40. public UV1 = false;
  41. public UV2 = false;
  42. public VERTEXCOLOR = false;
  43. public VERTEXALPHA = false;
  44. public NUM_BONE_INFLUENCERS = 0;
  45. public BonesPerMesh = 0;
  46. public INSTANCES = false;
  47. public GLOSSINESS = false;
  48. public ROUGHNESS = false;
  49. public EMISSIVEASILLUMINATION = false;
  50. public LINKEMISSIVEWITHDIFFUSE = false;
  51. public REFLECTIONFRESNELFROMSPECULAR = false;
  52. public LIGHTMAP = false;
  53. public LIGHTMAPDIRECTUV = 0;
  54. public OBJECTSPACE_NORMALMAP = false;
  55. public USELIGHTMAPASSHADOWMAP = false;
  56. public REFLECTIONMAP_3D = false;
  57. public REFLECTIONMAP_SPHERICAL = false;
  58. public REFLECTIONMAP_PLANAR = false;
  59. public REFLECTIONMAP_CUBIC = false;
  60. public USE_LOCAL_REFLECTIONMAP_CUBIC = false;
  61. public REFLECTIONMAP_PROJECTION = false;
  62. public REFLECTIONMAP_SKYBOX = false;
  63. public REFLECTIONMAP_SKYBOX_TRANSFORMED = false;
  64. public REFLECTIONMAP_EXPLICIT = false;
  65. public REFLECTIONMAP_EQUIRECTANGULAR = false;
  66. public REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  67. public REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  68. public INVERTCUBICMAP = false;
  69. public LOGARITHMICDEPTH = false;
  70. public REFRACTION = false;
  71. public REFRACTIONMAP_3D = false;
  72. public REFLECTIONOVERALPHA = false;
  73. public TWOSIDEDLIGHTING = false;
  74. public SHADOWFLOAT = false;
  75. public MORPHTARGETS = false;
  76. public MORPHTARGETS_NORMAL = false;
  77. public MORPHTARGETS_TANGENT = false;
  78. public NUM_MORPH_INFLUENCERS = 0;
  79. public NONUNIFORMSCALING = false; // https://playground.babylonjs.com#V6DWIH
  80. public PREMULTIPLYALPHA = false; // https://playground.babylonjs.com#LNVJJ7
  81. public IMAGEPROCESSING = false;
  82. public VIGNETTE = false;
  83. public VIGNETTEBLENDMODEMULTIPLY = false;
  84. public VIGNETTEBLENDMODEOPAQUE = false;
  85. public TONEMAPPING = false;
  86. public TONEMAPPING_ACES = false;
  87. public CONTRAST = false;
  88. public COLORCURVES = false;
  89. public COLORGRADING = false;
  90. public COLORGRADING3D = false;
  91. public SAMPLER3DGREENDEPTH = false;
  92. public SAMPLER3DBGRMAP = false;
  93. public IMAGEPROCESSINGPOSTPROCESS = false;
  94. /**
  95. * If the reflection texture on this material is in linear color space
  96. * @hidden
  97. */
  98. public IS_REFLECTION_LINEAR = false;
  99. /**
  100. * If the refraction texture on this material is in linear color space
  101. * @hidden
  102. */
  103. public IS_REFRACTION_LINEAR = false;
  104. public EXPOSURE = false;
  105. constructor() {
  106. super();
  107. this.rebuild();
  108. }
  109. public setReflectionMode(modeToEnable: string) {
  110. var modes = [
  111. "REFLECTIONMAP_CUBIC", "REFLECTIONMAP_EXPLICIT", "REFLECTIONMAP_PLANAR",
  112. "REFLECTIONMAP_PROJECTION", "REFLECTIONMAP_PROJECTION", "REFLECTIONMAP_SKYBOX",
  113. "REFLECTIONMAP_SPHERICAL", "REFLECTIONMAP_EQUIRECTANGULAR", "REFLECTIONMAP_EQUIRECTANGULAR_FIXED",
  114. "REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED"
  115. ];
  116. for (var mode of modes) {
  117. (<any>this)[mode] = (mode === modeToEnable);
  118. }
  119. }
  120. }
  121. export class StandardMaterial extends PushMaterial {
  122. @serializeAsTexture("diffuseTexture")
  123. private _diffuseTexture: Nullable<BaseTexture>;
  124. @expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  125. public diffuseTexture: Nullable<BaseTexture>;
  126. @serializeAsTexture("ambientTexture")
  127. private _ambientTexture: Nullable<BaseTexture>;
  128. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  129. public ambientTexture: Nullable<BaseTexture>;
  130. @serializeAsTexture("opacityTexture")
  131. private _opacityTexture: Nullable<BaseTexture>;
  132. @expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  133. public opacityTexture: Nullable<BaseTexture>;
  134. @serializeAsTexture("reflectionTexture")
  135. private _reflectionTexture: Nullable<BaseTexture>;
  136. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  137. public reflectionTexture: Nullable<BaseTexture>;
  138. @serializeAsTexture("emissiveTexture")
  139. private _emissiveTexture: Nullable<BaseTexture>;
  140. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  141. public emissiveTexture: Nullable<BaseTexture>;
  142. @serializeAsTexture("specularTexture")
  143. private _specularTexture: Nullable<BaseTexture>;
  144. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  145. public specularTexture: Nullable<BaseTexture>;
  146. @serializeAsTexture("bumpTexture")
  147. private _bumpTexture: Nullable<BaseTexture>;
  148. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  149. public bumpTexture: Nullable<BaseTexture>;
  150. @serializeAsTexture("lightmapTexture")
  151. private _lightmapTexture: Nullable<BaseTexture>;
  152. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  153. public lightmapTexture: Nullable<BaseTexture>;
  154. @serializeAsTexture("refractionTexture")
  155. private _refractionTexture: Nullable<BaseTexture>;
  156. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  157. public refractionTexture: Nullable<BaseTexture>;
  158. @serializeAsColor3("ambient")
  159. public ambientColor = new Color3(0, 0, 0);
  160. @serializeAsColor3("diffuse")
  161. public diffuseColor = new Color3(1, 1, 1);
  162. @serializeAsColor3("specular")
  163. public specularColor = new Color3(1, 1, 1);
  164. @serializeAsColor3("emissive")
  165. public emissiveColor = new Color3(0, 0, 0);
  166. @serialize()
  167. public specularPower = 64;
  168. @serialize("useAlphaFromDiffuseTexture")
  169. private _useAlphaFromDiffuseTexture = false;
  170. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  171. public useAlphaFromDiffuseTexture: boolean;
  172. @serialize("useEmissiveAsIllumination")
  173. private _useEmissiveAsIllumination = false;
  174. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  175. public useEmissiveAsIllumination: boolean;
  176. @serialize("linkEmissiveWithDiffuse")
  177. private _linkEmissiveWithDiffuse = false;
  178. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  179. public linkEmissiveWithDiffuse: boolean;
  180. @serialize("useSpecularOverAlpha")
  181. private _useSpecularOverAlpha = false;
  182. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  183. public useSpecularOverAlpha: boolean;
  184. @serialize("useReflectionOverAlpha")
  185. private _useReflectionOverAlpha = false;
  186. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  187. public useReflectionOverAlpha: boolean;
  188. @serialize("disableLighting")
  189. private _disableLighting = false;
  190. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  191. public disableLighting: boolean;
  192. @serialize("useObjectSpaceNormalMap")
  193. private _useObjectSpaceNormalMap = false;
  194. /**
  195. * Allows using an object space normal map (instead of tangent space).
  196. */
  197. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  198. public useObjectSpaceNormalMap: boolean;
  199. @serialize("useParallax")
  200. private _useParallax = false;
  201. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  202. public useParallax: boolean;
  203. @serialize("useParallaxOcclusion")
  204. private _useParallaxOcclusion = false;
  205. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  206. public useParallaxOcclusion: boolean;
  207. @serialize()
  208. public parallaxScaleBias = 0.05;
  209. @serialize("roughness")
  210. private _roughness = 0;
  211. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  212. public roughness: number;
  213. @serialize()
  214. public indexOfRefraction = 0.98;
  215. @serialize()
  216. public invertRefractionY = true;
  217. /**
  218. * Defines the alpha limits in alpha test mode
  219. */
  220. @serialize()
  221. public alphaCutOff = 0.4;
  222. @serialize("useLightmapAsShadowmap")
  223. private _useLightmapAsShadowmap = false;
  224. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  225. public useLightmapAsShadowmap: boolean;
  226. // Fresnel
  227. @serializeAsFresnelParameters("diffuseFresnelParameters")
  228. private _diffuseFresnelParameters: FresnelParameters;
  229. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  230. public diffuseFresnelParameters: FresnelParameters;
  231. @serializeAsFresnelParameters("opacityFresnelParameters")
  232. private _opacityFresnelParameters: FresnelParameters;
  233. @expandToProperty("_markAllSubMeshesAsFresnelAndMiscDirty")
  234. public opacityFresnelParameters: FresnelParameters;
  235. @serializeAsFresnelParameters("reflectionFresnelParameters")
  236. private _reflectionFresnelParameters: FresnelParameters;
  237. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  238. public reflectionFresnelParameters: FresnelParameters;
  239. @serializeAsFresnelParameters("refractionFresnelParameters")
  240. private _refractionFresnelParameters: FresnelParameters;
  241. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  242. public refractionFresnelParameters: FresnelParameters;
  243. @serializeAsFresnelParameters("emissiveFresnelParameters")
  244. private _emissiveFresnelParameters: FresnelParameters;
  245. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  246. public emissiveFresnelParameters: FresnelParameters;
  247. @serialize("useReflectionFresnelFromSpecular")
  248. private _useReflectionFresnelFromSpecular = false;
  249. @expandToProperty("_markAllSubMeshesAsFresnelDirty")
  250. public useReflectionFresnelFromSpecular: boolean;
  251. @serialize("useGlossinessFromSpecularMapAlpha")
  252. private _useGlossinessFromSpecularMapAlpha = false;
  253. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  254. public useGlossinessFromSpecularMapAlpha: boolean;
  255. @serialize("maxSimultaneousLights")
  256. private _maxSimultaneousLights = 4;
  257. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  258. public maxSimultaneousLights: number;
  259. /**
  260. * If sets to true, x component of normal map value will invert (x = 1.0 - x).
  261. */
  262. @serialize("invertNormalMapX")
  263. private _invertNormalMapX = false;
  264. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  265. public invertNormalMapX: boolean;
  266. /**
  267. * If sets to true, y component of normal map value will invert (y = 1.0 - y).
  268. */
  269. @serialize("invertNormalMapY")
  270. private _invertNormalMapY = false;
  271. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  272. public invertNormalMapY: boolean;
  273. /**
  274. * If sets to true and backfaceCulling is false, normals will be flipped on the backside.
  275. */
  276. @serialize("twoSidedLighting")
  277. private _twoSidedLighting = false;
  278. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  279. public twoSidedLighting: boolean;
  280. /**
  281. * Default configuration related to image processing available in the standard Material.
  282. */
  283. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  284. /**
  285. * Gets the image processing configuration used either in this material.
  286. */
  287. public get imageProcessingConfiguration(): ImageProcessingConfiguration {
  288. return this._imageProcessingConfiguration;
  289. }
  290. /**
  291. * Sets the Default image processing configuration used either in the this material.
  292. *
  293. * If sets to null, the scene one is in use.
  294. */
  295. public set imageProcessingConfiguration(value: ImageProcessingConfiguration) {
  296. this._attachImageProcessingConfiguration(value);
  297. // Ensure the effect will be rebuilt.
  298. this._markAllSubMeshesAsTexturesDirty();
  299. }
  300. /**
  301. * Keep track of the image processing observer to allow dispose and replace.
  302. */
  303. private _imageProcessingObserver: Nullable<Observer<ImageProcessingConfiguration>>;
  304. /**
  305. * Attaches a new image processing configuration to the Standard Material.
  306. * @param configuration
  307. */
  308. protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>): void {
  309. if (configuration === this._imageProcessingConfiguration) {
  310. return;
  311. }
  312. // Detaches observer.
  313. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  314. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  315. }
  316. // Pick the scene configuration if needed.
  317. if (!configuration) {
  318. this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
  319. }
  320. else {
  321. this._imageProcessingConfiguration = configuration;
  322. }
  323. // Attaches observer.
  324. if (this._imageProcessingConfiguration) {
  325. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(conf => {
  326. this._markAllSubMeshesAsImageProcessingDirty();
  327. });
  328. }
  329. }
  330. /**
  331. * Gets wether the color curves effect is enabled.
  332. */
  333. public get cameraColorCurvesEnabled(): boolean {
  334. return this.imageProcessingConfiguration.colorCurvesEnabled;
  335. }
  336. /**
  337. * Sets wether the color curves effect is enabled.
  338. */
  339. public set cameraColorCurvesEnabled(value: boolean) {
  340. this.imageProcessingConfiguration.colorCurvesEnabled = value;
  341. }
  342. /**
  343. * Gets wether the color grading effect is enabled.
  344. */
  345. public get cameraColorGradingEnabled(): boolean {
  346. return this.imageProcessingConfiguration.colorGradingEnabled;
  347. }
  348. /**
  349. * Gets wether the color grading effect is enabled.
  350. */
  351. public set cameraColorGradingEnabled(value: boolean) {
  352. this.imageProcessingConfiguration.colorGradingEnabled = value;
  353. }
  354. /**
  355. * Gets wether tonemapping is enabled or not.
  356. */
  357. public get cameraToneMappingEnabled(): boolean {
  358. return this._imageProcessingConfiguration.toneMappingEnabled;
  359. };
  360. /**
  361. * Sets wether tonemapping is enabled or not
  362. */
  363. public set cameraToneMappingEnabled(value: boolean) {
  364. this._imageProcessingConfiguration.toneMappingEnabled = value;
  365. };
  366. /**
  367. * The camera exposure used on this material.
  368. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  369. * This corresponds to a photographic exposure.
  370. */
  371. public get cameraExposure(): number {
  372. return this._imageProcessingConfiguration.exposure;
  373. };
  374. /**
  375. * The camera exposure used on this material.
  376. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  377. * This corresponds to a photographic exposure.
  378. */
  379. public set cameraExposure(value: number) {
  380. this._imageProcessingConfiguration.exposure = value;
  381. };
  382. /**
  383. * Gets The camera contrast used on this material.
  384. */
  385. public get cameraContrast(): number {
  386. return this._imageProcessingConfiguration.contrast;
  387. }
  388. /**
  389. * Sets The camera contrast used on this material.
  390. */
  391. public set cameraContrast(value: number) {
  392. this._imageProcessingConfiguration.contrast = value;
  393. }
  394. /**
  395. * Gets the Color Grading 2D Lookup Texture.
  396. */
  397. public get cameraColorGradingTexture(): Nullable<BaseTexture> {
  398. return this._imageProcessingConfiguration.colorGradingTexture;
  399. }
  400. /**
  401. * Sets the Color Grading 2D Lookup Texture.
  402. */
  403. public set cameraColorGradingTexture(value: Nullable<BaseTexture>) {
  404. this._imageProcessingConfiguration.colorGradingTexture = value;
  405. }
  406. /**
  407. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  408. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  409. * 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;
  410. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  411. */
  412. public get cameraColorCurves(): Nullable<ColorCurves> {
  413. return this._imageProcessingConfiguration.colorCurves;
  414. }
  415. /**
  416. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  417. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  418. * 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;
  419. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  420. */
  421. public set cameraColorCurves(value: Nullable<ColorCurves>) {
  422. this._imageProcessingConfiguration.colorCurves = value;
  423. }
  424. public customShaderNameResolve: (shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines) => string;
  425. protected _renderTargets = new SmartArray<RenderTargetTexture>(16);
  426. protected _worldViewProjectionMatrix = Matrix.Zero();
  427. protected _globalAmbientColor = new Color3(0, 0, 0);
  428. protected _useLogarithmicDepth: boolean;
  429. constructor(name: string, scene: Scene) {
  430. super(name, scene);
  431. // Setup the default processing configuration to the scene.
  432. this._attachImageProcessingConfiguration(null);
  433. this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
  434. this._renderTargets.reset();
  435. if (StandardMaterial.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  436. this._renderTargets.push(<RenderTargetTexture>this._reflectionTexture);
  437. }
  438. if (StandardMaterial.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  439. this._renderTargets.push(<RenderTargetTexture>this._refractionTexture);
  440. }
  441. return this._renderTargets;
  442. }
  443. }
  444. /**
  445. * Gets a boolean indicating that current material needs to register RTT
  446. */
  447. public get hasRenderTargetTextures(): boolean {
  448. if (StandardMaterial.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  449. return true;
  450. }
  451. if (StandardMaterial.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  452. return true;
  453. }
  454. return false;
  455. }
  456. public getClassName(): string {
  457. return "StandardMaterial";
  458. }
  459. @serialize()
  460. public get useLogarithmicDepth(): boolean {
  461. return this._useLogarithmicDepth;
  462. }
  463. public set useLogarithmicDepth(value: boolean) {
  464. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  465. this._markAllSubMeshesAsMiscDirty();
  466. }
  467. public needAlphaBlending(): boolean {
  468. return (this.alpha < 1.0) || (this._opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled;
  469. }
  470. public needAlphaTesting(): boolean {
  471. return this._diffuseTexture != null && this._diffuseTexture.hasAlpha;
  472. }
  473. protected _shouldUseAlphaFromDiffuseTexture(): boolean {
  474. return this._diffuseTexture != null && this._diffuseTexture.hasAlpha && this._useAlphaFromDiffuseTexture;
  475. }
  476. public getAlphaTestTexture(): Nullable<BaseTexture> {
  477. return this._diffuseTexture;
  478. }
  479. /**
  480. * Child classes can use it to update shaders
  481. */
  482. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances: boolean = false): boolean {
  483. if (subMesh.effect && this.isFrozen) {
  484. if (this._wasPreviouslyReady) {
  485. return true;
  486. }
  487. }
  488. if (!subMesh._materialDefines) {
  489. subMesh._materialDefines = new StandardMaterialDefines();
  490. }
  491. var scene = this.getScene();
  492. var defines = <StandardMaterialDefines>subMesh._materialDefines;
  493. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  494. if (defines._renderId === scene.getRenderId()) {
  495. return true;
  496. }
  497. }
  498. var engine = scene.getEngine();
  499. // Lights
  500. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
  501. // Textures
  502. if (defines._areTexturesDirty) {
  503. defines._needUVs = false;
  504. defines.MAINUV1 = false;
  505. defines.MAINUV2 = false;
  506. if (scene.texturesEnabled) {
  507. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  508. if (!this._diffuseTexture.isReadyOrNotBlocking()) {
  509. return false;
  510. } else {
  511. MaterialHelper.PrepareDefinesForMergedUV(this._diffuseTexture, defines, "DIFFUSE");
  512. }
  513. } else {
  514. defines.DIFFUSE = false;
  515. }
  516. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  517. if (!this._ambientTexture.isReadyOrNotBlocking()) {
  518. return false;
  519. } else {
  520. MaterialHelper.PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
  521. }
  522. } else {
  523. defines.AMBIENT = false;
  524. }
  525. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  526. if (!this._opacityTexture.isReadyOrNotBlocking()) {
  527. return false;
  528. } else {
  529. MaterialHelper.PrepareDefinesForMergedUV(this._opacityTexture, defines, "OPACITY");
  530. defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
  531. }
  532. } else {
  533. defines.OPACITY = false;
  534. }
  535. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  536. if (!this._reflectionTexture.isReadyOrNotBlocking()) {
  537. return false;
  538. } else {
  539. defines._needNormals = true;
  540. defines.REFLECTION = true;
  541. defines.ROUGHNESS = (this._roughness > 0);
  542. defines.REFLECTIONOVERALPHA = this._useReflectionOverAlpha;
  543. defines.INVERTCUBICMAP = (this._reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE);
  544. defines.REFLECTIONMAP_3D = this._reflectionTexture.isCube;
  545. switch (this._reflectionTexture.coordinatesMode) {
  546. case Texture.EXPLICIT_MODE:
  547. defines.setReflectionMode("REFLECTIONMAP_EXPLICIT");
  548. break;
  549. case Texture.PLANAR_MODE:
  550. defines.setReflectionMode("REFLECTIONMAP_PLANAR");
  551. break;
  552. case Texture.PROJECTION_MODE:
  553. defines.setReflectionMode("REFLECTIONMAP_PROJECTION");
  554. break;
  555. case Texture.SKYBOX_MODE:
  556. defines.setReflectionMode("REFLECTIONMAP_SKYBOX");
  557. defines.REFLECTIONMAP_SKYBOX_TRANSFORMED = !this._reflectionTexture.getReflectionTextureMatrix().isIdentity();
  558. break;
  559. case Texture.SPHERICAL_MODE:
  560. defines.setReflectionMode("REFLECTIONMAP_SPHERICAL");
  561. break;
  562. case Texture.EQUIRECTANGULAR_MODE:
  563. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR");
  564. break;
  565. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  566. defines.setReflectionMode("REFLECTIONMAP_EQUIRECTANGULAR_FIXED");
  567. break;
  568. case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
  569. defines.setReflectionMode("REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED");
  570. break;
  571. case Texture.CUBIC_MODE:
  572. case Texture.INVCUBIC_MODE:
  573. default:
  574. defines.setReflectionMode("REFLECTIONMAP_CUBIC");
  575. break;
  576. }
  577. defines.USE_LOCAL_REFLECTIONMAP_CUBIC = (<any>this._reflectionTexture).boundingBoxSize ? true : false;
  578. }
  579. } else {
  580. defines.REFLECTION = false;
  581. }
  582. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  583. if (!this._emissiveTexture.isReadyOrNotBlocking()) {
  584. return false;
  585. } else {
  586. MaterialHelper.PrepareDefinesForMergedUV(this._emissiveTexture, defines, "EMISSIVE");
  587. }
  588. } else {
  589. defines.EMISSIVE = false;
  590. }
  591. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  592. if (!this._lightmapTexture.isReadyOrNotBlocking()) {
  593. return false;
  594. } else {
  595. MaterialHelper.PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
  596. defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
  597. }
  598. } else {
  599. defines.LIGHTMAP = false;
  600. }
  601. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  602. if (!this._specularTexture.isReadyOrNotBlocking()) {
  603. return false;
  604. } else {
  605. MaterialHelper.PrepareDefinesForMergedUV(this._specularTexture, defines, "SPECULAR");
  606. defines.GLOSSINESS = this._useGlossinessFromSpecularMapAlpha;
  607. }
  608. } else {
  609. defines.SPECULAR = false;
  610. }
  611. if (scene.getEngine().getCaps().standardDerivatives && this._bumpTexture && StandardMaterial.BumpTextureEnabled) {
  612. // Bump texure can not be not blocking.
  613. if (!this._bumpTexture.isReady()) {
  614. return false;
  615. } else {
  616. MaterialHelper.PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
  617. defines.PARALLAX = this._useParallax;
  618. defines.PARALLAXOCCLUSION = this._useParallaxOcclusion;
  619. }
  620. defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
  621. } else {
  622. defines.BUMP = false;
  623. }
  624. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  625. if (!this._refractionTexture.isReadyOrNotBlocking()) {
  626. return false;
  627. } else {
  628. defines._needUVs = true;
  629. defines.REFRACTION = true;
  630. defines.REFRACTIONMAP_3D = this._refractionTexture.isCube;
  631. }
  632. } else {
  633. defines.REFRACTION = false;
  634. }
  635. defines.TWOSIDEDLIGHTING = !this._backFaceCulling && this._twoSidedLighting;
  636. } else {
  637. defines.DIFFUSE = false;
  638. defines.AMBIENT = false;
  639. defines.OPACITY = false;
  640. defines.REFLECTION = false;
  641. defines.EMISSIVE = false;
  642. defines.LIGHTMAP = false;
  643. defines.BUMP = false;
  644. defines.REFRACTION = false;
  645. }
  646. defines.ALPHAFROMDIFFUSE = this._shouldUseAlphaFromDiffuseTexture();
  647. defines.EMISSIVEASILLUMINATION = this._useEmissiveAsIllumination;
  648. defines.LINKEMISSIVEWITHDIFFUSE = this._linkEmissiveWithDiffuse;
  649. defines.SPECULAROVERALPHA = this._useSpecularOverAlpha;
  650. defines.PREMULTIPLYALPHA = (this.alphaMode === Engine.ALPHA_PREMULTIPLIED || this.alphaMode === Engine.ALPHA_PREMULTIPLIED_PORTERDUFF);
  651. }
  652. if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
  653. if (!this._imageProcessingConfiguration.isReady()) {
  654. return false;
  655. }
  656. this._imageProcessingConfiguration.prepareDefines(defines);
  657. defines.IS_REFLECTION_LINEAR = (this.reflectionTexture != null && !this.reflectionTexture.gammaSpace);
  658. defines.IS_REFRACTION_LINEAR = (this.refractionTexture != null && !this.refractionTexture.gammaSpace);
  659. }
  660. if (defines._areFresnelDirty) {
  661. if (StandardMaterial.FresnelEnabled) {
  662. // Fresnel
  663. if (this._diffuseFresnelParameters || this._opacityFresnelParameters ||
  664. this._emissiveFresnelParameters || this._refractionFresnelParameters ||
  665. this._reflectionFresnelParameters) {
  666. defines.DIFFUSEFRESNEL = (this._diffuseFresnelParameters && this._diffuseFresnelParameters.isEnabled);
  667. defines.OPACITYFRESNEL = (this._opacityFresnelParameters && this._opacityFresnelParameters.isEnabled);
  668. defines.REFLECTIONFRESNEL = (this._reflectionFresnelParameters && this._reflectionFresnelParameters.isEnabled);
  669. defines.REFLECTIONFRESNELFROMSPECULAR = this._useReflectionFresnelFromSpecular;
  670. defines.REFRACTIONFRESNEL = (this._refractionFresnelParameters && this._refractionFresnelParameters.isEnabled);
  671. defines.EMISSIVEFRESNEL = (this._emissiveFresnelParameters && this._emissiveFresnelParameters.isEnabled);
  672. defines._needNormals = true;
  673. defines.FRESNEL = true;
  674. }
  675. } else {
  676. defines.FRESNEL = false;
  677. }
  678. }
  679. // Misc.
  680. MaterialHelper.PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  681. // Attribs
  682. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true, true);
  683. // Values that need to be evaluated on every frame
  684. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  685. // Get correct effect
  686. if (defines.isDirty) {
  687. defines.markAsProcessed();
  688. scene.resetCachedMaterial();
  689. // Fallbacks
  690. var fallbacks = new EffectFallbacks();
  691. if (defines.REFLECTION) {
  692. fallbacks.addFallback(0, "REFLECTION");
  693. }
  694. if (defines.SPECULAR) {
  695. fallbacks.addFallback(0, "SPECULAR");
  696. }
  697. if (defines.BUMP) {
  698. fallbacks.addFallback(0, "BUMP");
  699. }
  700. if (defines.PARALLAX) {
  701. fallbacks.addFallback(1, "PARALLAX");
  702. }
  703. if (defines.PARALLAXOCCLUSION) {
  704. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  705. }
  706. if (defines.SPECULAROVERALPHA) {
  707. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  708. }
  709. if (defines.FOG) {
  710. fallbacks.addFallback(1, "FOG");
  711. }
  712. if (defines.POINTSIZE) {
  713. fallbacks.addFallback(0, "POINTSIZE");
  714. }
  715. if (defines.LOGARITHMICDEPTH) {
  716. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  717. }
  718. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights);
  719. if (defines.SPECULARTERM) {
  720. fallbacks.addFallback(0, "SPECULARTERM");
  721. }
  722. if (defines.DIFFUSEFRESNEL) {
  723. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  724. }
  725. if (defines.OPACITYFRESNEL) {
  726. fallbacks.addFallback(2, "OPACITYFRESNEL");
  727. }
  728. if (defines.REFLECTIONFRESNEL) {
  729. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  730. }
  731. if (defines.EMISSIVEFRESNEL) {
  732. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  733. }
  734. if (defines.FRESNEL) {
  735. fallbacks.addFallback(4, "FRESNEL");
  736. }
  737. //Attributes
  738. var attribs = [VertexBuffer.PositionKind];
  739. if (defines.NORMAL) {
  740. attribs.push(VertexBuffer.NormalKind);
  741. }
  742. if (defines.UV1) {
  743. attribs.push(VertexBuffer.UVKind);
  744. }
  745. if (defines.UV2) {
  746. attribs.push(VertexBuffer.UV2Kind);
  747. }
  748. if (defines.VERTEXCOLOR) {
  749. attribs.push(VertexBuffer.ColorKind);
  750. }
  751. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  752. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  753. MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, defines);
  754. var shaderName = "default";
  755. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  756. "vFogInfos", "vFogColor", "pointSize",
  757. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  758. "mBones",
  759. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "normalMatrix", "lightmapMatrix", "refractionMatrix",
  760. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor", "refractionLeftColor", "refractionRightColor",
  761. "vReflectionPosition", "vReflectionSize",
  762. "logarithmicDepthConstant", "vTangentSpaceParams", "alphaCutOff"
  763. ];
  764. var samplers = ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler"]
  765. var uniformBuffers = ["Material", "Scene"];
  766. if (ImageProcessingConfiguration) {
  767. ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
  768. ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
  769. }
  770. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  771. uniformsNames: uniforms,
  772. uniformBuffersNames: uniformBuffers,
  773. samplers: samplers,
  774. defines: defines,
  775. maxSimultaneousLights: this._maxSimultaneousLights
  776. });
  777. if (this.customShaderNameResolve) {
  778. shaderName = this.customShaderNameResolve(shaderName, uniforms, uniformBuffers, samplers, defines);
  779. }
  780. var join = defines.toString();
  781. subMesh.setEffect(scene.getEngine().createEffect(shaderName, <EffectCreationOptions>{
  782. attributes: attribs,
  783. uniformsNames: uniforms,
  784. uniformBuffersNames: uniformBuffers,
  785. samplers: samplers,
  786. defines: join,
  787. fallbacks: fallbacks,
  788. onCompiled: this.onCompiled,
  789. onError: this.onError,
  790. indexParameters: { maxSimultaneousLights: this._maxSimultaneousLights, maxSimultaneousMorphTargets: defines.NUM_MORPH_INFLUENCERS }
  791. }, engine), defines);
  792. this.buildUniformLayout();
  793. }
  794. if (!subMesh.effect || !subMesh.effect.isReady()) {
  795. return false;
  796. }
  797. defines._renderId = scene.getRenderId();
  798. this._wasPreviouslyReady = true;
  799. return true;
  800. }
  801. public buildUniformLayout(): void {
  802. // Order is important !
  803. this._uniformBuffer.addUniform("diffuseLeftColor", 4);
  804. this._uniformBuffer.addUniform("diffuseRightColor", 4);
  805. this._uniformBuffer.addUniform("opacityParts", 4);
  806. this._uniformBuffer.addUniform("reflectionLeftColor", 4);
  807. this._uniformBuffer.addUniform("reflectionRightColor", 4);
  808. this._uniformBuffer.addUniform("refractionLeftColor", 4);
  809. this._uniformBuffer.addUniform("refractionRightColor", 4);
  810. this._uniformBuffer.addUniform("emissiveLeftColor", 4);
  811. this._uniformBuffer.addUniform("emissiveRightColor", 4);
  812. this._uniformBuffer.addUniform("vDiffuseInfos", 2);
  813. this._uniformBuffer.addUniform("vAmbientInfos", 2);
  814. this._uniformBuffer.addUniform("vOpacityInfos", 2);
  815. this._uniformBuffer.addUniform("vReflectionInfos", 2);
  816. this._uniformBuffer.addUniform("vReflectionPosition", 3);
  817. this._uniformBuffer.addUniform("vReflectionSize", 3);
  818. this._uniformBuffer.addUniform("vEmissiveInfos", 2);
  819. this._uniformBuffer.addUniform("vLightmapInfos", 2);
  820. this._uniformBuffer.addUniform("vSpecularInfos", 2);
  821. this._uniformBuffer.addUniform("vBumpInfos", 3);
  822. this._uniformBuffer.addUniform("diffuseMatrix", 16);
  823. this._uniformBuffer.addUniform("ambientMatrix", 16);
  824. this._uniformBuffer.addUniform("opacityMatrix", 16);
  825. this._uniformBuffer.addUniform("reflectionMatrix", 16);
  826. this._uniformBuffer.addUniform("emissiveMatrix", 16);
  827. this._uniformBuffer.addUniform("lightmapMatrix", 16);
  828. this._uniformBuffer.addUniform("specularMatrix", 16);
  829. this._uniformBuffer.addUniform("bumpMatrix", 16);
  830. this._uniformBuffer.addUniform("vTangentSpaceParams", 2);
  831. this._uniformBuffer.addUniform("refractionMatrix", 16);
  832. this._uniformBuffer.addUniform("vRefractionInfos", 4);
  833. this._uniformBuffer.addUniform("vSpecularColor", 4);
  834. this._uniformBuffer.addUniform("vEmissiveColor", 3);
  835. this._uniformBuffer.addUniform("vDiffuseColor", 4);
  836. this._uniformBuffer.addUniform("pointSize", 1);
  837. this._uniformBuffer.create();
  838. }
  839. public unbind(): void {
  840. if (this._activeEffect) {
  841. let needFlag = false;
  842. if (this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  843. this._activeEffect.setTexture("reflection2DSampler", null);
  844. needFlag = true;
  845. }
  846. if (this._refractionTexture && this._refractionTexture.isRenderTarget) {
  847. this._activeEffect.setTexture("refraction2DSampler", null);
  848. needFlag = true;
  849. }
  850. if (needFlag) {
  851. this._markAllSubMeshesAsTexturesDirty();
  852. }
  853. }
  854. super.unbind();
  855. }
  856. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  857. var scene = this.getScene();
  858. var defines = <StandardMaterialDefines>subMesh._materialDefines;
  859. if (!defines) {
  860. return;
  861. }
  862. var effect = subMesh.effect;
  863. if (!effect) {
  864. return;
  865. }
  866. this._activeEffect = effect;
  867. // Matrices
  868. this.bindOnlyWorldMatrix(world);
  869. // Normal Matrix
  870. if (defines.OBJECTSPACE_NORMALMAP)
  871. {
  872. world.toNormalMatrix(this._normalMatrix);
  873. this.bindOnlyNormalMatrix(this._normalMatrix);
  874. }
  875. let mustRebind = this._mustRebind(scene, effect, mesh.visibility);
  876. // Bones
  877. MaterialHelper.BindBonesParameters(mesh, effect);
  878. if (mustRebind) {
  879. this._uniformBuffer.bindToEffect(effect, "Material");
  880. this.bindViewProjection(effect);
  881. if (!this._uniformBuffer.useUbo || !this.isFrozen || !this._uniformBuffer.isSync) {
  882. if (StandardMaterial.FresnelEnabled && defines.FRESNEL) {
  883. // Fresnel
  884. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  885. this._uniformBuffer.updateColor4("diffuseLeftColor", this.diffuseFresnelParameters.leftColor, this.diffuseFresnelParameters.power);
  886. this._uniformBuffer.updateColor4("diffuseRightColor", this.diffuseFresnelParameters.rightColor, this.diffuseFresnelParameters.bias);
  887. }
  888. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  889. this._uniformBuffer.updateColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  890. }
  891. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  892. this._uniformBuffer.updateColor4("reflectionLeftColor", this.reflectionFresnelParameters.leftColor, this.reflectionFresnelParameters.power);
  893. this._uniformBuffer.updateColor4("reflectionRightColor", this.reflectionFresnelParameters.rightColor, this.reflectionFresnelParameters.bias);
  894. }
  895. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  896. this._uniformBuffer.updateColor4("refractionLeftColor", this.refractionFresnelParameters.leftColor, this.refractionFresnelParameters.power);
  897. this._uniformBuffer.updateColor4("refractionRightColor", this.refractionFresnelParameters.rightColor, this.refractionFresnelParameters.bias);
  898. }
  899. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  900. this._uniformBuffer.updateColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  901. this._uniformBuffer.updateColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  902. }
  903. }
  904. // Textures
  905. if (scene.texturesEnabled) {
  906. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  907. this._uniformBuffer.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  908. MaterialHelper.BindTextureMatrix(this._diffuseTexture, this._uniformBuffer, "diffuse");
  909. if (this._diffuseTexture.hasAlpha) {
  910. effect.setFloat("alphaCutOff", this.alphaCutOff);
  911. }
  912. }
  913. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  914. this._uniformBuffer.updateFloat2("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level);
  915. MaterialHelper.BindTextureMatrix(this._ambientTexture, this._uniformBuffer, "ambient");
  916. }
  917. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  918. this._uniformBuffer.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
  919. MaterialHelper.BindTextureMatrix(this._opacityTexture, this._uniformBuffer, "opacity");
  920. }
  921. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  922. this._uniformBuffer.updateFloat2("vReflectionInfos", this._reflectionTexture.level, this.roughness);
  923. this._uniformBuffer.updateMatrix("reflectionMatrix", this._reflectionTexture.getReflectionTextureMatrix());
  924. if ((<any>this._reflectionTexture).boundingBoxSize) {
  925. let cubeTexture = <CubeTexture>this._reflectionTexture;
  926. this._uniformBuffer.updateVector3("vReflectionPosition", cubeTexture.boundingBoxPosition);
  927. this._uniformBuffer.updateVector3("vReflectionSize", cubeTexture.boundingBoxSize);
  928. }
  929. }
  930. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  931. this._uniformBuffer.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
  932. MaterialHelper.BindTextureMatrix(this._emissiveTexture, this._uniformBuffer, "emissive");
  933. }
  934. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  935. this._uniformBuffer.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
  936. MaterialHelper.BindTextureMatrix(this._lightmapTexture, this._uniformBuffer, "lightmap");
  937. }
  938. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  939. this._uniformBuffer.updateFloat2("vSpecularInfos", this._specularTexture.coordinatesIndex, this._specularTexture.level);
  940. MaterialHelper.BindTextureMatrix(this._specularTexture, this._uniformBuffer, "specular");
  941. }
  942. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  943. this._uniformBuffer.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, 1.0 / this._bumpTexture.level, this.parallaxScaleBias);
  944. MaterialHelper.BindTextureMatrix(this._bumpTexture, this._uniformBuffer, "bump");
  945. if (scene._mirroredCameraPosition) {
  946. this._uniformBuffer.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? 1.0 : -1.0, this._invertNormalMapY ? 1.0 : -1.0);
  947. } else {
  948. this._uniformBuffer.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? -1.0 : 1.0, this._invertNormalMapY ? -1.0 : 1.0);
  949. }
  950. }
  951. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  952. var depth = 1.0;
  953. if (!this._refractionTexture.isCube) {
  954. this._uniformBuffer.updateMatrix("refractionMatrix", this._refractionTexture.getReflectionTextureMatrix());
  955. if ((<any>this._refractionTexture).depth) {
  956. depth = (<any>this._refractionTexture).depth;
  957. }
  958. }
  959. this._uniformBuffer.updateFloat4("vRefractionInfos", this._refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  960. }
  961. }
  962. // Point size
  963. if (this.pointsCloud) {
  964. this._uniformBuffer.updateFloat("pointSize", this.pointSize);
  965. }
  966. if (defines.SPECULARTERM) {
  967. this._uniformBuffer.updateColor4("vSpecularColor", this.specularColor, this.specularPower);
  968. }
  969. this._uniformBuffer.updateColor3("vEmissiveColor", this.emissiveColor);
  970. // Diffuse
  971. this._uniformBuffer.updateColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  972. }
  973. // Textures
  974. if (scene.texturesEnabled) {
  975. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  976. effect.setTexture("diffuseSampler", this._diffuseTexture);
  977. }
  978. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  979. effect.setTexture("ambientSampler", this._ambientTexture);
  980. }
  981. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  982. effect.setTexture("opacitySampler", this._opacityTexture);
  983. }
  984. if (this._reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  985. if (this._reflectionTexture.isCube) {
  986. effect.setTexture("reflectionCubeSampler", this._reflectionTexture);
  987. } else {
  988. effect.setTexture("reflection2DSampler", this._reflectionTexture);
  989. }
  990. }
  991. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  992. effect.setTexture("emissiveSampler", this._emissiveTexture);
  993. }
  994. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  995. effect.setTexture("lightmapSampler", this._lightmapTexture);
  996. }
  997. if (this._specularTexture && StandardMaterial.SpecularTextureEnabled) {
  998. effect.setTexture("specularSampler", this._specularTexture);
  999. }
  1000. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled) {
  1001. effect.setTexture("bumpSampler", this._bumpTexture);
  1002. }
  1003. if (this._refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  1004. var depth = 1.0;
  1005. if (this._refractionTexture.isCube) {
  1006. effect.setTexture("refractionCubeSampler", this._refractionTexture);
  1007. } else {
  1008. effect.setTexture("refraction2DSampler", this._refractionTexture);
  1009. }
  1010. }
  1011. }
  1012. // Clip plane
  1013. MaterialHelper.BindClipPlane(effect, scene);
  1014. // Colors
  1015. scene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  1016. MaterialHelper.BindEyePosition(effect, scene);
  1017. effect.setColor3("vAmbientColor", this._globalAmbientColor);
  1018. }
  1019. if (mustRebind || !this.isFrozen) {
  1020. // Lights
  1021. if (scene.lightsEnabled && !this._disableLighting) {
  1022. MaterialHelper.BindLights(scene, mesh, effect, defines, this._maxSimultaneousLights);
  1023. }
  1024. // View
  1025. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE || this._reflectionTexture || this._refractionTexture) {
  1026. this.bindView(effect);
  1027. }
  1028. // Fog
  1029. MaterialHelper.BindFogParameters(scene, mesh, effect);
  1030. // Morph targets
  1031. if (defines.NUM_MORPH_INFLUENCERS) {
  1032. MaterialHelper.BindMorphTargetParameters(mesh, effect);
  1033. }
  1034. // Log. depth
  1035. MaterialHelper.BindLogDepth(defines, effect, scene);
  1036. // image processing
  1037. if (this._imageProcessingConfiguration && !this._imageProcessingConfiguration.applyByPostProcess) {
  1038. this._imageProcessingConfiguration.bind(this._activeEffect);
  1039. }
  1040. }
  1041. this._uniformBuffer.update();
  1042. this._afterBind(mesh, this._activeEffect);
  1043. }
  1044. public getAnimatables(): IAnimatable[] {
  1045. var results = [];
  1046. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  1047. results.push(this._diffuseTexture);
  1048. }
  1049. if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
  1050. results.push(this._ambientTexture);
  1051. }
  1052. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  1053. results.push(this._opacityTexture);
  1054. }
  1055. if (this._reflectionTexture && this._reflectionTexture.animations && this._reflectionTexture.animations.length > 0) {
  1056. results.push(this._reflectionTexture);
  1057. }
  1058. if (this._emissiveTexture && this._emissiveTexture.animations && this._emissiveTexture.animations.length > 0) {
  1059. results.push(this._emissiveTexture);
  1060. }
  1061. if (this._specularTexture && this._specularTexture.animations && this._specularTexture.animations.length > 0) {
  1062. results.push(this._specularTexture);
  1063. }
  1064. if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
  1065. results.push(this._bumpTexture);
  1066. }
  1067. if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
  1068. results.push(this._lightmapTexture);
  1069. }
  1070. if (this._refractionTexture && this._refractionTexture.animations && this._refractionTexture.animations.length > 0) {
  1071. results.push(this._refractionTexture);
  1072. }
  1073. return results;
  1074. }
  1075. public getActiveTextures(): BaseTexture[] {
  1076. var activeTextures = super.getActiveTextures();
  1077. if (this._diffuseTexture) {
  1078. activeTextures.push(this._diffuseTexture);
  1079. }
  1080. if (this._ambientTexture) {
  1081. activeTextures.push(this._ambientTexture);
  1082. }
  1083. if (this._opacityTexture) {
  1084. activeTextures.push(this._opacityTexture);
  1085. }
  1086. if (this._reflectionTexture) {
  1087. activeTextures.push(this._reflectionTexture);
  1088. }
  1089. if (this._emissiveTexture) {
  1090. activeTextures.push(this._emissiveTexture);
  1091. }
  1092. if (this._specularTexture) {
  1093. activeTextures.push(this._specularTexture);
  1094. }
  1095. if (this._bumpTexture) {
  1096. activeTextures.push(this._bumpTexture);
  1097. }
  1098. if (this._lightmapTexture) {
  1099. activeTextures.push(this._lightmapTexture);
  1100. }
  1101. if (this._refractionTexture) {
  1102. activeTextures.push(this._refractionTexture);
  1103. }
  1104. return activeTextures;
  1105. }
  1106. public hasTexture(texture: BaseTexture): boolean {
  1107. if (super.hasTexture(texture)) {
  1108. return true;
  1109. }
  1110. if (this._diffuseTexture === texture) {
  1111. return true;
  1112. }
  1113. if (this._ambientTexture === texture) {
  1114. return true;
  1115. }
  1116. if (this._opacityTexture === texture) {
  1117. return true;
  1118. }
  1119. if (this._reflectionTexture === texture) {
  1120. return true;
  1121. }
  1122. if (this._emissiveTexture === texture) {
  1123. return true;
  1124. }
  1125. if (this._specularTexture === texture) {
  1126. return true;
  1127. }
  1128. if (this._bumpTexture === texture) {
  1129. return true;
  1130. }
  1131. if (this._lightmapTexture === texture) {
  1132. return true;
  1133. }
  1134. if (this._refractionTexture === texture) {
  1135. return true;
  1136. }
  1137. return false;
  1138. }
  1139. public dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean): void {
  1140. if (forceDisposeTextures) {
  1141. if (this._diffuseTexture) {
  1142. this._diffuseTexture.dispose();
  1143. }
  1144. if (this._ambientTexture) {
  1145. this._ambientTexture.dispose();
  1146. }
  1147. if (this._opacityTexture) {
  1148. this._opacityTexture.dispose();
  1149. }
  1150. if (this._reflectionTexture) {
  1151. this._reflectionTexture.dispose();
  1152. }
  1153. if (this._emissiveTexture) {
  1154. this._emissiveTexture.dispose();
  1155. }
  1156. if (this._specularTexture) {
  1157. this._specularTexture.dispose();
  1158. }
  1159. if (this._bumpTexture) {
  1160. this._bumpTexture.dispose();
  1161. }
  1162. if (this._lightmapTexture) {
  1163. this._lightmapTexture.dispose();
  1164. }
  1165. if (this._refractionTexture) {
  1166. this._refractionTexture.dispose();
  1167. }
  1168. }
  1169. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  1170. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  1171. }
  1172. super.dispose(forceDisposeEffect, forceDisposeTextures);
  1173. }
  1174. public clone(name: string): StandardMaterial {
  1175. var result = SerializationHelper.Clone(() => new StandardMaterial(name, this.getScene()), this);
  1176. result.name = name;
  1177. result.id = name;
  1178. return result;
  1179. }
  1180. public serialize(): any {
  1181. return SerializationHelper.Serialize(this);
  1182. }
  1183. // Statics
  1184. public static Parse(source: any, scene: Scene, rootUrl: string): StandardMaterial {
  1185. return SerializationHelper.Parse(() => new StandardMaterial(source.name, scene), source, scene, rootUrl);
  1186. }
  1187. // Flags used to enable or disable a type of texture for all Standard Materials
  1188. static _DiffuseTextureEnabled = true;
  1189. public static get DiffuseTextureEnabled(): boolean {
  1190. return StandardMaterial._DiffuseTextureEnabled;
  1191. }
  1192. public static set DiffuseTextureEnabled(value: boolean) {
  1193. if (StandardMaterial._DiffuseTextureEnabled === value) {
  1194. return;
  1195. }
  1196. StandardMaterial._DiffuseTextureEnabled = value;
  1197. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1198. }
  1199. static _AmbientTextureEnabled = true;
  1200. public static get AmbientTextureEnabled(): boolean {
  1201. return StandardMaterial._AmbientTextureEnabled;
  1202. }
  1203. public static set AmbientTextureEnabled(value: boolean) {
  1204. if (StandardMaterial._AmbientTextureEnabled === value) {
  1205. return;
  1206. }
  1207. StandardMaterial._AmbientTextureEnabled = value;
  1208. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1209. }
  1210. static _OpacityTextureEnabled = true;
  1211. public static get OpacityTextureEnabled(): boolean {
  1212. return StandardMaterial._OpacityTextureEnabled;
  1213. }
  1214. public static set OpacityTextureEnabled(value: boolean) {
  1215. if (StandardMaterial._OpacityTextureEnabled === value) {
  1216. return;
  1217. }
  1218. StandardMaterial._OpacityTextureEnabled = value;
  1219. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1220. }
  1221. static _ReflectionTextureEnabled = true;
  1222. public static get ReflectionTextureEnabled(): boolean {
  1223. return StandardMaterial._ReflectionTextureEnabled;
  1224. }
  1225. public static set ReflectionTextureEnabled(value: boolean) {
  1226. if (StandardMaterial._ReflectionTextureEnabled === value) {
  1227. return;
  1228. }
  1229. StandardMaterial._ReflectionTextureEnabled = value;
  1230. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1231. }
  1232. static _EmissiveTextureEnabled = true;
  1233. public static get EmissiveTextureEnabled(): boolean {
  1234. return StandardMaterial._EmissiveTextureEnabled;
  1235. }
  1236. public static set EmissiveTextureEnabled(value: boolean) {
  1237. if (StandardMaterial._EmissiveTextureEnabled === value) {
  1238. return;
  1239. }
  1240. StandardMaterial._EmissiveTextureEnabled = value;
  1241. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1242. }
  1243. static _SpecularTextureEnabled = true;
  1244. public static get SpecularTextureEnabled(): boolean {
  1245. return StandardMaterial._SpecularTextureEnabled;
  1246. }
  1247. public static set SpecularTextureEnabled(value: boolean) {
  1248. if (StandardMaterial._SpecularTextureEnabled === value) {
  1249. return;
  1250. }
  1251. StandardMaterial._SpecularTextureEnabled = value;
  1252. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1253. }
  1254. static _BumpTextureEnabled = true;
  1255. public static get BumpTextureEnabled(): boolean {
  1256. return StandardMaterial._BumpTextureEnabled;
  1257. }
  1258. public static set BumpTextureEnabled(value: boolean) {
  1259. if (StandardMaterial._BumpTextureEnabled === value) {
  1260. return;
  1261. }
  1262. StandardMaterial._BumpTextureEnabled = value;
  1263. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1264. }
  1265. static _LightmapTextureEnabled = true;
  1266. public static get LightmapTextureEnabled(): boolean {
  1267. return StandardMaterial._LightmapTextureEnabled;
  1268. }
  1269. public static set LightmapTextureEnabled(value: boolean) {
  1270. if (StandardMaterial._LightmapTextureEnabled === value) {
  1271. return;
  1272. }
  1273. StandardMaterial._LightmapTextureEnabled = value;
  1274. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1275. }
  1276. static _RefractionTextureEnabled = true;
  1277. public static get RefractionTextureEnabled(): boolean {
  1278. return StandardMaterial._RefractionTextureEnabled;
  1279. }
  1280. public static set RefractionTextureEnabled(value: boolean) {
  1281. if (StandardMaterial._RefractionTextureEnabled === value) {
  1282. return;
  1283. }
  1284. StandardMaterial._RefractionTextureEnabled = value;
  1285. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1286. }
  1287. static _ColorGradingTextureEnabled = true;
  1288. public static get ColorGradingTextureEnabled(): boolean {
  1289. return StandardMaterial._ColorGradingTextureEnabled;
  1290. }
  1291. public static set ColorGradingTextureEnabled(value: boolean) {
  1292. if (StandardMaterial._ColorGradingTextureEnabled === value) {
  1293. return;
  1294. }
  1295. StandardMaterial._ColorGradingTextureEnabled = value;
  1296. Engine.MarkAllMaterialsAsDirty(Material.TextureDirtyFlag);
  1297. }
  1298. static _FresnelEnabled = true;
  1299. public static get FresnelEnabled(): boolean {
  1300. return StandardMaterial._FresnelEnabled;
  1301. }
  1302. public static set FresnelEnabled(value: boolean) {
  1303. if (StandardMaterial._FresnelEnabled === value) {
  1304. return;
  1305. }
  1306. StandardMaterial._FresnelEnabled = value;
  1307. Engine.MarkAllMaterialsAsDirty(Material.FresnelDirtyFlag);
  1308. }
  1309. }
  1310. }