babylon.skyMaterial.ts 9.9 KB

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