babylon.material.js 13 KB

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