babylon.skyMaterial.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  13. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  14. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  15. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  16. return c > 3 && r && Object.defineProperty(target, key, r), r;
  17. };
  18. var BABYLON;
  19. (function (BABYLON) {
  20. var SkyMaterialDefines = (function (_super) {
  21. __extends(SkyMaterialDefines, _super);
  22. function SkyMaterialDefines() {
  23. var _this = _super.call(this) || this;
  24. _this.CLIPPLANE = false;
  25. _this.POINTSIZE = false;
  26. _this.FOG = false;
  27. _this.VERTEXCOLOR = false;
  28. _this.VERTEXALPHA = false;
  29. _this.rebuild();
  30. return _this;
  31. }
  32. return SkyMaterialDefines;
  33. }(BABYLON.MaterialDefines));
  34. var SkyMaterial = (function (_super) {
  35. __extends(SkyMaterial, _super);
  36. function SkyMaterial(name, scene) {
  37. var _this = _super.call(this, name, scene) || this;
  38. // Public members
  39. _this.luminance = 1.0;
  40. _this.turbidity = 10.0;
  41. _this.rayleigh = 2.0;
  42. _this.mieCoefficient = 0.005;
  43. _this.mieDirectionalG = 0.8;
  44. _this.distance = 500;
  45. _this.inclination = 0.49;
  46. _this.azimuth = 0.25;
  47. _this.sunPosition = new BABYLON.Vector3(0, 100, 0);
  48. _this.useSunPosition = false;
  49. // Private members
  50. _this._cameraPosition = BABYLON.Vector3.Zero();
  51. return _this;
  52. }
  53. SkyMaterial.prototype.needAlphaBlending = function () {
  54. return (this.alpha < 1.0);
  55. };
  56. SkyMaterial.prototype.needAlphaTesting = function () {
  57. return false;
  58. };
  59. SkyMaterial.prototype.getAlphaTestTexture = function () {
  60. return null;
  61. };
  62. // Methods
  63. SkyMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  64. if (this.isFrozen) {
  65. if (this._wasPreviouslyReady && subMesh.effect) {
  66. return true;
  67. }
  68. }
  69. if (!subMesh._materialDefines) {
  70. subMesh._materialDefines = new SkyMaterialDefines();
  71. }
  72. var defines = subMesh._materialDefines;
  73. var scene = this.getScene();
  74. if (!this.checkReadyOnEveryCall) {
  75. if (this._renderId === scene.getRenderId()) {
  76. return true;
  77. }
  78. }
  79. var engine = scene.getEngine();
  80. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, defines);
  81. // Attribs
  82. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, false);
  83. // Get correct effect
  84. if (defines.isDirty) {
  85. defines.markAsProcessed();
  86. scene.resetCachedMaterial();
  87. // Fallbacks
  88. var fallbacks = new BABYLON.EffectFallbacks();
  89. if (defines.FOG) {
  90. fallbacks.addFallback(1, "FOG");
  91. }
  92. //Attributes
  93. var attribs = [BABYLON.VertexBuffer.PositionKind];
  94. if (defines.VERTEXCOLOR) {
  95. attribs.push(BABYLON.VertexBuffer.ColorKind);
  96. }
  97. var shaderName = "sky";
  98. var join = defines.toString();
  99. subMesh.setEffect(scene.getEngine().createEffect(shaderName, attribs, ["world", "viewProjection", "view",
  100. "vFogInfos", "vFogColor", "pointSize", "vClipPlane",
  101. "luminance", "turbidity", "rayleigh", "mieCoefficient", "mieDirectionalG", "sunPosition",
  102. "cameraPosition"
  103. ], [], join, fallbacks, this.onCompiled, this.onError), defines);
  104. }
  105. if (!subMesh.effect.isReady()) {
  106. return false;
  107. }
  108. this._renderId = scene.getRenderId();
  109. this._wasPreviouslyReady = true;
  110. return true;
  111. };
  112. SkyMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  113. var scene = this.getScene();
  114. var defines = subMesh._materialDefines;
  115. if (!defines) {
  116. return;
  117. }
  118. var effect = subMesh.effect;
  119. this._activeEffect = effect;
  120. // Matrices
  121. this.bindOnlyWorldMatrix(world);
  122. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  123. if (this._mustRebind(scene, effect)) {
  124. // Clip plane
  125. if (scene.clipPlane) {
  126. var clipPlane = scene.clipPlane;
  127. this._activeEffect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  128. }
  129. // Point size
  130. if (this.pointsCloud) {
  131. this._activeEffect.setFloat("pointSize", this.pointSize);
  132. }
  133. }
  134. // View
  135. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  136. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  137. }
  138. // Fog
  139. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  140. // Sky
  141. var camera = scene.activeCamera;
  142. if (camera) {
  143. var cameraWorldMatrix = camera.getWorldMatrix();
  144. this._cameraPosition.x = cameraWorldMatrix.m[12];
  145. this._cameraPosition.y = cameraWorldMatrix.m[13];
  146. this._cameraPosition.z = cameraWorldMatrix.m[14];
  147. this._activeEffect.setVector3("cameraPosition", this._cameraPosition);
  148. }
  149. if (this.luminance > 0) {
  150. this._activeEffect.setFloat("luminance", this.luminance);
  151. }
  152. this._activeEffect.setFloat("turbidity", this.turbidity);
  153. this._activeEffect.setFloat("rayleigh", this.rayleigh);
  154. this._activeEffect.setFloat("mieCoefficient", this.mieCoefficient);
  155. this._activeEffect.setFloat("mieDirectionalG", this.mieDirectionalG);
  156. if (!this.useSunPosition) {
  157. var theta = Math.PI * (this.inclination - 0.5);
  158. var phi = 2 * Math.PI * (this.azimuth - 0.5);
  159. this.sunPosition.x = this.distance * Math.cos(phi);
  160. this.sunPosition.y = this.distance * Math.sin(phi) * Math.sin(theta);
  161. this.sunPosition.z = this.distance * Math.sin(phi) * Math.cos(theta);
  162. }
  163. this._activeEffect.setVector3("sunPosition", this.sunPosition);
  164. this._afterBind(mesh, this._activeEffect);
  165. };
  166. SkyMaterial.prototype.getAnimatables = function () {
  167. return [];
  168. };
  169. SkyMaterial.prototype.dispose = function (forceDisposeEffect) {
  170. _super.prototype.dispose.call(this, forceDisposeEffect);
  171. };
  172. SkyMaterial.prototype.clone = function (name) {
  173. var _this = this;
  174. return BABYLON.SerializationHelper.Clone(function () { return new SkyMaterial(name, _this.getScene()); }, this);
  175. };
  176. SkyMaterial.prototype.serialize = function () {
  177. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  178. serializationObject.customType = "BABYLON.SkyMaterial";
  179. return serializationObject;
  180. };
  181. // Statics
  182. SkyMaterial.Parse = function (source, scene, rootUrl) {
  183. return BABYLON.SerializationHelper.Parse(function () { return new SkyMaterial(source.name, scene); }, source, scene, rootUrl);
  184. };
  185. return SkyMaterial;
  186. }(BABYLON.PushMaterial));
  187. __decorate([
  188. BABYLON.serialize()
  189. ], SkyMaterial.prototype, "luminance", void 0);
  190. __decorate([
  191. BABYLON.serialize()
  192. ], SkyMaterial.prototype, "turbidity", void 0);
  193. __decorate([
  194. BABYLON.serialize()
  195. ], SkyMaterial.prototype, "rayleigh", void 0);
  196. __decorate([
  197. BABYLON.serialize()
  198. ], SkyMaterial.prototype, "mieCoefficient", void 0);
  199. __decorate([
  200. BABYLON.serialize()
  201. ], SkyMaterial.prototype, "mieDirectionalG", void 0);
  202. __decorate([
  203. BABYLON.serialize()
  204. ], SkyMaterial.prototype, "distance", void 0);
  205. __decorate([
  206. BABYLON.serialize()
  207. ], SkyMaterial.prototype, "inclination", void 0);
  208. __decorate([
  209. BABYLON.serialize()
  210. ], SkyMaterial.prototype, "azimuth", void 0);
  211. __decorate([
  212. BABYLON.serializeAsVector3()
  213. ], SkyMaterial.prototype, "sunPosition", void 0);
  214. __decorate([
  215. BABYLON.serialize()
  216. ], SkyMaterial.prototype, "useSunPosition", void 0);
  217. BABYLON.SkyMaterial = SkyMaterial;
  218. })(BABYLON || (BABYLON = {}));
  219. //# sourceMappingURL=babylon.skyMaterial.js.map
  220. 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";
  221. 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);\n\n\n\nvec3 retColor=curr*whiteScale;\n\n\nfloat alpha=1.0;\n#ifdef VERTEXCOLOR\nretColor.rgb*=vColor.rgb;\n#endif\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\n\nvec4 color=clamp(vec4(retColor.rgb,alpha),0.0,1.0);\n\n#include<fogFragment>\ngl_FragColor=color;\n}";