babylon.material.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. module BABYLON {
  2. export class MaterialDefines {
  3. _keys: string[];
  4. public isEqual(other: MaterialDefines): boolean {
  5. for (var index = 0; index < this._keys.length; index++) {
  6. var prop = this._keys[index];
  7. if (this[prop] !== other[prop]) {
  8. return false;
  9. }
  10. }
  11. return true;
  12. }
  13. public cloneTo(other: MaterialDefines): void {
  14. for (var index = 0; index < this._keys.length; index++) {
  15. var prop = this._keys[index];
  16. other[prop] = this[prop];
  17. }
  18. }
  19. public reset(): void {
  20. for (var index = 0; index < this._keys.length; index++) {
  21. var prop = this._keys[index];
  22. if (typeof (this[prop]) === "number") {
  23. this[prop] = 0;
  24. } else {
  25. this[prop] = false;
  26. }
  27. }
  28. }
  29. public toString(): string {
  30. var result = "";
  31. for (var index = 0; index < this._keys.length; index++) {
  32. var prop = this._keys[index];
  33. if (typeof (this[prop]) === "number") {
  34. result += "#define " + prop + " " + this[prop] + "\n";
  35. } else if (this[prop]) {
  36. result += "#define " + prop + "\n";
  37. }
  38. }
  39. return result;
  40. }
  41. }
  42. export class Material {
  43. private static _TriangleFillMode = 0;
  44. private static _WireFrameFillMode = 1;
  45. private static _PointFillMode = 2;
  46. public static get TriangleFillMode(): number {
  47. return Material._TriangleFillMode;
  48. }
  49. public static get WireFrameFillMode(): number {
  50. return Material._WireFrameFillMode;
  51. }
  52. public static get PointFillMode(): number {
  53. return Material._PointFillMode;
  54. }
  55. private static _ClockWiseSideOrientation = 0;
  56. private static _CounterClockWiseSideOrientation = 1;
  57. public static get ClockWiseSideOrientation(): number {
  58. return Material._ClockWiseSideOrientation;
  59. }
  60. public static get CounterClockWiseSideOrientation(): number {
  61. return Material._CounterClockWiseSideOrientation;
  62. }
  63. public id: string;
  64. public checkReadyOnEveryCall = false;
  65. public checkReadyOnlyOnce = false;
  66. public state = "";
  67. public alpha = 1.0;
  68. public backFaceCulling = true;
  69. public sideOrientation = Material.CounterClockWiseSideOrientation;
  70. public onCompiled: (effect: Effect) => void;
  71. public onError: (effect: Effect, errors: string) => void;
  72. public onDispose: () => void;
  73. public onBind: (material: Material, mesh: Mesh) => void;
  74. public getRenderTargetTextures: () => SmartArray<RenderTargetTexture>;
  75. public alphaMode = Engine.ALPHA_COMBINE;
  76. public disableDepthWrite = false;
  77. public fogEnabled = true;
  78. public _effect: Effect;
  79. public _wasPreviouslyReady = false;
  80. private _scene: Scene;
  81. private _fillMode = Material.TriangleFillMode;
  82. private _cachedDepthWriteState: boolean;
  83. public pointSize = 1.0;
  84. public zOffset = 0;
  85. public get wireframe(): boolean {
  86. return this._fillMode === Material.WireFrameFillMode;
  87. }
  88. public set wireframe(value: boolean) {
  89. this._fillMode = (value ? Material.WireFrameFillMode : Material.TriangleFillMode);
  90. }
  91. public get pointsCloud(): boolean {
  92. return this._fillMode === Material.PointFillMode;
  93. }
  94. public set pointsCloud(value: boolean) {
  95. this._fillMode = (value ? Material.PointFillMode : Material.TriangleFillMode);
  96. }
  97. public get fillMode(): number {
  98. return this._fillMode;
  99. }
  100. public set fillMode(value: number) {
  101. this._fillMode = value;
  102. }
  103. constructor(public name: string, scene: Scene, doNotAdd?: boolean) {
  104. this.id = name;
  105. this._scene = scene;
  106. if (!doNotAdd) {
  107. scene.materials.push(this);
  108. }
  109. }
  110. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  111. return true;
  112. }
  113. public getEffect(): Effect {
  114. return this._effect;
  115. }
  116. public getScene(): Scene {
  117. return this._scene;
  118. }
  119. public needAlphaBlending(): boolean {
  120. return (this.alpha < 1.0);
  121. }
  122. public needAlphaTesting(): boolean {
  123. return false;
  124. }
  125. public getAlphaTestTexture(): BaseTexture {
  126. return null;
  127. }
  128. public trackCreation(onCompiled: (effect: Effect) => void, onError: (effect: Effect, errors: string) => void) {
  129. }
  130. public markDirty(): void {
  131. this._wasPreviouslyReady = false;
  132. }
  133. public _preBind(): void {
  134. var engine = this._scene.getEngine();
  135. engine.enableEffect(this._effect);
  136. engine.setState(this.backFaceCulling, this.zOffset, false, this.sideOrientation === Material.ClockWiseSideOrientation);
  137. }
  138. public bind(world: Matrix, mesh?: Mesh): void {
  139. this._scene._cachedMaterial = this;
  140. if (this.onBind) {
  141. this.onBind(this, mesh);
  142. }
  143. if (this.disableDepthWrite) {
  144. var engine = this._scene.getEngine();
  145. this._cachedDepthWriteState = engine.getDepthWrite();
  146. engine.setDepthWrite(false);
  147. }
  148. }
  149. public bindOnlyWorldMatrix(world: Matrix): void {
  150. }
  151. public unbind(): void {
  152. if (this.disableDepthWrite) {
  153. var engine = this._scene.getEngine();
  154. engine.setDepthWrite(this._cachedDepthWriteState);
  155. }
  156. }
  157. public clone(name: string): Material {
  158. return null;
  159. }
  160. public getBindedMeshes(): AbstractMesh[] {
  161. var result = new Array<AbstractMesh>();
  162. for (var index = 0; index < this._scene.meshes.length; index++) {
  163. var mesh = this._scene.meshes[index];
  164. if (mesh.material === this) {
  165. result.push(mesh);
  166. }
  167. }
  168. return result;
  169. }
  170. public dispose(forceDisposeEffect?: boolean): void {
  171. // Animations
  172. this.getScene().stopAnimation(this);
  173. // Remove from scene
  174. var index = this._scene.materials.indexOf(this);
  175. if (index >= 0) {
  176. this._scene.materials.splice(index, 1);
  177. }
  178. // Shader are kept in cache for further use but we can get rid of this by using forceDisposeEffect
  179. if (forceDisposeEffect && this._effect) {
  180. this._scene.getEngine()._releaseEffect(this._effect);
  181. this._effect = null;
  182. }
  183. // Remove from meshes
  184. for (index = 0; index < this._scene.meshes.length; index++) {
  185. var mesh = this._scene.meshes[index];
  186. if (mesh.material === this) {
  187. mesh.material = null;
  188. }
  189. }
  190. // Callback
  191. if (this.onDispose) {
  192. this.onDispose();
  193. }
  194. }
  195. public copyTo(other: Material): void {
  196. other.checkReadyOnlyOnce = this.checkReadyOnlyOnce;
  197. other.checkReadyOnEveryCall = this.checkReadyOnEveryCall;
  198. other.alpha = this.alpha;
  199. other.fillMode = this.fillMode;
  200. other.backFaceCulling = this.backFaceCulling;
  201. other.fogEnabled = this.fogEnabled;
  202. other.wireframe = this.wireframe;
  203. other.zOffset = this.zOffset;
  204. other.alphaMode = this.alphaMode;
  205. other.sideOrientation = this.sideOrientation;
  206. other.disableDepthWrite = this.disableDepthWrite;
  207. other.pointSize = this.pointSize;
  208. other.pointsCloud = this.pointsCloud;
  209. }
  210. public serialize(): any {
  211. var serializationObject: any = {};
  212. serializationObject.name = this.name;
  213. serializationObject.alpha = this.alpha;
  214. serializationObject.id = this.id;
  215. serializationObject.tags = Tags.GetTags(this);
  216. serializationObject.backFaceCulling = this.backFaceCulling;
  217. serializationObject.checkReadyOnlyOnce = this.checkReadyOnlyOnce;
  218. serializationObject.disableDepthWrite = this.disableDepthWrite;
  219. return serializationObject;
  220. }
  221. public static ParseMultiMaterial(parsedMultiMaterial: any, scene: Scene): MultiMaterial {
  222. var multiMaterial = new BABYLON.MultiMaterial(parsedMultiMaterial.name, scene);
  223. multiMaterial.id = parsedMultiMaterial.id;
  224. Tags.AddTagsTo(multiMaterial, parsedMultiMaterial.tags);
  225. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  226. var subMatId = parsedMultiMaterial.materials[matIndex];
  227. if (subMatId) {
  228. multiMaterial.subMaterials.push(scene.getMaterialByID(subMatId));
  229. } else {
  230. multiMaterial.subMaterials.push(null);
  231. }
  232. }
  233. return multiMaterial;
  234. }
  235. public static Parse(parsedMaterial: any, scene: Scene, rootUrl: string) {
  236. // could have been loaded already from another .babylon. Assume user wishes to share
  237. if (scene.getMaterialByID(parsedMaterial.id)) return scene.getMaterialByID(parsedMaterial.id);
  238. if (!parsedMaterial.customType) {
  239. return StandardMaterial.Parse(parsedMaterial, scene, rootUrl);
  240. }
  241. var materialType = Tools.Instantiate(parsedMaterial.customType);
  242. return materialType.Parse(parsedMaterial, scene, rootUrl);;
  243. }
  244. }
  245. }