babylon.shadowOnlyMaterial.js 11 KB

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