babylon.shadowOnlyMaterial.js 11 KB

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