babylon.triPlanarMaterial.js 20 KB

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