mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
[Feature] Added global fog (#6675)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -132,7 +132,10 @@ export class UBOGlobal {
|
||||
public static MAIN_LIT_COLOR_OFFSET: number = UBOGlobal.MAIN_LIT_DIR_OFFSET + 4;
|
||||
public static AMBIENT_SKY_OFFSET: number = UBOGlobal.MAIN_LIT_COLOR_OFFSET + 4;
|
||||
public static AMBIENT_GROUND_OFFSET: number = UBOGlobal.AMBIENT_SKY_OFFSET + 4;
|
||||
public static COUNT: number = UBOGlobal.AMBIENT_GROUND_OFFSET + 4;
|
||||
public static GLOBAL_FOG_COLOR_OFFSET: number = UBOGlobal.AMBIENT_GROUND_OFFSET + 4;
|
||||
public static GLOBAL_FOG_BASE_OFFSET: number = UBOGlobal.GLOBAL_FOG_COLOR_OFFSET + 4;
|
||||
public static GLOBAL_FOG_ADD_OFFSET: number = UBOGlobal.GLOBAL_FOG_BASE_OFFSET + 4;
|
||||
public static COUNT: number = UBOGlobal.GLOBAL_FOG_ADD_OFFSET + 4;
|
||||
public static SIZE: number = UBOGlobal.COUNT * 4;
|
||||
|
||||
public static BLOCK: GFXUniformBlock = {
|
||||
@@ -153,6 +156,9 @@ export class UBOGlobal {
|
||||
{ name: 'cc_mainLitColor', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_ambientSky', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_ambientGround', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_fogColor', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_fogBase', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_fogAdd', type: GFXType.FLOAT4, count: 1 }
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
@@ -510,6 +510,7 @@ export abstract class RenderPipeline {
|
||||
|
||||
const mainLight = scene.mainLight;
|
||||
const ambient = scene.ambient;
|
||||
const fog = scene.fog;
|
||||
const fv = this._uboGlobal.view;
|
||||
|
||||
// update UBOGlobal
|
||||
@@ -576,6 +577,16 @@ export abstract class RenderPipeline {
|
||||
this._uboGlobal.view.set(skyColor, UBOGlobal.AMBIENT_SKY_OFFSET);
|
||||
|
||||
this._uboGlobal.view.set(ambient.groundAlbedo, UBOGlobal.AMBIENT_GROUND_OFFSET);
|
||||
|
||||
this._uboGlobal.view.set(fog.fogColor, UBOGlobal.GLOBAL_FOG_COLOR_OFFSET);
|
||||
|
||||
fv[UBOGlobal.GLOBAL_FOG_BASE_OFFSET] = fog.fogStart;
|
||||
fv[UBOGlobal.GLOBAL_FOG_BASE_OFFSET + 1] = fog.fogEnd;
|
||||
fv[UBOGlobal.GLOBAL_FOG_BASE_OFFSET + 2] = fog.fogDensity;
|
||||
|
||||
fv[UBOGlobal.GLOBAL_FOG_ADD_OFFSET] = fog.fogTop;
|
||||
fv[UBOGlobal.GLOBAL_FOG_ADD_OFFSET + 1] = fog.fogRange;
|
||||
fv[UBOGlobal.GLOBAL_FOG_ADD_OFFSET + 2] = fog.fogAtten;
|
||||
|
||||
// update ubos
|
||||
this._globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!.update(this._uboGlobal.view);
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
import { RenderScene } from './render-scene';
|
||||
import { Enum } from '../../value-types';
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 全局雾类型。
|
||||
* @en
|
||||
* The global fog type
|
||||
* @static
|
||||
* @enum FogInfo.FogType
|
||||
*/
|
||||
export const FogType = Enum({
|
||||
/**
|
||||
* @zh
|
||||
* 线性雾。
|
||||
* @en
|
||||
* Linear fog
|
||||
* @property LINEAR
|
||||
* @readonly
|
||||
*/
|
||||
LINEAR: 0,
|
||||
/**
|
||||
* @zh
|
||||
* 指数雾。
|
||||
* @en
|
||||
* Exponential fog
|
||||
* @property EXP
|
||||
* @readonly
|
||||
*/
|
||||
EXP: 1,
|
||||
/**
|
||||
* @zh
|
||||
* 指数平方雾。
|
||||
* @en
|
||||
* Exponential square fog
|
||||
* @property EXP_SQUARED
|
||||
* @readonly
|
||||
*/
|
||||
EXP_SQUARED: 2,
|
||||
/**
|
||||
* @zh
|
||||
* 层叠雾。
|
||||
* @en
|
||||
* Layered fog
|
||||
* @property LAYERED
|
||||
* @readonly
|
||||
*/
|
||||
LAYERED: 3,
|
||||
});
|
||||
|
||||
export class Fog {
|
||||
protected _enabled = false;
|
||||
protected _fogColor = Float32Array.from([0.6, 0.6, 0.6, 1.0]);
|
||||
protected _fogDensity = 0.3;
|
||||
protected _type = FogType.LINEAR;
|
||||
protected _fogStart = 0;
|
||||
protected _fogEnd = 300;
|
||||
protected _fogAtten = 5;
|
||||
protected _fogTop = 1.5;
|
||||
protected _fogRange = 1.2;
|
||||
protected _scene: RenderScene;
|
||||
|
||||
private _currType = 0;
|
||||
/**
|
||||
* @zh
|
||||
* 当前雾化类型。
|
||||
* @returns {FogType}
|
||||
* 返回当前全局雾类型
|
||||
* - 0:不使用雾化
|
||||
* - 1:使用线性雾
|
||||
* - 2:使用指数雾
|
||||
* - 3:使用指数平方雾
|
||||
* - 4:使用层叠雾
|
||||
* @en
|
||||
* The current global fog type.
|
||||
* @returns {FogType}
|
||||
* Returns the current global fog type
|
||||
* - 0:Disable global Fog
|
||||
* - 1:Linear fog
|
||||
* - 2:Exponential fog
|
||||
* - 3:Exponential square fog
|
||||
* - 4:Layered fog
|
||||
*/
|
||||
get currType() {
|
||||
return this._currType;
|
||||
}
|
||||
|
||||
constructor (scene: RenderScene) {
|
||||
this._scene = scene;
|
||||
}
|
||||
|
||||
set enabled (val: boolean) {
|
||||
this._enabled = val;
|
||||
if (!val) {
|
||||
this._currType = 0;
|
||||
} else {
|
||||
this._currType = this._type + 1;
|
||||
}
|
||||
this._updatePipeline();
|
||||
}
|
||||
|
||||
get enabled () {
|
||||
return this._enabled;
|
||||
}
|
||||
|
||||
set type(val: any) {
|
||||
this._type = val;
|
||||
if (!this.enabled) {
|
||||
return;
|
||||
}
|
||||
this._currType = val + 1;
|
||||
this._updatePipeline();
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
set fogColor (val: Float32Array) {
|
||||
this._fogColor = val;
|
||||
}
|
||||
|
||||
get fogColor (): Float32Array {
|
||||
return this._fogColor;
|
||||
}
|
||||
|
||||
get fogDensity () {
|
||||
return this._fogDensity;
|
||||
}
|
||||
|
||||
set fogDensity (val) {
|
||||
this._fogDensity = val;
|
||||
}
|
||||
|
||||
get fogStart() {
|
||||
return this._fogStart;
|
||||
}
|
||||
|
||||
protected _updatePipeline () {
|
||||
const value = this._currType;
|
||||
const pipeline = this._scene!.root.pipeline;
|
||||
if (pipeline.macros.CC_USE_FOG === value) { return; }
|
||||
pipeline.macros.CC_USE_FOG = value;
|
||||
this._scene!.onGlobalPipelineStateChanged();
|
||||
}
|
||||
|
||||
set fogStart(val) {
|
||||
this._fogStart = val;
|
||||
}
|
||||
|
||||
get fogEnd() {
|
||||
return this._fogEnd;
|
||||
}
|
||||
|
||||
set fogEnd(val) {
|
||||
this._fogEnd = val;
|
||||
}
|
||||
|
||||
get fogAtten() {
|
||||
return this._fogAtten;
|
||||
}
|
||||
|
||||
set fogAtten(val) {
|
||||
this._fogAtten = val;
|
||||
}
|
||||
|
||||
get fogTop() {
|
||||
return this._fogTop;
|
||||
}
|
||||
|
||||
set fogTop(val) {
|
||||
this._fogTop = val;
|
||||
}
|
||||
|
||||
get fogRange() {
|
||||
return this._fogRange;
|
||||
}
|
||||
|
||||
set fogRange(val) {
|
||||
this._fogRange = val;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import { Skybox } from './skybox';
|
||||
import { SphereLight } from './sphere-light';
|
||||
import { SpotLight } from './spot-light';
|
||||
import { PREVIEW } from 'internal:constants';
|
||||
import { Fog } from './fog';
|
||||
|
||||
export interface IRenderSceneInfo {
|
||||
name: string;
|
||||
@@ -49,6 +50,10 @@ export class RenderScene {
|
||||
return this._ambient;
|
||||
}
|
||||
|
||||
get fog (): Fog {
|
||||
return this._fog;
|
||||
}
|
||||
|
||||
get skybox (): Skybox {
|
||||
return this._skybox;
|
||||
}
|
||||
@@ -120,12 +125,14 @@ export class RenderScene {
|
||||
private _spotLights: SpotLight[] = [];
|
||||
private _mainLight: DirectionalLight | null = null;
|
||||
private _modelId: number = 0;
|
||||
private _fog: Fog;
|
||||
|
||||
constructor (root: Root) {
|
||||
this._root = root;
|
||||
this._ambient = new Ambient(this);
|
||||
this._skybox = new Skybox(this);
|
||||
this._planarShadows = new PlanarShadows(this);
|
||||
this._fog = new Fog(this);
|
||||
}
|
||||
|
||||
public initialize (info: IRenderSceneInfo): boolean {
|
||||
|
||||
@@ -35,6 +35,7 @@ import { Ambient } from '../renderer/scene/ambient';
|
||||
import { PlanarShadows } from '../renderer/scene/planar-shadows';
|
||||
import { RenderScene } from '../renderer/scene/render-scene';
|
||||
import { Skybox } from '../renderer/scene/skybox';
|
||||
import { Fog, FogType } from '../renderer/scene/fog';
|
||||
import { Node } from './node';
|
||||
|
||||
const _up = new Vec3(0, 1, 0);
|
||||
@@ -175,6 +176,212 @@ export class SkyboxInfo {
|
||||
}
|
||||
cc.SkyboxInfo = SkyboxInfo;
|
||||
|
||||
/**
|
||||
* @zh 全局雾相关信息
|
||||
* @en Global fog info
|
||||
*/
|
||||
@ccclass('cc.FogInfo')
|
||||
export class FogInfo {
|
||||
public static FogType = FogType;
|
||||
@property
|
||||
protected _type = FogType.LINEAR;
|
||||
@property
|
||||
protected _fogColor = new Color('#C8C8C8');
|
||||
@property
|
||||
protected _enabled = false;
|
||||
@property
|
||||
protected _fogDensity = 0.3;
|
||||
@property
|
||||
protected _fogStart = 0.5;
|
||||
@property
|
||||
protected _fogEnd = 300;
|
||||
@property
|
||||
protected _fogAtten = 5;
|
||||
@property
|
||||
protected _fogTop = 1.5;
|
||||
@property
|
||||
protected _fogRange = 1.2;
|
||||
protected _resource: Fog | null = null;
|
||||
/**
|
||||
* @zh 是否启用全局雾效
|
||||
* @en Enable global fog
|
||||
*/
|
||||
@property({ type: CCBoolean })
|
||||
set enabled (val: boolean) {
|
||||
this._enabled = val;
|
||||
if (this._resource) { this._resource.enabled = val; }
|
||||
}
|
||||
|
||||
get enabled () {
|
||||
return this._enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 全局雾颜色
|
||||
* @en Global fog color
|
||||
*/
|
||||
@property({ type: Color })
|
||||
set fogColor (val: Color) {
|
||||
this._fogColor.set(val);
|
||||
if (this._resource) { Color.toArray(this._resource.fogColor, this.fogColor); }
|
||||
}
|
||||
|
||||
get fogColor () {
|
||||
return this._fogColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 全局雾类型
|
||||
* @en Global fog type
|
||||
*/
|
||||
@property({
|
||||
type: FogType
|
||||
})
|
||||
get type () {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
set type (val) {
|
||||
this._type = val;
|
||||
if (this._resource) { this._resource.type = val; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 全局雾浓度
|
||||
* @en Global fog density
|
||||
*/
|
||||
@property({
|
||||
type: CCFloat,
|
||||
range: [0, 1],
|
||||
step: 0.01,
|
||||
slide: true,
|
||||
visible: function(this: FogInfo) {
|
||||
return this._type !== FogType.LAYERED && this._type !== FogType.LINEAR;
|
||||
}
|
||||
})
|
||||
get fogDensity () {
|
||||
return this._fogDensity;
|
||||
}
|
||||
|
||||
set fogDensity (val) {
|
||||
this._fogDensity = val;
|
||||
if (this._resource) { this._resource.fogDensity = val; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 雾效起始位置,只适用于线性雾
|
||||
* @en Global fog start position, only for linear fog
|
||||
*/
|
||||
@property({
|
||||
type: CCFloat,
|
||||
step: 0.1,
|
||||
visible: function(this: FogInfo) {
|
||||
return this._type === FogType.LINEAR;
|
||||
}
|
||||
})
|
||||
get fogStart() {
|
||||
return this._fogStart;
|
||||
}
|
||||
|
||||
set fogStart(val) {
|
||||
this._fogStart = val;
|
||||
if (this._resource) { this._resource.fogStart = val; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 雾效结束位置,只适用于线性雾
|
||||
* @en Global fog end position, only for linear fog
|
||||
*/
|
||||
@property({
|
||||
type: CCFloat,
|
||||
step: 0.1,
|
||||
visible: function (this: FogInfo){
|
||||
return this._type === FogType.LINEAR;
|
||||
}
|
||||
})
|
||||
get fogEnd() {
|
||||
return this._fogEnd;
|
||||
}
|
||||
|
||||
set fogEnd(val) {
|
||||
this._fogEnd = val;
|
||||
if (this._resource) { this._resource.fogEnd = val; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 雾效衰减
|
||||
* @en Global fog attenuation
|
||||
*/
|
||||
@property({
|
||||
type: CCFloat,
|
||||
step: 0.1,
|
||||
visible: function (this: FogInfo){
|
||||
return this._type !== FogType.LINEAR;
|
||||
}
|
||||
})
|
||||
get fogAtten() {
|
||||
return this._fogAtten;
|
||||
}
|
||||
|
||||
set fogAtten(val) {
|
||||
this._fogAtten = val;
|
||||
if (this._resource) { this._resource.fogAtten = val; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 雾效顶部范围,只适用于层级雾
|
||||
* @en Global fog top range, only for layered fog
|
||||
*/
|
||||
@property({
|
||||
type: CCFloat,
|
||||
step: 0.1,
|
||||
visible: function (this: FogInfo){
|
||||
return this._type === FogType.LAYERED;
|
||||
}
|
||||
})
|
||||
get fogTop() {
|
||||
return this._fogTop;
|
||||
}
|
||||
|
||||
set fogTop(val) {
|
||||
this._fogTop = val;
|
||||
if (this._resource) { this._resource.fogTop = val; }
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 雾效范围,只适用于层级雾
|
||||
* @en Global fog range, only for layered fog
|
||||
*/
|
||||
@property({
|
||||
type: CCFloat,
|
||||
step: 0.1,
|
||||
visible: function (this: FogInfo){
|
||||
return this._type === FogType.LAYERED;
|
||||
}
|
||||
})
|
||||
get fogRange() {
|
||||
return this._fogRange;
|
||||
}
|
||||
|
||||
set fogRange(val) {
|
||||
this._fogRange = val;
|
||||
if (this._resource) { this._resource.fogRange = val; }
|
||||
}
|
||||
|
||||
set renderScene (val: RenderScene) {
|
||||
this._resource = val.fog;
|
||||
this.enabled = this._enabled;
|
||||
this.fogColor = this._fogColor;
|
||||
this.type = this._type;
|
||||
this.fogDensity = this._fogDensity;
|
||||
this.fogStart = this._fogStart;
|
||||
this.fogEnd = this._fogEnd;
|
||||
this.fogAtten = this._fogAtten;
|
||||
this.fogTop = this._fogTop;
|
||||
this.fogRange = this._fogRange;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 平面阴影相关信息
|
||||
*/
|
||||
@@ -271,7 +478,9 @@ export class SceneGlobals {
|
||||
public planarShadows = new PlanarShadowInfo();
|
||||
@property
|
||||
private _skybox = new SkyboxInfo();
|
||||
|
||||
@property
|
||||
public fog = new FogInfo();
|
||||
|
||||
@property({ type: SkyboxInfo })
|
||||
get skybox () {
|
||||
return this._skybox;
|
||||
@@ -285,6 +494,7 @@ export class SceneGlobals {
|
||||
this.ambient.renderScene = rs;
|
||||
this.skybox.renderScene = rs;
|
||||
this.planarShadows.renderScene = rs;
|
||||
this.fog.renderScene = rs;
|
||||
}
|
||||
}
|
||||
cc.SceneGlobals = SceneGlobals;
|
||||
|
||||
Reference in New Issue
Block a user