babylon.pushMaterial.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. module BABYLON {
  2. export class PushMaterial extends Material {
  3. protected _activeEffect: Effect;
  4. constructor(name: string, scene: Scene) {
  5. super(name, scene);
  6. this.storeEffectOnSubMeshes = true;
  7. }
  8. public getEffect(): Effect {
  9. return this._activeEffect;
  10. }
  11. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  12. if (!mesh) {
  13. return false;
  14. }
  15. if (!mesh.subMeshes || mesh.subMeshes.length === 0) {
  16. return true;
  17. }
  18. return this.isReadyForSubMesh(mesh, mesh.subMeshes[0], useInstances);
  19. }
  20. public bindOnlyWorldMatrix(world: Matrix): void {
  21. this._activeEffect.setMatrix("world", world);
  22. }
  23. public bind(world: Matrix, mesh?: Mesh): void {
  24. if (!mesh) {
  25. return;
  26. }
  27. this.bindForSubMesh(world, mesh, mesh.subMeshes[0]);
  28. }
  29. protected _afterBind(mesh: Mesh, effect?: Effect): void {
  30. super._afterBind(mesh);
  31. this.getScene()._cachedEffect = effect;
  32. }
  33. protected _mustRebind(scene: Scene, effect: Effect) {
  34. return scene.getCachedEffect() !== effect || scene.getCachedMaterial() !== this;
  35. }
  36. public markAsDirty(flag: number): void {
  37. if (flag & Material.TextureDirtyFlag) {
  38. this._markAllSubMeshesAsTexturesDirty();
  39. }
  40. if (flag & Material.LightDirtyFlag) {
  41. this._markAllSubMeshesAsLightsDirty();
  42. }
  43. if (flag & Material.FresnelDirtyFlag) {
  44. this._markAllSubMeshesAsFresnelDirty();
  45. }
  46. if (flag & Material.AttributesDirtyFlag) {
  47. this._markAllSubMeshesAsAttributesDirty();
  48. }
  49. if (flag & Material.MiscDirtyFlag) {
  50. this._markAllSubMeshesAsMiscDirty();
  51. }
  52. }
  53. protected _markAllSubMeshesAsDirty(func: (defines: MaterialDefines) => void) {
  54. for (var mesh of this.getScene().meshes) {
  55. if (!mesh.subMeshes) {
  56. continue;
  57. }
  58. for (var subMesh of mesh.subMeshes) {
  59. if (subMesh.getMaterial() !== this) {
  60. continue;
  61. }
  62. if (!subMesh._materialDefines) {
  63. return;
  64. }
  65. func(subMesh._materialDefines);
  66. }
  67. }
  68. }
  69. protected _markAllSubMeshesAsTexturesDirty() {
  70. this._markAllSubMeshesAsDirty(defines => defines.markAsTexturesDirty());
  71. }
  72. protected _markAllSubMeshesAsFresnelDirty() {
  73. this._markAllSubMeshesAsDirty(defines => defines.markAsFresnelDirty());
  74. }
  75. protected _markAllSubMeshesAsLightsDirty() {
  76. this._markAllSubMeshesAsDirty(defines => defines.markAsLightDirty());
  77. }
  78. protected _markAllSubMeshesAsAttributesDirty() {
  79. this._markAllSubMeshesAsDirty(defines => defines.markAsAttributesDirty());
  80. }
  81. protected _markAllSubMeshesAsMiscDirty() {
  82. this._markAllSubMeshesAsDirty(defines => defines.markAsMiscDirty());
  83. }
  84. }
  85. }