babylon.shadowOnlyMaterial.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 Material {
  17. @serialize()
  18. private _worldViewProjectionMatrix = Matrix.Zero();
  19. private _scaledDiffuse = new Color3();
  20. private _renderId: number;
  21. private _defines = new ShadowOnlyMaterialDefines();
  22. private _cachedDefines = new ShadowOnlyMaterialDefines();
  23. constructor(name: string, scene: Scene) {
  24. super(name, scene);
  25. this._cachedDefines.BonesPerMesh = -1;
  26. }
  27. public needAlphaBlending(): boolean {
  28. return true;
  29. }
  30. public needAlphaTesting(): boolean {
  31. return false;
  32. }
  33. public getAlphaTestTexture(): BaseTexture {
  34. return null;
  35. }
  36. // Methods
  37. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  38. if (!mesh) {
  39. return true;
  40. }
  41. if (this._defines.INSTANCES !== useInstances) {
  42. return false;
  43. }
  44. return false;
  45. }
  46. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  47. if (this.checkReadyOnlyOnce) {
  48. if (this._wasPreviouslyReady) {
  49. return true;
  50. }
  51. }
  52. var scene = this.getScene();
  53. if (!this.checkReadyOnEveryCall) {
  54. if (this._renderId === scene.getRenderId()) {
  55. if (this._checkCache(scene, mesh, useInstances)) {
  56. return true;
  57. }
  58. }
  59. }
  60. var engine = scene.getEngine();
  61. var needNormals = false;
  62. this._defines.reset();
  63. // Effect
  64. if (scene.clipPlane) {
  65. this._defines.CLIPPLANE = true;
  66. }
  67. // Point size
  68. if (this.pointsCloud || scene.forcePointsCloud) {
  69. this._defines.POINTSIZE = true;
  70. }
  71. // Fog
  72. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  73. this._defines.FOG = true;
  74. }
  75. if (scene.lightsEnabled) {
  76. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, false, 1);
  77. }
  78. // Attribs
  79. if (mesh) {
  80. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  81. this._defines.NORMAL = true;
  82. }
  83. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  84. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  85. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  86. }
  87. // Instances
  88. if (useInstances) {
  89. this._defines.INSTANCES = true;
  90. }
  91. }
  92. // Get correct effect
  93. if (!this._defines.isEqual(this._cachedDefines)) {
  94. this._defines.cloneTo(this._cachedDefines);
  95. scene.resetCachedMaterial();
  96. // Fallbacks
  97. var fallbacks = new EffectFallbacks();
  98. if (this._defines.FOG) {
  99. fallbacks.addFallback(1, "FOG");
  100. }
  101. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, 1);
  102. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  103. fallbacks.addCPUSkinningFallback(0, mesh);
  104. }
  105. //Attributes
  106. var attribs = [VertexBuffer.PositionKind];
  107. if (this._defines.NORMAL) {
  108. attribs.push(VertexBuffer.NormalKind);
  109. }
  110. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  111. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  112. var shaderName = "shadowOnly";
  113. var join = this._defines.toString();
  114. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  115. "vFogInfos", "vFogColor", "pointSize",
  116. "mBones",
  117. "vClipPlane", "depthValues"
  118. ];
  119. var samplers = [];
  120. MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, 1);
  121. this._effect = scene.getEngine().createEffect(shaderName,
  122. attribs, uniforms, samplers,
  123. join, fallbacks, this.onCompiled, this.onError, {maxSimultaneousLights: 1});
  124. }
  125. if (!this._effect.isReady()) {
  126. return false;
  127. }
  128. this._renderId = scene.getRenderId();
  129. this._wasPreviouslyReady = true;
  130. return true;
  131. }
  132. public bindOnlyWorldMatrix(world: Matrix): void {
  133. this._effect.setMatrix("world", world);
  134. }
  135. public bind(world: Matrix, mesh?: Mesh): void {
  136. var scene = this.getScene();
  137. // Matrices
  138. this.bindOnlyWorldMatrix(world);
  139. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  140. // Bones
  141. MaterialHelper.BindBonesParameters(mesh, this._effect);
  142. if (scene.getCachedMaterial() !== this) {
  143. // Clip plane
  144. MaterialHelper.BindClipPlane(this._effect, scene);
  145. // Point size
  146. if (this.pointsCloud) {
  147. this._effect.setFloat("pointSize", this.pointSize);
  148. }
  149. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  150. }
  151. // Lights
  152. if (scene.lightsEnabled) {
  153. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, 1);
  154. }
  155. // View
  156. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  157. this._effect.setMatrix("view", scene.getViewMatrix());
  158. }
  159. // Fog
  160. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  161. this._afterBind(mesh);
  162. }
  163. public clone(name: string): ShadowOnlyMaterial {
  164. return SerializationHelper.Clone<ShadowOnlyMaterial>(() => new ShadowOnlyMaterial(name, this.getScene()), this);
  165. }
  166. public serialize(): any {
  167. var serializationObject = SerializationHelper.Serialize(this);
  168. serializationObject.customType = "BABYLON.ShadowOnlyMaterial";
  169. return serializationObject;
  170. }
  171. // Statics
  172. public static Parse(source: any, scene: Scene, rootUrl: string): ShadowOnlyMaterial {
  173. return SerializationHelper.Parse(() => new ShadowOnlyMaterial(source.name, scene), source, scene, rootUrl);
  174. }
  175. }
  176. }