babylon.shadowOnlyMaterial.js 11 KB

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