babylon.skyMaterial.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
  4. switch (arguments.length) {
  5. case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
  6. case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
  7. case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
  8. }
  9. };
  10. var BABYLON;
  11. (function (BABYLON) {
  12. var SkyMaterialDefines = (function (_super) {
  13. __extends(SkyMaterialDefines, _super);
  14. function SkyMaterialDefines() {
  15. _super.call(this);
  16. this.CLIPPLANE = false;
  17. this.POINTSIZE = false;
  18. this.FOG = false;
  19. this.VERTEXCOLOR = false;
  20. this.VERTEXALPHA = false;
  21. this._keys = Object.keys(this);
  22. }
  23. return SkyMaterialDefines;
  24. })(BABYLON.MaterialDefines);
  25. var SkyMaterial = (function (_super) {
  26. __extends(SkyMaterial, _super);
  27. function SkyMaterial(name, scene) {
  28. _super.call(this, name, scene);
  29. // Public members
  30. this.luminance = 1.0;
  31. this.turbidity = 10.0;
  32. this.rayleigh = 2.0;
  33. this.mieCoefficient = 0.005;
  34. this.mieDirectionalG = 0.8;
  35. this.distance = 500;
  36. this.inclination = 0.49;
  37. this.azimuth = 0.25;
  38. this.sunPosition = new BABYLON.Vector3(0, 100, 0);
  39. this.useSunPosition = false;
  40. // Private members
  41. this._cameraPosition = BABYLON.Vector3.Zero();
  42. this._defines = new SkyMaterialDefines();
  43. this._cachedDefines = new SkyMaterialDefines();
  44. }
  45. SkyMaterial.prototype.needAlphaBlending = function () {
  46. return (this.alpha < 1.0);
  47. };
  48. SkyMaterial.prototype.needAlphaTesting = function () {
  49. return false;
  50. };
  51. SkyMaterial.prototype.getAlphaTestTexture = function () {
  52. return null;
  53. };
  54. // Methods
  55. SkyMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  56. if (!mesh) {
  57. return true;
  58. }
  59. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  60. return true;
  61. }
  62. return false;
  63. };
  64. SkyMaterial.prototype.isReady = function (mesh, useInstances) {
  65. if (this.checkReadyOnlyOnce) {
  66. if (this._wasPreviouslyReady) {
  67. return true;
  68. }
  69. }
  70. var scene = this.getScene();
  71. if (!this.checkReadyOnEveryCall) {
  72. if (this._renderId === scene.getRenderId()) {
  73. if (this._checkCache(scene, mesh, useInstances)) {
  74. return true;
  75. }
  76. }
  77. }
  78. var engine = scene.getEngine();
  79. this._defines.reset();
  80. // Effect
  81. if (scene.clipPlane) {
  82. this._defines.CLIPPLANE = true;
  83. }
  84. // Point size
  85. if (this.pointsCloud || scene.forcePointsCloud) {
  86. this._defines.POINTSIZE = true;
  87. }
  88. // Fog
  89. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  90. this._defines.FOG = true;
  91. }
  92. // Attribs
  93. if (mesh) {
  94. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  95. this._defines.VERTEXCOLOR = true;
  96. if (mesh.hasVertexAlpha) {
  97. this._defines.VERTEXALPHA = true;
  98. }
  99. }
  100. }
  101. // Get correct effect
  102. if (!this._defines.isEqual(this._cachedDefines) || !this._effect) {
  103. this._defines.cloneTo(this._cachedDefines);
  104. scene.resetCachedMaterial();
  105. // Fallbacks
  106. var fallbacks = new BABYLON.EffectFallbacks();
  107. if (this._defines.FOG) {
  108. fallbacks.addFallback(1, "FOG");
  109. }
  110. //Attributes
  111. var attribs = [BABYLON.VertexBuffer.PositionKind];
  112. if (this._defines.VERTEXCOLOR) {
  113. attribs.push(BABYLON.VertexBuffer.ColorKind);
  114. }
  115. // Legacy browser patch
  116. var shaderName = "sky";
  117. var join = this._defines.toString();
  118. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "viewProjection", "view",
  119. "vFogInfos", "vFogColor", "pointSize", "vClipPlane",
  120. "luminance", "turbidity", "rayleigh", "mieCoefficient", "mieDirectionalG", "sunPosition",
  121. "cameraPosition"
  122. ], [], join, fallbacks, this.onCompiled, this.onError);
  123. }
  124. if (!this._effect.isReady()) {
  125. return false;
  126. }
  127. this._renderId = scene.getRenderId();
  128. this._wasPreviouslyReady = true;
  129. if (mesh) {
  130. if (!mesh._materialDefines) {
  131. mesh._materialDefines = new SkyMaterialDefines();
  132. }
  133. this._defines.cloneTo(mesh._materialDefines);
  134. }
  135. return true;
  136. };
  137. SkyMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  138. this._effect.setMatrix("world", world);
  139. };
  140. SkyMaterial.prototype.bind = function (world, mesh) {
  141. var scene = this.getScene();
  142. // Matrices
  143. this.bindOnlyWorldMatrix(world);
  144. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  145. if (scene.getCachedMaterial() !== this) {
  146. // Clip plane
  147. if (scene.clipPlane) {
  148. var clipPlane = scene.clipPlane;
  149. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  150. }
  151. // Point size
  152. if (this.pointsCloud) {
  153. this._effect.setFloat("pointSize", this.pointSize);
  154. }
  155. }
  156. // View
  157. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  158. this._effect.setMatrix("view", scene.getViewMatrix());
  159. }
  160. // Fog
  161. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  162. // Sky
  163. var camera = scene.activeCamera;
  164. if (camera) {
  165. var cameraWorldMatrix = camera.getWorldMatrix();
  166. this._cameraPosition.x = cameraWorldMatrix.m[12];
  167. this._cameraPosition.y = cameraWorldMatrix.m[13];
  168. this._cameraPosition.z = cameraWorldMatrix.m[14];
  169. this._effect.setVector3("cameraPosition", this._cameraPosition);
  170. }
  171. this._effect.setFloat("luminance", this.luminance);
  172. this._effect.setFloat("turbidity", this.turbidity);
  173. this._effect.setFloat("rayleigh", this.rayleigh);
  174. this._effect.setFloat("mieCoefficient", this.mieCoefficient);
  175. this._effect.setFloat("mieDirectionalG", this.mieDirectionalG);
  176. if (!this.useSunPosition) {
  177. var theta = Math.PI * (this.inclination - 0.5);
  178. var phi = 2 * Math.PI * (this.azimuth - 0.5);
  179. this.sunPosition.x = this.distance * Math.cos(phi);
  180. this.sunPosition.y = this.distance * Math.sin(phi) * Math.sin(theta);
  181. this.sunPosition.z = this.distance * Math.sin(phi) * Math.cos(theta);
  182. }
  183. this._effect.setVector3("sunPosition", this.sunPosition);
  184. _super.prototype.bind.call(this, world, mesh);
  185. };
  186. SkyMaterial.prototype.getAnimatables = function () {
  187. return [];
  188. };
  189. SkyMaterial.prototype.dispose = function (forceDisposeEffect) {
  190. _super.prototype.dispose.call(this, forceDisposeEffect);
  191. };
  192. SkyMaterial.prototype.clone = function (name) {
  193. var _this = this;
  194. return BABYLON.SerializationHelper.Clone(function () { return new SkyMaterial(name, _this.getScene()); }, this);
  195. };
  196. SkyMaterial.prototype.serialize = function () {
  197. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  198. serializationObject.customType = "BABYLON.SkyMaterial";
  199. return serializationObject;
  200. };
  201. // Statics
  202. SkyMaterial.Parse = function (source, scene, rootUrl) {
  203. return BABYLON.SerializationHelper.Parse(function () { return new SkyMaterial(source.name, scene); }, source, scene, rootUrl);
  204. };
  205. __decorate([
  206. BABYLON.serialize()
  207. ], SkyMaterial.prototype, "luminance");
  208. __decorate([
  209. BABYLON.serialize()
  210. ], SkyMaterial.prototype, "turbidity");
  211. __decorate([
  212. BABYLON.serialize()
  213. ], SkyMaterial.prototype, "rayleigh");
  214. __decorate([
  215. BABYLON.serialize()
  216. ], SkyMaterial.prototype, "mieCoefficient");
  217. __decorate([
  218. BABYLON.serialize()
  219. ], SkyMaterial.prototype, "mieDirectionalG");
  220. __decorate([
  221. BABYLON.serialize()
  222. ], SkyMaterial.prototype, "distance");
  223. __decorate([
  224. BABYLON.serialize()
  225. ], SkyMaterial.prototype, "inclination");
  226. __decorate([
  227. BABYLON.serialize()
  228. ], SkyMaterial.prototype, "azimuth");
  229. __decorate([
  230. BABYLON.serializeAsVector3()
  231. ], SkyMaterial.prototype, "sunPosition");
  232. __decorate([
  233. BABYLON.serialize()
  234. ], SkyMaterial.prototype, "useSunPosition");
  235. return SkyMaterial;
  236. })(BABYLON.Material);
  237. BABYLON.SkyMaterial = SkyMaterial;
  238. })(BABYLON || (BABYLON = {}));
  239. BABYLON.Effect.ShadersStore['skyVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef VERTEXCOLOR\nattribute vec4 color;\n#endif\n\nuniform mat4 world;\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\nvoid main(void) {\ngl_Position=viewProjection*world*vec4(position,1.0);\nvec4 worldPos=world*vec4(position,1.0);\nvPositionW=vec3(worldPos);\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  240. BABYLON.Effect.ShadersStore['skyPixelShader'] = "precision highp float;\n\nvarying vec3 vPositionW;\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneFragmentDeclaration>\n\nuniform vec3 cameraPosition;\nuniform float luminance;\nuniform float turbidity;\nuniform float rayleigh;\nuniform float mieCoefficient;\nuniform float mieDirectionalG;\nuniform vec3 sunPosition;\n\n#include<fogFragmentDeclaration>\n\nconst float e=2.71828182845904523536028747135266249775724709369995957;\nconst float pi=3.141592653589793238462643383279502884197169;\nconst float n=1.0003;\nconst float N=2.545E25;\nconst float pn=0.035;\nconst vec3 lambda=vec3(680E-9,550E-9,450E-9);\nconst vec3 K=vec3(0.686,0.678,0.666);\nconst float v=4.0;\nconst float rayleighZenithLength=8.4E3;\nconst float mieZenithLength=1.25E3;\nconst vec3 up=vec3(0.0,1.0,0.0);\nconst float EE=1000.0;\nconst float sunAngularDiameterCos=0.999956676946448443553574619906976478926848692873900859324;\nconst float cutoffAngle=pi/1.95;\nconst float steepness=1.5;\nvec3 totalRayleigh(vec3 lambda)\n{\nreturn (8.0*pow(pi,3.0)*pow(pow(n,2.0)-1.0,2.0)*(6.0+3.0*pn))/(3.0*N*pow(lambda,vec3(4.0))*(6.0-7.0*pn));\n}\nvec3 simplifiedRayleigh()\n{\nreturn 0.0005/vec3(94,40,18);\n}\nfloat rayleighPhase(float cosTheta)\n{ \nreturn (3.0/(16.0*pi))*(1.0+pow(cosTheta,2.0));\n}\nvec3 totalMie(vec3 lambda,vec3 K,float T)\n{\nfloat c=(0.2*T )*10E-18;\nreturn 0.434*c*pi*pow((2.0*pi)/lambda,vec3(v-2.0))*K;\n}\nfloat hgPhase(float cosTheta,float g)\n{\nreturn (1.0/(4.0*pi))*((1.0-pow(g,2.0))/pow(1.0-2.0*g*cosTheta+pow(g,2.0),1.5));\n}\nfloat sunIntensity(float zenithAngleCos)\n{\nreturn EE*max(0.0,1.0-exp(-((cutoffAngle-acos(zenithAngleCos))/steepness)));\n}\nfloat A=0.15;\nfloat B=0.50;\nfloat C=0.10;\nfloat D=0.20;\nfloat EEE=0.02;\nfloat F=0.30;\nfloat W=1000.0;\nvec3 Uncharted2Tonemap(vec3 x)\n{\nreturn ((x*(A*x+C*B)+D*EEE)/(x*(A*x+B)+D*F))-EEE/F;\n}\nvoid main(void) {\n\n#include<clipPlaneFragment>\n\nfloat sunfade=1.0-clamp(1.0-exp((sunPosition.y/450000.0)),0.0,1.0);\nfloat rayleighCoefficient=rayleigh-(1.0*(1.0-sunfade));\nvec3 sunDirection=normalize(sunPosition);\nfloat sunE=sunIntensity(dot(sunDirection,up));\nvec3 betaR=simplifiedRayleigh()*rayleighCoefficient;\nvec3 betaM=totalMie(lambda,K,turbidity)*mieCoefficient;\nfloat zenithAngle=acos(max(0.0,dot(up,normalize(vPositionW-cameraPosition))));\nfloat sR=rayleighZenithLength/(cos(zenithAngle)+0.15*pow(93.885-((zenithAngle*180.0)/pi),-1.253));\nfloat sM=mieZenithLength/(cos(zenithAngle)+0.15*pow(93.885-((zenithAngle*180.0)/pi),-1.253));\nvec3 Fex=exp(-(betaR*sR+betaM*sM));\nfloat cosTheta=dot(normalize(vPositionW-cameraPosition),sunDirection);\nfloat rPhase=rayleighPhase(cosTheta*0.5+0.5);\nvec3 betaRTheta=betaR*rPhase;\nfloat mPhase=hgPhase(cosTheta,mieDirectionalG);\nvec3 betaMTheta=betaM*mPhase;\nvec3 Lin=pow(sunE*((betaRTheta+betaMTheta)/(betaR+betaM))*(1.0-Fex),vec3(1.5));\nLin*=mix(vec3(1.0),pow(sunE*((betaRTheta+betaMTheta)/(betaR+betaM))*Fex,vec3(1.0/2.0)),clamp(pow(1.0-dot(up,sunDirection),5.0),0.0,1.0));\nvec3 direction=normalize(vPositionW-cameraPosition);\nfloat theta=acos(direction.y);\nfloat phi=atan(direction.z,direction.x);\nvec2 uv=vec2(phi,theta)/vec2(2.0*pi,pi)+vec2(0.5,0.0);\nvec3 L0=vec3(0.1)*Fex;\nfloat sundisk=smoothstep(sunAngularDiameterCos,sunAngularDiameterCos+0.00002,cosTheta);\nL0+=(sunE*19000.0*Fex)*sundisk;\nvec3 whiteScale=1.0/Uncharted2Tonemap(vec3(W));\nvec3 texColor=(Lin+L0); \ntexColor*=0.04 ;\ntexColor+=vec3(0.0,0.001,0.0025)*0.3;\nfloat g_fMaxLuminance=1.0;\nfloat fLumScaled=0.1/luminance; \nfloat fLumCompressed=(fLumScaled*(1.0+(fLumScaled/(g_fMaxLuminance*g_fMaxLuminance))))/(1.0+fLumScaled); \nfloat ExposureBias=fLumCompressed;\nvec3 curr=Uncharted2Tonemap((log2(2.0/pow(luminance,4.0)))*texColor);\nvec3 skyColor=curr*whiteScale;\nvec3 retColor=pow(skyColor,vec3(1.0/(1.2+(1.2*sunfade))));\nvec4 baseColor=vec4(retColor,1.0);\n\n\nfloat alpha=1.0;\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\nvec3 diffuseBase=vec3(1.0,1.0,1.0);\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\n\nvec4 color=vec4(baseColor.rgb,alpha);\n\n#include<fogFragment>\ngl_FragColor=color;\n}";