babylon.backgroundMaterial.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. namespace BABYLON {
  3. /**
  4. * Background material defines definition.
  5. */
  6. class BackgroundMaterialDefines extends MaterialDefines implements IImageProcessingConfigurationDefines {
  7. /**
  8. * True if the diffuse texture is in use.
  9. */
  10. public DIFFUSE = false;
  11. /**
  12. * The direct UV channel to use.
  13. */
  14. public DIFFUSEDIRECTUV = 0;
  15. /**
  16. * True if the diffuse texture is in gamma space.
  17. */
  18. public GAMMADIFFUSE = false;
  19. /**
  20. * True if the diffuse texture has opacity in the alpha channel.
  21. */
  22. public DIFFUSEHASALPHA = false;
  23. /**
  24. * True if you want the material to fade to transparent at grazing angle.
  25. */
  26. public OPACITYFRESNEL = false;
  27. /**
  28. * True if an extra blur needs to be added in the reflection.
  29. */
  30. public REFLECTIONBLUR = false;
  31. /**
  32. * False if the current Webgl implementation does not support the texture lod extension.
  33. */
  34. public TEXTURELODSUPPORT = false;
  35. /**
  36. * True to ensure the data are premultiplied.
  37. */
  38. public PREMULTIPLYALPHA = false;
  39. /**
  40. * True if the texture contains cooked RGB values and not gray scaled multipliers.
  41. */
  42. public USERGBCOLOR = false;
  43. // Image Processing Configuration.
  44. public IMAGEPROCESSING = false;
  45. public VIGNETTE = false;
  46. public VIGNETTEBLENDMODEMULTIPLY = false;
  47. public VIGNETTEBLENDMODEOPAQUE = false;
  48. public TONEMAPPING = false;
  49. public CONTRAST = false;
  50. public COLORCURVES = false;
  51. public COLORGRADING = false;
  52. public COLORGRADING3D = false;
  53. public SAMPLER3DGREENDEPTH = false;
  54. public SAMPLER3DBGRMAP = false;
  55. public IMAGEPROCESSINGPOSTPROCESS = false;
  56. public EXPOSURE = false;
  57. // Reflection.
  58. public REFLECTION = false;
  59. public REFLECTIONMAP_3D = false;
  60. public REFLECTIONMAP_SPHERICAL = false;
  61. public REFLECTIONMAP_PLANAR = false;
  62. public REFLECTIONMAP_CUBIC = false;
  63. public REFLECTIONMAP_PROJECTION = false;
  64. public REFLECTIONMAP_SKYBOX = false;
  65. public REFLECTIONMAP_EXPLICIT = false;
  66. public REFLECTIONMAP_EQUIRECTANGULAR = false;
  67. public REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  68. public REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  69. public INVERTCUBICMAP = false;
  70. public REFLECTIONMAP_OPPOSITEZ = false;
  71. public LODINREFLECTIONALPHA = false;
  72. public GAMMAREFLECTION = false;
  73. // Default BJS.
  74. public MAINUV1 = false;
  75. public MAINUV2 = false;
  76. public UV1 = false;
  77. public UV2 = false;
  78. public CLIPPLANE = false;
  79. public POINTSIZE = false;
  80. public FOG = false;
  81. public NORMAL = false;
  82. public NUM_BONE_INFLUENCERS = 0;
  83. public BonesPerMesh = 0;
  84. public INSTANCES = false;
  85. public SHADOWFLOAT = false;
  86. /**
  87. * Constructor of the defines.
  88. */
  89. constructor() {
  90. super();
  91. this.rebuild();
  92. }
  93. }
  94. /**
  95. * Background material
  96. */
  97. export class BackgroundMaterial extends BABYLON.PushMaterial {
  98. /**
  99. * Key light Color (multiply against the R channel of the environement texture)
  100. */
  101. @serializeAsColor3()
  102. protected _primaryColor: Color3;
  103. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  104. public primaryColor = BABYLON.Color3.White();
  105. /**
  106. * Key light Level (allowing HDR output of the background)
  107. */
  108. @serialize()
  109. protected _primaryLevel: float;
  110. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  111. public primaryLevel: float = 1;
  112. /**
  113. * Secondary light Color (multiply against the G channel of the environement texture)
  114. */
  115. @serializeAsColor3()
  116. protected _secondaryColor: Color3;
  117. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  118. public secondaryColor = BABYLON.Color3.Gray();
  119. /**
  120. * Secondary light Level (allowing HDR output of the background)
  121. */
  122. @serialize()
  123. protected _secondaryLevel: float;
  124. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  125. public secondaryLevel: float = 1;
  126. /**
  127. * Third light Color (multiply against the B channel of the environement texture)
  128. */
  129. @serializeAsColor3()
  130. protected _thirdColor: Color3;
  131. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  132. public thirdColor = BABYLON.Color3.Black();
  133. /**
  134. * Third light Level (allowing HDR output of the background)
  135. */
  136. @serialize()
  137. protected _thirdLevel: float;
  138. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  139. public thirdLevel: float = 1;
  140. /**
  141. * Reflection Texture used in the material.
  142. * Should be author in a specific way for the best result (refer to the documentation).
  143. */
  144. @serializeAsTexture()
  145. protected _reflectionTexture: Nullable<BaseTexture>;
  146. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  147. public reflectionTexture: Nullable<BaseTexture> = null;
  148. /**
  149. * Reflection Texture level of blur.
  150. *
  151. * Can be use to reuse an existing HDR Texture and target a specific LOD to prevent authoring the
  152. * texture twice.
  153. */
  154. @serialize()
  155. protected _reflectionBlur: float;
  156. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  157. public reflectionBlur: float = 0;
  158. /**
  159. * Diffuse Texture used in the material.
  160. * Should be author in a specific way for the best result (refer to the documentation).
  161. */
  162. @serializeAsTexture()
  163. protected _diffuseTexture: Nullable<BaseTexture>;
  164. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  165. public diffuseTexture: Nullable<BaseTexture> = null;
  166. /**
  167. * Specify the list of lights casting shadow on the material.
  168. * All scene shadow lights will be included if null.
  169. */
  170. protected _shadowLights: Nullable<IShadowLight[]> = null;
  171. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  172. public shadowLights: Nullable<IShadowLight[]> = null;
  173. /**
  174. * For the lights having a blurred shadow generator, this can add a second blur pass in order to reach
  175. * soft lighting on the background.
  176. */
  177. @serialize()
  178. protected _shadowBlurScale: int;
  179. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  180. public shadowBlurScale: int = 1;
  181. /**
  182. * Helps adjusting the shadow to a softer level if required.
  183. * 0 means black shadows and 1 means no shadows.
  184. */
  185. @serialize()
  186. protected _shadowLevel: float;
  187. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  188. public shadowLevel: float = 0;
  189. /**
  190. * This helps specifying that the material is falling off to the sky box at grazing angle.
  191. * This helps ensuring a nice transition when the camera goes under the ground.
  192. */
  193. @serialize()
  194. protected _opacityFresnel: boolean;
  195. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  196. public opacityFresnel: boolean = true;
  197. /**
  198. * Helps to directly use the maps channels instead of their level.
  199. */
  200. @serialize()
  201. protected _useRGBColor: boolean;
  202. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  203. public useRGBColor: boolean = true;
  204. /**
  205. * Number of Simultaneous lights allowed on the material.
  206. */
  207. @serialize()
  208. private _maxSimultaneousLights: int = 4;
  209. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  210. public maxSimultaneousLights: int = 4;
  211. /**
  212. * Default configuration related to image processing available in the Background Material.
  213. */
  214. @serializeAsImageProcessingConfiguration()
  215. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  216. /**
  217. * Keep track of the image processing observer to allow dispose and replace.
  218. */
  219. private _imageProcessingObserver: Nullable<Observer<ImageProcessingConfiguration>> = null;
  220. /**
  221. * Attaches a new image processing configuration to the PBR Material.
  222. * @param configuration (if null the scene configuration will be use)
  223. */
  224. protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>): void {
  225. if (configuration === this._imageProcessingConfiguration) {
  226. return;
  227. }
  228. // Detaches observer.
  229. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  230. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  231. }
  232. // Pick the scene configuration if needed.
  233. if (!configuration) {
  234. this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
  235. }
  236. else {
  237. this._imageProcessingConfiguration = configuration;
  238. }
  239. // Attaches observer.
  240. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(conf => {
  241. this._markAllSubMeshesAsImageProcessingDirty();
  242. });
  243. }
  244. /**
  245. * Gets the image processing configuration used either in this material.
  246. */
  247. public get imageProcessingConfiguration(): Nullable<ImageProcessingConfiguration> {
  248. return this._imageProcessingConfiguration;
  249. }
  250. /**
  251. * Sets the Default image processing configuration used either in the this material.
  252. *
  253. * If sets to null, the scene one is in use.
  254. */
  255. public set imageProcessingConfiguration(value: Nullable<ImageProcessingConfiguration>) {
  256. this._attachImageProcessingConfiguration(value);
  257. // Ensure the effect will be rebuilt.
  258. this._markAllSubMeshesAsTexturesDirty();
  259. }
  260. /**
  261. * Gets wether the color curves effect is enabled.
  262. */
  263. public get cameraColorCurvesEnabled(): boolean {
  264. return (<ImageProcessingConfiguration>this.imageProcessingConfiguration).colorCurvesEnabled;
  265. }
  266. /**
  267. * Sets wether the color curves effect is enabled.
  268. */
  269. public set cameraColorCurvesEnabled(value: boolean) {
  270. (<ImageProcessingConfiguration>this.imageProcessingConfiguration).colorCurvesEnabled = value;
  271. }
  272. /**
  273. * Gets wether the color grading effect is enabled.
  274. */
  275. public get cameraColorGradingEnabled(): boolean {
  276. return (<ImageProcessingConfiguration>this.imageProcessingConfiguration).colorGradingEnabled;
  277. }
  278. /**
  279. * Gets wether the color grading effect is enabled.
  280. */
  281. public set cameraColorGradingEnabled(value: boolean) {
  282. (<ImageProcessingConfiguration>this.imageProcessingConfiguration).colorGradingEnabled = value;
  283. }
  284. /**
  285. * Gets wether tonemapping is enabled or not.
  286. */
  287. public get cameraToneMappingEnabled(): boolean {
  288. return this._imageProcessingConfiguration.toneMappingEnabled;
  289. };
  290. /**
  291. * Sets wether tonemapping is enabled or not
  292. */
  293. public set cameraToneMappingEnabled(value: boolean) {
  294. this._imageProcessingConfiguration.toneMappingEnabled = value;
  295. };
  296. /**
  297. * The camera exposure used on this material.
  298. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  299. * This corresponds to a photographic exposure.
  300. */
  301. public get cameraExposure(): float {
  302. return this._imageProcessingConfiguration.exposure;
  303. };
  304. /**
  305. * The camera exposure used on this material.
  306. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  307. * This corresponds to a photographic exposure.
  308. */
  309. public set cameraExposure(value: float) {
  310. this._imageProcessingConfiguration.exposure = value;
  311. };
  312. /**
  313. * Gets The camera contrast used on this material.
  314. */
  315. public get cameraContrast(): float {
  316. return this._imageProcessingConfiguration.contrast;
  317. }
  318. /**
  319. * Sets The camera contrast used on this material.
  320. */
  321. public set cameraContrast(value: float) {
  322. this._imageProcessingConfiguration.contrast = value;
  323. }
  324. /**
  325. * Gets the Color Grading 2D Lookup Texture.
  326. */
  327. public get cameraColorGradingTexture(): Nullable<BaseTexture> {
  328. return this._imageProcessingConfiguration.colorGradingTexture;
  329. }
  330. /**
  331. * Sets the Color Grading 2D Lookup Texture.
  332. */
  333. public set cameraColorGradingTexture(value: Nullable<BaseTexture>) {
  334. (<ImageProcessingConfiguration>this.imageProcessingConfiguration).colorGradingTexture = value;
  335. }
  336. /**
  337. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  338. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  339. * 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;
  340. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  341. */
  342. public get cameraColorCurves(): Nullable<ColorCurves> {
  343. return (<ImageProcessingConfiguration>this.imageProcessingConfiguration).colorCurves;
  344. }
  345. /**
  346. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  347. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  348. * 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;
  349. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  350. */
  351. public set cameraColorCurves(value: Nullable<ColorCurves>) {
  352. (<ImageProcessingConfiguration>this.imageProcessingConfiguration).colorCurves = value;
  353. }
  354. // Temp values kept as cache in the material.
  355. private _renderTargets = new SmartArray<RenderTargetTexture>(16);
  356. /**
  357. * constructor
  358. * @param name The name of the material
  359. * @param scene The scene to add the material to
  360. */
  361. constructor(name: string, scene: BABYLON.Scene) {
  362. super(name, scene);
  363. // Setup the default processing configuration to the scene.
  364. this._attachImageProcessingConfiguration(null);
  365. this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
  366. this._renderTargets.reset();
  367. if (this._diffuseTexture && this._diffuseTexture.isRenderTarget) {
  368. this._renderTargets.push(this._diffuseTexture as RenderTargetTexture);
  369. }
  370. if (this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  371. this._renderTargets.push(this._reflectionTexture as RenderTargetTexture);
  372. }
  373. return this._renderTargets;
  374. }
  375. }
  376. /**
  377. * The entire material has been created in order to prevent overdraw.
  378. * @returns false
  379. */
  380. public needAlphaTesting(): boolean {
  381. return false;
  382. }
  383. /**
  384. * The entire material has been created in order to prevent overdraw.
  385. * @returns true if blending is enable
  386. */
  387. public needAlphaBlending(): boolean {
  388. return ((this.alpha < 0) || (this._diffuseTexture != null && this._diffuseTexture.hasAlpha));
  389. }
  390. /**
  391. * Checks wether the material is ready to be rendered for a given mesh.
  392. * @param mesh The mesh to render
  393. * @param subMesh The submesh to check against
  394. * @param useInstances Specify wether or not the material is used with instances
  395. */
  396. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances: boolean = false): boolean {
  397. if (subMesh.effect && this.isFrozen) {
  398. if (this._wasPreviouslyReady) {
  399. return true;
  400. }
  401. }
  402. if (!subMesh._materialDefines) {
  403. subMesh._materialDefines = new BackgroundMaterialDefines();
  404. }
  405. var scene = this.getScene();
  406. var defines = <BackgroundMaterialDefines>subMesh._materialDefines;
  407. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  408. if (defines._renderId === scene.getRenderId()) {
  409. return true;
  410. }
  411. }
  412. var engine = scene.getEngine();
  413. // Lights
  414. MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights);
  415. defines._needNormals = true;
  416. // Textures
  417. if (defines._areTexturesDirty) {
  418. defines._needUVs = false;
  419. if (scene.texturesEnabled) {
  420. if (scene.getEngine().getCaps().textureLOD) {
  421. defines.TEXTURELODSUPPORT = true;
  422. }
  423. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  424. if (!this._diffuseTexture.isReadyOrNotBlocking()) {
  425. return false;
  426. }
  427. MaterialHelper.PrepareDefinesForMergedUV(this._diffuseTexture, defines, "DIFFUSE");
  428. defines.DIFFUSEHASALPHA = this._diffuseTexture.hasAlpha;
  429. defines.GAMMADIFFUSE = this._diffuseTexture.gammaSpace;
  430. defines.OPACITYFRESNEL = this._opacityFresnel;
  431. } else {
  432. defines.DIFFUSE = false;
  433. defines.DIFFUSEHASALPHA = false;
  434. defines.GAMMADIFFUSE = false;
  435. defines.OPACITYFRESNEL = false;
  436. }
  437. var reflectionTexture = this._reflectionTexture;
  438. if (reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  439. if (!reflectionTexture.isReadyOrNotBlocking()) {
  440. return false;
  441. }
  442. defines.REFLECTION = true;
  443. defines.GAMMAREFLECTION = reflectionTexture.gammaSpace;
  444. defines.REFLECTIONBLUR = this._reflectionBlur > 0;
  445. defines.REFLECTIONMAP_OPPOSITEZ = this.getScene().useRightHandedSystem ? !reflectionTexture.invertZ : reflectionTexture.invertZ;
  446. defines.LODINREFLECTIONALPHA = reflectionTexture.lodLevelInAlpha;
  447. if (reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE) {
  448. defines.INVERTCUBICMAP = true;
  449. }
  450. defines.REFLECTIONMAP_3D = reflectionTexture.isCube;
  451. switch (reflectionTexture.coordinatesMode) {
  452. case Texture.CUBIC_MODE:
  453. case Texture.INVCUBIC_MODE:
  454. defines.REFLECTIONMAP_CUBIC = true;
  455. break;
  456. case Texture.EXPLICIT_MODE:
  457. defines.REFLECTIONMAP_EXPLICIT = true;
  458. break;
  459. case Texture.PLANAR_MODE:
  460. defines.REFLECTIONMAP_PLANAR = true;
  461. break;
  462. case Texture.PROJECTION_MODE:
  463. defines.REFLECTIONMAP_PROJECTION = true;
  464. break;
  465. case Texture.SKYBOX_MODE:
  466. defines.REFLECTIONMAP_SKYBOX = true;
  467. break;
  468. case Texture.SPHERICAL_MODE:
  469. defines.REFLECTIONMAP_SPHERICAL = true;
  470. break;
  471. case Texture.EQUIRECTANGULAR_MODE:
  472. defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  473. break;
  474. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  475. defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
  476. break;
  477. case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
  478. defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = true;
  479. break;
  480. }
  481. } else {
  482. defines.REFLECTION = false;
  483. defines.REFLECTIONBLUR = false;
  484. defines.REFLECTIONMAP_3D = false;
  485. defines.REFLECTIONMAP_SPHERICAL = false;
  486. defines.REFLECTIONMAP_PLANAR = false;
  487. defines.REFLECTIONMAP_CUBIC = false;
  488. defines.REFLECTIONMAP_PROJECTION = false;
  489. defines.REFLECTIONMAP_SKYBOX = false;
  490. defines.REFLECTIONMAP_EXPLICIT = false;
  491. defines.REFLECTIONMAP_EQUIRECTANGULAR = false;
  492. defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  493. defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  494. defines.INVERTCUBICMAP = false;
  495. defines.REFLECTIONMAP_OPPOSITEZ = false;
  496. defines.LODINREFLECTIONALPHA = false;
  497. defines.GAMMAREFLECTION = false;
  498. }
  499. }
  500. defines.PREMULTIPLYALPHA = (this.alphaMode === Engine.ALPHA_PREMULTIPLIED || this.alphaMode === Engine.ALPHA_PREMULTIPLIED_PORTERDUFF);
  501. defines.USERGBCOLOR = this._useRGBColor;
  502. }
  503. if (defines._areImageProcessingDirty) {
  504. if (!this._imageProcessingConfiguration.isReady()) {
  505. return false;
  506. }
  507. this._imageProcessingConfiguration.prepareDefines(defines);
  508. }
  509. // Misc.
  510. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  511. // Values that need to be evaluated on every frame
  512. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances, false);
  513. // Attribs
  514. if (MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true, false)) {
  515. if (mesh) {
  516. if (!scene.getEngine().getCaps().standardDerivatives && !mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  517. mesh.createNormals(true);
  518. Tools.Warn("BackgroundMaterial: Normals have been created for the mesh: " + mesh.name);
  519. }
  520. }
  521. }
  522. // Get correct effect
  523. if (defines.isDirty) {
  524. defines.markAsProcessed();
  525. scene.resetCachedMaterial();
  526. // Fallbacks
  527. var fallbacks = new EffectFallbacks();
  528. if (defines.FOG) {
  529. fallbacks.addFallback(0, "FOG");
  530. }
  531. if (defines.POINTSIZE) {
  532. fallbacks.addFallback(1, "POINTSIZE");
  533. }
  534. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights);
  535. if (defines.NUM_BONE_INFLUENCERS > 0) {
  536. fallbacks.addCPUSkinningFallback(0, mesh);
  537. }
  538. //Attributes
  539. var attribs = [VertexBuffer.PositionKind];
  540. if (defines.NORMAL) {
  541. attribs.push(VertexBuffer.NormalKind);
  542. }
  543. if (defines.UV1) {
  544. attribs.push(VertexBuffer.UVKind);
  545. }
  546. if (defines.UV2) {
  547. attribs.push(VertexBuffer.UV2Kind);
  548. }
  549. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  550. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  551. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  552. "vFogInfos", "vFogColor", "pointSize",
  553. "vClipPlane", "mBones",
  554. "vPrimaryColor", "vSecondaryColor", "vThirdColor",
  555. "vReflectionInfos", "reflectionMatrix", "vReflectionMicrosurfaceInfos",
  556. "shadowLevel", "alpha",
  557. "vDiffuseInfos", "diffuseMatrix",
  558. ];
  559. var samplers = ["diffuseSampler", "reflectionSampler", "reflectionSamplerLow", "reflectionSamplerHigh"];
  560. var uniformBuffers = ["Material", "Scene"];
  561. ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
  562. ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
  563. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  564. uniformsNames: uniforms,
  565. uniformBuffersNames: uniformBuffers,
  566. samplers: samplers,
  567. defines: defines,
  568. maxSimultaneousLights: this._maxSimultaneousLights
  569. });
  570. var onCompiled = (effect: Effect) => {
  571. if (this.onCompiled) {
  572. this.onCompiled(effect);
  573. }
  574. this.bindSceneUniformBuffer(effect, scene.getSceneUniformBuffer());
  575. };
  576. var join = defines.toString();
  577. subMesh.setEffect(scene.getEngine().createEffect("background", <EffectCreationOptions>{
  578. attributes: attribs,
  579. uniformsNames: uniforms,
  580. uniformBuffersNames: uniformBuffers,
  581. samplers: samplers,
  582. defines: join,
  583. fallbacks: fallbacks,
  584. onCompiled: onCompiled,
  585. onError: this.onError,
  586. indexParameters: { maxSimultaneousLights: this._maxSimultaneousLights }
  587. }, engine), defines);
  588. this.buildUniformLayout();
  589. }
  590. if (!subMesh.effect || !subMesh.effect.isReady()) {
  591. return false;
  592. }
  593. defines._renderId = scene.getRenderId();
  594. this._wasPreviouslyReady = true;
  595. return true;
  596. }
  597. /**
  598. * Build the uniform buffer used in the material.
  599. */
  600. public buildUniformLayout(): void {
  601. // Order is important !
  602. this._uniformBuffer.addUniform("vPrimaryColor", 4);
  603. this._uniformBuffer.addUniform("vSecondaryColor", 4);
  604. this._uniformBuffer.addUniform("vThirdColor", 4);
  605. this._uniformBuffer.addUniform("vDiffuseInfos", 2);
  606. this._uniformBuffer.addUniform("vReflectionInfos", 2);
  607. this._uniformBuffer.addUniform("diffuseMatrix", 16);
  608. this._uniformBuffer.addUniform("reflectionMatrix", 16);
  609. this._uniformBuffer.addUniform("vReflectionMicrosurfaceInfos", 3);
  610. this._uniformBuffer.addUniform("pointSize", 1);
  611. this._uniformBuffer.addUniform("shadowLevel", 1);
  612. this._uniformBuffer.addUniform("alpha", 1);
  613. this._uniformBuffer.create();
  614. }
  615. /**
  616. * Unbind the material.
  617. */
  618. public unbind(): void {
  619. if (this._diffuseTexture && this._diffuseTexture.isRenderTarget) {
  620. this._uniformBuffer.setTexture("diffuseSampler", null);
  621. }
  622. if (this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  623. this._uniformBuffer.setTexture("reflectionSampler", null);
  624. }
  625. super.unbind();
  626. }
  627. /**
  628. * Bind only the world matrix to the material.
  629. * @param world The world matrix to bind.
  630. */
  631. public bindOnlyWorldMatrix(world: Matrix): void {
  632. this._activeEffect.setMatrix("world", world);
  633. }
  634. /**
  635. * Bind the material for a dedicated submeh (every used meshes will be considered opaque).
  636. * @param world The world matrix to bind.
  637. * @param subMesh The submesh to bind for.
  638. */
  639. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  640. var scene = this.getScene();
  641. var defines = <BackgroundMaterialDefines>subMesh._materialDefines;
  642. if (!defines) {
  643. return;
  644. }
  645. var effect = subMesh.effect;
  646. if (!effect) {
  647. return;
  648. }
  649. this._activeEffect = effect;
  650. // Matrices
  651. this.bindOnlyWorldMatrix(world);
  652. // Bones
  653. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  654. let mustRebind = this._mustRebind(scene, effect, mesh.visibility);
  655. if (mustRebind) {
  656. this._uniformBuffer.bindToEffect(effect, "Material");
  657. this.bindViewProjection(effect);
  658. let reflectionTexture = this._reflectionTexture;
  659. if (!this._uniformBuffer.useUbo || !this.isFrozen || !this._uniformBuffer.isSync) {
  660. // Texture uniforms
  661. if (scene.texturesEnabled) {
  662. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  663. this._uniformBuffer.updateFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  664. MaterialHelper.BindTextureMatrix(this._diffuseTexture, this._uniformBuffer, "diffuse");
  665. }
  666. if (reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  667. this._uniformBuffer.updateMatrix("reflectionMatrix", reflectionTexture.getReflectionTextureMatrix());
  668. this._uniformBuffer.updateFloat2("vReflectionInfos", reflectionTexture.level, this._reflectionBlur);
  669. this._uniformBuffer.updateFloat3("vReflectionMicrosurfaceInfos",
  670. reflectionTexture.getSize().width,
  671. reflectionTexture.lodGenerationScale,
  672. reflectionTexture.lodGenerationOffset);
  673. }
  674. }
  675. if (this.shadowLevel > 0) {
  676. this._uniformBuffer.updateFloat("shadowLevel", this.shadowLevel);
  677. }
  678. this._uniformBuffer.updateFloat("alpha", this.alpha);
  679. // Point size
  680. if (this.pointsCloud) {
  681. this._uniformBuffer.updateFloat("pointSize", this.pointSize);
  682. }
  683. this._uniformBuffer.updateColor4("vPrimaryColor", this._primaryColor, this._primaryLevel);
  684. this._uniformBuffer.updateColor4("vSecondaryColor", this._secondaryColor, this._secondaryLevel);
  685. this._uniformBuffer.updateColor4("vThirdColor", this._thirdColor, this._thirdLevel);
  686. }
  687. // Textures
  688. if (scene.texturesEnabled) {
  689. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  690. this._uniformBuffer.setTexture("diffuseSampler", this._diffuseTexture);
  691. }
  692. if (reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  693. if (defines.REFLECTIONBLUR && defines.TEXTURELODSUPPORT) {
  694. this._uniformBuffer.setTexture("reflectionSampler", reflectionTexture);
  695. }
  696. else if (!defines.REFLECTIONBLUR) {
  697. this._uniformBuffer.setTexture("reflectionSampler", reflectionTexture);
  698. }
  699. else {
  700. this._uniformBuffer.setTexture("reflectionSampler", reflectionTexture._lodTextureMid || reflectionTexture);
  701. this._uniformBuffer.setTexture("reflectionSamplerLow", reflectionTexture._lodTextureLow || reflectionTexture);
  702. this._uniformBuffer.setTexture("reflectionSamplerHigh", reflectionTexture._lodTextureHigh || reflectionTexture);
  703. }
  704. }
  705. }
  706. // Clip plane
  707. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  708. var eyePosition = scene._mirroredCameraPosition ? scene._mirroredCameraPosition : (<Camera>scene.activeCamera).globalPosition;
  709. // var invertNormal = (scene.useRightHandedSystem === (scene._mirroredCameraPosition != null));
  710. effect.setFloat3("vEyePosition",
  711. eyePosition.x,
  712. eyePosition.y,
  713. eyePosition.z);
  714. }
  715. if (mustRebind || !this.isFrozen) {
  716. if (scene.lightsEnabled) {
  717. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this._maxSimultaneousLights, false);
  718. }
  719. // View
  720. this.bindView(effect);
  721. // Fog
  722. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  723. // image processing
  724. this._imageProcessingConfiguration.bind(this._activeEffect);
  725. }
  726. this._uniformBuffer.update();
  727. this._afterBind(mesh);
  728. }
  729. /**
  730. * Dispose the material.
  731. * @forceDisposeEffect Force disposal of the associated effect.
  732. * @forceDisposeTextures Force disposal of the associated textures.
  733. */
  734. public dispose(forceDisposeEffect: boolean = false, forceDisposeTextures: boolean = false): void {
  735. if (forceDisposeTextures) {
  736. if (this.diffuseTexture) {
  737. this.diffuseTexture.dispose();
  738. }
  739. if (this.reflectionTexture) {
  740. this.reflectionTexture.dispose();
  741. }
  742. }
  743. this._renderTargets.dispose();
  744. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  745. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  746. }
  747. super.dispose(forceDisposeEffect);
  748. }
  749. /**
  750. * Clones the material.
  751. * @name The cloned name.
  752. * @returns The cloned material.
  753. */
  754. public clone(name: string): BackgroundMaterial {
  755. return SerializationHelper.Clone(() => new BackgroundMaterial(name, this.getScene()), this);
  756. }
  757. /**
  758. * Serializes the current material to its JSON representation.
  759. * @returns The JSON representation.
  760. */
  761. public serialize(): any {
  762. var serializationObject = SerializationHelper.Serialize(this);
  763. serializationObject.customType = "BABYLON.BackgroundMaterial";
  764. return serializationObject;
  765. }
  766. /**
  767. * Gets the class name of the material
  768. * @returns "BackgroundMaterial"
  769. */
  770. public getClassName(): string {
  771. return "BackgroundMaterial";
  772. }
  773. /**
  774. * Parse a JSON input to create back a background material.
  775. * @param source
  776. * @param scene
  777. * @param rootUrl
  778. * @returns the instantiated BackgroundMaterial.
  779. */
  780. public static Parse(source: any, scene: Scene, rootUrl: string): BackgroundMaterial {
  781. return SerializationHelper.Parse(() => new BackgroundMaterial(source.name, scene), source, scene, rootUrl);
  782. }
  783. }
  784. }