babylon.triPlanarMaterial.js 20 KB

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