mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
b162e21a93
* Update licenses * Update license for audio * Update license for core/3d * Update license for animation, assets, asset-manager, components, data * update license for event, geometry, gfx * update license for core modules math, memop, pipeline, platform, primitive, renderer, scene-graph, utils, value-types * Update license for core * Update license for particle and particle-2d * Update license for particle part2 * Update license for physics and physics-2d * Update license for all modules in cocos * Update license for exports * Fix API doc language declaration
623 lines
16 KiB
TypeScript
623 lines
16 KiB
TypeScript
/*
|
|
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
|
|
|
|
https://www.cocos.com/
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
/**
|
|
* @packageDocumentation
|
|
* @module core
|
|
*/
|
|
|
|
import { builtinResMgr } from './3d/builtin';
|
|
import { Pool } from './memop';
|
|
import { RenderPipeline, ForwardPipeline, RenderView } from './pipeline';
|
|
import { IRenderViewInfo } from './pipeline/define';
|
|
import { Camera, Light, Model } from './renderer/scene';
|
|
import { DataPoolManager } from './renderer/data-pool-manager';
|
|
import { LightType } from './renderer/scene/light';
|
|
import { IRenderSceneInfo, RenderScene } from './renderer/scene/render-scene';
|
|
import { SphereLight } from './renderer/scene/sphere-light';
|
|
import { SpotLight } from './renderer/scene/spot-light';
|
|
import { UI } from './renderer/ui/ui';
|
|
import { legacyCC } from './global-exports';
|
|
import { RenderWindow, IRenderWindowInfo } from './renderer/core/render-window';
|
|
import { ColorAttachment, DepthStencilAttachment, RenderPassInfo, StoreOp, Device } from './gfx';
|
|
import { RootHandle, RootPool, RootView, NULL_HANDLE } from './renderer/core/memory-pools';
|
|
|
|
/**
|
|
* @zh
|
|
* Root描述信息
|
|
*/
|
|
export interface IRootInfo {
|
|
enableHDR?: boolean;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 场景描述信息
|
|
*/
|
|
export interface ISceneInfo {
|
|
name: string;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* Root类
|
|
*/
|
|
export class Root {
|
|
|
|
/**
|
|
* @zh
|
|
* GFX 设备
|
|
*/
|
|
public get device (): Device {
|
|
return this._device;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 主窗口
|
|
*/
|
|
public get mainWindow (): RenderWindow | null {
|
|
return this._mainWindow;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 当前窗口
|
|
*/
|
|
public set curWindow (window: RenderWindow | null) {
|
|
this._curWindow = window;
|
|
}
|
|
|
|
public get curWindow (): RenderWindow | null {
|
|
return this._curWindow;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 临时窗口(用于数据传输)
|
|
*/
|
|
public set tempWindow (window: RenderWindow | null) {
|
|
this._tempWindow = window;
|
|
}
|
|
|
|
public get tempWindow (): RenderWindow | null {
|
|
return this._tempWindow;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 窗口列表
|
|
*/
|
|
public get windows (): RenderWindow[] {
|
|
return this._windows;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 渲染管线
|
|
*/
|
|
public get pipeline (): RenderPipeline {
|
|
return this._pipeline!;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* UI实例
|
|
*/
|
|
public get ui (): UI {
|
|
return this._ui as UI;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 场景列表
|
|
*/
|
|
public get scenes (): RenderScene[] {
|
|
return this._scenes;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 累计时间(秒)
|
|
*/
|
|
public get cumulativeTime (): number {
|
|
return RootPool.get(this._poolHandle, RootView.CUMULATIVE_TIME);
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 帧时间(秒)
|
|
*/
|
|
public get frameTime (): number {
|
|
return RootPool.get(this._poolHandle, RootView.FRAME_TIME);
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 一秒内的累计帧数
|
|
*/
|
|
public get frameCount (): number {
|
|
return this._frameCount;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 每秒帧率
|
|
*/
|
|
public get fps (): number {
|
|
return this._fps;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 每秒固定帧率
|
|
*/
|
|
public set fixedFPS (fps: number) {
|
|
if (fps > 0) {
|
|
this._fixedFPS = fps;
|
|
this._fixedFPSFrameTime = 1000.0 / fps;
|
|
} else {
|
|
this._fixedFPSFrameTime = 0;
|
|
}
|
|
}
|
|
|
|
public get fixedFPS (): number {
|
|
return this._fixedFPS;
|
|
}
|
|
|
|
public get dataPoolManager () {
|
|
return this._dataPoolMgr;
|
|
}
|
|
|
|
get handle () : RootHandle {
|
|
return this._poolHandle;
|
|
}
|
|
|
|
public _createSceneFun: (root: Root) => RenderScene = null!;
|
|
public _createWindowFun: (root: Root) => RenderWindow = null!;
|
|
|
|
private _device: Device;
|
|
private _windows: RenderWindow[] = [];
|
|
private _mainWindow: RenderWindow | null = null;
|
|
private _curWindow: RenderWindow | null = null;
|
|
private _tempWindow: RenderWindow | null = null;
|
|
private _pipeline: RenderPipeline | null = null;
|
|
private _ui: UI | null = null;
|
|
private _dataPoolMgr: DataPoolManager;
|
|
private _scenes: RenderScene[] = [];
|
|
private _cameras: Camera[] = [];
|
|
private _views: RenderView[] = [];
|
|
private _modelPools = new Map<Constructor<Model>, Pool<Model>>();
|
|
private _cameraPool: Pool<Camera> | null = null;
|
|
private _lightPools = new Map<Constructor<Light>, Pool<Light>>();
|
|
private _fpsTime = 0;
|
|
private _frameCount = 0;
|
|
private _fps = 0;
|
|
private _fixedFPS = 0;
|
|
private _fixedFPSFrameTime = 0;
|
|
private _poolHandle: RootHandle = NULL_HANDLE;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param device GFX 设备
|
|
*/
|
|
constructor (device: Device) {
|
|
this._device = device;
|
|
this._dataPoolMgr = new DataPoolManager(device);
|
|
|
|
RenderScene.registerCreateFunc(this);
|
|
RenderWindow.registerCreateFunc(this);
|
|
|
|
this._cameraPool = new Pool(() => new Camera(this._device), 4);
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 初始化函数
|
|
* @param info Root描述信息
|
|
*/
|
|
public initialize (info: IRootInfo): Promise<void> {
|
|
this._poolHandle = RootPool.alloc();
|
|
const colorAttachment = new ColorAttachment();
|
|
const depthStencilAttachment = new DepthStencilAttachment();
|
|
depthStencilAttachment.depthStoreOp = StoreOp.DISCARD;
|
|
depthStencilAttachment.stencilStoreOp = StoreOp.DISCARD;
|
|
const renderPassInfo = new RenderPassInfo([colorAttachment], depthStencilAttachment);
|
|
this._mainWindow = this.createWindow({
|
|
title: 'rootMainWindow',
|
|
width: this._device.width,
|
|
height: this._device.height,
|
|
renderPassInfo,
|
|
swapchainBufferIndices: -1, // always on screen
|
|
});
|
|
this._curWindow = this._mainWindow;
|
|
|
|
return Promise.resolve(builtinResMgr.initBuiltinRes(this._device)).then(() => {
|
|
legacyCC.view.on('design-resolution-changed', () => {
|
|
const width = legacyCC.game.canvas.width;
|
|
const height = legacyCC.game.canvas.height;
|
|
this.resize(width, height);
|
|
}, this);
|
|
});
|
|
}
|
|
|
|
public destroy () {
|
|
this.clearCameras();
|
|
this.destroyScenes();
|
|
|
|
if (this._pipeline) {
|
|
this._pipeline.destroy();
|
|
this._pipeline = null;
|
|
}
|
|
|
|
if (this._ui) {
|
|
this._ui.destroy();
|
|
this._ui = null;
|
|
}
|
|
|
|
this._curWindow = null;
|
|
this._mainWindow = null;
|
|
this.dataPoolManager.clear();
|
|
|
|
if (this._poolHandle) {
|
|
RootPool.free(this._poolHandle);
|
|
this._poolHandle = NULL_HANDLE;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 重置大小
|
|
* @param width 屏幕宽度
|
|
* @param height 屏幕高度
|
|
*/
|
|
public resize (width: number, height: number) {
|
|
// const w = width / cc.view._devicePixelRatio;
|
|
// const h = height / cc.view._devicePixelRatio;
|
|
|
|
this._device.resize(width, height);
|
|
|
|
this._mainWindow!.resize(width, height);
|
|
|
|
for (const window of this._windows) {
|
|
if (window.shouldSyncSizeWithSwapchain) {
|
|
window.resize(width, height);
|
|
}
|
|
}
|
|
|
|
for (const camera of this._cameras) {
|
|
if (camera.isWindowSize) {
|
|
camera.resize(width, height);
|
|
}
|
|
}
|
|
}
|
|
|
|
public setRenderPipeline (rppl: RenderPipeline): boolean {
|
|
if (!rppl) {
|
|
rppl = this.createDefaultPipeline();
|
|
}
|
|
this._pipeline = rppl;
|
|
if (!this._pipeline.activate()) {
|
|
return false;
|
|
}
|
|
|
|
const scene = legacyCC.director.getScene();
|
|
if (scene) {
|
|
scene.globals.activate();
|
|
}
|
|
|
|
this.onGlobalPipelineStateChanged();
|
|
if (!this._ui) {
|
|
this._ui = new UI(this);
|
|
if (!this._ui.initialize()) {
|
|
this.destroy();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public createDefaultPipeline () {
|
|
const rppl = new ForwardPipeline();
|
|
rppl.initialize({ flows: [] });
|
|
return rppl;
|
|
}
|
|
|
|
public onGlobalPipelineStateChanged () {
|
|
for (let i = 0; i < this._cameras.length; i++) {
|
|
this._cameras[i].view.onGlobalPipelineStateChanged();
|
|
}
|
|
for (let i = 0; i < this._scenes.length; i++) {
|
|
this._scenes[i].onGlobalPipelineStateChanged();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 激活指定窗口为当前窗口
|
|
* @param window GFX 窗口
|
|
*/
|
|
public activeWindow (window: RenderWindow) {
|
|
this._curWindow = window;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 重置累计时间
|
|
*/
|
|
public resetCumulativeTime () {
|
|
RootPool.set(this._poolHandle, RootView.CUMULATIVE_TIME, 0);
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 每帧执行函数
|
|
* @param deltaTime 间隔时间
|
|
*/
|
|
public frameMove (deltaTime: number) {
|
|
RootPool.set(this._poolHandle, RootView.FRAME_TIME, deltaTime);
|
|
|
|
/*
|
|
if (this._fixedFPSFrameTime > 0) {
|
|
|
|
const elapsed = this._frameTime * 1000.0;
|
|
if (this._fixedFPSFrameTime > elapsed) {
|
|
|
|
setTimeout(function () {}, this._fixedFPSFrameTime - elapsed);
|
|
}
|
|
}
|
|
*/
|
|
|
|
++this._frameCount;
|
|
RootPool.set(this._poolHandle, RootView.CUMULATIVE_TIME, RootPool.get(this._poolHandle, RootView.CUMULATIVE_TIME) + deltaTime);
|
|
this._fpsTime += deltaTime;
|
|
if (this._fpsTime > 1.0) {
|
|
this._fps = this._frameCount;
|
|
this._frameCount = 0;
|
|
this._fpsTime = 0.0;
|
|
}
|
|
if (this._ui) this._ui.update();
|
|
|
|
if (this._pipeline) {
|
|
this._device.acquire();
|
|
this._views.length = 0;
|
|
const views = this._cameras;
|
|
const stamp = legacyCC.director.getTotalFrames();
|
|
if (this._ui) this._ui.uploadBuffers();
|
|
for (let i = 0; i < views.length; i++) {
|
|
const camera = this._cameras[i];
|
|
const view = camera.view;
|
|
if (view.isEnable && view.window) {
|
|
camera.update();
|
|
camera.scene!.update(stamp);
|
|
this._views.push(view);
|
|
}
|
|
}
|
|
|
|
this._pipeline.render(this._views);
|
|
this._device.present();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 创建窗口
|
|
* @param info GFX 窗口描述信息
|
|
*/
|
|
public createWindow (info: IRenderWindowInfo): RenderWindow | null {
|
|
const window = this._createWindowFun(this);
|
|
window.initialize(this.device, info);
|
|
this._windows.push(window);
|
|
return window;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 销毁指定的窗口
|
|
* @param window GFX 窗口
|
|
*/
|
|
public destroyWindow (window: RenderWindow) {
|
|
for (let i = 0; i < this._windows.length; ++i) {
|
|
if (this._windows[i] === window) {
|
|
window.destroy();
|
|
this._windows.splice(i, 1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 销毁全部窗口
|
|
*/
|
|
public destroyWindows () {
|
|
for (const window of this._windows) {
|
|
window.destroy();
|
|
}
|
|
this._windows = [];
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 创建渲染场景
|
|
* @param info 渲染场景描述信息
|
|
*/
|
|
public createScene (info: IRenderSceneInfo): RenderScene {
|
|
const scene: RenderScene = this._createSceneFun(this);
|
|
scene.initialize(info);
|
|
this._scenes.push(scene);
|
|
return scene;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 销毁指定的渲染场景
|
|
* @param scene 渲染场景
|
|
*/
|
|
public destroyScene (scene: RenderScene) {
|
|
for (let i = 0; i < this._scenes.length; ++i) {
|
|
if (this._scenes[i] === scene) {
|
|
scene.destroy();
|
|
this._scenes.splice(i, 1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 销毁全部场景
|
|
*/
|
|
public destroyScenes () {
|
|
for (const scene of this._scenes) {
|
|
scene.destroy();
|
|
}
|
|
this._scenes = [];
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 创建渲染视图
|
|
* @param info 渲染视图描述信息
|
|
*/
|
|
public createView (info: IRenderViewInfo): RenderView {
|
|
const view: RenderView = new RenderView();
|
|
view.initialize(info);
|
|
return view;
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 添加渲染相机
|
|
* @param camera 渲染相机
|
|
*/
|
|
public attachCamera (camera: Camera) {
|
|
for (let i = 0; i < this._cameras.length; i++) {
|
|
if (this._cameras[i] === camera) {
|
|
return;
|
|
}
|
|
}
|
|
this._cameras.push(camera);
|
|
this.sortViews();
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 移除渲染相机
|
|
* @param camera 相机
|
|
*/
|
|
public detachCamera (camera: Camera) {
|
|
for (let i = 0; i < this._cameras.length; ++i) {
|
|
if (this._cameras[i] === camera) {
|
|
this._cameras.splice(i, 1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @zh
|
|
* 销毁全部渲染相机
|
|
*/
|
|
public clearCameras () {
|
|
this._cameras.length = 0;
|
|
}
|
|
|
|
public createModel<T extends Model> (mClass: typeof Model): T {
|
|
let p = this._modelPools.get(mClass);
|
|
if (!p) {
|
|
this._modelPools.set(mClass, new Pool(() => new mClass(), 10));
|
|
p = this._modelPools.get(mClass)!;
|
|
}
|
|
const model = p.alloc() as T;
|
|
model.initialize();
|
|
return model;
|
|
}
|
|
|
|
public destroyModel (m: Model) {
|
|
const p = this._modelPools.get(m.constructor as Constructor<Model>);
|
|
if (p) {
|
|
p.free(m);
|
|
m.destroy();
|
|
if (m.scene) {
|
|
m.scene.removeModel(m);
|
|
}
|
|
} else {
|
|
console.warn(`'${m.constructor.name}'is not in the model pool and cannot be destroyed by destroyModel.`);
|
|
}
|
|
}
|
|
|
|
public createCamera (): Camera {
|
|
return this._cameraPool!.alloc();
|
|
}
|
|
|
|
public destroyCamera (c: Camera) {
|
|
this._cameraPool!.free(c);
|
|
c.destroy();
|
|
if (c.scene) {
|
|
c.scene.removeCamera(c);
|
|
}
|
|
c.isWindowSize = true;
|
|
}
|
|
|
|
public createLight<T extends Light> (lClass: new () => T): T {
|
|
let l = this._lightPools.get(lClass);
|
|
if (!l) {
|
|
this._lightPools.set(lClass, new Pool(() => new lClass(), 4));
|
|
l = this._lightPools.get(lClass)!;
|
|
}
|
|
const light = l.alloc() as T;
|
|
light.initialize();
|
|
return light;
|
|
}
|
|
|
|
public destroyLight (l: Light) {
|
|
const p = this._lightPools.get(l.constructor as Constructor<Light>);
|
|
l.destroy();
|
|
if (p) {
|
|
p.free(l);
|
|
if (l.scene) {
|
|
switch (l.type) {
|
|
case LightType.SPHERE:
|
|
l.scene.removeSphereLight(l as SphereLight);
|
|
break;
|
|
case LightType.SPOT:
|
|
l.scene.removeSpotLight(l as SpotLight);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public sortViews () {
|
|
this._cameras.sort((a: Camera, b: Camera) => {
|
|
return a.view.priority - b.view.priority;
|
|
});
|
|
}
|
|
}
|
|
|
|
legacyCC.Root = Root;
|