babylon.skyMaterial.ts 9.0 KB

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