mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Merge pull request #3805 from YunHsiao/3d
when to create render scene resources and when not to
This commit is contained in:
@@ -23,9 +23,9 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { ccclass, mixins } from "../../core/data/class-decorator";
|
||||
import { ccclass } from "../../core/data/class-decorator";
|
||||
import { Asset } from "../../assets/asset";
|
||||
import { EventTarget } from "../../core/event/index";
|
||||
import { EventTargetFactory } from "../../core/event/event-target-factory";
|
||||
|
||||
export const PlayingState = {
|
||||
INITIALIZING: 0,
|
||||
@@ -52,8 +52,7 @@ export interface IAudioInfo {
|
||||
}
|
||||
|
||||
@ccclass('cc.AudioClip')
|
||||
@mixins(EventTarget)
|
||||
export class AudioClip extends Asset {
|
||||
export class AudioClip extends EventTargetFactory(Asset) {
|
||||
protected _audio: any = null;
|
||||
protected _duration = 0;
|
||||
protected _state = PlayingState.INITIALIZING;
|
||||
|
||||
+24
-25
@@ -20,7 +20,6 @@ class BuiltinResMgr {
|
||||
const context = canvas.getContext('2d')!;
|
||||
const imgAsset = new ImageAsset(canvas);
|
||||
const l = canvas.width = canvas.height = 2;
|
||||
const hl = l / 2;
|
||||
|
||||
// ============================
|
||||
// builtin textures
|
||||
@@ -51,18 +50,18 @@ class BuiltinResMgr {
|
||||
resources[whiteTexture._uuid] = whiteTexture;
|
||||
|
||||
// white cube texture
|
||||
// const whiteCubeTexture = new TextureCube();
|
||||
// whiteCubeTexture._uuid = 'white-cube-texture';
|
||||
// whiteCubeTexture.image = {
|
||||
// front: new ImageAsset(canvas),
|
||||
// back: new ImageAsset(canvas),
|
||||
// left: new ImageAsset(canvas),
|
||||
// right: new ImageAsset(canvas),
|
||||
// top: new ImageAsset(canvas),
|
||||
// bottom: new ImageAsset(canvas),
|
||||
// };
|
||||
// whiteCubeTexture.onLoaded();
|
||||
// resources[whiteCubeTexture._uuid] = whiteCubeTexture;
|
||||
const whiteCubeTexture = new TextureCube();
|
||||
whiteCubeTexture._uuid = 'white-cube-texture';
|
||||
whiteCubeTexture.image = {
|
||||
front: new ImageAsset(canvas),
|
||||
back: new ImageAsset(canvas),
|
||||
left: new ImageAsset(canvas),
|
||||
right: new ImageAsset(canvas),
|
||||
top: new ImageAsset(canvas),
|
||||
bottom: new ImageAsset(canvas),
|
||||
};
|
||||
whiteCubeTexture.onLoaded();
|
||||
resources[whiteCubeTexture._uuid] = whiteCubeTexture;
|
||||
|
||||
// normal texture
|
||||
context.fillStyle = '#7f7fff';
|
||||
@@ -86,18 +85,18 @@ class BuiltinResMgr {
|
||||
resources[defaultTexture._uuid] = defaultTexture;
|
||||
|
||||
// default cube texture
|
||||
// const defaultCubeTexture = new TextureCube();
|
||||
// defaultCubeTexture._uuid = 'default-cube-texture';
|
||||
// defaultCubeTexture.image = {
|
||||
// front: new ImageAsset(canvas),
|
||||
// back: new ImageAsset(canvas),
|
||||
// left: new ImageAsset(canvas),
|
||||
// right: new ImageAsset(canvas),
|
||||
// top: new ImageAsset(canvas),
|
||||
// bottom: new ImageAsset(canvas),
|
||||
// };
|
||||
// defaultCubeTexture.onLoaded();
|
||||
// resources.push(defaultCubeTexture);
|
||||
const defaultCubeTexture = new TextureCube();
|
||||
defaultCubeTexture._uuid = 'default-cube-texture';
|
||||
defaultCubeTexture.image = {
|
||||
front: new ImageAsset(canvas),
|
||||
back: new ImageAsset(canvas),
|
||||
left: new ImageAsset(canvas),
|
||||
right: new ImageAsset(canvas),
|
||||
top: new ImageAsset(canvas),
|
||||
bottom: new ImageAsset(canvas),
|
||||
};
|
||||
defaultCubeTexture.onLoaded();
|
||||
resources[defaultCubeTexture._uuid] = defaultCubeTexture;
|
||||
|
||||
const spriteFrame = new SpriteFrame();
|
||||
spriteFrame._uuid = 'default-spriteframe';
|
||||
|
||||
@@ -284,15 +284,9 @@ export class CameraComponent extends Component {
|
||||
if (this._camera) { this._camera.changeTargetDisplay(val); }
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
this.node.on(cc.Node.SCENE_CHANGED_FOR_PERSISTS, () => {
|
||||
if (this._camera) { this._getRenderScene().destroyCamera(this._camera); this._camera = null; }
|
||||
});
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
if (!this._camera) { this._camera = this._getRenderScene().createCamera(this); }
|
||||
this._camera.enabled = true;
|
||||
this._createCamera();
|
||||
this._camera!.enabled = true;
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
@@ -320,4 +314,11 @@ export class CameraComponent extends Component {
|
||||
if (this._camera) { this._camera.screenToWorld(out, screenPos); }
|
||||
return out;
|
||||
}
|
||||
|
||||
protected _createCamera () {
|
||||
if (!this.node.scene) { return; }
|
||||
const scene = this._getRenderScene();
|
||||
if (this._camera && scene.cameras.find((c) => c === this._camera)) { return; }
|
||||
this._camera = scene.createCamera(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import { toRadian } from '../../core/vmath';
|
||||
import { Light, LightType } from '../../renderer/scene/light';
|
||||
import { PointLight } from '../../renderer/scene/point-light';
|
||||
import { SpotLight } from '../../renderer/scene/spot-light';
|
||||
import { RenderScene } from '../../renderer/scene/render-scene';
|
||||
|
||||
/**
|
||||
* !#en The light source type
|
||||
@@ -250,10 +251,6 @@ export default class LightComponent extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
this.node.on(cc.Node.SCENE_CHANGED_FOR_PERSISTS, () => this._destroyLight());
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
this._createLight();
|
||||
if (this._light) { this._light.enabled = true; return; }
|
||||
@@ -267,18 +264,21 @@ export default class LightComponent extends Component {
|
||||
this._destroyLight();
|
||||
}
|
||||
|
||||
protected _createLight () {
|
||||
if (!this.node._scene || this._light) { return; }
|
||||
const scene = this._getRenderScene();
|
||||
protected _createLight (scene?: RenderScene) {
|
||||
if (!this.node.scene) { return; }
|
||||
if (!scene) scene = this._getRenderScene();
|
||||
switch (this._type) {
|
||||
case Type.DIRECTIONAL:
|
||||
if (this._light && scene.directionalLights.find((c) => c === this._light)) { break; }
|
||||
this._light = scene.createDirectionalLight(this.name, this.node);
|
||||
break;
|
||||
case Type.POINT:
|
||||
if (this._light && scene.pointLights.find((c) => c === this._light)) { break; }
|
||||
this._light = scene.createPointLight(this.name, this.node);
|
||||
this.range = this._range;
|
||||
break;
|
||||
case Type.SPOT:
|
||||
if (this._light && scene.spotLights.find((c) => c === this._light)) { break; }
|
||||
this._light = scene.createSpotLight(this.name, this.node);
|
||||
this.range = this._range;
|
||||
this.spotAngle = this._spotAngle;
|
||||
@@ -295,9 +295,9 @@ export default class LightComponent extends Component {
|
||||
this._light.enabled = this.enabledInHierarchy;
|
||||
}
|
||||
|
||||
protected _destroyLight () {
|
||||
if (!this.node._scene || !this._light) { return; }
|
||||
const scene = this._getRenderScene();
|
||||
protected _destroyLight (scene?: RenderScene) {
|
||||
if (!this.node.scene || !this._light) { return; }
|
||||
if (!scene) scene = this._getRenderScene();
|
||||
switch (this._type) {
|
||||
case Type.DIRECTIONAL:
|
||||
scene.destroyDirectionalLight(this._light);
|
||||
|
||||
@@ -104,6 +104,9 @@ export class ModelComponent extends RenderableComponent {
|
||||
|
||||
set mesh (val) {
|
||||
this._mesh = val;
|
||||
if (this._model) {
|
||||
this._model.destroy();
|
||||
}
|
||||
this._updateModels();
|
||||
}
|
||||
|
||||
@@ -162,17 +165,8 @@ export class ModelComponent extends RenderableComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
this.node.on(cc.Node.SCENE_CHANGED_FOR_PERSISTS, () => {
|
||||
if (this._model) {
|
||||
this._getRenderScene().destroyModel(this._model);
|
||||
this._model = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
if (!this._model) { this._updateModels(); }
|
||||
this._updateModels();
|
||||
this._updateCastShadow();
|
||||
this._updateReceiveShadow();
|
||||
if (this._model) {
|
||||
@@ -194,15 +188,11 @@ export class ModelComponent extends RenderableComponent {
|
||||
}
|
||||
|
||||
protected _updateModels () {
|
||||
if (!this.enabledInHierarchy || !this.node._scene || !this._mesh) {
|
||||
if (!this.enabledInHierarchy || !this._mesh) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._model) {
|
||||
this._model.destroy();
|
||||
} else {
|
||||
this._model = this._createModel();
|
||||
}
|
||||
this._createModel();
|
||||
|
||||
this._model!.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition);
|
||||
|
||||
@@ -214,10 +204,14 @@ export class ModelComponent extends RenderableComponent {
|
||||
}
|
||||
|
||||
protected _createModel () {
|
||||
return this._getRenderScene().createModel(Model, this.node);
|
||||
if (!this.node.scene) { return; }
|
||||
const scene = this._getRenderScene();
|
||||
if (this._model && scene.models.find((c) => c === this._model)) { return; }
|
||||
this._model = scene.createModel(Model, this.node);
|
||||
}
|
||||
|
||||
protected _updateModelParams () {
|
||||
// @ts-ignore
|
||||
this.node._hasChanged = true;
|
||||
if (!this._mesh || !this._model) {
|
||||
return;
|
||||
|
||||
Vendored
+1
-1
@@ -28,7 +28,7 @@ import { CCObject } from '../core/data/object';
|
||||
import { getClassName, value } from '../core/utils/js';
|
||||
import IDGenerator from '../core/utils/id-generator';
|
||||
import { ccclass, property } from '../core/data/class-decorator';
|
||||
import { Node } from '../scene-graph';
|
||||
import { Node } from '../scene-graph/node';
|
||||
import { RenderScene } from '../renderer/scene/render-scene';
|
||||
|
||||
export class Component extends CCObject {
|
||||
|
||||
@@ -407,6 +407,7 @@ class Director extends EventTarget {
|
||||
});
|
||||
for (let i = 0; i < persistNodeList.length; i++) {
|
||||
let node = persistNodeList[i];
|
||||
node.emit(cc.Node.SCENE_CHANGED_FOR_PERSISTS, scene.renderScene);
|
||||
var existNode = scene.getChildByUuid(node.uuid);
|
||||
if (existNode) {
|
||||
// scene also contains the persist node, select the old one
|
||||
@@ -417,7 +418,6 @@ class Director extends EventTarget {
|
||||
else {
|
||||
node.parent = scene;
|
||||
}
|
||||
node.emit(cc.Node.SCENE_CHANGED_FOR_PERSISTS);
|
||||
}
|
||||
CC_BUILD && CC_DEBUG && console.timeEnd('AttachPersist');
|
||||
|
||||
|
||||
@@ -8,9 +8,10 @@ type Constructor<T = {}> = new(...args: any[]) => T;
|
||||
|
||||
export type IEventTargetCallback = (...args: any[]) => void;
|
||||
|
||||
class Empty {}
|
||||
class Empty { protected constructor () {}; }
|
||||
|
||||
export function EventTarget<Base extends Constructor<{}>> (base: Base) {
|
||||
export function EventTargetFactory<Base extends Constructor<{}>> (b?: Base) {
|
||||
const base = b || Empty;
|
||||
class EventTarget extends base {
|
||||
private _callbacksInvoker = new CallbacksInvoker();
|
||||
|
||||
@@ -173,5 +174,3 @@ export function EventTarget<Base extends Constructor<{}>> (base: Base) {
|
||||
interface ITargetImpl extends Object {
|
||||
__eventTargets?: Object[];
|
||||
}
|
||||
|
||||
export const DefaultEventTarget = EventTarget(Empty);
|
||||
@@ -31,7 +31,7 @@ export class Model {
|
||||
private _scene: RenderScene;
|
||||
private _node: Node;
|
||||
private _id: number;
|
||||
private _isEnable: boolean = false;
|
||||
private _enabled: boolean = false;
|
||||
private _viewID: number = -1;
|
||||
private _cameraID: number = -1;
|
||||
private _userKey: number = -1;
|
||||
@@ -141,12 +141,12 @@ export class Model {
|
||||
this._worldBounds = aabb.clone(this._modelBounds);
|
||||
}
|
||||
|
||||
set enabled (isEnable) {
|
||||
this._isEnable = isEnable;
|
||||
set enabled (val) {
|
||||
this._enabled = val;
|
||||
}
|
||||
|
||||
get enabled () {
|
||||
return this._isEnable;
|
||||
return this._enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,8 +5,8 @@ import { Root } from '../../core/root';
|
||||
import { Mat4, Vec3 } from '../../core/value-types';
|
||||
import { mat4, vec3 } from '../../core/vmath';
|
||||
import { GFXPrimitiveMode } from '../../gfx/define';
|
||||
import { Node } from '../../scene-graph';
|
||||
import { Layers } from '../../scene-graph/layers';
|
||||
import { Node } from '../../scene-graph/node';
|
||||
import { Camera, ICameraInfo } from './camera';
|
||||
import { DirectionalLight } from './directional-light';
|
||||
import { Light } from './light';
|
||||
@@ -73,7 +73,7 @@ export class RenderScene {
|
||||
private _models: Model[] = [];
|
||||
private _modelId: number = 0;
|
||||
|
||||
private constructor (root: Root) {
|
||||
constructor (root: Root) {
|
||||
this._root = root;
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@ export class RenderScene {
|
||||
public destroy () {
|
||||
this.destroyCameras();
|
||||
this.destroyPointLights();
|
||||
this.destroyDirectionalLights();
|
||||
this.destroySpotLights();
|
||||
this.destroyModels();
|
||||
}
|
||||
|
||||
@@ -108,16 +110,6 @@ export class RenderScene {
|
||||
this._cameras = [];
|
||||
}
|
||||
|
||||
public getCamera (name: string): Camera | null {
|
||||
for (const camera of this._cameras) {
|
||||
if (camera.name === name) {
|
||||
return camera;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -342,6 +342,10 @@ export class BaseNode extends CCObject {
|
||||
*/
|
||||
private __eventTargets: any[] = [];
|
||||
|
||||
get scene () {
|
||||
return this._scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* @method constructor
|
||||
* @param {String} [name]
|
||||
|
||||
@@ -7,8 +7,6 @@ import { mat4, quat, vec3 } from '../core/vmath';
|
||||
import { BaseNode } from './base-node';
|
||||
import { Layers } from './layers';
|
||||
import { WidgetComponent } from '../3d/ui/components/widget-component';
|
||||
// import { EventTarget } from '../core/event/event-target-base';
|
||||
// import { NodeEventProcessor } from './node-event-processor';
|
||||
import { EventType } from './node-event-enum';
|
||||
|
||||
const v3_a = new Vec3();
|
||||
@@ -21,7 +19,6 @@ enum NodeSpace {
|
||||
}
|
||||
|
||||
@ccclass('cc.Node')
|
||||
// @mixins(EventTarget)
|
||||
class Node extends BaseNode {
|
||||
public static EventType = EventType;
|
||||
public static NodeSpace = NodeSpace;
|
||||
|
||||
@@ -75,6 +75,10 @@ export default class Scene extends Node {
|
||||
this._activeInHierarchy = false;
|
||||
}
|
||||
|
||||
get renderScene () {
|
||||
return this._renderScene;
|
||||
}
|
||||
|
||||
_onHierarchyChanged() { }
|
||||
_instantiate() { }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user