babylon.normalMaterial.js 17 KB

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