mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
fixed light pipeline
This commit is contained in:
Vendored
+7
-141
File diff suppressed because one or more lines are too long
@@ -251,8 +251,8 @@ export default class LightComponent extends Component {
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
if (this._light) { this._light.enabled = true; return; }
|
||||
this._createLight();
|
||||
if (this._light) { this._light.enabled = true; return; }
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
@@ -268,14 +268,14 @@ export default class LightComponent extends Component {
|
||||
const scene = this._getRenderScene();
|
||||
switch (this._type) {
|
||||
case Type.DIRECTIONAL:
|
||||
this._light = scene.createDirectionalLight(this.name);
|
||||
this._light = scene.createDirectionalLight(this.name, this.node);
|
||||
break;
|
||||
case Type.POINT:
|
||||
this._light = scene.createPointLight(this.name);
|
||||
this._light = scene.createPointLight(this.name, this.node);
|
||||
this.range = this._range;
|
||||
break;
|
||||
case Type.SPOT:
|
||||
this._light = scene.createSpotLight(this.name);
|
||||
this._light = scene.createSpotLight(this.name, this.node);
|
||||
this.range = this._range;
|
||||
this.spotAngle = this._spotAngle;
|
||||
break;
|
||||
|
||||
@@ -15,6 +15,10 @@ import { RenderQueue } from './render-queue';
|
||||
import { RenderView } from './render-view';
|
||||
|
||||
export class UBOGlobal {
|
||||
public static MAX_DIR_LIGHTS = 4;
|
||||
public static MAX_POINT_LIGHTS = 4;
|
||||
public static MAX_SPOT_LIGHTS = 4;
|
||||
|
||||
public static TIME_OFFSET: number = 0;
|
||||
public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4;
|
||||
public static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4;
|
||||
@@ -50,13 +54,13 @@ export class UBOGlobal {
|
||||
{ name: 'cc_matViewProjInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_cameraPos', type: GFXType.FLOAT4, count: 1 },
|
||||
// lights
|
||||
{ name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOGlobal.MAX_DIR_LIGHTS },
|
||||
{ name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_DIR_LIGHTS },
|
||||
{ name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOGlobal.MAX_POINT_LIGHTS },
|
||||
{ name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_POINT_LIGHTS },
|
||||
{ name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS },
|
||||
{ name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS },
|
||||
{ name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS },
|
||||
],
|
||||
};
|
||||
|
||||
@@ -79,6 +83,7 @@ export class UBOLocal {
|
||||
public view: Float32Array = new Float32Array(UBOLocal.COUNT);
|
||||
}
|
||||
|
||||
const _idVec4Array = Float32Array.from([0, 0, 0, 1]);
|
||||
const _vec4Array = new Float32Array(4);
|
||||
const _mat4Array = new Float32Array(16);
|
||||
const _outMat = new Mat4();
|
||||
@@ -354,20 +359,42 @@ export abstract class RenderPipeline {
|
||||
|
||||
const scene = view.camera.scene;
|
||||
const dLights = scene.directionalLights;
|
||||
for (let i = 0; i < dLights.length; i++) {
|
||||
this._uboGlobal.view.set(dLights[i].directionArray, UBOGlobal.DIR_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(dLights[i].colorData, UBOGlobal.DIR_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
for (let i = 0; i < UBOGlobal.MAX_DIR_LIGHTS; i++) {
|
||||
const light = dLights[i];
|
||||
if (light && light.enabled) {
|
||||
light.update();
|
||||
this._uboGlobal.view.set(light.directionArray, UBOGlobal.DIR_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(light.colorData, UBOGlobal.DIR_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
} else {
|
||||
this._uboGlobal.view.set(_idVec4Array, UBOGlobal.DIR_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(_idVec4Array, UBOGlobal.DIR_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
}
|
||||
}
|
||||
const pLights = scene.pointLights;
|
||||
for (let i = 0; i < pLights.length; i++) {
|
||||
this._uboGlobal.view.set(pLights[i].positionAndRange, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(pLights[i].colorData, UBOGlobal.POINT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
for (let i = 0; i < UBOGlobal.MAX_POINT_LIGHTS; i++) {
|
||||
const light = pLights[i];
|
||||
if (light && light.enabled) {
|
||||
light.update();
|
||||
this._uboGlobal.view.set(light.positionAndRange, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(light.colorData, UBOGlobal.POINT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
} else {
|
||||
this._uboGlobal.view.set(_idVec4Array, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(_idVec4Array, UBOGlobal.POINT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
}
|
||||
}
|
||||
const sLights = scene.spotLights;
|
||||
for (let i = 0; i < sLights.length; i++) {
|
||||
this._uboGlobal.view.set(sLights[i].positionAndRange, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(sLights[i].directionArray, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(sLights[i].colorData, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
for (let i = 0; i < UBOGlobal.MAX_SPOT_LIGHTS; i++) {
|
||||
const light = sLights[i];
|
||||
if (light && light.enabled) {
|
||||
light.update();
|
||||
this._uboGlobal.view.set(light.positionAndRange, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(light.directionArray, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(light.colorData, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
} else {
|
||||
this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
}
|
||||
}
|
||||
|
||||
// update ubos
|
||||
|
||||
@@ -125,7 +125,6 @@ class ProgramLib {
|
||||
if (program !== undefined) {
|
||||
return program;
|
||||
}
|
||||
defines.CC_NUM_DIR_LIGHTS = 1;
|
||||
|
||||
// get template
|
||||
const tmpl = this._templates[name];
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { Vec3, Quat } from '../../core/value-types';
|
||||
import { vec3 } from '../../core/vmath';
|
||||
import { Light, LightType } from './light';
|
||||
import { RenderScene } from './render-scene';
|
||||
import { Node } from '../../scene-graph/node';
|
||||
|
||||
const _forward = new Vec3(0, 0, 1);
|
||||
const _v3 = new Vec3();
|
||||
const _qt = new Quat();
|
||||
|
||||
export class DirectionalLight extends Light {
|
||||
protected _direction = Float32Array.from([0, 0, 1, 0]);
|
||||
@@ -13,8 +18,12 @@ export class DirectionalLight extends Light {
|
||||
return this._direction;
|
||||
}
|
||||
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
super(scene, name);
|
||||
constructor (scene: RenderScene, node: Node, name: string) {
|
||||
super(scene, node, name);
|
||||
this._type = LightType.DIRECTIONAL;
|
||||
}
|
||||
|
||||
public update () {
|
||||
this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { RenderScene } from './render-scene';
|
||||
import { Node } from '../../scene-graph/node';
|
||||
|
||||
export enum LightType {
|
||||
UNKNOWN,
|
||||
@@ -10,6 +11,10 @@ export enum LightType {
|
||||
export class Light {
|
||||
protected _color = Float32Array.from([1, 1, 1, 1]);
|
||||
protected _enabled = false;
|
||||
protected _scene: RenderScene;
|
||||
protected _node: Node;
|
||||
protected _type: LightType;
|
||||
protected _name: string;
|
||||
|
||||
set enabled (val) {
|
||||
this._enabled = val;
|
||||
@@ -32,12 +37,11 @@ export class Light {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
protected _scene: RenderScene;
|
||||
protected _type: LightType;
|
||||
protected _name: string;
|
||||
public update () {}
|
||||
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
constructor (scene: RenderScene, node: Node, name: string) {
|
||||
this._scene = scene;
|
||||
this._node = node;
|
||||
this._name = name;
|
||||
this._type = LightType.UNKNOWN;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ import { Vec3 } from '../../core/value-types';
|
||||
import { vec3 } from '../../core/vmath';
|
||||
import { Light, LightType } from './light';
|
||||
import { RenderScene } from './render-scene';
|
||||
import { Node } from '../../scene-graph/node';
|
||||
|
||||
const _v3 = new Vec3();
|
||||
|
||||
export class PointLight extends Light {
|
||||
protected _positionAndRange = Float32Array.from([0, 0, 0, 10]);
|
||||
@@ -17,8 +20,12 @@ export class PointLight extends Light {
|
||||
return this._positionAndRange;
|
||||
}
|
||||
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
super(scene, name);
|
||||
constructor (scene: RenderScene, node: Node, name: string) {
|
||||
super(scene, node, name);
|
||||
this._type = LightType.POINT;
|
||||
}
|
||||
|
||||
public update () {
|
||||
this.position = this._node.getWorldPosition(_v3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import { Light } from './light';
|
||||
import { Model } from './model';
|
||||
import { PointLight } from './point-light';
|
||||
import { SpotLight } from './spot-light';
|
||||
import { UBOGlobal } from '../../pipeline/render-pipeline';
|
||||
|
||||
export interface IRenderSceneInfo {
|
||||
name: string;
|
||||
@@ -97,7 +98,7 @@ export class RenderScene {
|
||||
public destroyCamera (camera: Camera) {
|
||||
for (let i = 0; i < this._cameras.length; ++i) {
|
||||
if (this._cameras[i] === camera) {
|
||||
this._cameras.slice(i);
|
||||
this._cameras.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -117,9 +118,9 @@ export class RenderScene {
|
||||
return null;
|
||||
}
|
||||
|
||||
public createPointLight (name: string): Light | null {
|
||||
if (this._pointLights.length >= 4) { return null; }
|
||||
const light = new PointLight(this, name);
|
||||
public createPointLight (name: string, node: Node): Light | null {
|
||||
if (this._pointLights.length >= UBOGlobal.MAX_POINT_LIGHTS) { return null; }
|
||||
const light = new PointLight(this, node, name);
|
||||
this._pointLights.push(light);
|
||||
return light;
|
||||
}
|
||||
@@ -127,15 +128,15 @@ export class RenderScene {
|
||||
public destroyPointLight (light: Light) {
|
||||
for (let i = 0; i < this._pointLights.length; ++i) {
|
||||
if (this._pointLights[i] === light) {
|
||||
this._pointLights.slice(i);
|
||||
this._pointLights.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public createDirectionalLight (name: string): Light | null {
|
||||
if (this._directionalLights.length >= 4) { return null; }
|
||||
const light = new DirectionalLight(this, name);
|
||||
public createDirectionalLight (name: string, node: Node): Light | null {
|
||||
if (this._directionalLights.length >= UBOGlobal.MAX_DIR_LIGHTS) { return null; }
|
||||
const light = new DirectionalLight(this, node, name);
|
||||
this._directionalLights.push(light);
|
||||
return light;
|
||||
}
|
||||
@@ -143,15 +144,15 @@ export class RenderScene {
|
||||
public destroyDirectionalLight (light: Light) {
|
||||
for (let i = 0; i < this._directionalLights.length; ++i) {
|
||||
if (this._directionalLights[i] === light) {
|
||||
this._directionalLights.slice(i);
|
||||
this._directionalLights.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public createSpotLight (name: string): Light | null {
|
||||
if (this._spotLights.length >= 4) { return null; }
|
||||
const light = new SpotLight(this, name);
|
||||
public createSpotLight (name: string, node: Node): Light | null {
|
||||
if (this._spotLights.length >= UBOGlobal.MAX_SPOT_LIGHTS) { return null; }
|
||||
const light = new SpotLight(this, node, name);
|
||||
this._spotLights.push(light);
|
||||
return light;
|
||||
}
|
||||
@@ -159,7 +160,7 @@ export class RenderScene {
|
||||
public destroySpotLight (light: Light) {
|
||||
for (let i = 0; i < this._spotLights.length; ++i) {
|
||||
if (this._spotLights[i] === light) {
|
||||
this._spotLights.slice(i);
|
||||
this._spotLights.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { Vec3, Quat } from '../../core/value-types';
|
||||
import { vec3 } from '../../core/vmath';
|
||||
import { Light, LightType } from './light';
|
||||
import { RenderScene } from './render-scene';
|
||||
import { Node } from '../../scene-graph/node';
|
||||
|
||||
const _forward = new Vec3(0, 0, 1);
|
||||
const _v3 = new Vec3();
|
||||
const _qt = new Quat();
|
||||
|
||||
export class SpotLight extends Light {
|
||||
protected _direction = Float32Array.from([0, 0, 1, 0]);
|
||||
protected _direction = Float32Array.from([0, 0, 1, Math.cos(Math.PI / 6)]);
|
||||
protected _positionAndRange = Float32Array.from([0, 0, 0, 10]);
|
||||
|
||||
set position (val: Vec3) {
|
||||
@@ -17,7 +22,7 @@ export class SpotLight extends Light {
|
||||
vec3.array(this._direction, val);
|
||||
}
|
||||
set spotAngle (val: number) {
|
||||
this._direction[3] = val;
|
||||
this._direction[3] = Math.cos(val * 0.5);
|
||||
}
|
||||
|
||||
get positionAndRange () {
|
||||
@@ -27,8 +32,13 @@ export class SpotLight extends Light {
|
||||
return this._direction;
|
||||
}
|
||||
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
super(scene, name);
|
||||
constructor (scene: RenderScene, node: Node, name: string) {
|
||||
super(scene, node, name);
|
||||
this._type = LightType.DIRECTIONAL;
|
||||
}
|
||||
|
||||
public update () {
|
||||
this.position = this._node.getWorldPosition(_v3);
|
||||
this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@
|
||||
const material = new cc.Material();
|
||||
material.setDefines({ USE_COLOR: true });
|
||||
// parsed effect file is embedded in cocos/3d/builtin/effects.js
|
||||
material.effectName = 'builtin-effect-pbr';
|
||||
material.effectName = 'builtin-effect-unlit';
|
||||
|
||||
const len = 50;
|
||||
const models = [], passes = [];
|
||||
@@ -84,7 +84,7 @@
|
||||
// material instance initialized here
|
||||
passes.push(modelCom.material.passes[0]);
|
||||
}
|
||||
const handle = passes[0].getHandle('albedo');
|
||||
const handle = passes[0].getHandle('color');
|
||||
|
||||
{
|
||||
const material = new cc.Material();
|
||||
|
||||
+4
-1
@@ -9,6 +9,10 @@
|
||||
true,
|
||||
"single"
|
||||
],
|
||||
"one-line": [
|
||||
true,
|
||||
"check-open-brace"
|
||||
],
|
||||
"no-console": false,
|
||||
"space-before-function-paren": true,
|
||||
"max-classes-per-file": false,
|
||||
@@ -18,7 +22,6 @@
|
||||
"no-bitwise": false,
|
||||
"no-empty": false,
|
||||
"radix": false,
|
||||
"one-line": false,
|
||||
},
|
||||
"rulesDirectory": []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user