shadowOnlyMaterial.ts 8.0 KB

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