babylon.normalMaterial.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var NormalMaterialDefines = (function (_super) {
  5. __extends(NormalMaterialDefines, _super);
  6. function NormalMaterialDefines() {
  7. _super.call(this);
  8. this.DIFFUSE = false;
  9. this.CLIPPLANE = false;
  10. this.ALPHATEST = false;
  11. this.POINTSIZE = false;
  12. this.FOG = false;
  13. this.LIGHT0 = false;
  14. this.LIGHT1 = false;
  15. this.LIGHT2 = false;
  16. this.LIGHT3 = false;
  17. this.SPOTLIGHT0 = false;
  18. this.SPOTLIGHT1 = false;
  19. this.SPOTLIGHT2 = false;
  20. this.SPOTLIGHT3 = false;
  21. this.HEMILIGHT0 = false;
  22. this.HEMILIGHT1 = false;
  23. this.HEMILIGHT2 = false;
  24. this.HEMILIGHT3 = false;
  25. this.DIRLIGHT0 = false;
  26. this.DIRLIGHT1 = false;
  27. this.DIRLIGHT2 = false;
  28. this.DIRLIGHT3 = false;
  29. this.POINTLIGHT0 = false;
  30. this.POINTLIGHT1 = false;
  31. this.POINTLIGHT2 = false;
  32. this.POINTLIGHT3 = false;
  33. this.SHADOW0 = false;
  34. this.SHADOW1 = false;
  35. this.SHADOW2 = false;
  36. this.SHADOW3 = false;
  37. this.SHADOWS = false;
  38. this.SHADOWVSM0 = false;
  39. this.SHADOWVSM1 = false;
  40. this.SHADOWVSM2 = false;
  41. this.SHADOWVSM3 = false;
  42. this.SHADOWPCF0 = false;
  43. this.SHADOWPCF1 = false;
  44. this.SHADOWPCF2 = false;
  45. this.SHADOWPCF3 = false;
  46. this.NORMAL = false;
  47. this.UV1 = false;
  48. this.UV2 = false;
  49. this.VERTEXCOLOR = false;
  50. this.VERTEXALPHA = false;
  51. this.NUM_BONE_INFLUENCERS = 0;
  52. this.BonesPerMesh = 0;
  53. this.INSTANCES = false;
  54. this._keys = Object.keys(this);
  55. }
  56. return NormalMaterialDefines;
  57. })(BABYLON.MaterialDefines);
  58. var NormalMaterial = (function (_super) {
  59. __extends(NormalMaterial, _super);
  60. function NormalMaterial(name, scene) {
  61. _super.call(this, name, scene);
  62. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  63. this.disableLighting = false;
  64. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  65. this._scaledDiffuse = new BABYLON.Color3();
  66. this._defines = new NormalMaterialDefines();
  67. this._cachedDefines = new NormalMaterialDefines();
  68. this._cachedDefines.BonesPerMesh = -1;
  69. }
  70. NormalMaterial.prototype.needAlphaBlending = function () {
  71. return (this.alpha < 1.0);
  72. };
  73. NormalMaterial.prototype.needAlphaTesting = function () {
  74. return false;
  75. };
  76. NormalMaterial.prototype.getAlphaTestTexture = function () {
  77. return null;
  78. };
  79. // Methods
  80. NormalMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  81. if (!mesh) {
  82. return true;
  83. }
  84. if (this._defines.INSTANCES !== useInstances) {
  85. return false;
  86. }
  87. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  88. return true;
  89. }
  90. return false;
  91. };
  92. NormalMaterial.prototype.isReady = function (mesh, useInstances) {
  93. if (this.checkReadyOnlyOnce) {
  94. if (this._wasPreviouslyReady) {
  95. return true;
  96. }
  97. }
  98. var scene = this.getScene();
  99. if (!this.checkReadyOnEveryCall) {
  100. if (this._renderId === scene.getRenderId()) {
  101. if (this._checkCache(scene, mesh, useInstances)) {
  102. return true;
  103. }
  104. }
  105. }
  106. var engine = scene.getEngine();
  107. var needNormals = false;
  108. var needUVs = false;
  109. this._defines.reset();
  110. // Textures
  111. if (scene.texturesEnabled) {
  112. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  113. if (!this.diffuseTexture.isReady()) {
  114. return false;
  115. }
  116. else {
  117. needUVs = true;
  118. this._defines.DIFFUSE = true;
  119. }
  120. }
  121. }
  122. // Effect
  123. if (scene.clipPlane) {
  124. this._defines.CLIPPLANE = true;
  125. }
  126. if (engine.getAlphaTesting()) {
  127. this._defines.ALPHATEST = true;
  128. }
  129. // Point size
  130. if (this.pointsCloud || scene.forcePointsCloud) {
  131. this._defines.POINTSIZE = true;
  132. }
  133. // Fog
  134. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  135. this._defines.FOG = true;
  136. }
  137. var lightIndex = 0;
  138. if (scene.lightsEnabled && !this.disableLighting) {
  139. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  140. }
  141. // Attribs
  142. if (mesh) {
  143. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  144. this._defines.NORMAL = true;
  145. }
  146. if (needUVs) {
  147. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  148. this._defines.UV1 = true;
  149. }
  150. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  151. this._defines.UV2 = true;
  152. }
  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);
  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.UV1) {
  188. attribs.push(BABYLON.VertexBuffer.UVKind);
  189. }
  190. if (this._defines.UV2) {
  191. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  192. }
  193. if (this._defines.VERTEXCOLOR) {
  194. attribs.push(BABYLON.VertexBuffer.ColorKind);
  195. }
  196. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  197. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  198. var shaderName = "normal";
  199. var join = this._defines.toString();
  200. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  201. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  202. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  203. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  204. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  205. "vFogInfos", "vFogColor", "pointSize",
  206. "vDiffuseInfos",
  207. "mBones",
  208. "vClipPlane", "diffuseMatrix",
  209. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues"
  210. ], ["diffuseSampler",
  211. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  212. ], join, fallbacks, this.onCompiled, this.onError);
  213. }
  214. if (!this._effect.isReady()) {
  215. return false;
  216. }
  217. this._renderId = scene.getRenderId();
  218. this._wasPreviouslyReady = true;
  219. if (mesh) {
  220. if (!mesh._materialDefines) {
  221. mesh._materialDefines = new NormalMaterialDefines();
  222. }
  223. this._defines.cloneTo(mesh._materialDefines);
  224. }
  225. return true;
  226. };
  227. NormalMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  228. this._effect.setMatrix("world", world);
  229. };
  230. NormalMaterial.prototype.bind = function (world, mesh) {
  231. var scene = this.getScene();
  232. // Matrices
  233. this.bindOnlyWorldMatrix(world);
  234. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  235. // Bones
  236. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  237. if (scene.getCachedMaterial() !== this) {
  238. // Textures
  239. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  240. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  241. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  242. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  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. // Lights
  254. if (scene.lightsEnabled && !this.disableLighting) {
  255. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  256. }
  257. // View
  258. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  259. this._effect.setMatrix("view", scene.getViewMatrix());
  260. }
  261. // Fog
  262. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  263. _super.prototype.bind.call(this, world, mesh);
  264. };
  265. NormalMaterial.prototype.getAnimatables = function () {
  266. var results = [];
  267. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  268. results.push(this.diffuseTexture);
  269. }
  270. return results;
  271. };
  272. NormalMaterial.prototype.dispose = function (forceDisposeEffect) {
  273. if (this.diffuseTexture) {
  274. this.diffuseTexture.dispose();
  275. }
  276. _super.prototype.dispose.call(this, forceDisposeEffect);
  277. };
  278. NormalMaterial.prototype.clone = function (name) {
  279. var newMaterial = new NormalMaterial(name, this.getScene());
  280. // Base material
  281. this.copyTo(newMaterial);
  282. // Normal material
  283. if (this.diffuseTexture && this.diffuseTexture.clone) {
  284. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  285. }
  286. newMaterial.diffuseColor = this.diffuseColor.clone();
  287. return newMaterial;
  288. };
  289. NormalMaterial.prototype.serialize = function () {
  290. var serializationObject = _super.prototype.serialize.call(this);
  291. serializationObject.customType = "BABYLON.NormalMaterial";
  292. serializationObject.diffuseColor = this.diffuseColor.asArray();
  293. serializationObject.disableLighting = this.disableLighting;
  294. if (this.diffuseTexture) {
  295. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  296. }
  297. return serializationObject;
  298. };
  299. NormalMaterial.Parse = function (source, scene, rootUrl) {
  300. var material = new NormalMaterial(source.name, scene);
  301. material.diffuseColor = BABYLON.Color3.FromArray(source.diffuseColor);
  302. material.disableLighting = source.disableLighting;
  303. material.alpha = source.alpha;
  304. material.id = source.id;
  305. BABYLON.Tags.AddTagsTo(material, source.tags);
  306. material.backFaceCulling = source.backFaceCulling;
  307. material.wireframe = source.wireframe;
  308. if (source.diffuseTexture) {
  309. material.diffuseTexture = BABYLON.Texture.Parse(source.diffuseTexture, scene, rootUrl);
  310. }
  311. if (source.checkReadyOnlyOnce) {
  312. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  313. }
  314. return material;
  315. };
  316. return NormalMaterial;
  317. })(BABYLON.Material);
  318. BABYLON.NormalMaterial = NormalMaterial;
  319. })(BABYLON || (BABYLON = {}));
  320. BABYLON.Effect.ShadersStore['normalVertexShader'] = "precision highp float;\r\n\r\n// Attributes\r\nattribute vec3 position;\r\n#ifdef NORMAL\r\nattribute vec3 normal;\r\n#endif\r\n#ifdef UV1\r\nattribute vec2 uv;\r\n#endif\r\n#ifdef UV2\r\nattribute vec2 uv2;\r\n#endif\r\n#ifdef VERTEXCOLOR\r\nattribute vec4 color;\r\n#endif\r\n\r\n#include<bonesDeclaration>\r\n\r\n// Uniforms\r\n#include<instancesDeclaration>\r\n\r\nuniform mat4 view;\r\nuniform mat4 viewProjection;\r\n\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform mat4 diffuseMatrix;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#ifdef POINTSIZE\r\nuniform float pointSize;\r\n#endif\r\n\r\n// Output\r\nvarying vec3 vPositionW;\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n\r\n#include<clipPlaneVertexDeclaration>\r\n\r\n#include<fogVertexDeclaration>\r\n#include<shadowsVertexDeclaration>\r\n\r\nvoid main(void) {\r\n\r\n#include<instancesVertex>\r\n#include<bonesVertex>\r\n\r\n\tgl_Position = viewProjection * finalWorld * vec4(position, 1.0);\r\n\r\n\tvec4 worldPos = finalWorld * vec4(position, 1.0);\r\n\tvPositionW = vec3(worldPos);\r\n\r\n#ifdef NORMAL\r\n\tvNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\r\n#endif\r\n\r\n\t// Texture coordinates\r\n#ifndef UV1\r\n\tvec2 uv = vec2(0., 0.);\r\n#endif\r\n#ifndef UV2\r\n\tvec2 uv2 = vec2(0., 0.);\r\n#endif\r\n\r\n#ifdef DIFFUSE\r\n\tif (vDiffuseInfos.x == 0.)\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\r\n\t}\r\n\telse\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\r\n\t}\r\n#endif\r\n\r\n\t// Clip plane\r\n#include<clipPlaneVertex>\r\n\r\n // Fog\r\n#include<fogVertex>\r\n#include<shadowsVertex>\r\n\r\n\t// Vertex color\r\n#ifdef VERTEXCOLOR\r\n\tvColor = color;\r\n#endif\r\n\r\n\t// Point size\r\n#ifdef POINTSIZE\r\n\tgl_PointSize = pointSize;\r\n#endif\r\n}\r\n";
  321. BABYLON.Effect.ShadersStore['normalPixelShader'] = "precision highp float;\r\n\r\n// Constants\r\nuniform vec3 vEyePosition;\r\nuniform vec4 vDiffuseColor;\r\n\r\n// Input\r\nvarying vec3 vPositionW;\r\n\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n// Lights\r\n#include<light0FragmentDeclaration>\r\n#include<light1FragmentDeclaration>\r\n#include<light2FragmentDeclaration>\r\n#include<light3FragmentDeclaration>\r\n\r\n\r\n#include<lightsFragmentFunctions>\r\n#include<shadowsFragmentFunctions>\r\n\r\n// Samplers\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform sampler2D diffuseSampler;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#include<clipPlaneFragmentDeclaration>\r\n\r\n// Fog\r\n#include<fogFragmentDeclaration>\r\n\r\nvoid main(void) {\r\n#include<clipPlaneFragment>\r\n\r\n\tvec3 viewDirectionW = normalize(vEyePosition - vPositionW);\r\n\r\n\t// Base color\r\n\tvec4 baseColor = vec4(1., 1., 1., 1.);\r\n\tvec3 diffuseColor = vDiffuseColor.rgb;\r\n\r\n\t// Alpha\r\n\tfloat alpha = vDiffuseColor.a;\r\n\r\n#ifdef DIFFUSE\r\n\tbaseColor = texture2D(diffuseSampler, vDiffuseUV);\r\n\r\n#ifdef ALPHATEST\r\n\tif (baseColor.a < 0.4)\r\n\t\tdiscard;\r\n#endif\r\n\r\n\tbaseColor.rgb *= vDiffuseInfos.y;\r\n#endif\r\n\r\n#ifdef NORMAL\r\n baseColor = mix(baseColor, vec4(vNormalW, 1.0), 0.5);\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\n\tbaseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n\t// Normal\r\n#ifdef NORMAL\r\n\tvec3 normalW = normalize(vNormalW);\r\n#else\r\n\tvec3 normalW = vec3(1.0, 1.0, 1.0);\r\n#endif\r\n\r\n\t// Lighting\r\n\tvec3 diffuseBase = vec3(0., 0., 0.);\r\n\tfloat shadow = 1.;\r\n float glossiness = 0.;\r\n \r\n#include<light0Fragment>\r\n#include<light1Fragment>\r\n#include<light2Fragment>\r\n#include<light3Fragment>\r\n\r\n\r\n#ifdef VERTEXALPHA\r\n\talpha *= vColor.a;\r\n#endif\r\n\r\n\tvec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;\r\n\r\n\t// Composition\r\n\tvec4 color = vec4(finalDiffuse, alpha);\r\n\r\n#include<fogFragment>\r\n\r\n\tgl_FragColor = color;\r\n}";