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 = 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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  13. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  14. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  15. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  16. return c > 3 && r && Object.defineProperty(target, key, r), r;
  17. };
  18. var BABYLON;
  19. (function (BABYLON) {
  20. var ShadowOnlyMaterialDefines = (function (_super) {
  21. __extends(ShadowOnlyMaterialDefines, _super);
  22. function ShadowOnlyMaterialDefines() {
  23. var _this = _super.call(this) || this;
  24. _this.CLIPPLANE = 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 = (function (_super) {
  37. __extends(ShadowOnlyMaterial, _super);
  38. function ShadowOnlyMaterial(name, scene) {
  39. var _this = _super.call(this, name, scene) || this;
  40. _this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  41. _this._scaledDiffuse = new BABYLON.Color3();
  42. return _this;
  43. }
  44. ShadowOnlyMaterial.prototype.needAlphaBlending = function () {
  45. return true;
  46. };
  47. ShadowOnlyMaterial.prototype.needAlphaTesting = function () {
  48. return false;
  49. };
  50. ShadowOnlyMaterial.prototype.getAlphaTestTexture = function () {
  51. return null;
  52. };
  53. Object.defineProperty(ShadowOnlyMaterial.prototype, "activeLight", {
  54. get: function () {
  55. return this._activeLight;
  56. },
  57. set: function (light) {
  58. this._activeLight = light;
  59. },
  60. enumerable: true,
  61. configurable: true
  62. });
  63. // Methods
  64. ShadowOnlyMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  65. if (this.isFrozen) {
  66. if (this._wasPreviouslyReady && subMesh.effect) {
  67. return true;
  68. }
  69. }
  70. if (!subMesh._materialDefines) {
  71. subMesh._materialDefines = new ShadowOnlyMaterialDefines();
  72. }
  73. var defines = subMesh._materialDefines;
  74. var scene = this.getScene();
  75. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  76. if (this._renderId === scene.getRenderId()) {
  77. return true;
  78. }
  79. }
  80. var engine = scene.getEngine();
  81. // Ensure that active light is the first shadow light
  82. if (this._activeLight) {
  83. for (var _i = 0, _a = mesh._lightSources; _i < _a.length; _i++) {
  84. var light = _a[_i];
  85. if (light.shadowEnabled) {
  86. if (this._activeLight === light) {
  87. break; // We are good
  88. }
  89. var lightPosition = mesh._lightSources.indexOf(this._activeLight);
  90. if (lightPosition !== -1) {
  91. mesh._lightSources.splice(lightPosition, 1);
  92. mesh._lightSources.splice(0, 0, this._activeLight);
  93. }
  94. break;
  95. }
  96. }
  97. }
  98. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  99. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  100. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, 1);
  101. // Attribs
  102. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  103. // Get correct effect
  104. if (defines.isDirty) {
  105. defines.markAsProcessed();
  106. scene.resetCachedMaterial();
  107. // Fallbacks
  108. var fallbacks = new BABYLON.EffectFallbacks();
  109. if (defines.FOG) {
  110. fallbacks.addFallback(1, "FOG");
  111. }
  112. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, 1);
  113. if (defines.NUM_BONE_INFLUENCERS > 0) {
  114. fallbacks.addCPUSkinningFallback(0, mesh);
  115. }
  116. //Attributes
  117. var attribs = [BABYLON.VertexBuffer.PositionKind];
  118. if (defines.NORMAL) {
  119. attribs.push(BABYLON.VertexBuffer.NormalKind);
  120. }
  121. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  122. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  123. var shaderName = "shadowOnly";
  124. var join = defines.toString();
  125. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  126. "vFogInfos", "vFogColor", "pointSize", "alpha",
  127. "mBones",
  128. "vClipPlane"
  129. ];
  130. var samplers = new Array();
  131. var uniformBuffers = new Array();
  132. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  133. uniformsNames: uniforms,
  134. uniformBuffersNames: uniformBuffers,
  135. samplers: samplers,
  136. defines: defines,
  137. maxSimultaneousLights: 1
  138. });
  139. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  140. attributes: attribs,
  141. uniformsNames: uniforms,
  142. uniformBuffersNames: uniformBuffers,
  143. samplers: samplers,
  144. defines: join,
  145. fallbacks: fallbacks,
  146. onCompiled: this.onCompiled,
  147. onError: this.onError,
  148. indexParameters: { maxSimultaneousLights: 1 }
  149. }, engine), defines);
  150. }
  151. if (!subMesh.effect.isReady()) {
  152. return false;
  153. }
  154. this._renderId = scene.getRenderId();
  155. this._wasPreviouslyReady = true;
  156. return true;
  157. };
  158. ShadowOnlyMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  159. var scene = this.getScene();
  160. var defines = subMesh._materialDefines;
  161. if (!defines) {
  162. return;
  163. }
  164. var effect = subMesh.effect;
  165. this._activeEffect = effect;
  166. // Matrices
  167. this.bindOnlyWorldMatrix(world);
  168. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  169. // Bones
  170. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  171. if (this._mustRebind(scene, effect)) {
  172. // Clip plane
  173. BABYLON.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  174. // Point size
  175. if (this.pointsCloud) {
  176. this._activeEffect.setFloat("pointSize", this.pointSize);
  177. }
  178. this._activeEffect.setFloat("alpha", this.alpha);
  179. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  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. __decorate([
  210. BABYLON.serialize()
  211. ], ShadowOnlyMaterial.prototype, "_worldViewProjectionMatrix", void 0);
  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;\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}";