babylon.shadowOnlyMaterial.js 12 KB

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