fix render scene error (#5790)

This commit is contained in:
zxg0622
2019-11-26 15:04:16 +08:00
committed by pandamicro
parent 14bcb2e49e
commit 53ed7da2db
12 changed files with 57 additions and 49 deletions
@@ -80,7 +80,6 @@ export class DirectionalLightComponent extends LightComponent {
protected _destroyLight (scene?: RenderScene) {
if (!this.node.scene || !this._light) { return; }
this._light.enabled = false;
scene = scene || this._getRenderScene();
this._light.node = scene.defaultMainLightNode;
}
+7 -1
View File
@@ -166,8 +166,11 @@ export class LightComponent extends Component {
protected _attachToScene() {
this._detachFromScene();
if (this._light && !this._light.scene && !this.node.scene) {
if (this._light && !this._light.scene && this.node.scene) {
switch (this._type) {
case LightType.DIRECTIONAL:
this._light.attachToScene(this._getRenderScene());
break;
case LightType.SPHERE:
this._getRenderScene().addSphereLight(this._light as any);
break;
@@ -181,6 +184,9 @@ export class LightComponent extends Component {
protected _detachFromScene() {
if (this._light && this._light.scene) {
switch (this._type) {
case LightType.DIRECTIONAL:
this._light.detachFromScene();
break;
case LightType.SPHERE:
this._light.scene.removeSphereLight(this._light as any);
break;
@@ -205,7 +205,7 @@ export class ForwardPipeline extends RenderPipeline {
let spotNum = 0;
for (let l = this._lightIndexOffset[i]; l < nextLightIndex; l++) {
const light = this._validLights[this._lightIndices[l]];
if (light && light.enabled) {
if (light) {
switch (light.type) {
case LightType.SPHERE:
if (sphereNum >= UBOForwardLight.MAX_SPHERE_LIGHTS) {
@@ -289,23 +289,19 @@ export class ForwardPipeline extends RenderPipeline {
const sphereLights = view.camera.scene!.sphereLights;
for (let i = 0; i < sphereLights.length; i++) {
const light = sphereLights[i];
if (light.enabled) {
light.update();
sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
if (intersect.sphere_frustum(_sphere, view.camera.frustum)) {
this._validLights.push(light);
}
light.update();
sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
if (intersect.sphere_frustum(_sphere, view.camera.frustum)) {
this._validLights.push(light);
}
}
const spotLights = view.camera.scene!.spotLights;
for (let i = 0; i < spotLights.length; i++) {
const light = spotLights[i];
if (light.enabled) {
light.update();
sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
if (intersect.sphere_frustum(_sphere, view.camera.frustum)) {
this._validLights.push(light);
}
light.update();
sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
if (intersect.sphere_frustum(_sphere, view.camera.frustum)) {
this._validLights.push(light);
}
}
+2 -2
View File
@@ -1163,7 +1163,7 @@ export abstract class RenderPipeline {
Vec3.toArray(fv, mainLight.direction, UBOGlobal.MAIN_LIT_DIR_OFFSET);
if (mainLight.enabled) {
if (mainLight.scene) {
Vec3.toArray(fv, mainLight.color, UBOGlobal.MAIN_LIT_COLOR_OFFSET);
if (mainLight.useColorTemperature) {
const colorTempRGB = mainLight.colorTemperatureRGB;
@@ -1208,7 +1208,7 @@ export abstract class RenderPipeline {
this._renderObjects.length = 0;
const mainLight = scene.mainLight;
if (mainLight && mainLight.enabled) {
if (mainLight && mainLight.scene) {
mainLight.update();
}
+3 -3
View File
@@ -85,7 +85,7 @@ export class SkinningModel extends Model {
this._mesh = mesh;
if (!skeleton || !skinningRoot || !mesh) { return; }
this._transform = skinningRoot;
this._jointsMedium.animInfo = this._scene!.root.dataPoolManager.jointsAnimationInfo.get(skinningRoot.uuid);
this._jointsMedium.animInfo = cc.director.root.dataPoolManager.jointsAnimationInfo.get(skinningRoot.uuid);
if (!this._jointsMedium.buffer) {
this._jointsMedium.buffer = this._device.createBuffer({
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
@@ -124,7 +124,7 @@ export class SkinningModel extends Model {
public uploadAnimation (anim: AnimationClip | null) {
if (!this._skeleton || !this._mesh) { return; }
this.uploadedAnim = anim;
const resMgr = this._scene!.root.dataPoolManager;
const resMgr = cc.director.root.dataPoolManager;
const texture = anim ? resMgr.jointsTexturePool.getJointsTextureWithAnimation(this._skeleton, anim) :
resMgr.jointsTexturePool.getDefaultJointsTexture(this._skeleton);
resMgr.jointsAnimationInfo.switchClip(this._jointsMedium.animInfo!, anim);
@@ -136,7 +136,7 @@ export class SkinningModel extends Model {
protected _applyJointsTexture (texture: IJointsTextureHandle | null) {
if (!texture) { return; }
const oldTex = this._jointsMedium.texture;
if (oldTex && oldTex !== texture) { this._scene!.root.dataPoolManager.jointsTexturePool.releaseHandle(oldTex); }
if (oldTex && oldTex !== texture) { cc.director.root.dataPoolManager.jointsTexturePool.releaseHandle(oldTex); }
this._jointsMedium.texture = texture;
const { buffer, jointsTextureInfo } = this._jointsMedium;
jointsTextureInfo[0] = texture.handle.texture.width;
@@ -1,5 +1,4 @@
import { Quat, Vec3 } from '../../math';
import { TransformBit } from '../../scene-graph/node-enum';
import { INode } from '../../utils/interfaces';
import { Light, LightType } from './light';
import { RenderScene } from './render-scene';
@@ -34,7 +33,6 @@ export class DirectionalLight extends Light {
constructor () {
super();
this._type = LightType.DIRECTIONAL;
this._node.hasChangedFlags = TransformBit.ROTATION;
}
public update () {
+4 -8
View File
@@ -1,6 +1,7 @@
import { Vec3 } from '../../math';
import { INode } from '../../utils/interfaces';
import { RenderScene } from './render-scene';
import { TransformBit } from '../../scene-graph/node-enum';
// Color temperature (in Kelvin) to RGB
export function ColorTemperatureToRGB (rgb: Vec3, kelvin: number) {
@@ -40,13 +41,6 @@ export const nt2lm = (size: number) => 4 * Math.PI * Math.PI * size * size;
export class Light {
set enabled (val) {
this._enabled = val;
}
get enabled () {
return this._enabled;
}
set color (color: Vec3) {
this._color.set(color);
}
@@ -78,6 +72,9 @@ export class Light {
set node (n) {
this._node = n;
if (this._node) {
this._node.hasChangedFlags = TransformBit.ROTATION;
}
}
get node () {
@@ -96,7 +93,6 @@ export class Light {
return this._scene;
}
protected _enabled = true;
protected _color: Vec3 = new Vec3(1, 1, 1);
protected _useColorTemp: boolean = false;
protected _colorTemp: number = 6550.0;
+1 -1
View File
@@ -146,7 +146,7 @@ export class Model {
protected _node: INode | null = null;
protected _transform: INode | null = null;
protected _id: number = -1;
protected _enabled: boolean = false;
protected _enabled: boolean = true;
protected _visFlags = Layers.Enum.NONE;
protected _cameraID = -1;
protected _userKey = -1;
@@ -132,7 +132,6 @@ export class RenderScene {
this._mainLight = new DirectionalLight();
this._mainLight.initialize('Main Light', this._defaultMainLightNode);
this._mainLight.illuminance = Ambient.SUN_ILLUM;
this._mainLight.enabled = false; // disabled by default
this._ambient = new Ambient(this);
this._skybox = new Skybox(this);
this._planarShadows = new PlanarShadows(this);
+1 -1
View File
@@ -376,7 +376,7 @@ export class UI {
}
}
const uiCanvas = this.getScreen(comp.node.uiTransfromComp.visibility);
const uiCanvas = this._currCanvas;
const curDrawBatch = this._drawBatchPool.alloc();
curDrawBatch.camera = uiCanvas && uiCanvas.camera;
curDrawBatch.model = model;
@@ -176,11 +176,10 @@ export const graphicsAssembler: IAssembler = {
},
end (graphics: GraphicsComponent){
const scene = director.root!.ui.renderScene as RenderScene;
if (graphics.model){
if (graphics.model) {
graphics.model.destroy();
scene.destroyModel(graphics.model);
graphics.model = null;
} else {
graphics.model = new Model();
}
const impl = graphics.impl;
@@ -226,9 +225,8 @@ export const graphicsAssembler: IAssembler = {
indices,
}, undefined, { calculateBounds: false });
graphics.model = scene.createModel(Model, graphics.node);
graphics.model.initSubModel(0, mesh.getSubMesh(0), graphics.material!);
graphics.model.enabled = true;
graphics.model!.initialize(graphics.node);
graphics.model!.initSubModel(0, mesh.getSubMesh(0), graphics.material!);
graphics.markForUpdateRenderData();
},
+25 -9
View File
@@ -214,22 +214,21 @@ export class GraphicsComponent extends UIRenderComponent {
public onLoad (){
this._sceneGetter = director.root!.ui.getRenderSceneGetter();
if (!this.model) {
this.model = new Model();
}
}
public onEnable () {
super.onEnable();
if (this.model){
this.model.enabled = true;
}
this._attachToScene();
this._activateMaterial();
}
public onDisable (){
if (this.model){
this.model.enabled = false;
}
this._detachFromScene();
}
public onDestroy () {
@@ -237,7 +236,7 @@ export class GraphicsComponent extends UIRenderComponent {
this._sceneGetter = null;
if (this.model) {
this._getRenderScene().destroyModel(this.model);
this.model.destroy();
this.model = null;
}
@@ -249,6 +248,23 @@ export class GraphicsComponent extends UIRenderComponent {
this.impl = null;
}
protected _attachToScene(){
const scene = director.root!.ui.renderScene;
if (!this.model) {
return;
}
if (this.model!.scene != null) {
this._detachFromScene();
}
scene.addModel(this.model!);
}
protected _detachFromScene() {
if (this.model && this.model.scene) {
this.model.scene.removeModel(this.model);
}
}
public _activateMaterial () {
if (!this._material) {
return;
@@ -435,8 +451,8 @@ export class GraphicsComponent extends UIRenderComponent {
this.impl.clear(clean);
if (this.model) {
this.model.scene.destroyModel(this.model);
this.model = null;
this._detachFromScene();
this.model.destroy();
}
this.markForUpdateRenderData();