pushMaterial.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Nullable } from "../types";
  2. import { Scene } from "../scene";
  3. import { Matrix } from "../Maths/math.vector";
  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. import { SubMesh } from '../Meshes/subMesh';
  9. /**
  10. * Base class of materials working in push mode in babylon JS
  11. * @hidden
  12. */
  13. export class PushMaterial extends Material {
  14. protected _activeEffect: Effect;
  15. protected _normalMatrix: Matrix = new Matrix();
  16. constructor(name: string, scene: Scene) {
  17. super(name, scene);
  18. this._storeEffectOnSubMeshes = true;
  19. }
  20. public getEffect(): Effect {
  21. return this._activeEffect;
  22. }
  23. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  24. if (!mesh) {
  25. return false;
  26. }
  27. if (!mesh.subMeshes || mesh.subMeshes.length === 0) {
  28. return true;
  29. }
  30. return this.isReadyForSubMesh(mesh, mesh.subMeshes[0], useInstances);
  31. }
  32. protected _isReadyForSubMesh(subMesh: SubMesh) {
  33. const defines = subMesh._materialDefines;
  34. if (!this.checkReadyOnEveryCall && subMesh.effect && defines) {
  35. if (defines._renderId === this.getScene().getRenderId()) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. /**
  42. * Binds the given world matrix to the active effect
  43. *
  44. * @param world the matrix to bind
  45. */
  46. public bindOnlyWorldMatrix(world: Matrix): void {
  47. this._activeEffect.setMatrix("world", world);
  48. }
  49. /**
  50. * Binds the given normal matrix to the active effect
  51. *
  52. * @param normalMatrix the matrix to bind
  53. */
  54. public bindOnlyNormalMatrix(normalMatrix: Matrix): void {
  55. this._activeEffect.setMatrix("normalMatrix", normalMatrix);
  56. }
  57. public bind(world: Matrix, mesh?: Mesh): void {
  58. if (!mesh) {
  59. return;
  60. }
  61. this.bindForSubMesh(world, mesh, mesh.subMeshes[0]);
  62. }
  63. protected _afterBind(mesh: Mesh, effect: Nullable<Effect> = null): void {
  64. super._afterBind(mesh);
  65. this.getScene()._cachedEffect = effect;
  66. }
  67. protected _mustRebind(scene: Scene, effect: Effect, visibility: number = 1) {
  68. return scene.isCachedMaterialInvalid(this, effect, visibility);
  69. }
  70. }