babylon.shadowOnlyMaterial.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // Methods
  55. ShadowOnlyMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  56. if (this.isFrozen) {
  57. if (this._wasPreviouslyReady && subMesh.effect) {
  58. return true;
  59. }
  60. }
  61. if (!subMesh._materialDefines) {
  62. subMesh._materialDefines = new ShadowOnlyMaterialDefines();
  63. }
  64. var defines = subMesh._materialDefines;
  65. var scene = this.getScene();
  66. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  67. if (this._renderId === scene.getRenderId()) {
  68. return true;
  69. }
  70. }
  71. var engine = scene.getEngine();
  72. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  73. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  74. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, 1);
  75. // Attribs
  76. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  77. // Get correct effect
  78. if (defines.isDirty) {
  79. defines.markAsProcessed();
  80. scene.resetCachedMaterial();
  81. // Fallbacks
  82. var fallbacks = new BABYLON.EffectFallbacks();
  83. if (defines.FOG) {
  84. fallbacks.addFallback(1, "FOG");
  85. }
  86. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, 1);
  87. if (defines.NUM_BONE_INFLUENCERS > 0) {
  88. fallbacks.addCPUSkinningFallback(0, mesh);
  89. }
  90. //Attributes
  91. var attribs = [BABYLON.VertexBuffer.PositionKind];
  92. if (defines.NORMAL) {
  93. attribs.push(BABYLON.VertexBuffer.NormalKind);
  94. }
  95. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  96. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  97. var shaderName = "shadowOnly";
  98. var join = defines.toString();
  99. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  100. "vFogInfos", "vFogColor", "pointSize",
  101. "mBones",
  102. "vClipPlane", "depthValues"
  103. ];
  104. var samplers = [];
  105. var uniformBuffers = [];
  106. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  107. uniformsNames: uniforms,
  108. uniformBuffersNames: uniformBuffers,
  109. samplers: samplers,
  110. defines: defines,
  111. maxSimultaneousLights: 1
  112. });
  113. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  114. attributes: attribs,
  115. uniformsNames: uniforms,
  116. uniformBuffersNames: uniformBuffers,
  117. samplers: samplers,
  118. defines: join,
  119. fallbacks: fallbacks,
  120. onCompiled: this.onCompiled,
  121. onError: this.onError,
  122. indexParameters: { maxSimultaneousLights: 1 }
  123. }, engine), defines);
  124. }
  125. if (!subMesh.effect.isReady()) {
  126. return false;
  127. }
  128. this._renderId = scene.getRenderId();
  129. this._wasPreviouslyReady = true;
  130. return true;
  131. };
  132. ShadowOnlyMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  133. var scene = this.getScene();
  134. var defines = subMesh._materialDefines;
  135. if (!defines) {
  136. return;
  137. }
  138. var effect = subMesh.effect;
  139. this._activeEffect = effect;
  140. // Matrices
  141. this.bindOnlyWorldMatrix(world);
  142. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  143. // Bones
  144. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  145. if (this._mustRebind(scene, effect)) {
  146. // Clip plane
  147. BABYLON.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  148. // Point size
  149. if (this.pointsCloud) {
  150. this._activeEffect.setFloat("pointSize", this.pointSize);
  151. }
  152. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  153. }
  154. // Lights
  155. if (scene.lightsEnabled) {
  156. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, 1);
  157. }
  158. // View
  159. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  160. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  161. }
  162. // Fog
  163. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  164. this._afterBind(mesh, this._activeEffect);
  165. };
  166. ShadowOnlyMaterial.prototype.clone = function (name) {
  167. var _this = this;
  168. return BABYLON.SerializationHelper.Clone(function () { return new ShadowOnlyMaterial(name, _this.getScene()); }, this);
  169. };
  170. ShadowOnlyMaterial.prototype.serialize = function () {
  171. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  172. serializationObject.customType = "BABYLON.ShadowOnlyMaterial";
  173. return serializationObject;
  174. };
  175. // Statics
  176. ShadowOnlyMaterial.Parse = function (source, scene, rootUrl) {
  177. return BABYLON.SerializationHelper.Parse(function () { return new ShadowOnlyMaterial(source.name, scene); }, source, scene, rootUrl);
  178. };
  179. return ShadowOnlyMaterial;
  180. }(BABYLON.PushMaterial));
  181. __decorate([
  182. BABYLON.serialize()
  183. ], ShadowOnlyMaterial.prototype, "_worldViewProjectionMatrix", void 0);
  184. BABYLON.ShadowOnlyMaterial = ShadowOnlyMaterial;
  185. })(BABYLON || (BABYLON = {}));
  186. //# sourceMappingURL=babylon.shadowOnlyMaterial.js.map
  187. 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<shadowsVertexDeclaration>[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";
  188. 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<__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}";