babylon.skyMaterial.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(): 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. var engine = scene.getEngine();
  69. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  70. // Attribs
  71. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, false);
  72. // Get correct effect
  73. if (defines.isDirty) {
  74. defines.markAsProcessed();
  75. scene.resetCachedMaterial();
  76. // Fallbacks
  77. var fallbacks = new EffectFallbacks();
  78. if (defines.FOG) {
  79. fallbacks.addFallback(1, "FOG");
  80. }
  81. //Attributes
  82. var attribs = [VertexBuffer.PositionKind];
  83. if (defines.VERTEXCOLOR) {
  84. attribs.push(VertexBuffer.ColorKind);
  85. }
  86. var shaderName = "sky";
  87. var join = defines.toString();
  88. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  89. attribs,
  90. ["world", "viewProjection", "view",
  91. "vFogInfos", "vFogColor", "pointSize", "vClipPlane",
  92. "luminance", "turbidity", "rayleigh", "mieCoefficient", "mieDirectionalG", "sunPosition",
  93. "cameraPosition"
  94. ],
  95. [],
  96. join, fallbacks, this.onCompiled, this.onError), defines);
  97. }
  98. if (!subMesh.effect.isReady()) {
  99. return false;
  100. }
  101. this._renderId = scene.getRenderId();
  102. this._wasPreviouslyReady = true;
  103. return true;
  104. }
  105. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  106. var scene = this.getScene();
  107. var defines = <SkyMaterialDefines>subMesh._materialDefines;
  108. if (!defines) {
  109. return;
  110. }
  111. var effect = subMesh.effect;
  112. this._activeEffect = effect;
  113. // Matrices
  114. this.bindOnlyWorldMatrix(world);
  115. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  116. if (this._mustRebind(scene, effect)) {
  117. // Clip plane
  118. if (scene.clipPlane) {
  119. var clipPlane = scene.clipPlane;
  120. this._activeEffect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  121. }
  122. // Point size
  123. if (this.pointsCloud) {
  124. this._activeEffect.setFloat("pointSize", this.pointSize);
  125. }
  126. }
  127. // View
  128. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  129. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  130. }
  131. // Fog
  132. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  133. // Sky
  134. var camera = scene.activeCamera;
  135. if (camera) {
  136. var cameraWorldMatrix = camera.getWorldMatrix();
  137. this._cameraPosition.x = cameraWorldMatrix.m[12];
  138. this._cameraPosition.y = cameraWorldMatrix.m[13];
  139. this._cameraPosition.z = cameraWorldMatrix.m[14];
  140. this._activeEffect.setVector3("cameraPosition", this._cameraPosition);
  141. }
  142. if (this.luminance > 0) {
  143. this._activeEffect.setFloat("luminance", this.luminance);
  144. }
  145. this._activeEffect.setFloat("turbidity", this.turbidity);
  146. this._activeEffect.setFloat("rayleigh", this.rayleigh);
  147. this._activeEffect.setFloat("mieCoefficient", this.mieCoefficient);
  148. this._activeEffect.setFloat("mieDirectionalG", this.mieDirectionalG);
  149. if (!this.useSunPosition) {
  150. var theta = Math.PI * (this.inclination - 0.5);
  151. var phi = 2 * Math.PI * (this.azimuth - 0.5);
  152. this.sunPosition.x = this.distance * Math.cos(phi);
  153. this.sunPosition.y = this.distance * Math.sin(phi) * Math.sin(theta);
  154. this.sunPosition.z = this.distance * Math.sin(phi) * Math.cos(theta);
  155. }
  156. this._activeEffect.setVector3("sunPosition", this.sunPosition);
  157. this._afterBind(mesh, this._activeEffect);
  158. }
  159. public getAnimatables(): IAnimatable[] {
  160. return [];
  161. }
  162. public dispose(forceDisposeEffect?: boolean): void {
  163. super.dispose(forceDisposeEffect);
  164. }
  165. public clone(name: string): SkyMaterial {
  166. return SerializationHelper.Clone<SkyMaterial>(() => new SkyMaterial(name, this.getScene()), this);
  167. }
  168. public serialize(): any {
  169. var serializationObject = SerializationHelper.Serialize(this);
  170. serializationObject.customType = "BABYLON.SkyMaterial";
  171. return serializationObject;
  172. }
  173. public getClassName(): string {
  174. return "SkyMaterial";
  175. }
  176. // Statics
  177. public static Parse(source: any, scene: Scene, rootUrl: string): SkyMaterial {
  178. return SerializationHelper.Parse(() => new SkyMaterial(source.name, scene), source, scene, rootUrl);
  179. }
  180. }
  181. }