babylon.skyMaterial.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. class SkyMaterialDefines extends MaterialDefines {
  4. public CLIPPLANE = false;
  5. public POINTSIZE = false;
  6. public FOG = false;
  7. public VERTEXCOLOR = false;
  8. public VERTEXALPHA = false;
  9. constructor() {
  10. super();
  11. this.rebuild();
  12. }
  13. }
  14. export class SkyMaterial extends PushMaterial {
  15. // Public members
  16. @serialize()
  17. public luminance: number = 1.0;
  18. @serialize()
  19. public turbidity: number = 10.0;
  20. @serialize()
  21. public rayleigh: number = 2.0;
  22. @serialize()
  23. public mieCoefficient: number = 0.005;
  24. @serialize()
  25. public mieDirectionalG: number = 0.8;
  26. @serialize()
  27. public distance: number = 500;
  28. @serialize()
  29. public inclination: number = 0.49;
  30. @serialize()
  31. public azimuth: number = 0.25;
  32. @serializeAsVector3()
  33. public sunPosition: Vector3 = new Vector3(0, 100, 0);
  34. @serialize()
  35. public useSunPosition: boolean = false;
  36. // Private members
  37. private _cameraPosition: Vector3 = Vector3.Zero();
  38. private _renderId: number;
  39. constructor(name: string, scene: Scene) {
  40. super(name, scene);
  41. }
  42. public needAlphaBlending(): boolean {
  43. return (this.alpha < 1.0);
  44. }
  45. public needAlphaTesting(): boolean {
  46. return false;
  47. }
  48. public getAlphaTestTexture(): Nullable<BaseTexture> {
  49. return null;
  50. }
  51. // Methods
  52. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  53. if (this.isFrozen) {
  54. if (this._wasPreviouslyReady && subMesh.effect) {
  55. return true;
  56. }
  57. }
  58. if (!subMesh._materialDefines) {
  59. subMesh._materialDefines = new SkyMaterialDefines();
  60. }
  61. var defines = <SkyMaterialDefines>subMesh._materialDefines;
  62. var scene = this.getScene();
  63. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  64. if (this._renderId === scene.getRenderId()) {
  65. return true;
  66. }
  67. }
  68. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, false, defines);
  69. // Attribs
  70. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, false);
  71. // Get correct effect
  72. if (defines.isDirty) {
  73. defines.markAsProcessed();
  74. scene.resetCachedMaterial();
  75. // Fallbacks
  76. var fallbacks = new EffectFallbacks();
  77. if (defines.FOG) {
  78. fallbacks.addFallback(1, "FOG");
  79. }
  80. //Attributes
  81. var attribs = [VertexBuffer.PositionKind];
  82. if (defines.VERTEXCOLOR) {
  83. attribs.push(VertexBuffer.ColorKind);
  84. }
  85. var shaderName = "sky";
  86. var join = defines.toString();
  87. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  88. attribs,
  89. ["world", "viewProjection", "view",
  90. "vFogInfos", "vFogColor", "pointSize", "vClipPlane",
  91. "luminance", "turbidity", "rayleigh", "mieCoefficient", "mieDirectionalG", "sunPosition",
  92. "cameraPosition"
  93. ],
  94. [],
  95. join, fallbacks, this.onCompiled, this.onError), defines);
  96. }
  97. if (!subMesh.effect || !subMesh.effect.isReady()) {
  98. return false;
  99. }
  100. this._renderId = scene.getRenderId();
  101. this._wasPreviouslyReady = true;
  102. return true;
  103. }
  104. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  105. var scene = this.getScene();
  106. var defines = <SkyMaterialDefines>subMesh._materialDefines;
  107. if (!defines) {
  108. return;
  109. }
  110. var effect = subMesh.effect;
  111. if (!effect) {
  112. return;
  113. }
  114. this._activeEffect = effect;
  115. // Matrices
  116. this.bindOnlyWorldMatrix(world);
  117. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  118. if (this._mustRebind(scene, effect)) {
  119. // Clip plane
  120. if (scene.clipPlane) {
  121. var clipPlane = scene.clipPlane;
  122. this._activeEffect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  123. }
  124. // Point size
  125. if (this.pointsCloud) {
  126. this._activeEffect.setFloat("pointSize", this.pointSize);
  127. }
  128. }
  129. // View
  130. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  131. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  132. }
  133. // Fog
  134. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  135. // Sky
  136. var camera = scene.activeCamera;
  137. if (camera) {
  138. var cameraWorldMatrix = camera.getWorldMatrix();
  139. this._cameraPosition.x = cameraWorldMatrix.m[12];
  140. this._cameraPosition.y = cameraWorldMatrix.m[13];
  141. this._cameraPosition.z = cameraWorldMatrix.m[14];
  142. this._activeEffect.setVector3("cameraPosition", this._cameraPosition);
  143. }
  144. if (this.luminance > 0) {
  145. this._activeEffect.setFloat("luminance", this.luminance);
  146. }
  147. this._activeEffect.setFloat("turbidity", this.turbidity);
  148. this._activeEffect.setFloat("rayleigh", this.rayleigh);
  149. this._activeEffect.setFloat("mieCoefficient", this.mieCoefficient);
  150. this._activeEffect.setFloat("mieDirectionalG", this.mieDirectionalG);
  151. if (!this.useSunPosition) {
  152. var theta = Math.PI * (this.inclination - 0.5);
  153. var phi = 2 * Math.PI * (this.azimuth - 0.5);
  154. this.sunPosition.x = this.distance * Math.cos(phi);
  155. this.sunPosition.y = this.distance * Math.sin(phi) * Math.sin(theta);
  156. this.sunPosition.z = this.distance * Math.sin(phi) * Math.cos(theta);
  157. }
  158. this._activeEffect.setVector3("sunPosition", this.sunPosition);
  159. this._afterBind(mesh, this._activeEffect);
  160. }
  161. public getAnimatables(): IAnimatable[] {
  162. return [];
  163. }
  164. public dispose(forceDisposeEffect?: boolean): void {
  165. super.dispose(forceDisposeEffect);
  166. }
  167. public clone(name: string): SkyMaterial {
  168. return SerializationHelper.Clone<SkyMaterial>(() => new SkyMaterial(name, this.getScene()), this);
  169. }
  170. public serialize(): any {
  171. var serializationObject = SerializationHelper.Serialize(this);
  172. serializationObject.customType = "BABYLON.SkyMaterial";
  173. return serializationObject;
  174. }
  175. public getClassName(): string {
  176. return "SkyMaterial";
  177. }
  178. // Statics
  179. public static Parse(source: any, scene: Scene, rootUrl: string): SkyMaterial {
  180. return SerializationHelper.Parse(() => new SkyMaterial(source.name, scene), source, scene, rootUrl);
  181. }
  182. }
  183. }