shadowOnlyMaterial.ts 8.8 KB

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