babylon.material.js 13 KB

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