babylon.material.js 9.8 KB

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