babylon.triPlanarMaterial.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  13. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  14. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  15. 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;
  16. return c > 3 && r && Object.defineProperty(target, key, r), r;
  17. };
  18. var BABYLON;
  19. (function (BABYLON) {
  20. var maxSimultaneousLights = 4;
  21. var TriPlanarMaterialDefines = (function (_super) {
  22. __extends(TriPlanarMaterialDefines, _super);
  23. function TriPlanarMaterialDefines() {
  24. var _this = _super.call(this) || this;
  25. _this.DIFFUSEX = false;
  26. _this.DIFFUSEY = false;
  27. _this.DIFFUSEZ = false;
  28. _this.BUMPX = false;
  29. _this.BUMPY = false;
  30. _this.BUMPZ = false;
  31. _this.CLIPPLANE = false;
  32. _this.ALPHATEST = false;
  33. _this.POINTSIZE = false;
  34. _this.FOG = false;
  35. _this.SPECULARTERM = false;
  36. _this.NORMAL = false;
  37. _this.VERTEXCOLOR = false;
  38. _this.VERTEXALPHA = false;
  39. _this.NUM_BONE_INFLUENCERS = 0;
  40. _this.BonesPerMesh = 0;
  41. _this.INSTANCES = false;
  42. _this.rebuild();
  43. return _this;
  44. }
  45. return TriPlanarMaterialDefines;
  46. }(BABYLON.MaterialDefines));
  47. var TriPlanarMaterial = (function (_super) {
  48. __extends(TriPlanarMaterial, _super);
  49. function TriPlanarMaterial(name, scene) {
  50. var _this = _super.call(this, name, scene) || this;
  51. _this.tileSize = 1;
  52. _this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  53. _this.specularColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  54. _this.specularPower = 64;
  55. _this.disableLighting = false;
  56. _this.maxSimultaneousLights = 4;
  57. _this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  58. _this._defines = new TriPlanarMaterialDefines();
  59. _this._cachedDefines = new TriPlanarMaterialDefines();
  60. _this._cachedDefines.BonesPerMesh = -1;
  61. return _this;
  62. }
  63. TriPlanarMaterial.prototype.needAlphaBlending = function () {
  64. return (this.alpha < 1.0);
  65. };
  66. TriPlanarMaterial.prototype.needAlphaTesting = function () {
  67. return false;
  68. };
  69. TriPlanarMaterial.prototype.getAlphaTestTexture = function () {
  70. return null;
  71. };
  72. // Methods
  73. TriPlanarMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  74. if (!mesh) {
  75. return true;
  76. }
  77. if (this._defines.INSTANCES !== useInstances) {
  78. return false;
  79. }
  80. return false;
  81. };
  82. TriPlanarMaterial.prototype.isReady = function (mesh, useInstances) {
  83. if (this.checkReadyOnlyOnce) {
  84. if (this._wasPreviouslyReady) {
  85. return true;
  86. }
  87. }
  88. var scene = this.getScene();
  89. if (!this.checkReadyOnEveryCall) {
  90. if (this._renderId === scene.getRenderId()) {
  91. if (this._checkCache(scene, mesh, useInstances)) {
  92. return true;
  93. }
  94. }
  95. }
  96. var engine = scene.getEngine();
  97. var needNormals = false;
  98. this._defines.reset();
  99. // Textures
  100. if (scene.texturesEnabled) {
  101. if (BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  102. var textures = [this.diffuseTextureX, this.diffuseTextureY, this.diffuseTextureZ];
  103. var textureDefines = ["DIFFUSEX", "DIFFUSEY", "DIFFUSEZ"];
  104. for (var i = 0; i < textures.length; i++) {
  105. if (textures[i]) {
  106. if (!textures[i].isReady()) {
  107. return false;
  108. }
  109. else {
  110. this._defines[textureDefines[i]] = true;
  111. }
  112. }
  113. }
  114. }
  115. if (BABYLON.StandardMaterial.BumpTextureEnabled) {
  116. var textures = [this.normalTextureX, this.normalTextureY, this.normalTextureZ];
  117. var textureDefines = ["BUMPX", "BUMPY", "BUMPZ"];
  118. for (var i = 0; i < textures.length; i++) {
  119. if (textures[i]) {
  120. if (!textures[i].isReady()) {
  121. return false;
  122. }
  123. else {
  124. this._defines[textureDefines[i]] = true;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. // Effect
  131. if (scene.clipPlane) {
  132. this._defines.CLIPPLANE = true;
  133. }
  134. if (engine.getAlphaTesting()) {
  135. this._defines.ALPHATEST = true;
  136. }
  137. // Point size
  138. if (this.pointsCloud || scene.forcePointsCloud) {
  139. this._defines.POINTSIZE = true;
  140. }
  141. // Fog
  142. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  143. this._defines.FOG = true;
  144. }
  145. // Lights
  146. if (scene.lightsEnabled && !this.disableLighting) {
  147. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  148. }
  149. // Attribs
  150. if (mesh) {
  151. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  152. this._defines.NORMAL = true;
  153. }
  154. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  155. this._defines.VERTEXCOLOR = true;
  156. if (mesh.hasVertexAlpha) {
  157. this._defines.VERTEXALPHA = true;
  158. }
  159. }
  160. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  161. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  162. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  163. }
  164. // Instances
  165. if (useInstances) {
  166. this._defines.INSTANCES = true;
  167. }
  168. }
  169. // Get correct effect
  170. if (!this._defines.isEqual(this._cachedDefines)) {
  171. this._defines.cloneTo(this._cachedDefines);
  172. scene.resetCachedMaterial();
  173. // Fallbacks
  174. var fallbacks = new BABYLON.EffectFallbacks();
  175. if (this._defines.FOG) {
  176. fallbacks.addFallback(1, "FOG");
  177. }
  178. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  179. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  180. fallbacks.addCPUSkinningFallback(0, mesh);
  181. }
  182. //Attributes
  183. var attribs = [BABYLON.VertexBuffer.PositionKind];
  184. if (this._defines.NORMAL) {
  185. attribs.push(BABYLON.VertexBuffer.NormalKind);
  186. }
  187. if (this._defines.VERTEXCOLOR) {
  188. attribs.push(BABYLON.VertexBuffer.ColorKind);
  189. }
  190. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  191. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  192. // Legacy browser patch
  193. var shaderName = "triplanar";
  194. var join = this._defines.toString();
  195. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  196. "vFogInfos", "vFogColor", "pointSize",
  197. "mBones",
  198. "vClipPlane",
  199. "tileSize"
  200. ];
  201. var samplers = ["diffuseSamplerX", "diffuseSamplerY", "diffuseSamplerZ",
  202. "normalSamplerX", "normalSamplerY", "normalSamplerZ"
  203. ];
  204. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  205. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, samplers, join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights });
  206. }
  207. if (!this._effect.isReady()) {
  208. return false;
  209. }
  210. this._renderId = scene.getRenderId();
  211. this._wasPreviouslyReady = true;
  212. if (mesh) {
  213. if (!mesh._materialDefines) {
  214. mesh._materialDefines = new TriPlanarMaterialDefines();
  215. }
  216. this._defines.cloneTo(mesh._materialDefines);
  217. }
  218. return true;
  219. };
  220. TriPlanarMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  221. this._effect.setMatrix("world", world);
  222. };
  223. TriPlanarMaterial.prototype.bind = function (world, mesh) {
  224. var scene = this.getScene();
  225. // Matrices
  226. this.bindOnlyWorldMatrix(world);
  227. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  228. // Bones
  229. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  230. this._effect.setFloat("tileSize", this.tileSize);
  231. if (scene.getCachedMaterial() !== this) {
  232. // Textures
  233. if (this.diffuseTextureX) {
  234. this._effect.setTexture("diffuseSamplerX", this.diffuseTextureX);
  235. }
  236. if (this.diffuseTextureY) {
  237. this._effect.setTexture("diffuseSamplerY", this.diffuseTextureY);
  238. }
  239. if (this.diffuseTextureZ) {
  240. this._effect.setTexture("diffuseSamplerZ", this.diffuseTextureZ);
  241. }
  242. if (this.normalTextureX) {
  243. this._effect.setTexture("normalSamplerX", this.normalTextureX);
  244. }
  245. if (this.normalTextureY) {
  246. this._effect.setTexture("normalSamplerY", this.normalTextureY);
  247. }
  248. if (this.normalTextureZ) {
  249. this._effect.setTexture("normalSamplerZ", this.normalTextureZ);
  250. }
  251. // Clip plane
  252. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  253. // Point size
  254. if (this.pointsCloud) {
  255. this._effect.setFloat("pointSize", this.pointSize);
  256. }
  257. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  258. }
  259. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  260. if (this._defines.SPECULARTERM) {
  261. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  262. }
  263. if (scene.lightsEnabled && !this.disableLighting) {
  264. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  265. }
  266. // View
  267. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  268. this._effect.setMatrix("view", scene.getViewMatrix());
  269. }
  270. // Fog
  271. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  272. _super.prototype.bind.call(this, world, mesh);
  273. };
  274. TriPlanarMaterial.prototype.getAnimatables = function () {
  275. var results = [];
  276. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  277. results.push(this.mixTexture);
  278. }
  279. return results;
  280. };
  281. TriPlanarMaterial.prototype.dispose = function (forceDisposeEffect) {
  282. if (this.mixTexture) {
  283. this.mixTexture.dispose();
  284. }
  285. _super.prototype.dispose.call(this, forceDisposeEffect);
  286. };
  287. TriPlanarMaterial.prototype.clone = function (name) {
  288. var _this = this;
  289. return BABYLON.SerializationHelper.Clone(function () { return new TriPlanarMaterial(name, _this.getScene()); }, this);
  290. };
  291. TriPlanarMaterial.prototype.serialize = function () {
  292. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  293. serializationObject.customType = "BABYLON.TriPlanarMaterial";
  294. return serializationObject;
  295. };
  296. // Statics
  297. TriPlanarMaterial.Parse = function (source, scene, rootUrl) {
  298. return BABYLON.SerializationHelper.Parse(function () { return new TriPlanarMaterial(source.name, scene); }, source, scene, rootUrl);
  299. };
  300. return TriPlanarMaterial;
  301. }(BABYLON.Material));
  302. __decorate([
  303. BABYLON.serializeAsTexture()
  304. ], TriPlanarMaterial.prototype, "mixTexture", void 0);
  305. __decorate([
  306. BABYLON.serializeAsTexture()
  307. ], TriPlanarMaterial.prototype, "diffuseTextureX", void 0);
  308. __decorate([
  309. BABYLON.serializeAsTexture()
  310. ], TriPlanarMaterial.prototype, "diffuseTextureY", void 0);
  311. __decorate([
  312. BABYLON.serializeAsTexture()
  313. ], TriPlanarMaterial.prototype, "diffuseTextureZ", void 0);
  314. __decorate([
  315. BABYLON.serializeAsTexture()
  316. ], TriPlanarMaterial.prototype, "normalTextureX", void 0);
  317. __decorate([
  318. BABYLON.serializeAsTexture()
  319. ], TriPlanarMaterial.prototype, "normalTextureY", void 0);
  320. __decorate([
  321. BABYLON.serializeAsTexture()
  322. ], TriPlanarMaterial.prototype, "normalTextureZ", void 0);
  323. __decorate([
  324. BABYLON.serialize()
  325. ], TriPlanarMaterial.prototype, "tileSize", void 0);
  326. __decorate([
  327. BABYLON.serializeAsColor3()
  328. ], TriPlanarMaterial.prototype, "diffuseColor", void 0);
  329. __decorate([
  330. BABYLON.serializeAsColor3()
  331. ], TriPlanarMaterial.prototype, "specularColor", void 0);
  332. __decorate([
  333. BABYLON.serialize()
  334. ], TriPlanarMaterial.prototype, "specularPower", void 0);
  335. __decorate([
  336. BABYLON.serialize()
  337. ], TriPlanarMaterial.prototype, "disableLighting", void 0);
  338. __decorate([
  339. BABYLON.serialize()
  340. ], TriPlanarMaterial.prototype, "maxSimultaneousLights", void 0);
  341. BABYLON.TriPlanarMaterial = TriPlanarMaterial;
  342. })(BABYLON || (BABYLON = {}));
  343. //# sourceMappingURL=babylon.triPlanarMaterial.js.map
  344. BABYLON.Effect.ShadersStore['triplanarVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\n#ifdef VERTEXCOLOR\nattribute vec4 color;\n#endif\n#include<bonesDeclaration>\n\n#include<instancesDeclaration>\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef DIFFUSEX\nvarying vec2 vTextureUVX;\n#endif\n#ifdef DIFFUSEY\nvarying vec2 vTextureUVY;\n#endif\n#ifdef DIFFUSEZ\nvarying vec2 vTextureUVZ;\n#endif\nuniform float tileSize;\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying mat3 tangentSpace;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<shadowsVertexDeclaration>[0..maxSimultaneousLights]\nvoid main(void)\n{\n#include<instancesVertex>\n#include<bonesVertex>\ngl_Position=viewProjection*finalWorld*vec4(position,1.0);\nvec4 worldPos=finalWorld*vec4(position,1.0);\nvPositionW=vec3(worldPos);\n#ifdef DIFFUSEX\nvTextureUVX=worldPos.zy/tileSize;\n#endif\n#ifdef DIFFUSEY\nvTextureUVY=worldPos.xz/tileSize;\n#endif\n#ifdef DIFFUSEZ\nvTextureUVZ=worldPos.xy/tileSize;\n#endif\n#ifdef NORMAL\n\nvec3 xtan=vec3(0,0,1);\nvec3 xbin=vec3(0,1,0);\nvec3 ytan=vec3(1,0,0);\nvec3 ybin=vec3(0,0,1);\nvec3 ztan=vec3(1,0,0);\nvec3 zbin=vec3(0,1,0);\nvec3 normalizedNormal=normalize(normal);\nnormalizedNormal*=normalizedNormal;\nvec3 worldBinormal=normalize(xbin*normalizedNormal.x+ybin*normalizedNormal.y+zbin*normalizedNormal.z);\nvec3 worldTangent=normalize(xtan*normalizedNormal.x+ytan*normalizedNormal.y+ztan*normalizedNormal.z);\nworldTangent=(world*vec4(worldTangent,1.0)).xyz;\nworldBinormal=(world*vec4(worldBinormal,1.0)).xyz;\nvec3 worldNormal=normalize(cross(worldTangent,worldBinormal));\ntangentSpace[0]=worldTangent;\ntangentSpace[1]=worldBinormal;\ntangentSpace[2]=worldNormal;\n#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n\n#include<shadowsVertex>[0..maxSimultaneousLights]\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  345. BABYLON.Effect.ShadersStore['triplanarPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n#ifdef SPECULARTERM\nuniform vec4 vSpecularColor;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#include<lightFragmentDeclaration>[0..maxSimultaneousLights]\n\n#ifdef DIFFUSEX\nvarying vec2 vTextureUVX;\nuniform sampler2D diffuseSamplerX;\n#ifdef BUMPX\nuniform sampler2D normalSamplerX;\n#endif\n#endif\n#ifdef DIFFUSEY\nvarying vec2 vTextureUVY;\nuniform sampler2D diffuseSamplerY;\n#ifdef BUMPY\nuniform sampler2D normalSamplerY;\n#endif\n#endif\n#ifdef DIFFUSEZ\nvarying vec2 vTextureUVZ;\nuniform sampler2D diffuseSamplerZ;\n#ifdef BUMPZ\nuniform sampler2D normalSamplerZ;\n#endif\n#endif\n#ifdef NORMAL\nvarying mat3 tangentSpace;\n#endif\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\n#include<clipPlaneFragmentDeclaration>\n#include<fogFragmentDeclaration>\nvoid main(void) {\n\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=vec4(0.,0.,0.,1.);\nvec3 diffuseColor=vDiffuseColor.rgb;\n\nfloat alpha=vDiffuseColor.a;\n\n#ifdef NORMAL\nvec3 normalW=tangentSpace[2];\n#else\nvec3 normalW=vec3(1.0,1.0,1.0);\n#endif\nvec4 baseNormal=vec4(0.0,0.0,0.0,1.0);\nnormalW*=normalW;\n#ifdef DIFFUSEX\nbaseColor+=texture2D(diffuseSamplerX,vTextureUVX)*normalW.x;\n#ifdef BUMPX\nbaseNormal+=texture2D(normalSamplerX,vTextureUVX)*normalW.x;\n#endif\n#endif\n#ifdef DIFFUSEY\nbaseColor+=texture2D(diffuseSamplerY,vTextureUVY)*normalW.y;\n#ifdef BUMPY\nbaseNormal+=texture2D(normalSamplerY,vTextureUVY)*normalW.y;\n#endif\n#endif\n#ifdef DIFFUSEZ\nbaseColor+=texture2D(diffuseSamplerZ,vTextureUVZ)*normalW.z;\n#ifdef BUMPZ\nbaseNormal+=texture2D(normalSamplerZ,vTextureUVZ)*normalW.z;\n#endif\n#endif\n#ifdef NORMAL\nnormalW=normalize((2.0*baseNormal.xyz-1.0)*tangentSpace);\n#endif\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\n#endif\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\nvec3 diffuseBase=vec3(0.,0.,0.);\nlightingInfo info;\nfloat shadow=1.;\n#ifdef SPECULARTERM\nfloat glossiness=vSpecularColor.a;\nvec3 specularBase=vec3(0.,0.,0.);\nvec3 specularColor=vSpecularColor.rgb;\n#else\nfloat glossiness=0.;\n#endif\n#include<lightFragment>[0..maxSimultaneousLights]\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\n#ifdef SPECULARTERM\nvec3 finalSpecular=specularBase*specularColor;\n#else\nvec3 finalSpecular=vec3(0.0);\n#endif\nvec3 finalDiffuse=clamp(diffuseBase*diffuseColor,0.0,1.0)*baseColor.rgb;\n\nvec4 color=vec4(finalDiffuse+finalSpecular,alpha);\n#include<fogFragment>\ngl_FragColor=color;\n}\n";