shadowOnlyMaterial.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import { Nullable } from "babylonjs/types";
  2. import { SerializationHelper } from "babylonjs/Misc/decorators";
  3. import { Matrix } from "babylonjs/Maths/math.vector";
  4. import { Color3 } from "babylonjs/Maths/math.color";
  5. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  6. import { IShadowLight } from "babylonjs/Lights/shadowLight";
  7. import { IEffectCreationOptions } from "babylonjs/Materials/effect";
  8. import { MaterialDefines } from "babylonjs/Materials/materialDefines";
  9. import { MaterialHelper } from "babylonjs/Materials/materialHelper";
  10. import { PushMaterial } from "babylonjs/Materials/pushMaterial";
  11. import { VertexBuffer } from "babylonjs/Meshes/buffer";
  12. import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
  13. import { SubMesh } from "babylonjs/Meshes/subMesh";
  14. import { Mesh } from "babylonjs/Meshes/mesh";
  15. import { Scene } from "babylonjs/scene";
  16. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  17. import "./shadowOnly.fragment";
  18. import "./shadowOnly.vertex";
  19. import { EffectFallbacks } from 'babylonjs/Materials/effectFallbacks';
  20. import { CascadedShadowGenerator } from 'babylonjs/Lights/Shadows/cascadedShadowGenerator';
  21. class ShadowOnlyMaterialDefines extends MaterialDefines {
  22. public CLIPPLANE = false;
  23. public CLIPPLANE2 = false;
  24. public CLIPPLANE3 = false;
  25. public CLIPPLANE4 = false;
  26. public CLIPPLANE5 = false;
  27. public CLIPPLANE6 = false;
  28. public POINTSIZE = false;
  29. public FOG = false;
  30. public NORMAL = false;
  31. public NUM_BONE_INFLUENCERS = 0;
  32. public BonesPerMesh = 0;
  33. public INSTANCES = false;
  34. public IMAGEPROCESSINGPOSTPROCESS = false;
  35. constructor() {
  36. super();
  37. this.rebuild();
  38. }
  39. }
  40. export class ShadowOnlyMaterial extends PushMaterial {
  41. private _activeLight: IShadowLight;
  42. private _needAlphaBlending = true;
  43. constructor(name: string, scene: Scene) {
  44. super(name, scene);
  45. }
  46. public shadowColor = Color3.Black();
  47. public needAlphaBlending(): boolean {
  48. return this._needAlphaBlending;
  49. }
  50. public needAlphaTesting(): boolean {
  51. return false;
  52. }
  53. public getAlphaTestTexture(): Nullable<BaseTexture> {
  54. return null;
  55. }
  56. public get activeLight(): IShadowLight {
  57. return this._activeLight;
  58. }
  59. public set activeLight(light: IShadowLight) {
  60. this._activeLight = light;
  61. }
  62. private _getFirstShadowLightForMesh(mesh: AbstractMesh): Nullable<IShadowLight> {
  63. for (var light of mesh.lightSources) {
  64. if (light.shadowEnabled) {
  65. return light as IShadowLight;
  66. }
  67. }
  68. return null;
  69. }
  70. // Methods
  71. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  72. if (this.isFrozen) {
  73. if (subMesh.effect && subMesh.effect._wasPreviouslyReady) {
  74. return true;
  75. }
  76. }
  77. if (!subMesh._materialDefines) {
  78. subMesh._materialDefines = new ShadowOnlyMaterialDefines();
  79. }
  80. var defines = <ShadowOnlyMaterialDefines>subMesh._materialDefines;
  81. var scene = this.getScene();
  82. if (this._isReadyForSubMesh(subMesh)) {
  83. return true;
  84. }
  85. var engine = scene.getEngine();
  86. // Ensure that active light is the first shadow light
  87. if (this._activeLight) {
  88. for (var light of mesh.lightSources) {
  89. if (light.shadowEnabled) {
  90. if (this._activeLight === light) {
  91. break; // We are good
  92. }
  93. var lightPosition = mesh.lightSources.indexOf(this._activeLight);
  94. if (lightPosition !== -1) {
  95. mesh.lightSources.splice(lightPosition, 1);
  96. mesh.lightSources.splice(0, 0, this._activeLight);
  97. }
  98. break;
  99. }
  100. }
  101. }
  102. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  103. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  104. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, 1);
  105. const shadowGenerator = this._getFirstShadowLightForMesh(mesh)?.getShadowGenerator();
  106. this._needAlphaBlending = true;
  107. if (shadowGenerator && (shadowGenerator as any).getClassName && (shadowGenerator as any).getClassName() === 'CascadedShadowGenerator') {
  108. const csg = shadowGenerator as CascadedShadowGenerator;
  109. this._needAlphaBlending = !csg.autoCalcDepthBounds;
  110. }
  111. // Attribs
  112. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  113. // Get correct effect
  114. if (defines.isDirty) {
  115. defines.markAsProcessed();
  116. scene.resetCachedMaterial();
  117. // Fallbacks
  118. var fallbacks = new EffectFallbacks();
  119. if (defines.FOG) {
  120. fallbacks.addFallback(1, "FOG");
  121. }
  122. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, 1);
  123. if (defines.NUM_BONE_INFLUENCERS > 0) {
  124. fallbacks.addCPUSkinningFallback(0, mesh);
  125. }
  126. defines.IMAGEPROCESSINGPOSTPROCESS = scene.imageProcessingConfiguration.applyByPostProcess;
  127. //Attributes
  128. var attribs = [VertexBuffer.PositionKind];
  129. if (defines.NORMAL) {
  130. attribs.push(VertexBuffer.NormalKind);
  131. }
  132. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  133. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  134. var shaderName = "shadowOnly";
  135. var join = defines.toString();
  136. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  137. "vFogInfos", "vFogColor", "pointSize", "alpha", "shadowColor",
  138. "mBones",
  139. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "vClipPlane5", "vClipPlane6"
  140. ];
  141. var samplers = new Array<string>();
  142. var uniformBuffers = new Array<string>();
  143. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  144. uniformsNames: uniforms,
  145. uniformBuffersNames: uniformBuffers,
  146. samplers: samplers,
  147. defines: defines,
  148. maxSimultaneousLights: 1
  149. });
  150. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  151. <IEffectCreationOptions>{
  152. attributes: attribs,
  153. uniformsNames: uniforms,
  154. uniformBuffersNames: uniformBuffers,
  155. samplers: samplers,
  156. defines: join,
  157. fallbacks: fallbacks,
  158. onCompiled: this.onCompiled,
  159. onError: this.onError,
  160. indexParameters: { maxSimultaneousLights: 1 }
  161. }, engine), defines);
  162. }
  163. if (!subMesh.effect || !subMesh.effect.isReady()) {
  164. return false;
  165. }
  166. defines._renderId = scene.getRenderId();
  167. subMesh.effect._wasPreviouslyReady = true;
  168. return true;
  169. }
  170. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  171. var scene = this.getScene();
  172. var defines = <ShadowOnlyMaterialDefines>subMesh._materialDefines;
  173. if (!defines) {
  174. return;
  175. }
  176. var effect = subMesh.effect;
  177. if (!effect) {
  178. return;
  179. }
  180. this._activeEffect = effect;
  181. // Matrices
  182. this.bindOnlyWorldMatrix(world);
  183. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  184. // Bones
  185. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  186. if (this._mustRebind(scene, effect)) {
  187. // Clip plane
  188. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  189. // Point size
  190. if (this.pointsCloud) {
  191. this._activeEffect.setFloat("pointSize", this.pointSize);
  192. }
  193. this._activeEffect.setFloat("alpha", this.alpha);
  194. this._activeEffect.setColor3("shadowColor", this.shadowColor);
  195. MaterialHelper.BindEyePosition(effect, scene);
  196. }
  197. // Lights
  198. if (scene.lightsEnabled) {
  199. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, 1);
  200. const light = this._getFirstShadowLightForMesh(mesh);
  201. if (light) {
  202. // Make sure the uniforms for this light will be rebound for other materials using this light when rendering the current frame.
  203. // Indeed, there is an optimization in Light that binds the light uniforms only once per frame for a given light (if using ubo).
  204. // Doing this way assumes that all uses of this light are the same, meaning all parameters passed to Light._bindLlight
  205. // are the same, notably useSpecular. However, isReadyForSubMesh (see above) is passing false for this parameter, which may not be
  206. // the value the other materials may pass.
  207. light._renderId = -1;
  208. }
  209. }
  210. // View
  211. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE || defines["SHADOWCSM0"]) {
  212. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  213. }
  214. // Fog
  215. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  216. this._afterBind(mesh, this._activeEffect);
  217. }
  218. public clone(name: string): ShadowOnlyMaterial {
  219. return SerializationHelper.Clone<ShadowOnlyMaterial>(() => new ShadowOnlyMaterial(name, this.getScene()), this);
  220. }
  221. public serialize(): any {
  222. var serializationObject = SerializationHelper.Serialize(this);
  223. serializationObject.customType = "BABYLON.ShadowOnlyMaterial";
  224. return serializationObject;
  225. }
  226. public getClassName(): string {
  227. return "ShadowOnlyMaterial";
  228. }
  229. // Statics
  230. public static Parse(source: any, scene: Scene, rootUrl: string): ShadowOnlyMaterial {
  231. return SerializationHelper.Parse(() => new ShadowOnlyMaterial(source.name, scene), source, scene, rootUrl);
  232. }
  233. }
  234. _TypeStore.RegisteredTypes["BABYLON.ShadowOnlyMaterial"] = ShadowOnlyMaterial;