planar shadow (#3975)

* pass switch

* planar shadows: basic

* planar shadow: framework
This commit is contained in:
Yun Hsiao Wu
2019-03-06 19:37:35 +08:00
committed by Weiliang Wang
parent 2b3a26ec7d
commit cd5e88adeb
17 changed files with 427 additions and 492 deletions
+1
View File
@@ -15,6 +15,7 @@ export interface IPropertyInfo {
export interface IPassInfo {
program: string;
// effect-writer part
switch?: string;
priority?: number;
primitive?: GFXPrimitiveMode;
stage?: RenderPassStage;
+4 -4
View File
@@ -98,7 +98,7 @@ export class Material extends Asset {
const cur = this._defines;
let patch = info.defines;
if (!Array.isArray(patch) && this._effectAsset) { // fill all the passes if not specified
const len = Effect.getPassInfos(this._effectAsset, this._techIdx).length;
const len = Effect.getPassesInfo(this._effectAsset, this._techIdx).length;
patch = Array(len).fill(info.defines);
}
for (let i = 0; i < patch.length; i++) {
@@ -181,10 +181,10 @@ export class Material extends Asset {
public overridePipelineStates (overrides: PassOverrides, passIdx?: number) {
if (!this._passes || !this._effectAsset) { return; }
const passInfos = Effect.getPassInfos(this._effectAsset, this._techIdx);
const passInfos = Effect.getPassesInfo(this._effectAsset, this._techIdx);
if (passIdx === undefined) {
for (let i = 0; i < this._passes.length; i++) {
this._passes[i].overridePipelineStates(passInfos[i], overrides);
for (const pass of this._passes) {
pass.overridePipelineStates(passInfos[pass.idxInTech], overrides);
}
} else {
this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides);
+25 -404
View File
File diff suppressed because one or more lines are too long
+2
View File
@@ -296,6 +296,8 @@ export class LightComponent extends Component {
scene.destroySpotLight(this._light);
break;
}
} else {
this._light.node = scene.defaultMainLightNode;
}
this._light = null;
}
+4 -4
View File
@@ -26,10 +26,10 @@ interface IPlaneOptions extends RecursivePartial<IGeometryOptions> {
function applyDefaultPlaneOptions (options?: RecursivePartial<IPlaneOptions>): IPlaneOptions {
options = applyDefaultGeometryOptions<IPlaneOptions>(options);
options.width = 10;
options.length = 10;
options.widthSegments = 10;
options.lengthSegments = 10;
options.width = options.width || 10;
options.length = options.length || 10;
options.widthSegments = options.widthSegments || 10;
options.lengthSegments = options.lengthSegments || 10;
return options as IPlaneOptions;
}
-4
View File
@@ -1,5 +1,3 @@
import { ccenum } from '../core/value-types/enum';
// Extensions
export enum WebGLEXT {
COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0,
@@ -360,8 +358,6 @@ export enum GFXBlendFactor {
ONE_MINUS_CONSTANT_ALPHA,
}
ccenum(GFXBlendFactor);
export enum GFXColorMask {
NONE = 0x0,
R = 0x1,
+32 -17
View File
@@ -22,6 +22,7 @@ export enum UniformBinding {
UBO_FORWARD_LIGHTS = MAX_BINDING_SUPPORTED - 3,
UBO_SKINNING = MAX_BINDING_SUPPORTED - 4,
UBO_UI = MAX_BINDING_SUPPORTED - 5,
UBO_SHADOW = MAX_BINDING_SUPPORTED - 6,
// samplers
SAMPLER_JOINTS = MAX_BINDING_SUPPORTED + 1,
}
@@ -84,34 +85,34 @@ export class UBOLocal {
public view: Float32Array = new Float32Array(UBOLocal.COUNT);
}
export class UBOForwardLights {
export class UBOForwardLight {
public static MAX_DIR_LIGHTS = 4;
public static MAX_POINT_LIGHTS = 4;
public static MAX_SPOT_LIGHTS = 4;
public static DIR_LIGHT_DIR_OFFSET: number = 0;
public static DIR_LIGHT_COLOR_OFFSET: number = UBOForwardLights.DIR_LIGHT_DIR_OFFSET + UBOForwardLights.MAX_DIR_LIGHTS * 4;
public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_DIR_LIGHTS * 4;
public static POINT_LIGHT_COLOR_OFFSET: number = UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + UBOForwardLights.MAX_POINT_LIGHTS * 4;
public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_POINT_LIGHTS * 4;
public static SPOT_LIGHT_DIR_OFFSET: number = UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4;
public static SPOT_LIGHT_COLOR_OFFSET: number = UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4;
public static COUNT: number = UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4;
public static SIZE: number = UBOForwardLights.COUNT * 4;
public static DIR_LIGHT_COLOR_OFFSET: number = UBOForwardLight.DIR_LIGHT_DIR_OFFSET + UBOForwardLight.MAX_DIR_LIGHTS * 4;
public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLight.DIR_LIGHT_COLOR_OFFSET + UBOForwardLight.MAX_DIR_LIGHTS * 4;
public static POINT_LIGHT_COLOR_OFFSET: number = UBOForwardLight.POINT_LIGHT_POS_RANGE_OFFSET + UBOForwardLight.MAX_POINT_LIGHTS * 4;
public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLight.POINT_LIGHT_COLOR_OFFSET + UBOForwardLight.MAX_POINT_LIGHTS * 4;
public static SPOT_LIGHT_DIR_OFFSET: number = UBOForwardLight.SPOT_LIGHT_POS_RANGE_OFFSET + UBOForwardLight.MAX_SPOT_LIGHTS * 4;
public static SPOT_LIGHT_COLOR_OFFSET: number = UBOForwardLight.SPOT_LIGHT_DIR_OFFSET + UBOForwardLight.MAX_SPOT_LIGHTS * 4;
public static COUNT: number = UBOForwardLight.SPOT_LIGHT_COLOR_OFFSET + UBOForwardLight.MAX_SPOT_LIGHTS * 4;
public static SIZE: number = UBOForwardLight.COUNT * 4;
public static BLOCK: GFXUniformBlock = {
binding: UniformBinding.UBO_FORWARD_LIGHTS, name: 'CCForwardLights', members: [
{ name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS },
{ name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS },
{ name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_POINT_LIGHTS },
{ name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_POINT_LIGHTS },
{ name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS },
{ name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS },
{ name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS },
{ name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_DIR_LIGHTS },
{ name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_DIR_LIGHTS },
{ name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_POINT_LIGHTS },
{ name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_POINT_LIGHTS },
{ name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_SPOT_LIGHTS },
{ name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_SPOT_LIGHTS },
{ name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_SPOT_LIGHTS },
],
};
public view: Float32Array = new Float32Array(UBOForwardLights.COUNT);
public view: Float32Array = new Float32Array(UBOForwardLight.COUNT);
}
export class UBOSkinning {
@@ -146,6 +147,20 @@ export class UBOUI {
public view: Float32Array = new Float32Array(UBOUI.COUNT);
}
export class UBOShadow {
public static MAT_LIGHT_PLANE_PROJ_OFFSET: number = 0;
public static SHADOW_COLOR_OFFSET: number = UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET + 16;
public static COUNT: number = UBOShadow.SHADOW_COLOR_OFFSET + 4;
public static SIZE: number = UBOShadow.COUNT * 4;
public static BLOCK: GFXUniformBlock = {
binding: UniformBinding.UBO_SHADOW, name: 'CCShadow', members: [
{ name: 'cc_matLightPlaneProj', type: GFXType.MAT4, count: 1 },
{ name: 'cc_shadowColor', type: GFXType.FLOAT4, count: 1 },
],
};
}
export interface IGlobalBindingDesc {
type: GFXBindingType;
blockInfo?: GFXUniformBlock;
+26 -26
View File
@@ -2,7 +2,7 @@ import { Root } from '../../core/root';
import { GFXBuffer } from '../../gfx/buffer';
import { GFXBindingType, GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define';
import { GFXRenderPass } from '../../gfx/render-pass';
import { RenderPassStage, UBOForwardLights } from '../define';
import { RenderPassStage, UBOForwardLight } from '../define';
import { ToneMapFlow } from '../ppfx/tonemap-flow';
import { RenderPipeline } from '../render-pipeline';
import { RenderView } from '../render-view';
@@ -16,7 +16,7 @@ const _idVec4Array = Float32Array.from([1, 1, 1, 0]);
export class ForwardPipeline extends RenderPipeline {
protected _uboLights: UBOForwardLights = new UBOForwardLights();
protected _uboLights: UBOForwardLight = new UBOForwardLight();
protected _lightsUBO: GFXBuffer | null = null;
public get lightsUBO (): GFXBuffer {
@@ -80,20 +80,20 @@ export class ForwardPipeline extends RenderPipeline {
protected createUBOs (): boolean {
super.createUBOs();
if (!this._globalBindings.get(UBOForwardLights.BLOCK.name)) {
if (!this._globalBindings.get(UBOForwardLight.BLOCK.name)) {
const lightsUBO = this._root.device.createBuffer({
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: UBOForwardLights.SIZE,
size: UBOForwardLight.SIZE,
});
if (!lightsUBO) {
return false;
}
this._globalBindings.set(UBOForwardLights.BLOCK.name, {
this._globalBindings.set(UBOForwardLight.BLOCK.name, {
type: GFXBindingType.UNIFORM_BUFFER,
blockInfo: UBOForwardLights.BLOCK,
blockInfo: UBOForwardLight.BLOCK,
buffer: lightsUBO,
});
}
@@ -104,10 +104,10 @@ export class ForwardPipeline extends RenderPipeline {
protected destroyUBOs () {
super.destroyUBOs();
const lightsUBO = this._globalBindings.get(UBOForwardLights.BLOCK.name);
const lightsUBO = this._globalBindings.get(UBOForwardLight.BLOCK.name);
if (lightsUBO) {
lightsUBO.buffer!.destroy();
this._globalBindings.delete(UBOForwardLights.BLOCK.name);
this._globalBindings.delete(UBOForwardLight.BLOCK.name);
}
}
@@ -117,45 +117,45 @@ export class ForwardPipeline extends RenderPipeline {
const scene = view.camera.scene;
const dLights = scene.directionalLights;
for (let i = 0; i < UBOForwardLights.MAX_DIR_LIGHTS; i++) {
for (let i = 0; i < UBOForwardLight.MAX_DIR_LIGHTS; i++) {
const light = dLights[i];
if (light && light.enabled) {
light.update();
this._uboLights.view.set(light.directionArray, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(light.directionArray, UBOForwardLight.DIR_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLight.DIR_LIGHT_COLOR_OFFSET + i * 4);
} else {
this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLight.DIR_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLight.DIR_LIGHT_COLOR_OFFSET + i * 4);
}
}
const pLights = scene.pointLights;
for (let i = 0; i < UBOForwardLights.MAX_POINT_LIGHTS; i++) {
for (let i = 0; i < UBOForwardLight.MAX_POINT_LIGHTS; i++) {
const light = pLights[i];
if (light && light.enabled) {
light.update();
this._uboLights.view.set(light.positionAndRange, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(light.positionAndRange, UBOForwardLight.POINT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLight.POINT_LIGHT_COLOR_OFFSET + i * 4);
} else {
this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLight.POINT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLight.POINT_LIGHT_COLOR_OFFSET + i * 4);
}
}
const sLights = scene.spotLights;
for (let i = 0; i < UBOForwardLights.MAX_SPOT_LIGHTS; i++) {
for (let i = 0; i < UBOForwardLight.MAX_SPOT_LIGHTS; i++) {
const light = sLights[i];
if (light && light.enabled) {
light.update();
this._uboLights.view.set(light.positionAndRange, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(light.directionArray, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(light.positionAndRange, UBOForwardLight.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(light.directionArray, UBOForwardLight.SPOT_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLight.SPOT_LIGHT_COLOR_OFFSET + i * 4);
} else {
this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLight.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLight.SPOT_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLight.SPOT_LIGHT_COLOR_OFFSET + i * 4);
}
}
// update ubos
this._globalBindings.get(UBOForwardLights.BLOCK.name)!.buffer!.update(this._uboLights.view);
this._globalBindings.get(UBOForwardLight.BLOCK.name)!.buffer!.update(this._uboLights.view);
}
}
+31 -4
View File
@@ -21,7 +21,7 @@ import { GFXInputAssembler, IGFXInputAttribute } from '../gfx/input-assembler';
import { GFXRenderPass } from '../gfx/render-pass';
import { GFXTexture } from '../gfx/texture';
import { GFXTextureView } from '../gfx/texture-view';
import { UBOGlobal } from './define';
import { UBOGlobal, UBOShadow } from './define';
import { IGlobalBindingDesc } from './define';
import { IRenderFlowInfo, RenderFlow } from './render-flow';
import { RenderQueue } from './render-queue';
@@ -390,15 +390,38 @@ export abstract class RenderPipeline {
});
}
if (!this._globalBindings.get(UBOShadow.BLOCK.name)) {
const shadowUBO = this._root.device.createBuffer({
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: UBOShadow.SIZE,
});
if (!shadowUBO) {
return false;
}
this._globalBindings.set(UBOShadow.BLOCK.name, {
type: GFXBindingType.UNIFORM_BUFFER,
blockInfo: UBOShadow.BLOCK,
buffer: shadowUBO,
});
}
return true;
}
protected destroyUBOs () {
const defaultUBO = this._globalBindings.get(UBOGlobal.BLOCK.name);
if (defaultUBO) {
defaultUBO.buffer!.destroy();
const globalUBO = this._globalBindings.get(UBOGlobal.BLOCK.name);
if (globalUBO) {
globalUBO.buffer!.destroy();
this._globalBindings.delete(UBOGlobal.BLOCK.name);
}
const shadowUBO = this._globalBindings.get(UBOShadow.BLOCK.name);
if (shadowUBO) {
shadowUBO.buffer!.destroy();
this._globalBindings.delete(UBOShadow.BLOCK.name);
}
}
protected updateUBOs (view: RenderView) {
@@ -473,6 +496,10 @@ export abstract class RenderPipeline {
// update ubos
this._globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!.update(this._defaultUboGlobal!.view.buffer);
const planarShadow = scene.planarShadow;
planarShadow.update(scene.mainLight);
this._globalBindings.get(UBOShadow.BLOCK.name)!.buffer!.update(planarShadow.data);
}
protected sceneCulling (view: RenderView) {
+6 -4
View File
@@ -10,7 +10,7 @@ export interface IEffectInfo {
}
export class Effect {
public static getPassInfos (effect: EffectAsset, techIdx: number) {
public static getPassesInfo (effect: EffectAsset, techIdx: number) {
return effect.techniques[techIdx].passes;
}
@@ -19,13 +19,15 @@ export class Effect {
const { techIdx, defines } = info || {} as IEffectInfo;
const tech = effect.techniques[techIdx || 0];
const passNum = tech.passes.length;
const passes: Pass[] = new Array(passNum);
const passes: Pass[] = [];
for (let k = 0; k < passNum; ++k) {
const passInfo = tech.passes[k] as IPassInfoFull;
passInfo.defines = defines && defines[k] || {};
const defs = passInfo.curDefs = defines && defines[k] || {};
if (passInfo.switch && !defs[passInfo.switch]) { continue; }
passInfo.idxInTech = k;
const pass = new Pass(cc.game._gfxDevice);
pass.initialize(passInfo);
passes[k] = pass;
passes.push(pass);
}
return passes;
}
+8 -9
View File
@@ -23,7 +23,8 @@ import { programLib } from './program-lib';
export interface IDefineMap { [name: string]: number | boolean; }
export interface IPassInfoFull extends IPassInfo {
// generated part
defines: IDefineMap;
idxInTech: number;
curDefs: IDefineMap;
}
export type PassOverrides = RecursivePartial<IPassInfo>;
@@ -101,7 +102,8 @@ export class Pass {
protected _textureViews: Record<number, GFXTextureView> = {};
protected _resources: IPassResources[] = [];
// internal data
protected _programName: string = '';
protected _idxInTech = 0;
protected _programName = '';
protected _priority: RenderPriority = RenderPriority.DEFAULT;
protected _primitive: GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST;
protected _stage: RenderPassStage = RenderPassStage.DEFAULT;
@@ -125,8 +127,9 @@ export class Pass {
}
public initialize (info: IPassInfoFull) {
this._idxInTech = info.idxInTech;
this._programName = info.program;
this._defines = info.defines;
this._defines = info.curDefs;
const shaderInfo = this._shaderInfo = programLib.getTemplate(info.program);
// pipeline state
const device = this._device;
@@ -144,7 +147,6 @@ export class Pass {
else { console.error('create buffer failed.'); return; }
// buffer data processing system
const block: IBlock = this._blocks[u.binding] = {
// buffer: buffer.buffer as ArrayBuffer,
buffer: new ArrayBuffer(u.size),
dirty: false,
views: [],
@@ -182,11 +184,7 @@ export class Pass {
}
public getHandle (name: string) {
const handle = this._handleMap[name];
if (!handle) {
console.warn('illegal property name ' + name);
}
return handle;
return this._handleMap[name];
}
public getBinding (name: string) {
@@ -376,6 +374,7 @@ export class Pass {
Object.assign(this._dss, info.depthStencilState);
}
get idxInTech () { return this._idxInTech; }
get programName () { return this._programName; }
get priority () { return this._priority; }
get primitive () { return this._primitive; }
+2 -2
View File
@@ -123,7 +123,7 @@ class ProgramLib {
// get template
const tmpl = this._templates[name];
const customDef = _generateDefines(device, defines, tmpl.defines, tmpl.dependencies) + '\n';
if (!tmpl.builtinInited) { insertPipelineBuiltins(tmpl, pipeline); }
if (!tmpl.builtinInited) { insertPipelineGlobalBindings(tmpl, pipeline); }
let vert: string = '';
let frag: string = '';
@@ -150,7 +150,7 @@ class ProgramLib {
}
}
function insertPipelineBuiltins (tmpl: IProgramInfo, pipeline: RenderPipeline) {
function insertPipelineGlobalBindings (tmpl: IProgramInfo, pipeline: RenderPipeline) {
const source = pipeline.globalBindings;
const target = tmpl.builtins;
for (const b of target.blocks) {
+2 -2
View File
@@ -9,8 +9,8 @@ const _v3 = new Vec3();
const _qt = new Quat();
export class DirectionalLight extends Light {
protected _direction: Vec3 = new Vec3(0, 0, 1);
protected _directionArray: Float32Array = Float32Array.from([0, 0, 1, 0]);
protected _direction = new Vec3(0, 0, 1);
protected _directionArray = Float32Array.from([0, 0, 1, 0]);
set direction (val: Vec3) {
this._direction = val;
+79
View File
@@ -0,0 +1,79 @@
import { Color, Vec3 } from '../../core/value-types';
import { color4, vec3 } from '../../core/vmath';
import { UBOShadow } from '../../pipeline/define';
import { Light } from './light';
import { RenderScene } from './render-scene';
const _v3 = new Vec3();
export class PlanarShadow {
set normal (val: Vec3) {
vec3.copy(this._normal, val);
}
get normal () {
return this._normal;
}
set distance (val: number) {
this._distance = val;
}
get distance () {
return this._distance;
}
set offset (val: number) {
this._offset = val;
}
get offset () {
return this._offset;
}
set shadowColor (color: Color) {
color4.array(this._data, color, UBOShadow.SHADOW_COLOR_OFFSET);
}
get data () {
return this._data;
}
protected _scene: RenderScene;
protected _normal = new Vec3(0, 1, 0);
protected _distance = 0;
protected _offset = 0.01;
protected _data = Float32Array.from([
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, // matLightPlaneProj
0.3, 0.3, 0.3, 1.0, // shadowColor
]);
constructor (scene: RenderScene) {
this._scene = scene;
}
// tslint:disable: one-variable-per-declaration
public update (light: Light) {
light.node.getWorldPosition(_v3);
const n = this._normal, d = this._distance + this._offset;
const lx = _v3.x, ly = _v3.y, lz = _v3.z;
const nx = n.x, ny = n.y, nz = n.z;
const NdL = vec3.dot(n, _v3);
const m = this._data;
m[0] = NdL - d - lx * nx;
m[1] = -ly * nx;
m[2] = -lz * nx;
m[3] = -nx;
m[4] = -lx * ny;
m[5] = NdL - d - ly * ny;
m[6] = -lz * ny;
m[7] = -ny;
m[8] = -lx * nz;
m[9] = -ly * nz;
m[10] = NdL - d - lz * nz;
m[11] = -nz;
m[12] = lx * d;
m[13] = ly * d;
m[14] = lz * d;
m[15] = NdL;
}
// tslint:enable: one-variable-per-declaration
}
+19 -7
View File
@@ -5,7 +5,7 @@ import { Root } from '../../core/root';
import { Mat4, Vec3 } from '../../core/value-types';
import { mat4, vec3 } from '../../core/vmath';
import { GFXPrimitiveMode } from '../../gfx/define';
import { UBOForwardLights } from '../../pipeline/define';
import { UBOForwardLight } from '../../pipeline/define';
import { Layers } from '../../scene-graph/layers';
import { Node } from '../../scene-graph/node';
import { Ambient } from './ambient';
@@ -13,6 +13,7 @@ import { Camera, ICameraInfo } from './camera';
import { DirectionalLight } from './directional-light';
import { Light } from './light';
import { Model } from './model';
import { PlanarShadow } from './planar-shadow';
import { PointLight } from './point-light';
import { Skybox } from './skybox';
import { SpotLight } from './spot-light';
@@ -54,6 +55,14 @@ export class RenderScene {
return this._skybox;
}
get planarShadow (): PlanarShadow {
return this._planarShadow;
}
get defaultMainLightNode (): Node {
return this._defaultMainLightNode;
}
get mainLight (): DirectionalLight {
return this._mainLight;
}
@@ -83,7 +92,9 @@ export class RenderScene {
private _cameras: Camera[] = [];
private _ambient: Ambient;
private _skybox: Skybox;
private _planarShadow: PlanarShadow;
private _mainLight: DirectionalLight;
private _defaultMainLightNode: Node;
private _pointLights: PointLight[] = [];
private _directionalLights: DirectionalLight[] = [];
private _spotLights: SpotLight[] = [];
@@ -92,14 +103,15 @@ export class RenderScene {
constructor (root: Root) {
this._root = root;
this._ambient = new Ambient (this);
this._mainLight = new DirectionalLight(this, 'Main Light', new Node('Main Light'));
this._ambient = new Ambient(this);
this._defaultMainLightNode = new Node('Main Light');
this._mainLight = new DirectionalLight(this, 'Main Light', this._defaultMainLightNode);
this._skybox = new Skybox(this);
this._planarShadow = new PlanarShadow(this);
}
public initialize (info: IRenderSceneInfo): boolean {
this._name = info.name;
// this._mainLight.direction = new Vec3(1.0, -1.0, -1.0); // should be in editor prefab
return true;
}
@@ -131,7 +143,7 @@ export class RenderScene {
}
public createDirectionalLight (name: string, node: Node): Light | null {
if (this._directionalLights.length >= UBOForwardLights.MAX_DIR_LIGHTS) { return null; }
if (this._directionalLights.length >= UBOForwardLight.MAX_DIR_LIGHTS) { return null; }
const light = new DirectionalLight(this, name, node);
this._directionalLights.push(light);
return light;
@@ -147,7 +159,7 @@ export class RenderScene {
}
public createPointLight (name: string, node: Node): Light | null {
if (this._pointLights.length >= UBOForwardLights.MAX_POINT_LIGHTS) { return null; }
if (this._pointLights.length >= UBOForwardLight.MAX_POINT_LIGHTS) { return null; }
const light = new PointLight(this, name, node);
this._pointLights.push(light);
return light;
@@ -163,7 +175,7 @@ export class RenderScene {
}
public createSpotLight (name: string, node: Node): Light | null {
if (this._spotLights.length >= UBOForwardLights.MAX_SPOT_LIGHTS) { return null; }
if (this._spotLights.length >= UBOForwardLight.MAX_SPOT_LIGHTS) { return null; }
const light = new SpotLight(this, name, node);
this._spotLights.push(light);
return light;
+68 -5
View File
@@ -25,11 +25,17 @@
import { TextureCube } from '../3d/assets/texture-cube';
import { ccclass, property } from '../core/data/class-decorator';
import { Color } from '../core/value-types';
import { color4 } from '../core/vmath';
import { Color, Quat, Vec3 } from '../core/value-types';
import { color4, vec3 } from '../core/vmath';
import { Ambient } from '../renderer/scene/ambient';
import { PlanarShadow } from '../renderer/scene/planar-shadow';
import { RenderScene } from '../renderer/scene/render-scene';
import { Skybox } from '../renderer/scene/skybox';
import { Node } from './node';
const _up = new Vec3(0, 1, 0);
const _v3 = new Vec3();
const _qt = new Quat();
@ccclass('cc.AmbientInfo')
export class AmbientInfo {
@@ -44,7 +50,7 @@ export class AmbientInfo {
@property({ type: Color })
set skyColor (val: Color) {
this._skyColor = val;
this._skyColor.set(val);
if (this._resource) { color4.array(this._resource.skyColor, this.skyColor); }
}
get skyColor () {
@@ -62,7 +68,7 @@ export class AmbientInfo {
@property({ type: Color })
set groundAlbedo (val: Color) {
this._groundAlbedo = val;
this._groundAlbedo.set(val);
if (this._resource) { color4.array(this._resource.groundAlbedo, this.groundAlbedo); }
}
get groundAlbedo () {
@@ -80,7 +86,7 @@ cc.AmbientInfo = AmbientInfo;
@ccclass('cc.SkyboxInfo')
export class SkyboxInfo {
@property
@property(TextureCube)
protected _cubemap: TextureCube | null = null;
@property
protected _isRGBE: boolean = false;
@@ -125,16 +131,73 @@ export class SkyboxInfo {
}
cc.SkyboxInfo = SkyboxInfo;
@ccclass('cc.PlanarShadowInfo')
export class PlanarShadowInfo {
@property
protected _normal = new Vec3(0, 1, 0);
@property
protected _distance = 0;
@property
protected _shadowColor = new Color(76, 76, 76, 255);
protected _resource: PlanarShadow | null = null;
@property({ type: Vec3 })
set normal (val: Vec3) {
vec3.copy(this._normal, val);
if (this._resource) { this._resource.normal = val; }
}
get normal () {
return this._normal;
}
@property({ type: Number })
set distance (val: number) {
this._distance = val;
if (this._resource) { this._resource.distance = val; }
}
get distance () {
return this._distance;
}
@property({ type: Color })
set shadowColor (val: Color) {
this._shadowColor.set(val);
if (this._resource) { this._resource.shadowColor = val; }
}
get shadowColor () {
return this._shadowColor;
}
public setPlaneFromNode (node: Node) {
node.getWorldRotation(_qt);
this.normal = vec3.transformQuat(_v3, _up, _qt);
node.getWorldPosition(_v3);
this.distance = vec3.dot(this._normal, _v3);
}
set renderScene (val: RenderScene) {
this._resource = val.planarShadow;
this.normal = this._normal;
this.distance = this._distance;
this.shadowColor = this._shadowColor;
}
}
cc.PlanarShadowInfo = PlanarShadowInfo;
@ccclass('cc.SceneGlobals')
export class SceneGlobals {
@property({ type: AmbientInfo })
public ambient = new AmbientInfo();
@property({ type: SkyboxInfo })
public skybox = new SkyboxInfo();
@property({ type: PlanarShadowInfo })
public planarShadow = new PlanarShadowInfo();
set renderScene (rs: RenderScene) {
this.ambient.renderScene = rs;
this.skybox.renderScene = rs;
this.planarShadow.renderScene = rs;
}
}
cc.SceneGlobals = SceneGlobals;
+118
View File
@@ -0,0 +1,118 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>planar shadow</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #333;
margin: 0px;
overflow: hidden;
}
.fit {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body class="fit">
<canvas id="canvas" tabindex="-1" class="fit"></canvas>
<script src="../bin/cocos-3d.dev.js"></script>
<script src="./camera.js"></script>
<script>
const $body = document.body;
const bcr = $body.getBoundingClientRect();
const canv = document.getElementById('canvas');
canv.width = bcr.width;
canv.height = bcr.height;
window.addEventListener('resize', () => {
const bcr = $body.getBoundingClientRect();
cc.director.root.resize(bcr.width, bcr.height);
});
const getLightPlaneProj = (light, plane) => {
const lx = light.x, ly = light.y, lz = light.z;
const n = plane.n, d = plane.d + 0.01;
const nx = n.x, ny = n.y, nz = n.z;
const NdL = cc.vmath.vec3.dot(plane.n, light);
return cc.mat4(
NdL - d - lx * nx, -ly * nx, -lz * nx, -nx,
-lx * ny, NdL - d - ly * ny, -lz * ny, -ny,
-lx * nz, -ly * nz, NdL - d - lz * nz, -nz,
lx * d, ly * d, lz * d, NdL);
};
function onStart() {
const root = cc.director.root;
const scene = new cc.Scene();
const rt = new cc.Node();
rt.parent = scene;
const altitude = 0;
rt.setPosition(0, altitude, 0);
const cameraNode = new cc.Node('Camera');
cameraNode.parent = rt;
cameraNode.setPosition(-4, 10, -17);
cameraNode.setRotationFromEuler(-30, -165, 0);
cameraNode.addComponent(cc.CameraComponent);
cameraNode.addComponent(FirstPersonCamera);
const lightNode = new cc.Node('Light');
lightNode.parent = rt;
lightNode.setPosition(-10, 10, -10);
const lightComp = lightNode.addComponent(cc.LightComponent);
lightComp.isMainLight = true;
// parsed effect file is embedded in cocos/3d/builtin/effects.js
const noShadow = new cc.Material();
noShadow.initialize({ effectName: 'builtin-standard' });
const material = new cc.Material();
material.initialize({
effectName: 'builtin-standard',
defines: { USE_PLANAR_SHADOW: true },
});
const manifest = [
{ name: 'box', pos: cc.v3(0, 1, 0) },
{ name: 'sphere', pos: cc.v3(5, 1, 5) },
{ name: 'cylinder', pos: cc.v3(-5, 1, -5) },
{ name: 'torus', pos: cc.v3(5, 1, -5) },
{ name: 'cone', pos: cc.v3(-5, 1, 5) },
{ name: 'plane', pos: cc.v3(0, 0, 0), mat: noShadow, param: { width: 100, length: 100 } },
];
const models = {};
for (const info of manifest) {
const modelNode = new cc.Node(`${info.name}`);
modelNode.parent = rt;
const modelCom = modelNode.addComponent('cc.ModelComponent');
modelCom.material = info.mat || material;
modelCom.mesh = cc.utils.createMesh(cc.primitives[info.name](info.param));
modelNode.setPosition(info.pos);
models[info.name] = modelCom;
}
const qt = cc.vmath.quat.fromEuler(cc.quat(), 0, 0, 0);
const nm = cc.vmath.vec3.transformQuat(cc.v3(), cc.v3(0, 1, 0), qt);
models.plane.node.setRotation(qt);
scene.globals.planarShadow.setPlaneFromNode(models.plane.node);
// material.setProperty('matLightPlaneProj', getLightPlaneProj(cc.v3(-10, altitude + 10, -10), // light
// cc.geometry.plane.create(nm.x, nm.y, nm.z, cc.vmath.vec3.dot(nm, models.plane.node.getWorldPosition()))));
cc.director.runSceneImmediate(scene);
}
cc.game.run({ id: 'canvas' }, onStart);
</script>
</body>
</html>