shadowOnlyMaterial.ts 11 KB

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