babylon.shadowOnlyMaterial.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 BABYLON;
  13. (function (BABYLON) {
  14. var ShadowOnlyMaterialDefines = (function (_super) {
  15. __extends(ShadowOnlyMaterialDefines, _super);
  16. function ShadowOnlyMaterialDefines() {
  17. var _this = _super.call(this) || this;
  18. _this.CLIPPLANE = false;
  19. _this.POINTSIZE = false;
  20. _this.FOG = false;
  21. _this.NORMAL = false;
  22. _this.NUM_BONE_INFLUENCERS = 0;
  23. _this.BonesPerMesh = 0;
  24. _this.INSTANCES = false;
  25. _this.rebuild();
  26. return _this;
  27. }
  28. return ShadowOnlyMaterialDefines;
  29. }(BABYLON.MaterialDefines));
  30. var ShadowOnlyMaterial = (function (_super) {
  31. __extends(ShadowOnlyMaterial, _super);
  32. function ShadowOnlyMaterial(name, scene) {
  33. return _super.call(this, name, scene) || this;
  34. }
  35. ShadowOnlyMaterial.prototype.needAlphaBlending = function () {
  36. return true;
  37. };
  38. ShadowOnlyMaterial.prototype.needAlphaTesting = function () {
  39. return false;
  40. };
  41. ShadowOnlyMaterial.prototype.getAlphaTestTexture = function () {
  42. return null;
  43. };
  44. Object.defineProperty(ShadowOnlyMaterial.prototype, "activeLight", {
  45. get: function () {
  46. return this._activeLight;
  47. },
  48. set: function (light) {
  49. this._activeLight = light;
  50. },
  51. enumerable: true,
  52. configurable: true
  53. });
  54. // Methods
  55. ShadowOnlyMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  56. if (this.isFrozen) {
  57. if (this._wasPreviouslyReady && subMesh.effect) {
  58. return true;
  59. }
  60. }
  61. if (!subMesh._materialDefines) {
  62. subMesh._materialDefines = new ShadowOnlyMaterialDefines();
  63. }
  64. var defines = subMesh._materialDefines;
  65. var scene = this.getScene();
  66. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  67. if (this._renderId === scene.getRenderId()) {
  68. return true;
  69. }
  70. }
  71. var engine = scene.getEngine();
  72. // Ensure that active light is the first shadow light
  73. if (this._activeLight) {
  74. for (var _i = 0, _a = mesh._lightSources; _i < _a.length; _i++) {
  75. var light = _a[_i];
  76. if (light.shadowEnabled) {
  77. if (this._activeLight === light) {
  78. break; // We are good
  79. }
  80. var lightPosition = mesh._lightSources.indexOf(this._activeLight);
  81. if (lightPosition !== -1) {
  82. mesh._lightSources.splice(lightPosition, 1);
  83. mesh._lightSources.splice(0, 0, this._activeLight);
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  90. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  91. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, 1);
  92. // Attribs
  93. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  94. // Get correct effect
  95. if (defines.isDirty) {
  96. defines.markAsProcessed();
  97. scene.resetCachedMaterial();
  98. // Fallbacks
  99. var fallbacks = new BABYLON.EffectFallbacks();
  100. if (defines.FOG) {
  101. fallbacks.addFallback(1, "FOG");
  102. }
  103. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, 1);
  104. if (defines.NUM_BONE_INFLUENCERS > 0) {
  105. fallbacks.addCPUSkinningFallback(0, mesh);
  106. }
  107. //Attributes
  108. var attribs = [BABYLON.VertexBuffer.PositionKind];
  109. if (defines.NORMAL) {
  110. attribs.push(BABYLON.VertexBuffer.NormalKind);
  111. }
  112. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  113. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  114. var shaderName = "shadowOnly";
  115. var join = defines.toString();
  116. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  117. "vFogInfos", "vFogColor", "pointSize", "alpha",
  118. "mBones",
  119. "vClipPlane"
  120. ];
  121. var samplers = new Array();
  122. var uniformBuffers = new Array();
  123. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  124. uniformsNames: uniforms,
  125. uniformBuffersNames: uniformBuffers,
  126. samplers: samplers,
  127. defines: defines,
  128. maxSimultaneousLights: 1
  129. });
  130. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  131. attributes: attribs,
  132. uniformsNames: uniforms,
  133. uniformBuffersNames: uniformBuffers,
  134. samplers: samplers,
  135. defines: join,
  136. fallbacks: fallbacks,
  137. onCompiled: this.onCompiled,
  138. onError: this.onError,
  139. indexParameters: { maxSimultaneousLights: 1 }
  140. }, engine), defines);
  141. }
  142. if (!subMesh.effect.isReady()) {
  143. return false;
  144. }
  145. this._renderId = scene.getRenderId();
  146. this._wasPreviouslyReady = true;
  147. return true;
  148. };
  149. ShadowOnlyMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  150. var scene = this.getScene();
  151. var defines = subMesh._materialDefines;
  152. if (!defines) {
  153. return;
  154. }
  155. var effect = subMesh.effect;
  156. this._activeEffect = effect;
  157. // Matrices
  158. this.bindOnlyWorldMatrix(world);
  159. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  160. // Bones
  161. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  162. if (this._mustRebind(scene, effect)) {
  163. // Clip plane
  164. BABYLON.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  165. // Point size
  166. if (this.pointsCloud) {
  167. this._activeEffect.setFloat("pointSize", this.pointSize);
  168. }
  169. this._activeEffect.setFloat("alpha", this.alpha);
  170. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  171. }
  172. // Lights
  173. if (scene.lightsEnabled) {
  174. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, 1);
  175. }
  176. // View
  177. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  178. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  179. }
  180. // Fog
  181. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  182. this._afterBind(mesh, this._activeEffect);
  183. };
  184. ShadowOnlyMaterial.prototype.clone = function (name) {
  185. var _this = this;
  186. return BABYLON.SerializationHelper.Clone(function () { return new ShadowOnlyMaterial(name, _this.getScene()); }, this);
  187. };
  188. ShadowOnlyMaterial.prototype.serialize = function () {
  189. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  190. serializationObject.customType = "BABYLON.ShadowOnlyMaterial";
  191. return serializationObject;
  192. };
  193. ShadowOnlyMaterial.prototype.getClassName = function () {
  194. return "ShadowOnlyMaterial";
  195. };
  196. // Statics
  197. ShadowOnlyMaterial.Parse = function (source, scene, rootUrl) {
  198. return BABYLON.SerializationHelper.Parse(function () { return new ShadowOnlyMaterial(source.name, scene); }, source, scene, rootUrl);
  199. };
  200. return ShadowOnlyMaterial;
  201. }(BABYLON.PushMaterial));
  202. BABYLON.ShadowOnlyMaterial = ShadowOnlyMaterial;
  203. })(BABYLON || (BABYLON = {}));
  204. //# sourceMappingURL=babylon.shadowOnlyMaterial.js.map
  205. 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<__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#include<clipPlaneVertex>\n\n#include<fogVertex>\n#include<shadowsVertex>[0..maxSimultaneousLights]\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  206. BABYLON.Effect.ShadersStore['shadowOnlyPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform float alpha;\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#include<helperFunctions>\n\n#include<__decl__lightFragment>[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.))*alpha);\n#include<fogFragment>\ngl_FragColor=color;\n}";