babylon.skyMaterial.ts 8.0 KB

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