babylon.normalMaterial.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  13. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  14. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  15. 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;
  16. return c > 3 && r && Object.defineProperty(target, key, r), r;
  17. };
  18. var BABYLON;
  19. (function (BABYLON) {
  20. var NormalMaterialDefines = (function (_super) {
  21. __extends(NormalMaterialDefines, _super);
  22. function NormalMaterialDefines() {
  23. var _this = _super.call(this) || this;
  24. _this.DIFFUSE = false;
  25. _this.CLIPPLANE = false;
  26. _this.ALPHATEST = false;
  27. _this.POINTSIZE = false;
  28. _this.FOG = false;
  29. _this.LIGHT0 = false;
  30. _this.LIGHT1 = false;
  31. _this.LIGHT2 = false;
  32. _this.LIGHT3 = false;
  33. _this.SPOTLIGHT0 = false;
  34. _this.SPOTLIGHT1 = false;
  35. _this.SPOTLIGHT2 = false;
  36. _this.SPOTLIGHT3 = false;
  37. _this.HEMILIGHT0 = false;
  38. _this.HEMILIGHT1 = false;
  39. _this.HEMILIGHT2 = false;
  40. _this.HEMILIGHT3 = false;
  41. _this.DIRLIGHT0 = false;
  42. _this.DIRLIGHT1 = false;
  43. _this.DIRLIGHT2 = false;
  44. _this.DIRLIGHT3 = false;
  45. _this.POINTLIGHT0 = false;
  46. _this.POINTLIGHT1 = false;
  47. _this.POINTLIGHT2 = false;
  48. _this.POINTLIGHT3 = false;
  49. _this.SHADOW0 = false;
  50. _this.SHADOW1 = false;
  51. _this.SHADOW2 = false;
  52. _this.SHADOW3 = false;
  53. _this.SHADOWS = false;
  54. _this.SHADOWESM0 = false;
  55. _this.SHADOWESM1 = false;
  56. _this.SHADOWESM2 = false;
  57. _this.SHADOWESM3 = false;
  58. _this.SHADOWPCF0 = false;
  59. _this.SHADOWPCF1 = false;
  60. _this.SHADOWPCF2 = false;
  61. _this.SHADOWPCF3 = false;
  62. _this.NORMAL = false;
  63. _this.UV1 = false;
  64. _this.UV2 = false;
  65. _this.VERTEXCOLOR = false;
  66. _this.VERTEXALPHA = false;
  67. _this.NUM_BONE_INFLUENCERS = 0;
  68. _this.BonesPerMesh = 0;
  69. _this.INSTANCES = false;
  70. _this.rebuild();
  71. return _this;
  72. }
  73. return NormalMaterialDefines;
  74. }(BABYLON.MaterialDefines));
  75. var NormalMaterial = (function (_super) {
  76. __extends(NormalMaterial, _super);
  77. function NormalMaterial(name, scene) {
  78. var _this = _super.call(this, name, scene) || this;
  79. _this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  80. _this._disableLighting = false;
  81. _this._maxSimultaneousLights = 4;
  82. _this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  83. _this._scaledDiffuse = new BABYLON.Color3();
  84. return _this;
  85. }
  86. NormalMaterial.prototype.needAlphaBlending = function () {
  87. return (this.alpha < 1.0);
  88. };
  89. NormalMaterial.prototype.needAlphaTesting = function () {
  90. return false;
  91. };
  92. NormalMaterial.prototype.getAlphaTestTexture = function () {
  93. return null;
  94. };
  95. // Methods
  96. NormalMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  97. if (this.isFrozen) {
  98. if (this._wasPreviouslyReady && subMesh.effect) {
  99. return true;
  100. }
  101. }
  102. if (!subMesh._materialDefines) {
  103. subMesh._materialDefines = new NormalMaterialDefines();
  104. }
  105. var defines = subMesh._materialDefines;
  106. var scene = this.getScene();
  107. if (!this.checkReadyOnEveryCall) {
  108. if (this._renderId === scene.getRenderId()) {
  109. return true;
  110. }
  111. }
  112. var engine = scene.getEngine();
  113. // Textures
  114. if (defines._areTexturesDirty) {
  115. defines._needUVs = false;
  116. if (scene.texturesEnabled) {
  117. if (this._diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  118. if (!this._diffuseTexture.isReady()) {
  119. return false;
  120. }
  121. else {
  122. defines._needUVs = true;
  123. defines.DIFFUSE = true;
  124. }
  125. }
  126. }
  127. }
  128. // Misc.
  129. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  130. // Lights
  131. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  132. // Values that need to be evaluated on every frame
  133. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  134. // Attribs
  135. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  136. // Get correct effect
  137. if (defines.isDirty) {
  138. defines.markAsProcessed();
  139. scene.resetCachedMaterial();
  140. // Fallbacks
  141. var fallbacks = new BABYLON.EffectFallbacks();
  142. if (defines.FOG) {
  143. fallbacks.addFallback(1, "FOG");
  144. }
  145. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  146. if (defines.NUM_BONE_INFLUENCERS > 0) {
  147. fallbacks.addCPUSkinningFallback(0, mesh);
  148. }
  149. //Attributes
  150. var attribs = [BABYLON.VertexBuffer.PositionKind];
  151. if (defines.NORMAL) {
  152. attribs.push(BABYLON.VertexBuffer.NormalKind);
  153. }
  154. if (defines.UV1) {
  155. attribs.push(BABYLON.VertexBuffer.UVKind);
  156. }
  157. if (defines.UV2) {
  158. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  159. }
  160. if (defines.VERTEXCOLOR) {
  161. attribs.push(BABYLON.VertexBuffer.ColorKind);
  162. }
  163. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  164. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  165. var shaderName = "normal";
  166. var join = defines.toString();
  167. subMesh.setEffect(scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  168. "vLightData0", "vLightDiffuse0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  169. "vLightData1", "vLightDiffuse1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  170. "vLightData2", "vLightDiffuse2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  171. "vLightData3", "vLightDiffuse3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  172. "vFogInfos", "vFogColor", "pointSize",
  173. "vDiffuseInfos",
  174. "mBones",
  175. "vClipPlane", "diffuseMatrix",
  176. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues"
  177. ], ["diffuseSampler",
  178. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  179. ], join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: 4 }), defines);
  180. }
  181. if (!subMesh.effect.isReady()) {
  182. return false;
  183. }
  184. this._renderId = scene.getRenderId();
  185. this._wasPreviouslyReady = true;
  186. return true;
  187. };
  188. NormalMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  189. var scene = this.getScene();
  190. var defines = subMesh._materialDefines;
  191. if (!defines) {
  192. return;
  193. }
  194. var effect = subMesh.effect;
  195. this._activeEffect = effect;
  196. // Matrices
  197. this.bindOnlyWorldMatrix(world);
  198. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  199. // Bones
  200. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  201. if (this._mustRebind(scene, effect)) {
  202. // Textures
  203. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  204. this._activeEffect.setTexture("diffuseSampler", this.diffuseTexture);
  205. this._activeEffect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  206. this._activeEffect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  207. }
  208. // Clip plane
  209. BABYLON.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  210. // Point size
  211. if (this.pointsCloud) {
  212. this._activeEffect.setFloat("pointSize", this.pointSize);
  213. }
  214. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  215. }
  216. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  217. // Lights
  218. if (scene.lightsEnabled && !this.disableLighting) {
  219. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines);
  220. }
  221. // View
  222. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  223. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  224. }
  225. // Fog
  226. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  227. this._afterBind(mesh, this._activeEffect);
  228. };
  229. NormalMaterial.prototype.getAnimatables = function () {
  230. var results = [];
  231. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  232. results.push(this.diffuseTexture);
  233. }
  234. return results;
  235. };
  236. NormalMaterial.prototype.dispose = function (forceDisposeEffect) {
  237. if (this.diffuseTexture) {
  238. this.diffuseTexture.dispose();
  239. }
  240. _super.prototype.dispose.call(this, forceDisposeEffect);
  241. };
  242. NormalMaterial.prototype.clone = function (name) {
  243. var _this = this;
  244. return BABYLON.SerializationHelper.Clone(function () { return new NormalMaterial(name, _this.getScene()); }, this);
  245. };
  246. NormalMaterial.prototype.serialize = function () {
  247. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  248. serializationObject.customType = "BABYLON.NormalMaterial";
  249. return serializationObject;
  250. };
  251. // Statics
  252. NormalMaterial.Parse = function (source, scene, rootUrl) {
  253. return BABYLON.SerializationHelper.Parse(function () { return new NormalMaterial(source.name, scene); }, source, scene, rootUrl);
  254. };
  255. return NormalMaterial;
  256. }(BABYLON.PushMaterial));
  257. __decorate([
  258. BABYLON.serializeAsTexture("diffuseTexture")
  259. ], NormalMaterial.prototype, "_diffuseTexture", void 0);
  260. __decorate([
  261. BABYLON.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  262. ], NormalMaterial.prototype, "diffuseTexture", void 0);
  263. __decorate([
  264. BABYLON.serializeAsColor3()
  265. ], NormalMaterial.prototype, "diffuseColor", void 0);
  266. __decorate([
  267. BABYLON.serialize("disableLighting")
  268. ], NormalMaterial.prototype, "_disableLighting", void 0);
  269. __decorate([
  270. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  271. ], NormalMaterial.prototype, "disableLighting", void 0);
  272. __decorate([
  273. BABYLON.serialize("maxSimultaneousLights")
  274. ], NormalMaterial.prototype, "_maxSimultaneousLights", void 0);
  275. __decorate([
  276. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  277. ], NormalMaterial.prototype, "maxSimultaneousLights", void 0);
  278. BABYLON.NormalMaterial = NormalMaterial;
  279. })(BABYLON || (BABYLON = {}));
  280. //# sourceMappingURL=babylon.normalMaterial.js.map
  281. 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";
  282. 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}";