Modify the initialization of scene global. (#7513)

* Modify the initialization of scene global.

* delete type of fog

* fix render error

* merge upstream 3d

* modify fog chunk

* commit effect
This commit is contained in:
cary
2020-11-19 18:08:32 +08:00
committed by GitHub
parent ca23ea9bd6
commit b266d9ef03
10 changed files with 121 additions and 92 deletions
+3 -3
View File
@@ -337,7 +337,7 @@ export default [
"shaders": [
{
"name": "standard|standard-vs:vert|standard-fs:frag",
"hash": 17747634,
"hash": 43723081,
"builtins": {
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCShadow", "defines": [] }], "samplers": [{ "name": "cc_environment", "defines": ["CC_USE_IBL"] }, { "name": "cc_shadowMap", "defines": ["CC_RECEIVE_SHADOW"] }] },
"locals": { "blocks": [{ "name": "CCMorph", "defines": ["CC_USE_MORPH"] }, { "name": "CCSkinningTexture", "defines": ["CC_USE_SKINNING", "CC_USE_BAKED_ANIMATION"] }, { "name": "CCSkinningAnimation", "defines": ["CC_USE_SKINNING", "CC_USE_BAKED_ANIMATION"] }, { "name": "CCSkinning", "defines": ["CC_USE_SKINNING", "!CC_USE_BAKED_ANIMATION"] }, { "name": "CCLocalBatched", "defines": ["!USE_INSTANCING", "USE_BATCHING"] }, { "name": "CCLocal", "defines": ["!USE_INSTANCING", "!USE_BATCHING"] }, { "name": "CCForwardLight", "defines": ["CC_FORWARD_ADD"] }], "samplers": [{ "name": "cc_PositionDisplacements", "defines": ["CC_USE_MORPH", "CC_MORPH_TARGET_HAS_POSITION"] }, { "name": "cc_NormalDisplacements", "defines": ["CC_USE_MORPH", "CC_MORPH_TARGET_HAS_NORMAL"] }, { "name": "cc_TangentDisplacements", "defines": ["CC_USE_MORPH", "CC_MORPH_TARGET_HAS_TANGENT"] }, { "name": "cc_jointTexture", "defines": ["CC_USE_SKINNING", "CC_USE_BAKED_ANIMATION"] }, { "name": "cc_spotLightingMap", "defines": ["CC_RECEIVE_SHADOW"] }, { "name": "cc_lightingMap", "defines": ["USE_LIGHTMAP", "!USE_BATCHING", "!CC_FORWARD_ADD"] }] }
@@ -477,7 +477,7 @@ export default [
"shaders": [
{
"name": "terrain|terrain-vs:vert|terrain-fs:frag",
"hash": 3803646709,
"hash": 2096214189,
"builtins": {
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCShadow", "defines": [] }], "samplers": [{ "name": "cc_environment", "defines": ["CC_USE_IBL"] }, { "name": "cc_shadowMap", "defines": ["CC_RECEIVE_SHADOW"] }] },
"locals": { "blocks": [{ "name": "CCLocal", "defines": [] }, { "name": "CCForwardLight", "defines": ["CC_FORWARD_ADD"] }], "samplers": [{ "name": "cc_spotLightingMap", "defines": ["CC_RECEIVE_SHADOW"] }] }
@@ -538,7 +538,7 @@ export default [
"shaders": [
{
"name": "unlit|unlit-vs:vert|unlit-fs:frag",
"hash": 561717302,
"hash": 3822871803,
"builtins": {
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }], "samplers": [] },
"locals": { "blocks": [{ "name": "CCMorph", "defines": ["CC_USE_MORPH"] }, { "name": "CCSkinningTexture", "defines": ["CC_USE_SKINNING", "CC_USE_BAKED_ANIMATION"] }, { "name": "CCSkinningAnimation", "defines": ["CC_USE_SKINNING", "CC_USE_BAKED_ANIMATION"] }, { "name": "CCSkinning", "defines": ["CC_USE_SKINNING", "!CC_USE_BAKED_ANIMATION"] }, { "name": "CCLocalBatched", "defines": ["!USE_INSTANCING", "USE_BATCHING"] }, { "name": "CCLocal", "defines": ["!USE_INSTANCING", "!USE_BATCHING"] }], "samplers": [{ "name": "cc_PositionDisplacements", "defines": ["CC_USE_MORPH", "CC_MORPH_TARGET_HAS_POSITION"] }, { "name": "cc_NormalDisplacements", "defines": ["CC_USE_MORPH", "CC_MORPH_TARGET_HAS_NORMAL"] }, { "name": "cc_TangentDisplacements", "defines": ["CC_USE_MORPH", "CC_MORPH_TARGET_HAS_TANGENT"] }, { "name": "cc_jointTexture", "defines": ["CC_USE_SKINNING", "CC_USE_BAKED_ANIMATION"] }] }
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+7 -4
View File
@@ -26,6 +26,7 @@
import { Color, Vec3 } from '../../math';
import { AmbientPool, NULL_HANDLE, AmbientView, AmbientHandle } from '../core/memory-pools';
import { legacyCC } from '../../global-exports';
import { AmbientInfo } from '../../scene-graph/scene-globals';
export class Ambient {
public static SUN_ILLUM = 65000.0;
@@ -45,7 +46,6 @@ export class Ambient {
*/
set enabled (val: boolean) {
AmbientPool.set(this._handle, AmbientView.ENABLE, val ? 1 : 0);
this.activate();
}
get enabled (): boolean {
return AmbientPool.get(this._handle, AmbientView.ENABLE) as unknown as boolean;
@@ -59,7 +59,7 @@ export class Ambient {
}
set skyColor (color: Color) {
this._skyColor = color;
this._skyColor.set(color);
Color.toArray(this._colorArray, this._skyColor);
AmbientPool.setVec4(this._handle, AmbientView.SKY_COLOR, this._skyColor);
}
@@ -85,7 +85,7 @@ export class Ambient {
}
set groundAlbedo (color: Color) {
this._groundAlbedo = color;
this._groundAlbedo.set(color);
Vec3.toArray(this._albedoArray, this._groundAlbedo);
AmbientPool.setVec4(this._handle, AmbientView.GROUND_ALBEDO, this._groundAlbedo);
}
@@ -103,11 +103,14 @@ export class Ambient {
this._handle = AmbientPool.alloc();
}
public activate () {
public initialize (ambientInfo: AmbientInfo) {
this._skyColor.set(ambientInfo.skyColor);
this._groundAlbedo.set(ambientInfo.groundAlbedo);
Color.toArray(this._colorArray, this._skyColor);
Vec3.toArray(this._albedoArray, this._groundAlbedo);
AmbientPool.setVec4(this._handle, AmbientView.SKY_COLOR, this._skyColor);
AmbientPool.setVec4(this._handle, AmbientView.GROUND_ALBEDO, this._groundAlbedo);
AmbientPool.set(this._handle, AmbientView.ILLUM, ambientInfo.skyIllum);
}
public destroy () {
+29 -26
View File
@@ -27,6 +27,7 @@ import { Enum } from '../../value-types';
import { Color } from '../../../core/math';
import { legacyCC } from '../../global-exports';
import { FogPool, NULL_HANDLE, FogView, FogHandle } from '../core/memory-pools';
import { FogInfo } from '../../scene-graph/scene-globals';
/**
* @zh
@@ -71,6 +72,8 @@ export const FogType = Enum({
LAYERED: 3,
});
const FOG_TYPE_NONE = FogType.LAYERED + 1;
export class Fog {
/**
* @zh 是否启用全局雾效
@@ -78,7 +81,7 @@ export class Fog {
*/
set enabled (val: boolean) {
FogPool.set(this._handle, FogView.ENABLE, val ? 1 : 0);
FogPool.set(this._handle, FogView.TYPE, val ? this._type + 1 : 0);
if (!val) FogPool.set(this._handle, FogView.TYPE, FOG_TYPE_NONE);
val ? this.activate() : this._updatePipeline();
}
@@ -101,17 +104,22 @@ export class Fog {
}
/**
* @zh 全局雾类型
* @en Global fog type
* @zh 当前雾化类型。
* @en The current global fog type.
* @returns {FogType}
* Returns the current global fog type
* - -1:Disable global Fog
* - 0:Linear fog
* - 1:Exponential fog
* - 2:Exponential square fog
* - 3:Layered fog
*/
get type (): number {
return this._type;
return FogPool.get(this._handle, FogView.TYPE);
}
set type (val: number) {
this._type = val;
FogPool.set(this._handle, FogView.TYPE, this.enabled ? val : FOG_TYPE_NONE);
if (this.enabled) this._updatePipeline();
FogPool.set(this._handle, FogView.TYPE, this.enabled ? this._type + 1 : 0);
}
/**
@@ -185,26 +193,10 @@ export class Fog {
set fogRange (val: number) {
FogPool.set(this._handle, FogView.RANGE, val);
}
/**
* @zh 当前雾化类型。
* @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 (): number {
return FogPool.get(this._handle, FogView.TYPE);
}
get colorArray (): Float32Array {
return this._colorArray;
}
protected _type = FogType.LINEAR;
protected _fogColor = new Color('#C8C8C8');
protected _colorArray: Float32Array = new Float32Array([0.2, 0.2, 0.2, 1.0]);
protected _handle: FogHandle = NULL_HANDLE;
@@ -217,16 +209,27 @@ export class Fog {
this._handle = FogPool.alloc();
}
public activate () {
public initialize (fogInfo : FogInfo) {
FogPool.set(this._handle, FogView.ENABLE, fogInfo.enabled ? 1 : 0);
FogPool.set(this._handle, FogView.TYPE, fogInfo.enabled ? fogInfo.type : FOG_TYPE_NONE);
this._fogColor.set(fogInfo.fogColor);
Color.toArray(this._colorArray, this._fogColor);
FogPool.setVec4(this._handle, FogView.COLOR, this._fogColor);
FogPool.set(this._handle, FogView.TYPE, this.enabled ? this._type + 1 : 0);
FogPool.set(this._handle, FogView.DENSITY, fogInfo.fogDensity);
FogPool.set(this._handle, FogView.START, fogInfo.fogStart);
FogPool.set(this._handle, FogView.END, fogInfo.fogEnd);
FogPool.set(this._handle, FogView.ATTEN, fogInfo.fogAtten);
FogPool.set(this._handle, FogView.TOP, fogInfo.fogTop);
FogPool.set(this._handle, FogView.RANGE, fogInfo.fogRange);
}
public activate () {
this._updatePipeline();
}
protected _updatePipeline () {
const root = legacyCC.director.root
const value = this.currType;
const value = this.enabled ? this.type : FOG_TYPE_NONE;
const pipeline = root.pipeline;
if (pipeline.macros.CC_USE_FOG === value) { return; }
pipeline.macros.CC_USE_FOG = value;
+25 -1
View File
@@ -29,6 +29,7 @@ import { Color, Mat4, Vec3, Vec2 } from '../../math';
import { legacyCC } from '../../global-exports';
import { Enum } from '../../value-types';
import { ShadowsPool, NULL_HANDLE, ShadowsView, ShadowsHandle } from '../core/memory-pools';
import { ShadowsInfo } from '../../scene-graph/scene-globals';
import { IMacroPatch } from '../core/pass';
const shadowPatches: IMacroPatch[] = [
@@ -94,6 +95,8 @@ export const PCFType = Enum({
FILTER_X25: 3,
});
const SHADOW_TYPE_NONE = ShadowType.ShadowMap + 1;
export class Shadows {
/**
@@ -119,6 +122,7 @@ export class Shadows {
set enabled (val: boolean) {
ShadowsPool.set(this._handle, ShadowsView.ENABLE, val ? 1 : 0);
if (!val) ShadowsPool.set(this._handle, ShadowsView.TYPE, SHADOW_TYPE_NONE);
if (val) this.activate(); else this._updatePipeline();
}
@@ -168,7 +172,7 @@ export class Shadows {
return ShadowsPool.get(this._handle, ShadowsView.TYPE);
}
set type (val: number) {
ShadowsPool.set(this._handle, ShadowsView.TYPE, val);
ShadowsPool.set(this._handle, ShadowsView.TYPE, this.enabled ? val : SHADOW_TYPE_NONE);
this._updatePipeline();
this._updatePlanarInfo();
}
@@ -315,6 +319,26 @@ export class Shadows {
this._handle = ShadowsPool.alloc();
}
public initialize (shadowsInfo: ShadowsInfo) {
ShadowsPool.set(this._handle, ShadowsView.TYPE, shadowsInfo.enabled ? shadowsInfo.type : SHADOW_TYPE_NONE);
ShadowsPool.set(this._handle, ShadowsView.NEAR, shadowsInfo.near);
ShadowsPool.set(this._handle, ShadowsView.FAR, shadowsInfo.far);
ShadowsPool.set(this._handle, ShadowsView.ASPECT, shadowsInfo.aspect);
ShadowsPool.set(this._handle, ShadowsView.ORTHO_SIZE, shadowsInfo.orthoSize);
this._size = shadowsInfo.shadowMapSize;
ShadowsPool.setVec2(this._handle, ShadowsView.SIZE, this._size);
ShadowsPool.set(this._handle, ShadowsView.PCF_TYPE, shadowsInfo.pcf);
Vec3.copy(this._normal, shadowsInfo.normal);
ShadowsPool.setVec3(this._handle, ShadowsView.NORMAL, this._normal);
ShadowsPool.set(this._handle, ShadowsView.DISTANCE, shadowsInfo.distance);
this._shadowColor.set(shadowsInfo.shadowColor);
ShadowsPool.setVec4(this._handle, ShadowsView.COLOR, this._shadowColor);
ShadowsPool.set(this._handle, ShadowsView.BIAS, shadowsInfo.bias);
ShadowsPool.set(this._handle, ShadowsView.ENABLE, shadowsInfo.enabled ? 1 : 0);
this.maxReceived = shadowsInfo.maxReceived;
ShadowsPool.set(this._handle, ShadowsView.AUTO_ADAPT, shadowsInfo.autoAdapt ? 1 : 0);
}
public activate () {
if (this.type === ShadowType.ShadowMap) {
this._updatePipeline();
+14 -6
View File
@@ -36,6 +36,7 @@ import { Model } from './model';
import { legacyCC } from '../../global-exports';
import { DescriptorSet } from '../../gfx';
import { SkyboxPool, NULL_HANDLE, SkyboxView, SkyboxHandle } from '../core/memory-pools';
import { SkyboxInfo } from '../../scene-graph/scene-globals';
let skybox_mesh: Mesh | null = null;
let skybox_material: Material | null = null;
@@ -123,20 +124,29 @@ export class Skybox {
this._handle = SkyboxPool.alloc();
}
public initialize (skyboxInfo: SkyboxInfo) {
SkyboxPool.set(this._handle, SkyboxView.ENABLE, skyboxInfo.enabled ? 1 : 0);
SkyboxPool.set(this._handle, SkyboxView.USE_IBL, skyboxInfo.useIBL ? 1 : 0);
SkyboxPool.set(this._handle, SkyboxView.IS_RGBE, skyboxInfo.isRGBE ? 1 : 0);
this._envmap = skyboxInfo.envmap;
}
public activate () {
const pipeline = legacyCC.director.root.pipeline;
this._globalDescriptorSet = pipeline.descriptorSet;
this._default = builtinResMgr.get<TextureCube>('default-cube-texture');
if (!this._model) {
this._model = new legacyCC.renderer.scene.Model() as Model;
// @ts-expect-error skybox don't need local buffers
this._model = legacyCC.director.root.createModel(legacyCC.renderer.scene.Model) as Model;
// @ts-ignore skybox don't need local buffers
this._model._initLocalDescriptors = () => {};
}
SkyboxPool.set(this._handle, SkyboxView.MODEL, this._model.handle);
pipeline.ambient.groundAlbedo[3] = this._envmap ? this._envmap.mipmapLevel : this._default.mipmapLevel;
if (!this._envmap) {
this._envmap = this._default;
}
pipeline.ambient.groundAlbedo[3] = this._envmap.mipmapLevel;
if (!skybox_material) {
const mat = new Material();
@@ -148,8 +158,6 @@ export class Skybox {
if (!skybox_mesh) { skybox_mesh = createMesh(box({ width: 2, height: 2, length: 2 })); }
this._model.initSubModel(0, skybox_mesh.renderingSubMeshes[0], skybox_material);
this.envmap = this._envmap;
this._updateGlobalBinding();
this._updatePipeline();
}
+24 -33
View File
@@ -97,9 +97,7 @@ export class AmbientInfo {
public activate (resource: Ambient) {
this._resource = resource;
this._resource.skyColor = this._skyColor;
this._resource.skyIllum = this._skyIllum;
this._resource.groundAlbedo = this._groundAlbedo;
this._resource.initialize(this);
}
}
legacyCC.AmbientInfo = AmbientInfo;
@@ -127,8 +125,11 @@ export class SkyboxInfo {
*/
@editable
set enabled (val) {
if (this._enabled === val) return;
this._enabled = val;
if (this._resource) { this._resource.enabled = this._enabled; }
if (this._resource) {
this._resource.enabled = this._enabled;
}
}
get enabled () {
return this._enabled;
@@ -177,11 +178,8 @@ export class SkyboxInfo {
public activate (resource: Skybox) {
this._resource = resource;
this._resource.initialize(this);
this._resource.activate(); // update global DS first
this._resource.enabled = this._enabled;
this._resource.isRGBE = this._isRGBE;
this._resource.envmap = this._envmap;
this._resource.useIBL = this._useIBL;
}
}
legacyCC.SkyboxInfo = SkyboxInfo;
@@ -218,8 +216,14 @@ export class FogInfo {
*/
@editable
set enabled (val: boolean) {
if (this._enabled === val) return;
this._enabled = val;
if (this._resource) { this._resource.enabled = val; }
if (this._resource) {
this._resource.enabled = val;
if (val) {
this._resource.type = this._type;
}
}
}
get enabled () {
@@ -364,15 +368,8 @@ export class FogInfo {
public activate (resource: Fog) {
this._resource = resource;
this._resource.enabled = this._enabled;
this._resource.fogColor = this._fogColor;
this._resource.type = this._type;
this._resource.fogDensity = this._fogDensity;
this._resource.fogStart = this._fogStart;
this._resource.fogEnd = this._fogEnd;
this._resource.fogAtten = this._fogAtten;
this._resource.fogTop = this._fogTop;
this._resource.fogRange = this._fogRange;
this._resource.initialize(this);
this._resource.activate();
}
}
@@ -419,8 +416,14 @@ export class ShadowsInfo {
*/
@editable
set enabled (val: boolean) {
if (this._enabled === val) return;
this._enabled = val;
if (this._resource) { this._resource.enabled = val; }
if (this._resource) {
this._resource.enabled = val;
if (val) {
this._resource.type = this._type;
}
}
}
get enabled () {
return this._enabled;
@@ -617,20 +620,8 @@ export class ShadowsInfo {
public activate (resource: Shadows) {
this._resource = resource;
this._resource.type = this._type;
this._resource.autoAdapt = this._autoAdapt;
this._resource.near = this._near;
this._resource.far = this._far;
this._resource.aspect = this._aspect;
this._resource.orthoSize = this._orthoSize;
this._resource.size = this._size;
this._resource.normal = this._normal;
this._resource.distance = this._distance;
this._resource.shadowColor = this._shadowColor;
this._resource.pcf = this._pcf;
this._resource.bias = this._bias;
this._resource.enabled = this._enabled;
this._resource.maxReceived = this._maxReceived;
this._resource.initialize(this);
this._resource.activate();
}
}
legacyCC.ShadowsInfo = ShadowsInfo;
+4 -4
View File
@@ -1,10 +1,10 @@
#include <cc-global>
#pragma define CC_USE_FOG range([0, 4])
#define CC_FOG_LINEAR 1
#define CC_FOG_EXP 2
#define CC_FOG_EXP_SQUARED 3
#define CC_FOG_LAYERED 4
#define CC_FOG_LINEAR 0
#define CC_FOG_EXP 1
#define CC_FOG_EXP_SQUARED 2
#define CC_FOG_LAYERED 3
float LinearFog(vec4 pos) {
vec4 wPos = pos;