shadowOnlyMaterial.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 { EffectCreationOptions } 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. class ShadowOnlyMaterialDefines extends MaterialDefines {
  21. public CLIPPLANE = false;
  22. public CLIPPLANE2 = false;
  23. public CLIPPLANE3 = false;
  24. public CLIPPLANE4 = false;
  25. public POINTSIZE = false;
  26. public FOG = false;
  27. public NORMAL = false;
  28. public NUM_BONE_INFLUENCERS = 0;
  29. public BonesPerMesh = 0;
  30. public INSTANCES = false;
  31. constructor() {
  32. super();
  33. this.rebuild();
  34. }
  35. }
  36. export class ShadowOnlyMaterial extends PushMaterial {
  37. private _renderId: number;
  38. private _activeLight: IShadowLight;
  39. constructor(name: string, scene: Scene) {
  40. super(name, scene);
  41. }
  42. public shadowColor = Color3.Black();
  43. public needAlphaBlending(): boolean {
  44. return true;
  45. }
  46. public needAlphaTesting(): boolean {
  47. return false;
  48. }
  49. public getAlphaTestTexture(): Nullable<BaseTexture> {
  50. return null;
  51. }
  52. public get activeLight(): IShadowLight {
  53. return this._activeLight;
  54. }
  55. public set activeLight(light: IShadowLight) {
  56. this._activeLight = light;
  57. }
  58. // Methods
  59. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  60. if (this.isFrozen) {
  61. if (this._wasPreviouslyReady && subMesh.effect) {
  62. return true;
  63. }
  64. }
  65. if (!subMesh._materialDefines) {
  66. subMesh._materialDefines = new ShadowOnlyMaterialDefines();
  67. }
  68. var defines = <ShadowOnlyMaterialDefines>subMesh._materialDefines;
  69. var scene = this.getScene();
  70. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  71. if (this._renderId === scene.getRenderId()) {
  72. return true;
  73. }
  74. }
  75. var engine = scene.getEngine();
  76. // Ensure that active light is the first shadow light
  77. if (this._activeLight) {
  78. for (var light of mesh.lightSources) {
  79. if (light.shadowEnabled) {
  80. if (this._activeLight === light) {
  81. break; // We are good
  82. }
  83. var lightPosition = mesh.lightSources.indexOf(this._activeLight);
  84. if (lightPosition !== -1) {
  85. mesh.lightSources.splice(lightPosition, 1);
  86. mesh.lightSources.splice(0, 0, this._activeLight);
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  93. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  94. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, 1);
  95. // Attribs
  96. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  97. // Get correct effect
  98. if (defines.isDirty) {
  99. defines.markAsProcessed();
  100. scene.resetCachedMaterial();
  101. // Fallbacks
  102. var fallbacks = new EffectFallbacks();
  103. if (defines.FOG) {
  104. fallbacks.addFallback(1, "FOG");
  105. }
  106. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, 1);
  107. if (defines.NUM_BONE_INFLUENCERS > 0) {
  108. fallbacks.addCPUSkinningFallback(0, mesh);
  109. }
  110. //Attributes
  111. var attribs = [VertexBuffer.PositionKind];
  112. if (defines.NORMAL) {
  113. attribs.push(VertexBuffer.NormalKind);
  114. }
  115. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  116. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  117. var shaderName = "shadowOnly";
  118. var join = defines.toString();
  119. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  120. "vFogInfos", "vFogColor", "pointSize", "alpha", "shadowColor",
  121. "mBones",
  122. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4"
  123. ];
  124. var samplers = new Array<string>();
  125. var uniformBuffers = new Array<string>();
  126. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  127. uniformsNames: uniforms,
  128. uniformBuffersNames: uniformBuffers,
  129. samplers: samplers,
  130. defines: defines,
  131. maxSimultaneousLights: 1
  132. });
  133. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  134. <EffectCreationOptions>{
  135. attributes: attribs,
  136. uniformsNames: uniforms,
  137. uniformBuffersNames: uniformBuffers,
  138. samplers: samplers,
  139. defines: join,
  140. fallbacks: fallbacks,
  141. onCompiled: this.onCompiled,
  142. onError: this.onError,
  143. indexParameters: { maxSimultaneousLights: 1 }
  144. }, engine), defines);
  145. }
  146. if (!subMesh.effect || !subMesh.effect.isReady()) {
  147. return false;
  148. }
  149. this._renderId = scene.getRenderId();
  150. this._wasPreviouslyReady = true;
  151. return true;
  152. }
  153. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  154. var scene = this.getScene();
  155. var defines = <ShadowOnlyMaterialDefines>subMesh._materialDefines;
  156. if (!defines) {
  157. return;
  158. }
  159. var effect = subMesh.effect;
  160. if (!effect) {
  161. return;
  162. }
  163. this._activeEffect = effect;
  164. // Matrices
  165. this.bindOnlyWorldMatrix(world);
  166. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  167. // Bones
  168. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  169. if (this._mustRebind(scene, effect)) {
  170. // Clip plane
  171. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  172. // Point size
  173. if (this.pointsCloud) {
  174. this._activeEffect.setFloat("pointSize", this.pointSize);
  175. }
  176. this._activeEffect.setFloat("alpha", this.alpha);
  177. this._activeEffect.setColor3("shadowColor", this.shadowColor);
  178. MaterialHelper.BindEyePosition(effect, scene);
  179. }
  180. // Lights
  181. if (scene.lightsEnabled) {
  182. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, 1);
  183. }
  184. // View
  185. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  186. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  187. }
  188. // Fog
  189. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  190. this._afterBind(mesh, this._activeEffect);
  191. }
  192. public clone(name: string): ShadowOnlyMaterial {
  193. return SerializationHelper.Clone<ShadowOnlyMaterial>(() => new ShadowOnlyMaterial(name, this.getScene()), this);
  194. }
  195. public serialize(): any {
  196. var serializationObject = SerializationHelper.Serialize(this);
  197. serializationObject.customType = "BABYLON.ShadowOnlyMaterial";
  198. return serializationObject;
  199. }
  200. public getClassName(): string {
  201. return "ShadowOnlyMaterial";
  202. }
  203. // Statics
  204. public static Parse(source: any, scene: Scene, rootUrl: string): ShadowOnlyMaterial {
  205. return SerializationHelper.Parse(() => new ShadowOnlyMaterial(source.name, scene), source, scene, rootUrl);
  206. }
  207. }
  208. _TypeStore.RegisteredTypes["BABYLON.ShadowOnlyMaterial"] = ShadowOnlyMaterial;