add terrain shadow (#7500)

* add terrain shadow

* update

* update

* update

* delete temp source code

* Optimize the auto shadow

* shadow 和 0 号对齐

* add reciveshadow control

* cancel

* fix shadow effect display error

* update

* use 1.0, doesnt use 1 in glsl

* update effects

* change bbMin  && bbMax use Number.MAX_VALUE && Number.MIN_VALUE

* update

* update
This commit is contained in:
troublemaker52025
2020-10-15 16:48:44 +08:00
committed by GitHub
parent 69e65b848a
commit 82cc28847b
5 changed files with 162 additions and 74 deletions
File diff suppressed because one or more lines are too long
-1
View File
@@ -21,7 +21,6 @@ import { ShaderPool, SubModelPool, SubModelView } from '../core/memory-pools';
import { IGFXAttribute, GFXDescriptorSet } from '../../gfx';
import { INST_MAT_WORLD, UBOLocal, UniformLightingMapSampler } from '../../pipeline/define';
import { getTypedArrayConstructor, GFXBufferUsageBit, GFXFormat, GFXFormatInfos, GFXMemoryUsageBit, GFXFilter, GFXAddress } from '../../gfx/define';
import { ShadowType } from './shadows';
const m4_1 = new Mat4();
+41 -30
View File
@@ -25,6 +25,10 @@ import { HeightField } from './height-field';
import { legacyCC } from '../core/global-exports';
import { TerrainAsset, TerrainLayerInfo, TERRAIN_HEIGHT_BASE, TERRAIN_HEIGHT_FACTORY, TERRAIN_BLOCK_TILE_COMPLEXITY, TERRAIN_BLOCK_VERTEX_SIZE, TERRAIN_BLOCK_VERTEX_COMPLEXITY, TERRAIN_MAX_LAYER_COUNT, TERRAIN_HEIGHT_FMIN, TERRAIN_HEIGHT_FMAX, } from './terrain-asset';
const bbMin = new Vec3();
const bbMax = new Vec3();
/**
* @en Terrain info
* @zh 地形信息
@@ -284,6 +288,8 @@ export class TerrainBlock {
// vertex buffer
const vertexData = new Float32Array(TERRAIN_BLOCK_VERTEX_SIZE * TERRAIN_BLOCK_VERTEX_COMPLEXITY * TERRAIN_BLOCK_VERTEX_COMPLEXITY);
let index = 0;
bbMin.set(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
bbMax.set(Number.MIN_VALUE, Number.MIN_VALUE, Number.MIN_VALUE);
for (let j = 0; j < TERRAIN_BLOCK_VERTEX_COMPLEXITY; ++j) {
for (let i = 0; i < TERRAIN_BLOCK_VERTEX_COMPLEXITY; ++i) {
const x = this._index[0] * TERRAIN_BLOCK_TILE_COMPLEXITY + i;
@@ -299,6 +305,9 @@ export class TerrainBlock {
vertexData[index++] = normal.z;
vertexData[index++] = uv.x;
vertexData[index++] = uv.y;
Vec3.min(bbMin, bbMin, position);
Vec3.max(bbMax, bbMax, position);
}
}
@@ -322,6 +331,7 @@ export class TerrainBlock {
this._renderable._model = (legacyCC.director.root as Root).createModel(scene.Model);
this._renderable._model.initialize(this._node);
this._renderable._model.createBoundingShape(bbMin, bbMax);
this._renderable._getRenderScene().addModel(this._renderable._model);
// reset weightmap
@@ -549,36 +559,11 @@ export class TerrainBlock {
}
public _getMaterialDefines (nlayers: number): MacroRecord {
if (this.lightmap != null) {
if (nlayers === 0) {
return { LAYERS: 1, LIGHT_MAP: 1 };
}
else if (nlayers === 1) {
return { LAYERS: 2, LIGHT_MAP: 1 };
}
else if (nlayers === 2) {
return { LAYERS: 3, LIGHT_MAP: 1 };
}
else if (nlayers === 3) {
return { LAYERS: 4, LIGHT_MAP: 1 };
}
}
else {
if (nlayers === 0) {
return { LAYERS: 1 };
}
else if (nlayers === 1) {
return { LAYERS: 2 };
}
else if (nlayers === 2) {
return { LAYERS: 3 };
}
else if (nlayers === 3) {
return { LAYERS: 4 };
}
}
return { LAYERS: 0 };
return {
LAYERS: nlayers + 1,
USE_LIGHTMAP: this.lightmap !== null ? 1 : 0,
CC_RECEIVE_SHADOW: this._terrain.receiveShadow ? 1 : 0,
};
}
public _invalidMaterial () {
@@ -597,6 +582,8 @@ export class TerrainBlock {
const vertexData = new Float32Array(TERRAIN_BLOCK_VERTEX_SIZE * TERRAIN_BLOCK_VERTEX_COMPLEXITY * TERRAIN_BLOCK_VERTEX_COMPLEXITY);
let index = 0;
bbMin.set(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
bbMax.set(Number.MIN_VALUE, Number.MIN_VALUE, Number.MIN_VALUE);
for (let j = 0; j < TERRAIN_BLOCK_VERTEX_COMPLEXITY; ++j) {
for (let i = 0; i < TERRAIN_BLOCK_VERTEX_COMPLEXITY; ++i) {
const x = this._index[0] * TERRAIN_BLOCK_TILE_COMPLEXITY + i;
@@ -614,10 +601,14 @@ export class TerrainBlock {
vertexData[index++] = normal.z;
vertexData[index++] = uv.x;
vertexData[index++] = uv.y;
Vec3.min(bbMin, bbMin, position);
Vec3.max(bbMax, bbMax, position);
}
}
this._renderable._meshData.vertexBuffers[0].update(vertexData);
this._renderable._model!.createBoundingShape(bbMin, bbMax);
}
public _updateWeightMap () {
@@ -698,6 +689,10 @@ export class Terrain extends Component {
@disallowAnimation
protected _lightmapInfos: TerrainBlockLightmapInfo[] = [];
@serializable
@disallowAnimation
protected _receiveShadow: boolean = false;
protected _tileSize: number = 1;
protected _blockCount: number[] = [1, 1];
protected _weightMapSize: number = 128;
@@ -759,6 +754,22 @@ export class Terrain extends Component {
return this._effectAsset;
}
/**
* @en Receive shadow
* @zh 是否接受阴影
*/
@editable
get receiveShadow () {
return this._receiveShadow;
}
set receiveShadow (val) {
this._receiveShadow = val;
for (let i = 0; i < this._blocks.length; i++) {
this._blocks[i]._invalidMaterial();
}
}
/**
* @en get terrain size
* @zh 获得地形大小
+11 -11
View File
@@ -81,15 +81,15 @@ float CCGetShadowFactorX25 () {
return shadow /= 25.0;
}
#define CC_SHADOW_FACTOR(finalColor, atten) \
{ \
float pcf = cc_shadowInfo.z + 0.001; \
float shadowAttenuation = 0.0; \
if (pcf > 3.0) shadowAttenuation = CCGetShadowFactorX25(); \
else if (3.0 > pcf && pcf > 2.0) shadowAttenuation = CCGetShadowFactorX9(); \
else if (2.0 > pcf && pcf > 1.0) shadowAttenuation = CCGetShadowFactorX5(); \
else shadowAttenuation = CCGetShadowFactorX1(); \
vec3 shadowColor = cc_shadowColor.rgb * cc_shadowColor.a + finalColor * (1.0 - cc_shadowColor.a); \
finalColor = shadowColor.rgb * shadowAttenuation * atten + finalColor.rgb * (1.0 - shadowAttenuation * atten); \
} \
#define CC_SHADOW_FACTOR(finalColor, atten) \
{ \
float pcf = cc_shadowInfo.z + 0.001; \
float shadowAttenuation = 0.0; \
if (pcf > 3.0) shadowAttenuation = CCGetShadowFactorX25(); \
else if (3.0 > pcf && pcf > 2.0) shadowAttenuation = CCGetShadowFactorX9(); \
else if (2.0 > pcf && pcf > 1.0) shadowAttenuation = CCGetShadowFactorX5(); \
else shadowAttenuation = CCGetShadowFactorX1(); \
vec3 shadowColor = cc_shadowColor.rgb * cc_shadowColor.a + finalColor.rgb * (1.0 - cc_shadowColor.a); \
finalColor.rgb = shadowColor.rgb * shadowAttenuation * atten + finalColor.rgb * (1.0 - shadowAttenuation * atten); \
} \
#pragma // empty pragma trick to get rid of trailing semicolons at effect compile time
+49 -2
View File
@@ -31,6 +31,12 @@ CCEffect %{
blendSrcAlpha: zero
blendDstAlpha: one
properties: *props
- vert: shadow-caster-vs:vert
frag: shadow-caster-fs:frag
phase: shadow-add
propertyIndex: 0
rasterizerState:
cullMode: back
}%
CCProgram terrain-vs %{
@@ -38,6 +44,7 @@ CCProgram terrain-vs %{
#include <cc-global>
#include <cc-local>
#include <cc-fog>
#include <cc-shadow-map-vs>
in vec3 a_position;
in vec3 a_normal;
@@ -65,7 +72,7 @@ CCProgram terrain-vs %{
worldPos.y = cc_matWorld[3][1] + a_position.y;
worldPos.z = cc_matWorld[3][2] + a_position.z;
vec4 pos = vec4(worldPos, 1);
vec4 pos = vec4(worldPos, 1.0);
pos = cc_matViewProj * pos;
uvw = a_texCoord;
@@ -79,7 +86,9 @@ CCProgram terrain-vs %{
v_position = worldPos;
v_normal = a_normal;
v_fog_factor = CC_TRANSFER_FOG(vec4(worldPos, 1));
v_fog_factor = CC_TRANSFER_FOG(vec4(worldPos, 1.0));
CCPassShadowParams(vec4(worldPos, 1.0));
return pos;
}
@@ -162,3 +171,41 @@ CCProgram terrain-fs %{
return CCFragOutput(color);
}
}%
CCProgram shadow-caster-vs %{
precision highp float;
#include <cc-global>
#include <cc-local>
#include <cc-shadow>
in vec3 a_position;
in vec3 a_normal;
in vec2 a_texCoord;
out vec2 v_clip_depth;
vec4 vert () {
vec4 worldPos;
worldPos.x = cc_matWorld[3][0] + a_position.x;
worldPos.y = cc_matWorld[3][1] + a_position.y;
worldPos.z = cc_matWorld[3][2] + a_position.z;
worldPos.w = 1.0;
vec4 clipPos = cc_matLightViewProj * worldPos;
v_clip_depth = clipPos.zw;
return clipPos;
}
}%
CCProgram shadow-caster-fs %{
precision highp float;
#include <packing>
in vec2 v_clip_depth;
vec4 frag () {
return packDepthToRGBA(v_clip_depth.x / v_clip_depth.y * 0.5 + 0.5);
}
}%