fix spot light culling (#5841)

This commit is contained in:
Yun Hsiao Wu
2019-12-04 19:12:47 +08:00
committed by pandamicro
parent bb5934fcac
commit 851ea13ef2
4 changed files with 16 additions and 10 deletions
+2 -2
View File
@@ -3,7 +3,7 @@
*/
import { frustum, intersect } from '../geom-utils';
import { Mat4, Vec3, Quat } from '../math';
import { Mat4, Quat, Vec3 } from '../math';
import { Camera, Light, Model } from '../renderer';
import { DirectionalLight } from '../renderer/scene/directional-light';
import { SphereLight } from '../renderer/scene/sphere-light';
@@ -24,7 +24,7 @@ export function cullSphereLight (light: SphereLight, model: Model) {
export function cullSpotLight (light: SpotLight, model: Model) {
return cullLight(light, model) || !!(model.worldBounds &&
(!intersect.aabb_aabb(model.worldBounds, light.aabb) || !intersect.aabb_frustum(model.worldBounds, light.frustum)));
(!intersect.aabb_aabb(model.worldBounds, light.aabb) || !intersect.aabb_frustum(model.worldBounds, light.frustum)));
}
export const cullSceneWithDirectionalLight = (() => {
@@ -321,7 +321,7 @@ export class ForwardPipeline extends RenderPipeline {
* @param model 模型。
*/
private cullLightPerModel (model: Model) {
_tempLightIndex.splice(0);
_tempLightIndex.length = 0;
for (let i = 0; i < this._validLights.length; i++) {
let isCulled = false;
switch (this._validLights[i].type) {
@@ -345,7 +345,7 @@ export class ForwardPipeline extends RenderPipeline {
}
}
_tempLightIndex.sort(this.sortLight);
this._lightIndices.push(..._tempLightIndex);
Array.prototype.push.apply(this._lightIndices, _tempLightIndex);
}
private sortLight (a, b) {
+5 -3
View File
@@ -1,11 +1,11 @@
import { aabb } from '../../geom-utils';
import { Vec3 } from '../../math';
import { INode } from '../../utils/interfaces';
import { Light, LightType, nt2lm } from './light';
import { RenderScene } from './render-scene';
export class SphereLight extends Light {
protected _needUpdate = false;
get position () {
return this._pos;
}
@@ -20,6 +20,7 @@ export class SphereLight extends Light {
set range (range: number) {
this._range = range;
this._needUpdate = true;
}
get range (): number {
@@ -52,9 +53,10 @@ export class SphereLight extends Light {
}
public update () {
if (this._node) {
if (this._node && (this._node.hasChangedFlags || this._needUpdate)) {
this._node.getWorldPosition(this._pos);
aabb.set(this._aabb, this._pos.x, this._pos.y, this._pos.z, this._range, this._range, this._range);
this._needUpdate = false;
}
}
}
+7 -3
View File
@@ -22,6 +22,7 @@ export class SpotLight extends Light {
protected _aabb: aabb;
protected _frustum: frustum;
protected _angle: number = 0;
protected _needUpdate = false;
get position () {
return this._pos;
@@ -37,6 +38,7 @@ export class SpotLight extends Light {
set range (range: number) {
this._range = range;
this._needUpdate = true;
}
get range (): number {
@@ -62,6 +64,7 @@ export class SpotLight extends Light {
set spotAngle (val: number) {
this._angle = val * 0.5;
this._spotAngle = Math.cos(val * 0.5);
this._needUpdate = true;
}
get aabb () {
@@ -81,9 +84,9 @@ export class SpotLight extends Light {
}
public update () {
if (this._node) {
if (this._node && (this._node.hasChangedFlags || this._needUpdate)) {
this._node.getWorldPosition(this._pos);
this._dir = Vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
Vec3.transformQuat(this._dir, _forward, this._node.getWorldRotation(_qt));
Vec3.normalize(this._dir, this._dir);
aabb.set(this._aabb, this._pos.x, this._pos.y, this._pos.z, this._range, this._range, this._range);
@@ -95,9 +98,10 @@ export class SpotLight extends Light {
// view-projection
Mat4.multiply(_matViewProj, _matProj, _matView);
Mat4.invert(_matViewProjInv, _matViewProj);
// Mat4.invert(_matViewProjInv, _matViewProj);
this._frustum.update(_matViewProj, _matViewProjInv);
this._needUpdate = false;
}
}
}