babylon.skyMaterial.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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._keys = Object.keys(this);
  12. }
  13. }
  14. export class SkyMaterial extends Material {
  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. // Private members
  33. private _sunPosition: Vector3 = Vector3.Zero();
  34. private _renderId: number;
  35. private _defines = new SkyMaterialDefines();
  36. private _cachedDefines = new SkyMaterialDefines();
  37. constructor(name: string, scene: Scene) {
  38. super(name, scene);
  39. }
  40. public needAlphaBlending(): boolean {
  41. return (this.alpha < 1.0);
  42. }
  43. public needAlphaTesting(): boolean {
  44. return false;
  45. }
  46. public getAlphaTestTexture(): BaseTexture {
  47. return null;
  48. }
  49. // Methods
  50. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  51. if (!mesh) {
  52. return true;
  53. }
  54. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  60. if (this.checkReadyOnlyOnce) {
  61. if (this._wasPreviouslyReady) {
  62. return true;
  63. }
  64. }
  65. var scene = this.getScene();
  66. if (!this.checkReadyOnEveryCall) {
  67. if (this._renderId === scene.getRenderId()) {
  68. if (this._checkCache(scene, mesh, useInstances)) {
  69. return true;
  70. }
  71. }
  72. }
  73. var engine = scene.getEngine();
  74. this._defines.reset();
  75. // Effect
  76. if (scene.clipPlane) {
  77. this._defines.CLIPPLANE = true;
  78. }
  79. // Point size
  80. if (this.pointsCloud || scene.forcePointsCloud) {
  81. this._defines.POINTSIZE = true;
  82. }
  83. // Fog
  84. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  85. this._defines.FOG = true;
  86. }
  87. // Attribs
  88. if (mesh) {
  89. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  90. this._defines.VERTEXCOLOR = true;
  91. if (mesh.hasVertexAlpha) {
  92. this._defines.VERTEXALPHA = true;
  93. }
  94. }
  95. }
  96. // Get correct effect
  97. if (!this._defines.isEqual(this._cachedDefines) || !this._effect) {
  98. this._defines.cloneTo(this._cachedDefines);
  99. scene.resetCachedMaterial();
  100. // Fallbacks
  101. var fallbacks = new EffectFallbacks();
  102. if (this._defines.FOG) {
  103. fallbacks.addFallback(1, "FOG");
  104. }
  105. //Attributes
  106. var attribs = [VertexBuffer.PositionKind];
  107. if (this._defines.VERTEXCOLOR) {
  108. attribs.push(VertexBuffer.ColorKind);
  109. }
  110. // Legacy browser patch
  111. var shaderName = "sky";
  112. var join = this._defines.toString();
  113. this._effect = scene.getEngine().createEffect(shaderName,
  114. attribs,
  115. ["world", "viewProjection", "view",
  116. "vFogInfos", "vFogColor", "pointSize", "vClipPlane",
  117. "luminance", "turbidity", "rayleigh", "mieCoefficient", "mieDirectionalG", "sunPosition"
  118. ],
  119. [],
  120. join, fallbacks, this.onCompiled, this.onError);
  121. }
  122. if (!this._effect.isReady()) {
  123. return false;
  124. }
  125. this._renderId = scene.getRenderId();
  126. this._wasPreviouslyReady = true;
  127. if (mesh) {
  128. if (!mesh._materialDefines) {
  129. mesh._materialDefines = new SkyMaterialDefines();
  130. }
  131. this._defines.cloneTo(mesh._materialDefines);
  132. }
  133. return true;
  134. }
  135. public bindOnlyWorldMatrix(world: Matrix): void {
  136. this._effect.setMatrix("world", world);
  137. }
  138. public bind(world: Matrix, mesh?: Mesh): void {
  139. var scene = this.getScene();
  140. // Matrices
  141. this.bindOnlyWorldMatrix(world);
  142. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  143. if (scene.getCachedMaterial() !== this) {
  144. // Clip plane
  145. if (scene.clipPlane) {
  146. var clipPlane = scene.clipPlane;
  147. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  148. }
  149. // Point size
  150. if (this.pointsCloud) {
  151. this._effect.setFloat("pointSize", this.pointSize);
  152. }
  153. }
  154. // View
  155. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  156. this._effect.setMatrix("view", scene.getViewMatrix());
  157. }
  158. // Fog
  159. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  160. // Sky
  161. this._effect.setFloat("luminance", this.luminance);
  162. this._effect.setFloat("turbidity", this.turbidity);
  163. this._effect.setFloat("rayleigh", this.rayleigh);
  164. this._effect.setFloat("mieCoefficient", this.mieCoefficient);
  165. this._effect.setFloat("mieDirectionalG", this.mieDirectionalG);
  166. var theta = Math.PI * (this.inclination - 0.5);
  167. var phi = 2 * Math.PI * (this.azimuth - 0.5);
  168. this._sunPosition.x = this.distance * Math.cos( phi );
  169. this._sunPosition.y = this.distance * Math.sin( phi ) * Math.sin( theta );
  170. this._sunPosition.z = this.distance * Math.sin( phi ) * Math.cos( theta );
  171. this._effect.setVector3("sunPosition", this._sunPosition);
  172. super.bind(world, mesh);
  173. }
  174. public getAnimatables(): IAnimatable[] {
  175. return [];
  176. }
  177. public dispose(forceDisposeEffect?: boolean): void {
  178. super.dispose(forceDisposeEffect);
  179. }
  180. public clone(name: string): SkyMaterial {
  181. return SerializationHelper.Clone<SkyMaterial>(() => new SkyMaterial(name, this.getScene()), this);
  182. }
  183. public serialize(): any {
  184. var serializationObject = SerializationHelper.Serialize(this);
  185. serializationObject.customType = "BABYLON.SkyMaterial";
  186. return serializationObject;
  187. }
  188. // Statics
  189. public static Parse(source: any, scene: Scene, rootUrl: string): SkyMaterial {
  190. return SerializationHelper.Parse(() => new SkyMaterial(source.name, scene), source, scene, rootUrl);
  191. }
  192. }
  193. }