babylon.material.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. module BABYLON {
  2. export class Material {
  3. private static _TriangleFillMode = 0;
  4. private static _WireFrameFillMode = 1;
  5. private static _PointFillMode = 2;
  6. public static get TriangleFillMode(): number {
  7. return Material._TriangleFillMode;
  8. }
  9. public static get WireFrameFillMode(): number {
  10. return Material._WireFrameFillMode;
  11. }
  12. public static get PointFillMode(): number {
  13. return Material._PointFillMode;
  14. }
  15. public id: string;
  16. public checkReadyOnEveryCall = true;
  17. public checkReadyOnlyOnce = false;
  18. public state = "";
  19. public alpha = 1.0;
  20. public backFaceCulling = true;
  21. public onCompiled: (effect: Effect) => void;
  22. public onError: (effect: Effect, errors: string) => void;
  23. public onDispose: () => void;
  24. public onBind: (material: Material) => void;
  25. public getRenderTargetTextures: () => SmartArray<RenderTargetTexture>;
  26. public _effect: Effect;
  27. public _wasPreviouslyReady = false;
  28. private _scene: Scene;
  29. private _fillMode = Material.TriangleFillMode;
  30. public pointSize = 1.0;
  31. public get wireframe(): boolean {
  32. return this._fillMode === Material.WireFrameFillMode;
  33. }
  34. public set wireframe(value:boolean) {
  35. this._fillMode = (value ? Material.WireFrameFillMode : Material.TriangleFillMode);
  36. }
  37. public get pointsCloud(): boolean {
  38. return this._fillMode === Material.PointFillMode;
  39. }
  40. public set pointsCloud(value: boolean) {
  41. this._fillMode = (value ? Material.PointFillMode : Material.TriangleFillMode);
  42. }
  43. public get fillMode(): number {
  44. return this._fillMode;
  45. }
  46. public set fillMode(value: number) {
  47. this._fillMode = value;
  48. }
  49. constructor(public name: string, scene: Scene, doNotAdd?: boolean) {
  50. this.id = name;
  51. this._scene = scene;
  52. if (!doNotAdd) {
  53. scene.materials.push(this);
  54. }
  55. }
  56. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  57. return true;
  58. }
  59. public getEffect(): Effect {
  60. return this._effect;
  61. }
  62. public getScene(): Scene {
  63. return this._scene;
  64. }
  65. public needAlphaBlending(): boolean {
  66. return (this.alpha < 1.0);
  67. }
  68. public needAlphaTesting(): boolean {
  69. return false;
  70. }
  71. public getAlphaTestTexture(): BaseTexture {
  72. return null;
  73. }
  74. public trackCreation(onCompiled: (effect: Effect) => void, onError: (effect: Effect, errors: string) => void) {
  75. }
  76. public _preBind(): void {
  77. var engine = this._scene.getEngine();
  78. engine.enableEffect(this._effect);
  79. engine.setState(this.backFaceCulling);
  80. }
  81. public bind(world: Matrix, mesh: Mesh): void {
  82. this._scene._cachedMaterial = this;
  83. if (this.onBind) {
  84. this.onBind(this);
  85. }
  86. }
  87. public bindOnlyWorldMatrix(world: Matrix): void {
  88. }
  89. public unbind(): void {
  90. }
  91. public dispose(forceDisposeEffect?: boolean): void {
  92. // Remove from scene
  93. var index = this._scene.materials.indexOf(this);
  94. this._scene.materials.splice(index, 1);
  95. // Shader are kept in cache for further use but we can get rid of this by using forceDisposeEffect
  96. if (forceDisposeEffect && this._effect) {
  97. this._scene.getEngine()._releaseEffect(this._effect);
  98. this._effect = null;
  99. }
  100. // Callback
  101. if (this.onDispose) {
  102. this.onDispose();
  103. }
  104. }
  105. }
  106. }