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