babylon.shadowOnlyMaterial.js 11 KB

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