babylon.shadowOnlyMaterial.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 ShadowOnlyMaterialDefines = (function (_super) {
  21. __extends(ShadowOnlyMaterialDefines, _super);
  22. function ShadowOnlyMaterialDefines() {
  23. var _this = _super.call(this) || this;
  24. _this.CLIPPLANE = false;
  25. _this.POINTSIZE = false;
  26. _this.FOG = false;
  27. _this.NORMAL = false;
  28. _this.NUM_BONE_INFLUENCERS = 0;
  29. _this.BonesPerMesh = 0;
  30. _this.INSTANCES = false;
  31. _this.rebuild();
  32. return _this;
  33. }
  34. return ShadowOnlyMaterialDefines;
  35. }(BABYLON.MaterialDefines));
  36. var ShadowOnlyMaterial = (function (_super) {
  37. __extends(ShadowOnlyMaterial, _super);
  38. function ShadowOnlyMaterial(name, scene) {
  39. var _this = _super.call(this, name, scene) || this;
  40. _this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  41. _this._scaledDiffuse = new BABYLON.Color3();
  42. _this._defines = new ShadowOnlyMaterialDefines();
  43. _this._cachedDefines = new ShadowOnlyMaterialDefines();
  44. _this._cachedDefines.BonesPerMesh = -1;
  45. return _this;
  46. }
  47. ShadowOnlyMaterial.prototype.needAlphaBlending = function () {
  48. return true;
  49. };
  50. ShadowOnlyMaterial.prototype.needAlphaTesting = function () {
  51. return false;
  52. };
  53. ShadowOnlyMaterial.prototype.getAlphaTestTexture = function () {
  54. return null;
  55. };
  56. // Methods
  57. ShadowOnlyMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  58. if (!mesh) {
  59. return true;
  60. }
  61. if (this._defines.INSTANCES !== useInstances) {
  62. return false;
  63. }
  64. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  65. return true;
  66. }
  67. return false;
  68. };
  69. ShadowOnlyMaterial.prototype.isReady = function (mesh, useInstances) {
  70. if (this.checkReadyOnlyOnce) {
  71. if (this._wasPreviouslyReady) {
  72. return true;
  73. }
  74. }
  75. var scene = this.getScene();
  76. if (!this.checkReadyOnEveryCall) {
  77. if (this._renderId === scene.getRenderId()) {
  78. if (this._checkCache(scene, mesh, useInstances)) {
  79. return true;
  80. }
  81. }
  82. }
  83. var engine = scene.getEngine();
  84. var needNormals = false;
  85. this._defines.reset();
  86. // Effect
  87. if (scene.clipPlane) {
  88. this._defines.CLIPPLANE = true;
  89. }
  90. // Point size
  91. if (this.pointsCloud || scene.forcePointsCloud) {
  92. this._defines.POINTSIZE = true;
  93. }
  94. // Fog
  95. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  96. this._defines.FOG = true;
  97. }
  98. if (scene.lightsEnabled) {
  99. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, 1);
  100. }
  101. // Attribs
  102. if (mesh) {
  103. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  104. this._defines.NORMAL = true;
  105. }
  106. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  107. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  108. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  109. }
  110. // Instances
  111. if (useInstances) {
  112. this._defines.INSTANCES = true;
  113. }
  114. }
  115. // Get correct effect
  116. if (!this._defines.isEqual(this._cachedDefines)) {
  117. this._defines.cloneTo(this._cachedDefines);
  118. scene.resetCachedMaterial();
  119. // Fallbacks
  120. var fallbacks = new BABYLON.EffectFallbacks();
  121. if (this._defines.FOG) {
  122. fallbacks.addFallback(1, "FOG");
  123. }
  124. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, 1);
  125. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  126. fallbacks.addCPUSkinningFallback(0, mesh);
  127. }
  128. //Attributes
  129. var attribs = [BABYLON.VertexBuffer.PositionKind];
  130. if (this._defines.NORMAL) {
  131. attribs.push(BABYLON.VertexBuffer.NormalKind);
  132. }
  133. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  134. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  135. var shaderName = "shadowOnly";
  136. var join = this._defines.toString();
  137. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  138. "vFogInfos", "vFogColor", "pointSize",
  139. "mBones",
  140. "vClipPlane", "depthValues"
  141. ];
  142. var samplers = [];
  143. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, 1);
  144. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, samplers, join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: 1 });
  145. }
  146. if (!this._effect.isReady()) {
  147. return false;
  148. }
  149. this._renderId = scene.getRenderId();
  150. this._wasPreviouslyReady = true;
  151. if (mesh) {
  152. if (!mesh._materialDefines) {
  153. mesh._materialDefines = new ShadowOnlyMaterialDefines();
  154. }
  155. this._defines.cloneTo(mesh._materialDefines);
  156. }
  157. return true;
  158. };
  159. ShadowOnlyMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  160. this._effect.setMatrix("world", world);
  161. };
  162. ShadowOnlyMaterial.prototype.bind = function (world, mesh) {
  163. var scene = this.getScene();
  164. // Matrices
  165. this.bindOnlyWorldMatrix(world);
  166. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  167. // Bones
  168. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  169. if (scene.getCachedMaterial() !== this) {
  170. // Clip plane
  171. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  172. // Point size
  173. if (this.pointsCloud) {
  174. this._effect.setFloat("pointSize", this.pointSize);
  175. }
  176. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  177. }
  178. // Lights
  179. if (scene.lightsEnabled) {
  180. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, 1);
  181. }
  182. // View
  183. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  184. this._effect.setMatrix("view", scene.getViewMatrix());
  185. }
  186. // Fog
  187. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  188. _super.prototype.bind.call(this, world, mesh);
  189. };
  190. ShadowOnlyMaterial.prototype.clone = function (name) {
  191. var _this = this;
  192. return BABYLON.SerializationHelper.Clone(function () { return new ShadowOnlyMaterial(name, _this.getScene()); }, this);
  193. };
  194. ShadowOnlyMaterial.prototype.serialize = function () {
  195. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  196. serializationObject.customType = "BABYLON.ShadowOnlyMaterial";
  197. return serializationObject;
  198. };
  199. // Statics
  200. ShadowOnlyMaterial.Parse = function (source, scene, rootUrl) {
  201. return BABYLON.SerializationHelper.Parse(function () { return new ShadowOnlyMaterial(source.name, scene); }, source, scene, rootUrl);
  202. };
  203. return ShadowOnlyMaterial;
  204. }(BABYLON.Material));
  205. __decorate([
  206. BABYLON.serialize()
  207. ], ShadowOnlyMaterial.prototype, "_worldViewProjectionMatrix", void 0);
  208. BABYLON.ShadowOnlyMaterial = ShadowOnlyMaterial;
  209. })(BABYLON || (BABYLON = {}));
  210. //# sourceMappingURL=babylon.shadowOnlyMaterial.js.map
  211. BABYLON.Effect.ShadersStore['shadowOnlyVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\n#include<bonesDeclaration>\n\n#include<instancesDeclaration>\nuniform mat4 view;\nuniform mat4 viewProjection;\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#include<clipPlaneVertex>\n\n#include<fogVertex>\n#include<shadowsVertex>[0..maxSimultaneousLights]\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  212. BABYLON.Effect.ShadersStore['shadowOnlyPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#include<lightFragmentDeclaration>[0..maxSimultaneousLights]\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\n#include<clipPlaneFragmentDeclaration>\n\n#include<fogFragmentDeclaration>\nvoid main(void) {\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\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..1]\n\nvec4 color=vec4(0.,0.,0.,1.0-clamp(shadow,0.,1.));\n#include<fogFragment>\ngl_FragColor=color;\n}";