babylon.normalMaterial.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 NormalMaterialDefines = (function (_super) {
  13. __extends(NormalMaterialDefines, _super);
  14. function NormalMaterialDefines() {
  15. _super.call(this);
  16. this.DIFFUSE = false;
  17. this.CLIPPLANE = false;
  18. this.ALPHATEST = false;
  19. this.POINTSIZE = false;
  20. this.FOG = false;
  21. this.LIGHT0 = false;
  22. this.LIGHT1 = false;
  23. this.LIGHT2 = false;
  24. this.LIGHT3 = false;
  25. this.SPOTLIGHT0 = false;
  26. this.SPOTLIGHT1 = false;
  27. this.SPOTLIGHT2 = false;
  28. this.SPOTLIGHT3 = false;
  29. this.HEMILIGHT0 = false;
  30. this.HEMILIGHT1 = false;
  31. this.HEMILIGHT2 = false;
  32. this.HEMILIGHT3 = false;
  33. this.DIRLIGHT0 = false;
  34. this.DIRLIGHT1 = false;
  35. this.DIRLIGHT2 = false;
  36. this.DIRLIGHT3 = false;
  37. this.POINTLIGHT0 = false;
  38. this.POINTLIGHT1 = false;
  39. this.POINTLIGHT2 = false;
  40. this.POINTLIGHT3 = false;
  41. this.SHADOW0 = false;
  42. this.SHADOW1 = false;
  43. this.SHADOW2 = false;
  44. this.SHADOW3 = false;
  45. this.SHADOWS = false;
  46. this.SHADOWVSM0 = false;
  47. this.SHADOWVSM1 = false;
  48. this.SHADOWVSM2 = false;
  49. this.SHADOWVSM3 = false;
  50. this.SHADOWPCF0 = false;
  51. this.SHADOWPCF1 = false;
  52. this.SHADOWPCF2 = false;
  53. this.SHADOWPCF3 = false;
  54. this.NORMAL = false;
  55. this.UV1 = false;
  56. this.UV2 = false;
  57. this.VERTEXCOLOR = false;
  58. this.VERTEXALPHA = false;
  59. this.NUM_BONE_INFLUENCERS = 0;
  60. this.BonesPerMesh = 0;
  61. this.INSTANCES = false;
  62. this._keys = Object.keys(this);
  63. }
  64. return NormalMaterialDefines;
  65. })(BABYLON.MaterialDefines);
  66. var NormalMaterial = (function (_super) {
  67. __extends(NormalMaterial, _super);
  68. function NormalMaterial(name, scene) {
  69. _super.call(this, name, scene);
  70. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  71. this.disableLighting = false;
  72. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  73. this._scaledDiffuse = new BABYLON.Color3();
  74. this._defines = new NormalMaterialDefines();
  75. this._cachedDefines = new NormalMaterialDefines();
  76. this._cachedDefines.BonesPerMesh = -1;
  77. }
  78. NormalMaterial.prototype.needAlphaBlending = function () {
  79. return (this.alpha < 1.0);
  80. };
  81. NormalMaterial.prototype.needAlphaTesting = function () {
  82. return false;
  83. };
  84. NormalMaterial.prototype.getAlphaTestTexture = function () {
  85. return null;
  86. };
  87. // Methods
  88. NormalMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  89. if (!mesh) {
  90. return true;
  91. }
  92. if (this._defines.INSTANCES !== useInstances) {
  93. return false;
  94. }
  95. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  96. return true;
  97. }
  98. return false;
  99. };
  100. NormalMaterial.prototype.isReady = function (mesh, useInstances) {
  101. if (this.checkReadyOnlyOnce) {
  102. if (this._wasPreviouslyReady) {
  103. return true;
  104. }
  105. }
  106. var scene = this.getScene();
  107. if (!this.checkReadyOnEveryCall) {
  108. if (this._renderId === scene.getRenderId()) {
  109. if (this._checkCache(scene, mesh, useInstances)) {
  110. return true;
  111. }
  112. }
  113. }
  114. var engine = scene.getEngine();
  115. var needNormals = false;
  116. var needUVs = false;
  117. this._defines.reset();
  118. // Textures
  119. if (scene.texturesEnabled) {
  120. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  121. if (!this.diffuseTexture.isReady()) {
  122. return false;
  123. }
  124. else {
  125. needUVs = true;
  126. this._defines.DIFFUSE = true;
  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. var lightIndex = 0;
  146. if (scene.lightsEnabled && !this.disableLighting) {
  147. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  148. }
  149. // Attribs
  150. if (mesh) {
  151. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  152. this._defines.NORMAL = true;
  153. }
  154. if (needUVs) {
  155. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  156. this._defines.UV1 = true;
  157. }
  158. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  159. this._defines.UV2 = true;
  160. }
  161. }
  162. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  163. this._defines.VERTEXCOLOR = true;
  164. if (mesh.hasVertexAlpha) {
  165. this._defines.VERTEXALPHA = true;
  166. }
  167. }
  168. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  169. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  170. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  171. }
  172. // Instances
  173. if (useInstances) {
  174. this._defines.INSTANCES = true;
  175. }
  176. }
  177. // Get correct effect
  178. if (!this._defines.isEqual(this._cachedDefines)) {
  179. this._defines.cloneTo(this._cachedDefines);
  180. scene.resetCachedMaterial();
  181. // Fallbacks
  182. var fallbacks = new BABYLON.EffectFallbacks();
  183. if (this._defines.FOG) {
  184. fallbacks.addFallback(1, "FOG");
  185. }
  186. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  187. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  188. fallbacks.addCPUSkinningFallback(0, mesh);
  189. }
  190. //Attributes
  191. var attribs = [BABYLON.VertexBuffer.PositionKind];
  192. if (this._defines.NORMAL) {
  193. attribs.push(BABYLON.VertexBuffer.NormalKind);
  194. }
  195. if (this._defines.UV1) {
  196. attribs.push(BABYLON.VertexBuffer.UVKind);
  197. }
  198. if (this._defines.UV2) {
  199. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  200. }
  201. if (this._defines.VERTEXCOLOR) {
  202. attribs.push(BABYLON.VertexBuffer.ColorKind);
  203. }
  204. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  205. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  206. var shaderName = "normal";
  207. var join = this._defines.toString();
  208. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  209. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  210. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  211. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  212. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  213. "vFogInfos", "vFogColor", "pointSize",
  214. "vDiffuseInfos",
  215. "mBones",
  216. "vClipPlane", "diffuseMatrix",
  217. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues"
  218. ], ["diffuseSampler",
  219. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  220. ], join, fallbacks, this.onCompiled, this.onError);
  221. }
  222. if (!this._effect.isReady()) {
  223. return false;
  224. }
  225. this._renderId = scene.getRenderId();
  226. this._wasPreviouslyReady = true;
  227. if (mesh) {
  228. if (!mesh._materialDefines) {
  229. mesh._materialDefines = new NormalMaterialDefines();
  230. }
  231. this._defines.cloneTo(mesh._materialDefines);
  232. }
  233. return true;
  234. };
  235. NormalMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  236. this._effect.setMatrix("world", world);
  237. };
  238. NormalMaterial.prototype.bind = function (world, mesh) {
  239. var scene = this.getScene();
  240. // Matrices
  241. this.bindOnlyWorldMatrix(world);
  242. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  243. // Bones
  244. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  245. if (scene.getCachedMaterial() !== this) {
  246. // Textures
  247. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  248. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  249. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  250. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  251. }
  252. // Clip plane
  253. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  254. // Point size
  255. if (this.pointsCloud) {
  256. this._effect.setFloat("pointSize", this.pointSize);
  257. }
  258. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  259. }
  260. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  261. // Lights
  262. if (scene.lightsEnabled && !this.disableLighting) {
  263. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  264. }
  265. // View
  266. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  267. this._effect.setMatrix("view", scene.getViewMatrix());
  268. }
  269. // Fog
  270. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  271. _super.prototype.bind.call(this, world, mesh);
  272. };
  273. NormalMaterial.prototype.getAnimatables = function () {
  274. var results = [];
  275. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  276. results.push(this.diffuseTexture);
  277. }
  278. return results;
  279. };
  280. NormalMaterial.prototype.dispose = function (forceDisposeEffect) {
  281. if (this.diffuseTexture) {
  282. this.diffuseTexture.dispose();
  283. }
  284. _super.prototype.dispose.call(this, forceDisposeEffect);
  285. };
  286. NormalMaterial.prototype.clone = function (name) {
  287. var _this = this;
  288. return BABYLON.SerializationHelper.Clone(function () { return new NormalMaterial(name, _this.getScene()); }, this);
  289. };
  290. NormalMaterial.prototype.serialize = function () {
  291. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  292. serializationObject.customType = "BABYLON.NormalMaterial";
  293. return serializationObject;
  294. };
  295. // Statics
  296. NormalMaterial.Parse = function (source, scene, rootUrl) {
  297. return BABYLON.SerializationHelper.Parse(function () { return new NormalMaterial(source.name, scene); }, source, scene, rootUrl);
  298. };
  299. __decorate([
  300. BABYLON.serializeAsTexture()
  301. ], NormalMaterial.prototype, "diffuseTexture");
  302. __decorate([
  303. BABYLON.serializeAsColor3()
  304. ], NormalMaterial.prototype, "diffuseColor");
  305. __decorate([
  306. BABYLON.serialize()
  307. ], NormalMaterial.prototype, "disableLighting");
  308. return NormalMaterial;
  309. })(BABYLON.Material);
  310. BABYLON.NormalMaterial = NormalMaterial;
  311. })(BABYLON || (BABYLON = {}));
  312. BABYLON.Effect.ShadersStore['normalVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\n#ifdef UV1\nattribute vec2 uv;\n#endif\n#ifdef UV2\nattribute vec2 uv2;\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 DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform mat4 diffuseMatrix;\nuniform vec2 vDiffuseInfos;\n#endif\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<shadowsVertexDeclaration>\nvoid main(void) {\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 NORMAL\nvNormalW=normalize(vec3(finalWorld*vec4(normal,0.0)));\n#endif\n\n#ifndef UV1\nvec2 uv=vec2(0.,0.);\n#endif\n#ifndef UV2\nvec2 uv2=vec2(0.,0.);\n#endif\n#ifdef DIFFUSE\nif (vDiffuseInfos.x == 0.)\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));\n}\nelse\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));\n}\n#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n#include<shadowsVertex>\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  313. BABYLON.Effect.ShadersStore['normalPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#include<lightFragmentDeclaration>[0]\n#include<lightFragmentDeclaration>[1]\n#include<lightFragmentDeclaration>[2]\n#include<lightFragmentDeclaration>[3]\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n#include<clipPlaneFragmentDeclaration>\n\n#include<fogFragmentDeclaration>\nvoid main(void) {\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=vec4(1.,1.,1.,1.);\nvec3 diffuseColor=vDiffuseColor.rgb;\n\nfloat alpha=vDiffuseColor.a;\n#ifdef DIFFUSE\nbaseColor=texture2D(diffuseSampler,vDiffuseUV);\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\n#endif\nbaseColor.rgb*=vDiffuseInfos.y;\n#endif\n#ifdef NORMAL\nbaseColor=mix(baseColor,vec4(vNormalW,1.0),0.5);\n#endif\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\n#ifdef NORMAL\nvec3 normalW=normalize(vNormalW);\n#else\nvec3 normalW=vec3(1.0,1.0,1.0);\n#endif\n\nvec3 diffuseBase=vec3(0.,0.,0.);\nlightingInfo info;\nfloat shadow=1.;\nfloat glossiness=0.;\n#include<lightFragment>[0]\n#include<lightFragment>[1]\n#include<lightFragment>[2]\n#include<lightFragment>[3]\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\nvec3 finalDiffuse=clamp(diffuseBase*diffuseColor,0.0,1.0)*baseColor.rgb;\n\nvec4 color=vec4(finalDiffuse,alpha);\n#include<fogFragment>\ngl_FragColor=color;\n}";