pushMaterial.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Nullable } from "../types";
  2. import { Scene } from "../scene";
  3. import { Matrix } from "../Maths/math";
  4. import { AbstractMesh } from "../Meshes/abstractMesh";
  5. import { Mesh } from "../Meshes/mesh";
  6. import { Material } from "../Materials/material";
  7. import { Effect } from "../Materials/effect";
  8. /**
  9. * Base class of materials working in push mode in babylon JS
  10. * @hidden
  11. */
  12. export class PushMaterial extends Material {
  13. protected _activeEffect: Effect;
  14. protected _normalMatrix: Matrix = new Matrix();
  15. /**
  16. * Gets or sets a boolean indicating that the material is allowed to do shader hot swapping.
  17. * This means that the material can keep using a previous shader while a new one is being compiled.
  18. * This is mostly used when shader parallel compilation is supported (true by default)
  19. */
  20. public allowShaderHotSwapping = true;
  21. constructor(name: string, scene: Scene) {
  22. super(name, scene);
  23. this._storeEffectOnSubMeshes = true;
  24. }
  25. public getEffect(): Effect {
  26. return this._activeEffect;
  27. }
  28. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  29. if (!mesh) {
  30. return false;
  31. }
  32. if (!mesh.subMeshes || mesh.subMeshes.length === 0) {
  33. return true;
  34. }
  35. return this.isReadyForSubMesh(mesh, mesh.subMeshes[0], useInstances);
  36. }
  37. /**
  38. * Binds the given world matrix to the active effect
  39. *
  40. * @param world the matrix to bind
  41. */
  42. public bindOnlyWorldMatrix(world: Matrix): void {
  43. this._activeEffect.setMatrix("world", world);
  44. }
  45. /**
  46. * Binds the given normal matrix to the active effect
  47. *
  48. * @param normalMatrix the matrix to bind
  49. */
  50. public bindOnlyNormalMatrix(normalMatrix: Matrix): void {
  51. this._activeEffect.setMatrix("normalMatrix", normalMatrix);
  52. }
  53. public bind(world: Matrix, mesh?: Mesh): void {
  54. if (!mesh) {
  55. return;
  56. }
  57. this.bindForSubMesh(world, mesh, mesh.subMeshes[0]);
  58. }
  59. protected _afterBind(mesh: Mesh, effect: Nullable<Effect> = null): void {
  60. super._afterBind(mesh);
  61. this.getScene()._cachedEffect = effect;
  62. }
  63. protected _mustRebind(scene: Scene, effect: Effect, visibility: number = 1) {
  64. return scene.isCachedMaterialInvalid(this, effect, visibility);
  65. }
  66. }