babylon.normalMaterial.js 18 KB

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