123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- /// <reference path="../../../dist/preview release/babylon.d.ts"/>
- module BABYLON {
- class SkyMaterialDefines extends MaterialDefines {
- public CLIPPLANE = false;
- public POINTSIZE = false;
- public FOG = false;
- public VERTEXCOLOR = false;
- public VERTEXALPHA = false;
- constructor() {
- super();
- this.rebuild();
- }
- }
-
- export class SkyMaterial extends PushMaterial {
- // Public members
- @serialize()
- public luminance: number = 1.0;
-
- @serialize()
- public turbidity: number = 10.0;
-
- @serialize()
- public rayleigh: number = 2.0;
-
- @serialize()
- public mieCoefficient: number = 0.005;
-
- @serialize()
- public mieDirectionalG: number = 0.8;
-
- @serialize()
- public distance: number = 500;
-
- @serialize()
- public inclination: number = 0.49;
-
- @serialize()
- public azimuth: number = 0.25;
-
- @serializeAsVector3()
- public sunPosition: Vector3 = new Vector3(0, 100, 0);
-
- @serialize()
- public useSunPosition: boolean = false;
-
- // Private members
- private _cameraPosition: Vector3 = Vector3.Zero();
-
- private _renderId: number;
- constructor(name: string, scene: Scene) {
- super(name, scene);
- }
- public needAlphaBlending(): boolean {
- return (this.alpha < 1.0);
- }
- public needAlphaTesting(): boolean {
- return false;
- }
- public getAlphaTestTexture(): Nullable<BaseTexture> {
- return null;
- }
- // Methods
- public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
- if (this.isFrozen) {
- if (this._wasPreviouslyReady && subMesh.effect) {
- return true;
- }
- }
- if (!subMesh._materialDefines) {
- subMesh._materialDefines = new SkyMaterialDefines();
- }
- var defines = <SkyMaterialDefines>subMesh._materialDefines;
- var scene = this.getScene();
- if (!this.checkReadyOnEveryCall && subMesh.effect) {
- if (this._renderId === scene.getRenderId()) {
- return true;
- }
- }
- MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, false, defines);
-
- // Attribs
- MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, false);
- // Get correct effect
- if (defines.isDirty) {
- defines.markAsProcessed();
-
- scene.resetCachedMaterial();
-
- // Fallbacks
- var fallbacks = new EffectFallbacks();
- if (defines.FOG) {
- fallbacks.addFallback(1, "FOG");
- }
-
- //Attributes
- var attribs = [VertexBuffer.PositionKind];
- if (defines.VERTEXCOLOR) {
- attribs.push(VertexBuffer.ColorKind);
- }
- var shaderName = "sky";
-
- var join = defines.toString();
- subMesh.setEffect(scene.getEngine().createEffect(shaderName,
- attribs,
- ["world", "viewProjection", "view",
- "vFogInfos", "vFogColor", "pointSize", "vClipPlane",
- "luminance", "turbidity", "rayleigh", "mieCoefficient", "mieDirectionalG", "sunPosition",
- "cameraPosition"
- ],
- [],
- join, fallbacks, this.onCompiled, this.onError), defines);
- }
-
- if (!subMesh.effect || !subMesh.effect.isReady()) {
- return false;
- }
- this._renderId = scene.getRenderId();
- this._wasPreviouslyReady = true;
- return true;
- }
- public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
- var scene = this.getScene();
- var defines = <SkyMaterialDefines>subMesh._materialDefines;
- if (!defines) {
- return;
- }
- var effect = subMesh.effect;
- if (!effect) {
- return;
- }
- this._activeEffect = effect;
- // Matrices
- this.bindOnlyWorldMatrix(world);
- this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
- if (this._mustRebind(scene, effect)) {
- // Clip plane
- if (scene.clipPlane) {
- var clipPlane = scene.clipPlane;
- this._activeEffect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
- }
- // Point size
- if (this.pointsCloud) {
- this._activeEffect.setFloat("pointSize", this.pointSize);
- }
- }
- // View
- if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
- this._activeEffect.setMatrix("view", scene.getViewMatrix());
- }
-
- // Fog
- MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
-
- // Sky
- var camera = scene.activeCamera;
- if (camera) {
- var cameraWorldMatrix = camera.getWorldMatrix();
- this._cameraPosition.x = cameraWorldMatrix.m[12];
- this._cameraPosition.y = cameraWorldMatrix.m[13];
- this._cameraPosition.z = cameraWorldMatrix.m[14];
- this._activeEffect.setVector3("cameraPosition", this._cameraPosition);
- }
-
- if (this.luminance > 0) {
- this._activeEffect.setFloat("luminance", this.luminance);
- }
-
- this._activeEffect.setFloat("turbidity", this.turbidity);
- this._activeEffect.setFloat("rayleigh", this.rayleigh);
- this._activeEffect.setFloat("mieCoefficient", this.mieCoefficient);
- this._activeEffect.setFloat("mieDirectionalG", this.mieDirectionalG);
-
- if (!this.useSunPosition) {
- var theta = Math.PI * (this.inclination - 0.5);
- var phi = 2 * Math.PI * (this.azimuth - 0.5);
-
- this.sunPosition.x = this.distance * Math.cos(phi);
- this.sunPosition.y = this.distance * Math.sin(phi) * Math.sin(theta);
- this.sunPosition.z = this.distance * Math.sin(phi) * Math.cos(theta);
- }
-
- this._activeEffect.setVector3("sunPosition", this.sunPosition);
- this._afterBind(mesh, this._activeEffect);
- }
- public getAnimatables(): IAnimatable[] {
- return [];
- }
- public dispose(forceDisposeEffect?: boolean): void {
- super.dispose(forceDisposeEffect);
- }
- public clone(name: string): SkyMaterial {
- return SerializationHelper.Clone<SkyMaterial>(() => new SkyMaterial(name, this.getScene()), this);
- }
-
- public serialize(): any {
- var serializationObject = SerializationHelper.Serialize(this);
- serializationObject.customType = "BABYLON.SkyMaterial";
- return serializationObject;
- }
- public getClassName(): string {
- return "SkyMaterial";
- }
- // Statics
- public static Parse(source: any, scene: Scene, rootUrl: string): SkyMaterial {
- return SerializationHelper.Parse(() => new SkyMaterial(source.name, scene), source, scene, rootUrl);
- }
- }
- }
|