babylon.material.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var MaterialDefines = (function () {
  10. function MaterialDefines() {
  11. }
  12. MaterialDefines.prototype.rebuild = function () {
  13. this._keys = Object.keys(this);
  14. };
  15. MaterialDefines.prototype.isEqual = function (other) {
  16. for (var index = 0; index < this._keys.length; index++) {
  17. var prop = this._keys[index];
  18. if (this[prop] !== other[prop]) {
  19. return false;
  20. }
  21. }
  22. return true;
  23. };
  24. MaterialDefines.prototype.cloneTo = function (other) {
  25. for (var index = 0; index < this._keys.length; index++) {
  26. var prop = this._keys[index];
  27. other[prop] = this[prop];
  28. }
  29. };
  30. MaterialDefines.prototype.reset = function () {
  31. for (var index = 0; index < this._keys.length; index++) {
  32. var prop = this._keys[index];
  33. if (typeof (this[prop]) === "number") {
  34. this[prop] = 0;
  35. }
  36. else {
  37. this[prop] = false;
  38. }
  39. }
  40. };
  41. MaterialDefines.prototype.toString = function () {
  42. var result = "";
  43. for (var index = 0; index < this._keys.length; index++) {
  44. var prop = this._keys[index];
  45. if (typeof (this[prop]) === "number") {
  46. result += "#define " + prop + " " + this[prop] + "\n";
  47. }
  48. else if (this[prop]) {
  49. result += "#define " + prop + "\n";
  50. }
  51. }
  52. return result;
  53. };
  54. return MaterialDefines;
  55. })();
  56. BABYLON.MaterialDefines = MaterialDefines;
  57. var Material = (function () {
  58. function Material(name, scene, doNotAdd) {
  59. this.name = name;
  60. this.checkReadyOnEveryCall = false;
  61. this.checkReadyOnlyOnce = false;
  62. this.state = "";
  63. this.alpha = 1.0;
  64. this.backFaceCulling = true;
  65. this.sideOrientation = Material.CounterClockWiseSideOrientation;
  66. this.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
  67. this.disableDepthWrite = false;
  68. this.fogEnabled = true;
  69. this.pointSize = 1.0;
  70. this.zOffset = 0;
  71. this._wasPreviouslyReady = false;
  72. this._fillMode = Material.TriangleFillMode;
  73. this.id = name;
  74. this._scene = scene;
  75. if (!doNotAdd) {
  76. scene.materials.push(this);
  77. }
  78. }
  79. Object.defineProperty(Material, "TriangleFillMode", {
  80. get: function () {
  81. return Material._TriangleFillMode;
  82. },
  83. enumerable: true,
  84. configurable: true
  85. });
  86. Object.defineProperty(Material, "WireFrameFillMode", {
  87. get: function () {
  88. return Material._WireFrameFillMode;
  89. },
  90. enumerable: true,
  91. configurable: true
  92. });
  93. Object.defineProperty(Material, "PointFillMode", {
  94. get: function () {
  95. return Material._PointFillMode;
  96. },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Object.defineProperty(Material, "ClockWiseSideOrientation", {
  101. get: function () {
  102. return Material._ClockWiseSideOrientation;
  103. },
  104. enumerable: true,
  105. configurable: true
  106. });
  107. Object.defineProperty(Material, "CounterClockWiseSideOrientation", {
  108. get: function () {
  109. return Material._CounterClockWiseSideOrientation;
  110. },
  111. enumerable: true,
  112. configurable: true
  113. });
  114. Object.defineProperty(Material.prototype, "wireframe", {
  115. get: function () {
  116. return this._fillMode === Material.WireFrameFillMode;
  117. },
  118. set: function (value) {
  119. this._fillMode = (value ? Material.WireFrameFillMode : Material.TriangleFillMode);
  120. },
  121. enumerable: true,
  122. configurable: true
  123. });
  124. Object.defineProperty(Material.prototype, "pointsCloud", {
  125. get: function () {
  126. return this._fillMode === Material.PointFillMode;
  127. },
  128. set: function (value) {
  129. this._fillMode = (value ? Material.PointFillMode : Material.TriangleFillMode);
  130. },
  131. enumerable: true,
  132. configurable: true
  133. });
  134. Object.defineProperty(Material.prototype, "fillMode", {
  135. get: function () {
  136. return this._fillMode;
  137. },
  138. set: function (value) {
  139. this._fillMode = value;
  140. },
  141. enumerable: true,
  142. configurable: true
  143. });
  144. /**
  145. * @param {boolean} fullDetails - support for multiple levels of logging within scene loading
  146. * subclasses should override adding information pertainent to themselves
  147. */
  148. Material.prototype.toString = function (fullDetails) {
  149. var ret = "Name: " + this.name;
  150. if (fullDetails) {
  151. }
  152. return ret;
  153. };
  154. Object.defineProperty(Material.prototype, "isFrozen", {
  155. get: function () {
  156. return this.checkReadyOnlyOnce;
  157. },
  158. enumerable: true,
  159. configurable: true
  160. });
  161. Material.prototype.freeze = function () {
  162. this.checkReadyOnlyOnce = true;
  163. };
  164. Material.prototype.unfreeze = function () {
  165. this.checkReadyOnlyOnce = false;
  166. };
  167. Material.prototype.isReady = function (mesh, useInstances) {
  168. return true;
  169. };
  170. Material.prototype.getEffect = function () {
  171. return this._effect;
  172. };
  173. Material.prototype.getScene = function () {
  174. return this._scene;
  175. };
  176. Material.prototype.needAlphaBlending = function () {
  177. return (this.alpha < 1.0);
  178. };
  179. Material.prototype.needAlphaTesting = function () {
  180. return false;
  181. };
  182. Material.prototype.getAlphaTestTexture = function () {
  183. return null;
  184. };
  185. Material.prototype.trackCreation = function (onCompiled, onError) {
  186. };
  187. Material.prototype.markDirty = function () {
  188. this._wasPreviouslyReady = false;
  189. };
  190. Material.prototype._preBind = function () {
  191. var engine = this._scene.getEngine();
  192. engine.enableEffect(this._effect);
  193. engine.setState(this.backFaceCulling, this.zOffset, false, this.sideOrientation === Material.ClockWiseSideOrientation);
  194. };
  195. Material.prototype.bind = function (world, mesh) {
  196. this._scene._cachedMaterial = this;
  197. if (this.onBind) {
  198. this.onBind(this, mesh);
  199. }
  200. if (this.disableDepthWrite) {
  201. var engine = this._scene.getEngine();
  202. this._cachedDepthWriteState = engine.getDepthWrite();
  203. engine.setDepthWrite(false);
  204. }
  205. };
  206. Material.prototype.bindOnlyWorldMatrix = function (world) {
  207. };
  208. Material.prototype.unbind = function () {
  209. if (this.disableDepthWrite) {
  210. var engine = this._scene.getEngine();
  211. engine.setDepthWrite(this._cachedDepthWriteState);
  212. }
  213. };
  214. Material.prototype.clone = function (name) {
  215. return null;
  216. };
  217. Material.prototype.getBindedMeshes = function () {
  218. var result = new Array();
  219. for (var index = 0; index < this._scene.meshes.length; index++) {
  220. var mesh = this._scene.meshes[index];
  221. if (mesh.material === this) {
  222. result.push(mesh);
  223. }
  224. }
  225. return result;
  226. };
  227. Material.prototype.dispose = function (forceDisposeEffect, forceDisposeTextures) {
  228. // Animations
  229. this.getScene().stopAnimation(this);
  230. // Remove from scene
  231. var index = this._scene.materials.indexOf(this);
  232. if (index >= 0) {
  233. this._scene.materials.splice(index, 1);
  234. }
  235. // Shader are kept in cache for further use but we can get rid of this by using forceDisposeEffect
  236. if (forceDisposeEffect && this._effect) {
  237. this._scene.getEngine()._releaseEffect(this._effect);
  238. this._effect = null;
  239. }
  240. // Remove from meshes
  241. for (index = 0; index < this._scene.meshes.length; index++) {
  242. var mesh = this._scene.meshes[index];
  243. if (mesh.material === this) {
  244. mesh.material = null;
  245. }
  246. }
  247. // Callback
  248. if (this.onDispose) {
  249. this.onDispose();
  250. }
  251. };
  252. Material.prototype.serialize = function () {
  253. return BABYLON.SerializationHelper.Serialize(this);
  254. };
  255. Material.ParseMultiMaterial = function (parsedMultiMaterial, scene) {
  256. var multiMaterial = new BABYLON.MultiMaterial(parsedMultiMaterial.name, scene);
  257. multiMaterial.id = parsedMultiMaterial.id;
  258. BABYLON.Tags.AddTagsTo(multiMaterial, parsedMultiMaterial.tags);
  259. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  260. var subMatId = parsedMultiMaterial.materials[matIndex];
  261. if (subMatId) {
  262. multiMaterial.subMaterials.push(scene.getMaterialByID(subMatId));
  263. }
  264. else {
  265. multiMaterial.subMaterials.push(null);
  266. }
  267. }
  268. return multiMaterial;
  269. };
  270. Material.Parse = function (parsedMaterial, scene, rootUrl) {
  271. if (!parsedMaterial.customType) {
  272. return BABYLON.StandardMaterial.Parse(parsedMaterial, scene, rootUrl);
  273. }
  274. var materialType = BABYLON.Tools.Instantiate(parsedMaterial.customType);
  275. return materialType.Parse(parsedMaterial, scene, rootUrl);
  276. ;
  277. };
  278. Material._TriangleFillMode = 0;
  279. Material._WireFrameFillMode = 1;
  280. Material._PointFillMode = 2;
  281. Material._ClockWiseSideOrientation = 0;
  282. Material._CounterClockWiseSideOrientation = 1;
  283. __decorate([
  284. BABYLON.serialize()
  285. ], Material.prototype, "id", void 0);
  286. __decorate([
  287. BABYLON.serialize()
  288. ], Material.prototype, "checkReadyOnEveryCall", void 0);
  289. __decorate([
  290. BABYLON.serialize()
  291. ], Material.prototype, "checkReadyOnlyOnce", void 0);
  292. __decorate([
  293. BABYLON.serialize()
  294. ], Material.prototype, "state", void 0);
  295. __decorate([
  296. BABYLON.serialize()
  297. ], Material.prototype, "alpha", void 0);
  298. __decorate([
  299. BABYLON.serialize()
  300. ], Material.prototype, "backFaceCulling", void 0);
  301. __decorate([
  302. BABYLON.serialize()
  303. ], Material.prototype, "sideOrientation", void 0);
  304. __decorate([
  305. BABYLON.serialize()
  306. ], Material.prototype, "alphaMode", void 0);
  307. __decorate([
  308. BABYLON.serialize()
  309. ], Material.prototype, "disableDepthWrite", void 0);
  310. __decorate([
  311. BABYLON.serialize()
  312. ], Material.prototype, "fogEnabled", void 0);
  313. __decorate([
  314. BABYLON.serialize()
  315. ], Material.prototype, "pointSize", void 0);
  316. __decorate([
  317. BABYLON.serialize()
  318. ], Material.prototype, "zOffset", void 0);
  319. __decorate([
  320. BABYLON.serialize()
  321. ], Material.prototype, "wireframe", null);
  322. __decorate([
  323. BABYLON.serialize()
  324. ], Material.prototype, "pointsCloud", null);
  325. __decorate([
  326. BABYLON.serialize()
  327. ], Material.prototype, "fillMode", null);
  328. return Material;
  329. })();
  330. BABYLON.Material = Material;
  331. })(BABYLON || (BABYLON = {}));