babylon.material.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. module BABYLON {
  2. export class Material {
  3. public id: string;
  4. public checkReadyOnEveryCall = true;
  5. public checkReadyOnlyOnce = false;
  6. public alpha = 1.0;
  7. public wireframe = false;
  8. public backFaceCulling = true;
  9. public onCompiled: (effect: Effect) => void;
  10. public onError: (effect: Effect, errors: string) => void;
  11. public onDispose: () => void;
  12. public getRenderTargetTextures: () => SmartArray;
  13. public _effect: Effect;
  14. public _wasPreviouslyReady = false;
  15. private _scene: Scene;
  16. constructor(public name: string, scene: Scene, doNotAdd?: boolean) {
  17. this.id = name;
  18. this._scene = scene;
  19. if (!doNotAdd) {
  20. scene.materials.push(this);
  21. }
  22. }
  23. public isReady(mesh?: Mesh): boolean {
  24. return true;
  25. }
  26. public getEffect(): Effect {
  27. return this._effect;
  28. }
  29. public getScene(): Scene {
  30. return this._scene;
  31. }
  32. public needAlphaBlending(): boolean {
  33. return (this.alpha < 1.0);
  34. }
  35. public needAlphaTesting(): boolean {
  36. return false;
  37. }
  38. public trackCreation(onCompiled: (effect: Effect) => void, onError: (effect: Effect, errors: string) => void) {
  39. }
  40. public _preBind(): void {
  41. var engine = this._scene.getEngine();
  42. engine.enableEffect(this._effect);
  43. engine.setState(this.backFaceCulling);
  44. }
  45. public bind(world: Matrix, mesh: Mesh): void {
  46. }
  47. public unbind(): void {
  48. }
  49. public dispose(forceDisposeEffect?: boolean): void {
  50. // Remove from scene
  51. var index = this._scene.materials.indexOf(this);
  52. this._scene.materials.splice(index, 1);
  53. // Shader are kept in cache for further use but we can get rid of this by using forceDisposeEffect
  54. if (forceDisposeEffect && this._effect) {
  55. this._scene.getEngine()._releaseEffect(this._effect);
  56. this._effect = null;
  57. }
  58. // Callback
  59. if (this.onDispose) {
  60. this.onDispose();
  61. }
  62. }
  63. }
  64. }