babylon.skyMaterial.ts 9.5 KB

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