babylon.shadowOnlyMaterial.js 11 KB

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