babylon.shadowOnlyMaterial.ts 8.3 KB

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