From aaaac3797edb7ec47c21d904dfc22c853ab4e75f Mon Sep 17 00:00:00 2001 From: GengineJS <476393671@qq.com> Date: Tue, 11 May 2021 13:45:04 +0800 Subject: [PATCH] [Change] coding style for scene objects (#8644) --- cocos/core/renderer/core/render-window.ts | 89 +++++--- cocos/core/renderer/scene/ambient.ts | 45 ++-- cocos/core/renderer/scene/camera.ts | 239 ++++++++++++++-------- cocos/core/renderer/scene/fog.ts | 165 +++++++++------ cocos/core/renderer/scene/render-scene.ts | 126 +++++++----- cocos/core/renderer/scene/shadows.ts | 219 +++++++++++++------- cocos/core/renderer/scene/skybox.ts | 58 ++++-- cocos/core/scene-graph/node.ts | 80 ++++++-- 8 files changed, 684 insertions(+), 337 deletions(-) diff --git a/cocos/core/renderer/core/render-window.ts b/cocos/core/renderer/core/render-window.ts index a98d5996ab..339f97e3a1 100644 --- a/cocos/core/renderer/core/render-window.ts +++ b/cocos/core/renderer/core/render-window.ts @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - +import { JSB } from 'internal:constants'; import { TextureType, TextureUsageBit, Format, RenderPass, Texture, Framebuffer, RenderPassInfo, Device, TextureInfo, FramebufferInfo } from '../../gfx'; @@ -65,7 +65,10 @@ export class RenderWindow { * @zh 帧缓冲对象。 */ get framebuffer (): Framebuffer { - return FramebufferPool.get(RenderWindowPool.get(this._poolHandle, RenderWindowView.FRAMEBUFFER)); + if (JSB) { + return FramebufferPool.get(RenderWindowPool.get(this._poolHandle, RenderWindowView.FRAMEBUFFER)); + } + return this._framebuffer!; } get shouldSyncSizeWithSwapchain () { @@ -77,7 +80,7 @@ export class RenderWindow { * @zh 这个渲染窗口是否指向在屏缓冲 */ get hasOnScreenAttachments () { - return RenderWindowPool.get(this._poolHandle, RenderWindowView.HAS_ON_SCREEN_ATTACHMENTS) === 1; + return this._hasOnScreenAttachments; } /** @@ -85,7 +88,7 @@ export class RenderWindow { * @zh 这个渲染窗口是否指向离屏缓冲 */ get hasOffScreenAttachments () { - return RenderWindowPool.get(this._poolHandle, RenderWindowView.HAS_OFF_SCREEN_ATTACHMENTS) === 1; + return this._hasOffScreenAttachments; } get handle () : RenderWindowHandle { @@ -115,13 +118,51 @@ export class RenderWindow { protected _shouldSyncSizeWithSwapchain = false; protected _poolHandle: RenderWindowHandle = NULL_HANDLE; protected _cameras: Camera[] = []; - + protected _hasOnScreenAttachments = false; + protected _hasOffScreenAttachments = false; + protected _framebuffer: Framebuffer | null = null; private constructor (root: Root) { } - public initialize (device: Device, info: IRenderWindowInfo): boolean { - this._poolHandle = RenderWindowPool.alloc(); + private _setHasOffScreenAttachments (val) { + this._hasOffScreenAttachments = val; + if (JSB) { + RenderWindowPool.set(this._poolHandle, RenderWindowView.HAS_OFF_SCREEN_ATTACHMENTS, val ? 1 : 0); + } + } + private _setHasOnScreenAttachments (val) { + this._hasOnScreenAttachments = val; + if (JSB) { + RenderWindowPool.set(this._poolHandle, RenderWindowView.HAS_ON_SCREEN_ATTACHMENTS, val ? 1 : 0); + } + } + + private _createFrameBuffer (device: Device, renderPass) { + if (JSB) { + const hFBO = FramebufferPool.alloc(device, new FramebufferInfo( + renderPass, + this._colorTextures, + this._depthStencilTexture, + )); + RenderWindowPool.set(this._poolHandle, RenderWindowView.FRAMEBUFFER, hFBO); + return; + } + this._framebuffer = device.createFramebuffer(new FramebufferInfo( + renderPass, + this._colorTextures, + this._depthStencilTexture, + )); + } + + protected _init () { + if (JSB) { + this._poolHandle = RenderWindowPool.alloc(); + } + } + + public initialize (device: Device, info: IRenderWindowInfo): boolean { + this._init(); if (info.title !== undefined) { this._title = info.title; } @@ -161,9 +202,9 @@ export class RenderWindow { this._width, this._height, )); - RenderWindowPool.set(this._poolHandle, RenderWindowView.HAS_OFF_SCREEN_ATTACHMENTS, 1); + this._setHasOffScreenAttachments(true); } else { - RenderWindowPool.set(this._poolHandle, RenderWindowView.HAS_ON_SCREEN_ATTACHMENTS, 1); + this._setHasOnScreenAttachments(true); } this._colorTextures.push(colorTex); } @@ -178,22 +219,22 @@ export class RenderWindow { this._width, this._height, )); - RenderWindowPool.set(this._poolHandle, RenderWindowView.HAS_OFF_SCREEN_ATTACHMENTS, 1); - } else { - RenderWindowPool.set(this._poolHandle, RenderWindowView.HAS_ON_SCREEN_ATTACHMENTS, 1); + this._setHasOffScreenAttachments(true); } + } else { + this._setHasOnScreenAttachments(true); } - - const hFBO = FramebufferPool.alloc(device, new FramebufferInfo( - this._renderPass, - this._colorTextures, - this._depthStencilTexture, - )); - RenderWindowPool.set(this._poolHandle, RenderWindowView.FRAMEBUFFER, hFBO); - + this._createFrameBuffer(device, this._renderPass); return true; } + protected _destroy () { + this.framebuffer.destroy(); + if (this._poolHandle) { + this._poolHandle = NULL_HANDLE; + } + } + public destroy () { this.clearCameras(); if (this._depthStencilTexture) { @@ -208,11 +249,7 @@ export class RenderWindow { } } this._colorTextures.length = 0; - - if (this._poolHandle) { - FramebufferPool.get(RenderWindowPool.get(this._poolHandle, RenderWindowView.FRAMEBUFFER)).destroy(); - this._poolHandle = NULL_HANDLE; - } + this._destroy(); } /** @@ -245,7 +282,7 @@ export class RenderWindow { } } - const framebuffer = FramebufferPool.get(RenderWindowPool.get(this._poolHandle, RenderWindowView.FRAMEBUFFER)); + const framebuffer = this.framebuffer; if (needRebuild && framebuffer) { framebuffer.destroy(); framebuffer.initialize(new FramebufferInfo( diff --git a/cocos/core/renderer/scene/ambient.ts b/cocos/core/renderer/scene/ambient.ts index ab6a53797b..c952164257 100644 --- a/cocos/core/renderer/scene/ambient.ts +++ b/cocos/core/renderer/scene/ambient.ts @@ -23,6 +23,7 @@ THE SOFTWARE. */ +import { JSB } from 'internal:constants'; import { Color, Vec3 } from '../../math'; import { AmbientPool, NULL_HANDLE, AmbientView, AmbientHandle } from '../core/memory-pools'; import { legacyCC } from '../../global-exports'; @@ -45,10 +46,13 @@ export class Ambient { * @zh 是否开启环境光 */ set enabled (val: boolean) { - AmbientPool.set(this._handle, AmbientView.ENABLE, val ? 1 : 0); + this._enabled = val; + if (JSB) { + AmbientPool.set(this._handle, AmbientView.ENABLE, val ? 1 : 0); + } } get enabled (): boolean { - return AmbientPool.get(this._handle, AmbientView.ENABLE) as unknown as boolean; + return this._enabled; } /** * @en Sky color @@ -61,7 +65,9 @@ export class Ambient { set skyColor (color: Color) { this._skyColor.set(color); Color.toArray(this._colorArray, this._skyColor); - AmbientPool.setVec4(this._handle, AmbientView.SKY_COLOR, this._skyColor); + if (JSB) { + AmbientPool.setVec4(this._handle, AmbientView.SKY_COLOR, this._skyColor); + } } /** @@ -69,13 +75,15 @@ export class Ambient { * @zh 天空亮度 */ get skyIllum (): number { - return AmbientPool.get(this._handle, AmbientView.ILLUM); + return this._skyIllum; } set skyIllum (illum: number) { - AmbientPool.set(this._handle, AmbientView.ILLUM, illum); + this._skyIllum = illum; + if (JSB) { + AmbientPool.set(this._handle, AmbientView.ILLUM, illum); + } } - /** * @en Ground color * @zh 地面颜色 @@ -87,14 +95,17 @@ export class Ambient { set groundAlbedo (color: Color) { this._groundAlbedo.set(color); Vec3.toArray(this._albedoArray, this._groundAlbedo); - AmbientPool.setVec4(this._handle, AmbientView.GROUND_ALBEDO, this._groundAlbedo); + if (JSB) { + AmbientPool.setVec4(this._handle, AmbientView.GROUND_ALBEDO, this._groundAlbedo); + } } protected _skyColor = new Color(51, 128, 204, 1.0); protected _groundAlbedo = new Color(51, 51, 51, 255); protected _albedoArray = Float32Array.from([0.2, 0.2, 0.2, 1.0]); protected _colorArray = Float32Array.from([0.2, 0.5, 0.8, 1.0]); protected _handle: AmbientHandle = NULL_HANDLE; - + protected _enabled = false; + protected _skyIllum = 0; get handle () : AmbientHandle { return this._handle; } @@ -104,21 +115,21 @@ export class Ambient { } public initialize (ambientInfo: AmbientInfo) { - this._skyColor.set(ambientInfo.skyColor); - this._groundAlbedo.set(ambientInfo.groundAlbedo); - Color.toArray(this._colorArray, this._skyColor); - Vec3.toArray(this._albedoArray, this._groundAlbedo); - AmbientPool.setVec4(this._handle, AmbientView.SKY_COLOR, this._skyColor); - AmbientPool.setVec4(this._handle, AmbientView.GROUND_ALBEDO, this._groundAlbedo); - AmbientPool.set(this._handle, AmbientView.ILLUM, ambientInfo.skyIllum); + this.skyColor = ambientInfo.skyColor; + this.groundAlbedo = ambientInfo.groundAlbedo; + this.skyIllum = ambientInfo.skyIllum; } - public destroy () { - if (this._handle) { + protected _destroy () { + if (JSB && this._handle) { AmbientPool.free(this._handle); this._handle = NULL_HANDLE; } } + + public destroy () { + this._destroy(); + } } legacyCC.Ambient = Ambient; diff --git a/cocos/core/renderer/scene/camera.ts b/cocos/core/renderer/scene/camera.ts index 08c406a1d7..5ed4cf0b45 100644 --- a/cocos/core/renderer/scene/camera.ts +++ b/cocos/core/renderer/scene/camera.ts @@ -40,6 +40,7 @@ import { import { recordFrustumToSharedMemory } from '../../geometry/frustum'; import { preTransforms } from '../../math/mat4'; import { director } from '../../director'; +import { ClearFlag } from '../../components/camera-component'; export enum CameraFOVAxis { VERTICAL, @@ -163,7 +164,13 @@ export class Camera { private _poolHandle: CameraHandle = NULL_HANDLE; private _frustumHandle: FrustumHandle = NULL_HANDLE; private _window: RenderWindow | null = null; - + private _width = 1; + private _height = 1; + private _clearFlag = ClearFlagBit.NONE; + private _clearDepth = 1.0; + private _visibility = CAMERA_DEFAULT_MASK; + private _exposure = 0; + private _clearStencil = 0; constructor (device: Device) { this._device = device; this._apertureValue = FSTOPS[this._aperture]; @@ -181,40 +188,55 @@ export class Camera { } } - public initialize (info: ICameraInfo) { - this._name = info.name; - this._node = info.node; - this._proj = info.projection; - this._priority = info.priority || 0; - - this._aspect = this.screenScale = 1; - const handle = this._poolHandle = CameraPool.alloc(); - CameraPool.set(handle, CameraView.WIDTH, 1); - CameraPool.set(handle, CameraView.HEIGHT, 1); - CameraPool.set(handle, CameraView.CLEAR_FLAGS, ClearFlagBit.NONE); - CameraPool.set(handle, CameraView.CLEAR_DEPTH, 1.0); - CameraPool.set(handle, CameraView.NODE, this._node.handle); - CameraPool.set(handle, CameraView.VISIBILITY, CAMERA_DEFAULT_MASK); - if (this._scene) CameraPool.set(handle, CameraView.SCENE, this._scene.handle); + private _setWidth (val) { + this._width = val; if (JSB) { - this._frustumHandle = FrustumPool.alloc(); - CameraPool.set(handle, CameraView.FRUSTUM, this._frustumHandle); + CameraPool.set(this._poolHandle, CameraView.WIDTH, val); } - - this.updateExposure(); - this.changeTargetWindow(info.window); - - console.log(`Created Camera: ${this._name} ${CameraPool.get(handle, - CameraView.WIDTH)}x${CameraPool.get(handle, CameraView.HEIGHT)}`); } - public destroy () { - if (this._window) { - this._window.detachCamera(this); - this._window = null; + private _setHeight (val) { + this._height = val; + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.HEIGHT, val); } - this._name = null; - if (this._poolHandle) { + } + + private _setScene (val) { + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.SCENE, val); + } + } + + protected _init (info: ICameraInfo) { + if (JSB) { + const handle = this._poolHandle = CameraPool.alloc(); + if (this._scene) this._setScene(this._scene.handle); + this._frustumHandle = FrustumPool.alloc(); + CameraPool.set(handle, CameraView.FRUSTUM, this._frustumHandle); + console.log(`Created Camera: ${this._name} ${CameraPool.get(this._poolHandle, + CameraView.WIDTH)}x${CameraPool.get(this._poolHandle, CameraView.HEIGHT)}`); + } + } + + public initialize (info: ICameraInfo) { + this._init(info); + this.node = info.node; + this._setWidth(1); + this._setHeight(1); + this.clearFlag = ClearFlagBit.NONE; + this.clearDepth = 1.0; + this.visibility = CAMERA_DEFAULT_MASK; + this._name = info.name; + this._proj = info.projection; + this._priority = info.priority || 0; + this._aspect = this.screenScale = 1; + this.updateExposure(); + this.changeTargetWindow(info.window); + } + + protected _destroy () { + if (JSB && this._poolHandle) { CameraPool.free(this._poolHandle); this._poolHandle = NULL_HANDLE; if (this._frustumHandle) { @@ -224,31 +246,39 @@ export class Camera { } } + public destroy () { + if (this._window) { + this._window.detachCamera(this); + this.window = null; + } + this._name = null; + this._destroy(); + } + public attachToScene (scene: RenderScene) { this._scene = scene; this._enabled = true; - CameraPool.set(this._poolHandle, CameraView.SCENE, scene.handle); + this._setScene(scene.handle); } public detachFromScene () { this._scene = null; this._enabled = false; - CameraPool.set(this._poolHandle, CameraView.SCENE, 0 as unknown as SceneHandle); + this._setScene(0 as unknown as SceneHandle); } public resize (width: number, height: number) { if (!this._window) return; - const handle = this._poolHandle; - CameraPool.set(handle, CameraView.WIDTH, width); - CameraPool.set(handle, CameraView.HEIGHT, height); + + this._setWidth(width); + this._setHeight(height); this._aspect = (width * this._viewport.width) / (height * this._viewport.height); this._isProjDirty = true; } public setFixedSize (width: number, height: number) { - const handle = this._poolHandle; - CameraPool.set(handle, CameraView.WIDTH, width); - CameraPool.set(handle, CameraView.HEIGHT, height); + this._setWidth(width); + this._setHeight(height); this._aspect = (width * this._viewport.width) / (height * this._viewport.height); this.isWindowSize = false; } @@ -260,14 +290,17 @@ export class Camera { // view matrix if (this._node.hasChangedFlags || forceUpdate) { Mat4.invert(this._matView, this._node.worldMatrix); - CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW, this._matView); - + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW, this._matView); + } this._forward.x = -this._matView.m02; this._forward.y = -this._matView.m06; this._forward.z = -this._matView.m10; this._node.getWorldPosition(this._position); - CameraPool.setVec3(this._poolHandle, CameraView.POSITION, this._position); - CameraPool.setVec3(this._poolHandle, CameraView.FORWARD, this._forward); + if (JSB) { + CameraPool.setVec3(this._poolHandle, CameraView.POSITION, this._position); + CameraPool.setVec3(this._poolHandle, CameraView.FORWARD, this._forward); + } viewProjDirty = true; } @@ -290,9 +323,10 @@ export class Camera { this._device.capabilities.clipSpaceMinZ, projectionSignY, orientation); } Mat4.invert(this._matProjInv, this._matProj); - - CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ, this._matProj); - CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ_INV, this._matProjInv); + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ, this._matProj); + CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ_INV, this._matProjInv); + } viewProjDirty = true; this._isProjDirty = false; } @@ -302,14 +336,19 @@ export class Camera { Mat4.multiply(this._matViewProj, this._matProj, this._matView); Mat4.invert(this._matViewProjInv, this._matViewProj); this._frustum.update(this._matViewProj, this._matViewProjInv); - CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ, this._matViewProj); - CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ_INV, this._matViewProjInv); + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ, this._matViewProj); + CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ_INV, this._matViewProjInv); + } recordFrustumToSharedMemory(this._frustumHandle, this._frustum); } } set node (val: Node) { this._node = val; + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.NODE, this._node.handle); + } } get node () { @@ -383,7 +422,9 @@ export class Camera { this._clearColor.y = val.y; this._clearColor.z = val.z; this._clearColor.w = val.w; - CameraPool.setVec4(this._poolHandle, CameraView.CLEAR_COLOR, val); + if (JSB) { + CameraPool.setVec4(this._poolHandle, CameraView.CLEAR_COLOR, val); + } } get clearColor () { @@ -425,8 +466,9 @@ export class Camera { break; default: } - - CameraPool.setVec4(this._poolHandle, CameraView.VIEW_PORT, this._viewport); + if (JSB) { + CameraPool.setVec4(this._poolHandle, CameraView.VIEW_PORT, this._viewport); + } this.resize(this.width, this.height); } @@ -439,11 +481,17 @@ export class Camera { } get width () { - return CameraPool.get(this._poolHandle, CameraView.WIDTH); + if (JSB) { + return CameraPool.get(this._poolHandle, CameraView.WIDTH); + } + return this._width; } get height () { - return CameraPool.get(this._poolHandle, CameraView.HEIGHT); + if (JSB) { + return CameraPool.get(this._poolHandle, CameraView.HEIGHT); + } + return this._height; } get aspect () { @@ -452,7 +500,9 @@ export class Camera { set matView (val) { this._matView = val; - CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW, this._matView); + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW, this._matView); + } } get matView () { @@ -469,7 +519,9 @@ export class Camera { set matProj (val) { this._matProj = val; - CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ, this._matProj); + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ, this._matProj); + } } get matProj () { @@ -478,7 +530,9 @@ export class Camera { set matProjInv (val) { this._matProjInv = val; - CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ_INV, this._matProjInv); + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_PROJ_INV, this._matProjInv); + } } get matProjInv () { @@ -487,7 +541,9 @@ export class Camera { set matViewProj (val) { this._matViewProj = val; - CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ, this._matViewProj); + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ, this._matViewProj); + } } get matViewProj () { @@ -496,7 +552,9 @@ export class Camera { set matViewProjInv (val) { this._matViewProjInv = val; - CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ_INV, this._matViewProjInv); + if (JSB) { + CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ_INV, this._matViewProjInv); + } } get matViewProjInv () { @@ -514,7 +572,7 @@ export class Camera { set window (val) { this._window = val; - if (val) CameraPool.set(this._poolHandle, CameraView.WINDOW, val.handle); + if (JSB && val) CameraPool.set(this._poolHandle, CameraView.WINDOW, val.handle); } get window () { @@ -523,7 +581,9 @@ export class Camera { set forward (val) { this._forward = val; - CameraPool.setVec3(this._poolHandle, CameraView.FORWARD, this._forward); + if (JSB) { + CameraPool.setVec3(this._poolHandle, CameraView.FORWARD, this._forward); + } } get forward () { @@ -532,7 +592,9 @@ export class Camera { set position (val) { this._position = val; - CameraPool.setVec3(this._poolHandle, CameraView.POSITION, this._position); + if (JSB) { + CameraPool.setVec3(this._poolHandle, CameraView.POSITION, this._position); + } } get position () { @@ -540,10 +602,13 @@ export class Camera { } set visibility (vis: number) { - CameraPool.set(this._poolHandle, CameraView.VISIBILITY, vis); + this._visibility = vis; + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.VISIBILITY, vis); + } } get visibility (): number { - return CameraPool.get(this._poolHandle, CameraView.VISIBILITY); + return this._visibility; } get priority (): number { @@ -605,31 +670,40 @@ export class Camera { } get exposure (): number { - return CameraPool.get(this._poolHandle, CameraView.EXPOSURE); + return this._exposure; } get clearFlag () : ClearFlags { - return CameraPool.get(this._poolHandle, CameraView.CLEAR_FLAGS); + return this._clearFlag; } set clearFlag (flag: ClearFlags) { - CameraPool.set(this._poolHandle, CameraView.CLEAR_FLAGS, flag); + this._clearFlag = flag; + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.CLEAR_FLAGS, flag); + } } get clearDepth () : number { - return CameraPool.get(this._poolHandle, CameraView.CLEAR_DEPTH); + return this._clearDepth; } set clearDepth (depth: number) { - CameraPool.set(this._poolHandle, CameraView.CLEAR_DEPTH, depth); + this._clearDepth = depth; + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.CLEAR_DEPTH, depth); + } } get clearStencil () : number { - return CameraPool.get(this._poolHandle, CameraView.CLEAR_STENCIL); + return this._clearStencil; } set clearStencil (stencil: number) { - CameraPool.set(this._poolHandle, CameraView.CLEAR_STENCIL, stencil); + this._clearStencil = stencil; + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.CLEAR_STENCIL, stencil); + } } get handle () : CameraHandle { @@ -643,8 +717,7 @@ export class Camera { const win = window || legacyCC.director.root.mainWindow; if (win) { win.attachCamera(this); - this._window = win; - CameraPool.set(this._poolHandle, CameraView.WINDOW, win.handle); + this.window = win; this.resize(win.width, win.height); } } @@ -661,9 +734,8 @@ export class Camera { public screenPointToRay (out: Ray, x: number, y: number): Ray { if (!this._node) return null!; - const handle = this._poolHandle; - const width = CameraPool.get(handle, CameraView.WIDTH); - const height = CameraPool.get(handle, CameraView.HEIGHT); + const width = this.width; + const height = this.height; const cx = this._viewport.x * width; const cy = this._viewport.y * height; const cw = this._viewport.width * width; @@ -695,9 +767,8 @@ export class Camera { * transform a screen position (in oriented space) to world space */ public screenToWorld (out: Vec3, screenPos: Vec3): Vec3 { - const handle = this._poolHandle; - const width = CameraPool.get(handle, CameraView.WIDTH); - const height = CameraPool.get(handle, CameraView.HEIGHT); + const width = this.width; + const height = this.height; const cx = this._viewport.x * width; const cy = this._viewport.y * height; const cw = this._viewport.width * width; @@ -742,9 +813,8 @@ export class Camera { * transform a world space position to screen space */ public worldToScreen (out: Vec3, worldPos: Vec3): Vec3 { - const handle = this._poolHandle; - const width = CameraPool.get(handle, CameraView.WIDTH); - const height = CameraPool.get(handle, CameraView.HEIGHT); + const width = this.width; + const height = this.height; const cx = this._viewport.x * width; const cy = this._viewport.y * height; const cw = this._viewport.width * width; @@ -788,8 +858,15 @@ export class Camera { return out; } + protected setExposure (ev100) { + this._exposure = 0.833333 / (2.0 ** ev100); + if (JSB) { + CameraPool.set(this._poolHandle, CameraView.EXPOSURE, this._exposure); + } + } + private updateExposure () { const ev100 = Math.log2((this._apertureValue * this._apertureValue) / this._shutterValue * 100.0 / this._isoValue); - CameraPool.set(this._poolHandle, CameraView.EXPOSURE, 0.833333 / (2.0 ** ev100)); + this.setExposure(ev100); } } diff --git a/cocos/core/renderer/scene/fog.ts b/cocos/core/renderer/scene/fog.ts index d5322bb1be..ec7a6e7f12 100644 --- a/cocos/core/renderer/scene/fog.ts +++ b/cocos/core/renderer/scene/fog.ts @@ -1,28 +1,29 @@ -/* - 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. - */ - +/* + 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. + */ + +import { JSB } from 'internal:constants'; import { Enum } from '../../value-types'; import { Color } from '../../math'; import { legacyCC } from '../../global-exports'; @@ -80,13 +81,13 @@ export class Fog { * @en Enable global fog */ set enabled (val: boolean) { - FogPool.set(this._handle, FogView.ENABLE, val ? 1 : 0); - if (!val) FogPool.set(this._handle, FogView.TYPE, FOG_TYPE_NONE); + this._setEnable(val); + if (!val) this._type = FOG_TYPE_NONE; val ? this.activate() : this._updatePipeline(); } get enabled (): boolean { - return FogPool.get(this._handle, FogView.ENABLE) as unknown as boolean; + return this._enabled; } /** @@ -96,7 +97,9 @@ export class Fog { set fogColor (val: Color) { this._fogColor.set(val); Color.toArray(this._colorArray, this._fogColor); - FogPool.setVec4(this._handle, FogView.COLOR, this._fogColor); + if (JSB) { + FogPool.setVec4(this._handle, FogView.COLOR, this._fogColor); + } } get fogColor () { @@ -115,10 +118,10 @@ export class Fog { * - 3:Layered fog */ get type (): number { - return FogPool.get(this._handle, FogView.TYPE); + return this._type; } set type (val: number) { - FogPool.set(this._handle, FogView.TYPE, this.enabled ? val : FOG_TYPE_NONE); + this._setType(val); if (this.enabled) this._updatePipeline(); } @@ -127,23 +130,28 @@ export class Fog { * @en Global fog density */ get fogDensity (): number { - return FogPool.get(this._handle, FogView.DENSITY); + return this._fogDensity; } set fogDensity (val: number) { - FogPool.set(this._handle, FogView.DENSITY, val); + this._fogDensity = val; + if (JSB) { + FogPool.set(this._handle, FogView.DENSITY, val); + } } - /** * @zh 雾效起始位置,只适用于线性雾 * @en Global fog start position, only for linear fog */ get fogStart (): number { - return FogPool.get(this._handle, FogView.START); + return this._fogStart; } set fogStart (val: number) { - FogPool.set(this._handle, FogView.START, val); + this._fogStart = val; + if (JSB) { + FogPool.set(this._handle, FogView.START, val); + } } /** @@ -151,11 +159,14 @@ export class Fog { * @en Global fog end position, only for linear fog */ get fogEnd (): number { - return FogPool.get(this._handle, FogView.END); + return this._fogEnd; } set fogEnd (val: number) { - FogPool.set(this._handle, FogView.END, val); + this._fogEnd = val; + if (JSB) { + FogPool.set(this._handle, FogView.END, val); + } } /** @@ -163,11 +174,14 @@ export class Fog { * @en Global fog attenuation */ get fogAtten (): number { - return FogPool.get(this._handle, FogView.ATTEN); + return this._fogAtten; } set fogAtten (val: number) { - FogPool.set(this._handle, FogView.ATTEN, val); + this._fogAtten = val; + if (JSB) { + FogPool.set(this._handle, FogView.ATTEN, val); + } } /** @@ -175,11 +189,14 @@ export class Fog { * @en Global fog top range, only for layered fog */ get fogTop (): number { - return FogPool.get(this._handle, FogView.TOP); + return this._fogTop; } set fogTop (val: number) { - FogPool.set(this._handle, FogView.TOP, val); + this._fogTop = val; + if (JSB) { + FogPool.set(this._handle, FogView.TOP, val); + } } /** @@ -187,40 +204,64 @@ export class Fog { * @en Global fog range, only for layered fog */ get fogRange (): number { - return FogPool.get(this._handle, FogView.RANGE); + return this._fogRange; } set fogRange (val: number) { - FogPool.set(this._handle, FogView.RANGE, val); + this._fogRange = val; + if (JSB) { + FogPool.set(this._handle, FogView.RANGE, val); + } } - get colorArray (): Float32Array { return this._colorArray; } protected _fogColor = new Color('#C8C8C8'); protected _colorArray: Float32Array = new Float32Array([0.2, 0.2, 0.2, 1.0]); protected _handle: FogHandle = NULL_HANDLE; - + protected _enabled = false; + protected _type = 0; + protected _fogDensity = 0.3; + protected _fogStart = 0.5; + protected _fogEnd = 300; + protected _fogAtten = 5; + protected _fogTop = 1.5; + protected _fogRange = 1.2; get handle () : FogHandle { return this._handle; } constructor () { - this._handle = FogPool.alloc(); + if (JSB) { + this._handle = FogPool.alloc(); + } + } + + protected _setType (val) { + this._type = this.enabled ? val : FOG_TYPE_NONE; + if (JSB) { + FogPool.set(this._handle, FogView.TYPE, this._type); + } + } + + protected _setEnable (val) { + if (JSB) { + FogPool.set(this._handle, FogView.ENABLE, val ? 1 : 0); + if (!val) FogPool.set(this._handle, FogView.TYPE, FOG_TYPE_NONE); + } + this._enabled = val; } public initialize (fogInfo : FogInfo) { - FogPool.set(this._handle, FogView.ENABLE, fogInfo.enabled ? 1 : 0); - FogPool.set(this._handle, FogView.TYPE, fogInfo.enabled ? fogInfo.type : FOG_TYPE_NONE); - this._fogColor.set(fogInfo.fogColor); - Color.toArray(this._colorArray, this._fogColor); - FogPool.setVec4(this._handle, FogView.COLOR, this._fogColor); - FogPool.set(this._handle, FogView.DENSITY, fogInfo.fogDensity); - FogPool.set(this._handle, FogView.START, fogInfo.fogStart); - FogPool.set(this._handle, FogView.END, fogInfo.fogEnd); - FogPool.set(this._handle, FogView.ATTEN, fogInfo.fogAtten); - FogPool.set(this._handle, FogView.TOP, fogInfo.fogTop); - FogPool.set(this._handle, FogView.RANGE, fogInfo.fogRange); + this.fogColor = fogInfo.fogColor; + this._setEnable(fogInfo.enabled); + this._setType(fogInfo.type); + this.fogDensity = fogInfo.fogDensity; + this.fogStart = fogInfo.fogStart; + this.fogEnd = fogInfo.fogEnd; + this.fogAtten = fogInfo.fogAtten; + this.fogTop = fogInfo.fogTop; + this.fogRange = fogInfo.fogRange; } public activate () { @@ -236,12 +277,16 @@ export class Fog { root.onGlobalPipelineStateChanged(); } - public destroy () { - if (this._handle) { + protected _destroy () { + if (JSB && this._handle) { FogPool.free(this._handle); this._handle = NULL_HANDLE; } } + + public destroy () { + this._destroy(); + } } legacyCC.Fog = Fog; diff --git a/cocos/core/renderer/scene/render-scene.ts b/cocos/core/renderer/scene/render-scene.ts index c9dc33b530..75f711f46c 100644 --- a/cocos/core/renderer/scene/render-scene.ts +++ b/cocos/core/renderer/scene/render-scene.ts @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - +import { JSB } from 'internal:constants'; import { Root } from '../../root'; import { Node } from '../../scene-graph'; import { Camera } from './camera'; @@ -147,31 +147,37 @@ export class RenderScene { } } + protected _destroy () { + if (JSB) { + if (this._modelArrayHandle) { + ModelArrayPool.free(this._modelArrayHandle); + this._modelArrayHandle = NULL_HANDLE; + } + if (this._scenePoolHandle) { + ScenePool.free(this._scenePoolHandle); + this._scenePoolHandle = NULL_HANDLE; + } + if (this._sphereLightsHandle) { + LightArrayPool.free(this._sphereLightsHandle); + this._sphereLightsHandle = NULL_HANDLE; + } + if (this._spotLightsHandle) { + LightArrayPool.free(this._spotLightsHandle); + this._spotLightsHandle = NULL_HANDLE; + } + if (this._batchArrayHandle) { + UIBatchArrayPool.free(this._batchArrayHandle); + this._batchArrayHandle = NULL_HANDLE; + } + } + } + public destroy () { this.removeCameras(); this.removeSphereLights(); this.removeSpotLights(); this.removeModels(); - if (this._modelArrayHandle) { - ModelArrayPool.free(this._modelArrayHandle); - this._modelArrayHandle = NULL_HANDLE; - } - if (this._scenePoolHandle) { - ScenePool.free(this._scenePoolHandle); - this._scenePoolHandle = NULL_HANDLE; - } - if (this._sphereLightsHandle) { - LightArrayPool.free(this._sphereLightsHandle); - this._sphereLightsHandle = NULL_HANDLE; - } - if (this._spotLightsHandle) { - LightArrayPool.free(this._spotLightsHandle); - this._spotLightsHandle = NULL_HANDLE; - } - if (this._batchArrayHandle) { - UIBatchArrayPool.free(this._batchArrayHandle); - this._batchArrayHandle = NULL_HANDLE; - } + this._destroy(); } public addCamera (cam: Camera) { @@ -198,7 +204,9 @@ export class RenderScene { public setMainLight (dl: DirectionalLight) { this._mainLight = dl; - ScenePool.set(this._scenePoolHandle, SceneView.MAIN_LIGHT, dl.handle); + if (JSB) { + ScenePool.set(this._scenePoolHandle, SceneView.MAIN_LIGHT, dl.handle); + } } public unsetMainLight (dl: DirectionalLight) { @@ -233,7 +241,9 @@ export class RenderScene { public addSphereLight (pl: SphereLight) { pl.attachToScene(this); this._sphereLights.push(pl); - LightArrayPool.push(this._sphereLightsHandle, pl.handle); + if (JSB) { + LightArrayPool.push(this._sphereLightsHandle, pl.handle); + } } public removeSphereLight (pl: SphereLight) { @@ -241,7 +251,9 @@ export class RenderScene { if (this._sphereLights[i] === pl) { pl.detachFromScene(); this._sphereLights.splice(i, 1); - LightArrayPool.erase(this._sphereLightsHandle, i); + if (JSB) { + LightArrayPool.erase(this._sphereLightsHandle, i); + } return; } } @@ -250,7 +262,9 @@ export class RenderScene { public addSpotLight (sl: SpotLight) { sl.attachToScene(this); this._spotLights.push(sl); - LightArrayPool.push(this._spotLightsHandle, sl.handle); + if (JSB) { + LightArrayPool.push(this._spotLightsHandle, sl.handle); + } } public removeSpotLight (sl: SpotLight) { @@ -258,7 +272,9 @@ export class RenderScene { if (this._spotLights[i] === sl) { sl.detachFromScene(); this._spotLights.splice(i, 1); - LightArrayPool.erase(this._spotLightsHandle, i); + if (JSB) { + LightArrayPool.erase(this._spotLightsHandle, i); + } return; } } @@ -269,7 +285,9 @@ export class RenderScene { this._sphereLights[i].detachFromScene(); } this._sphereLights.length = 0; - LightArrayPool.clear(this._sphereLightsHandle); + if (JSB) { + LightArrayPool.clear(this._sphereLightsHandle); + } } public removeSpotLights () { @@ -277,13 +295,17 @@ export class RenderScene { this._spotLights[i].detachFromScene(); } this._spotLights = []; - LightArrayPool.clear(this._spotLightsHandle); + if (JSB) { + LightArrayPool.clear(this._spotLightsHandle); + } } public addModel (m: Model) { m.attachToScene(this); this._models.push(m); - ModelArrayPool.push(this._modelArrayHandle, m.handle); + if (JSB) { + ModelArrayPool.push(this._modelArrayHandle, m.handle); + } } public removeModel (model: Model) { @@ -291,7 +313,9 @@ export class RenderScene { if (this._models[i] === model) { model.detachFromScene(); this._models.splice(i, 1); - ModelArrayPool.erase(this._modelArrayHandle, i); + if (JSB) { + ModelArrayPool.erase(this._modelArrayHandle, i); + } return; } } @@ -303,19 +327,25 @@ export class RenderScene { m.destroy(); } this._models.length = 0; - ModelArrayPool.clear(this._modelArrayHandle); + if (JSB) { + ModelArrayPool.clear(this._modelArrayHandle); + } } public addBatch (batch: DrawBatch2D) { this._batches.push(batch); - UIBatchArrayPool.push(this._batchArrayHandle, batch.handle); + if (JSB) { + UIBatchArrayPool.push(this._batchArrayHandle, batch.handle); + } } public removeBatch (batch: DrawBatch2D) { for (let i = 0; i < this._batches.length; ++i) { if (this._batches[i] === batch) { this._batches.splice(i, 1); - UIBatchArrayPool.erase(this._batchArrayHandle, i); + if (JSB) { + UIBatchArrayPool.erase(this._batchArrayHandle, i); + } return; } } @@ -323,7 +353,9 @@ export class RenderScene { public removeBatches () { this._batches.length = 0; - UIBatchArrayPool.clear(this._batchArrayHandle); + if (JSB) { + UIBatchArrayPool.clear(this._batchArrayHandle); + } } public onGlobalPipelineStateChanged () { @@ -337,21 +369,23 @@ export class RenderScene { } private _createHandles () { - if (!this._modelArrayHandle) { - this._modelArrayHandle = ModelArrayPool.alloc(); - this._scenePoolHandle = ScenePool.alloc(); - ScenePool.set(this._scenePoolHandle, SceneView.MODEL_ARRAY, this._modelArrayHandle); + if (JSB) { + if (!this._modelArrayHandle) { + this._modelArrayHandle = ModelArrayPool.alloc(); + this._scenePoolHandle = ScenePool.alloc(); + ScenePool.set(this._scenePoolHandle, SceneView.MODEL_ARRAY, this._modelArrayHandle); - this._spotLightsHandle = LightArrayPool.alloc(); - ScenePool.set(this._scenePoolHandle, SceneView.SPOT_LIGHT_ARRAY, this._spotLightsHandle); + this._spotLightsHandle = LightArrayPool.alloc(); + ScenePool.set(this._scenePoolHandle, SceneView.SPOT_LIGHT_ARRAY, this._spotLightsHandle); - this._sphereLightsHandle = LightArrayPool.alloc(); - ScenePool.set(this._scenePoolHandle, SceneView.SPHERE_LIGHT_ARRAY, this._sphereLightsHandle); - } + this._sphereLightsHandle = LightArrayPool.alloc(); + ScenePool.set(this._scenePoolHandle, SceneView.SPHERE_LIGHT_ARRAY, this._sphereLightsHandle); + } - if (!this._batchArrayHandle) { - this._batchArrayHandle = UIBatchArrayPool.alloc(); - ScenePool.set(this._scenePoolHandle, SceneView.BATCH_ARRAY_2D, this._batchArrayHandle); + if (!this._batchArrayHandle) { + this._batchArrayHandle = UIBatchArrayPool.alloc(); + ScenePool.set(this._scenePoolHandle, SceneView.BATCH_ARRAY_2D, this._batchArrayHandle); + } } } } diff --git a/cocos/core/renderer/scene/shadows.ts b/cocos/core/renderer/scene/shadows.ts index c0155fee96..63ce7e6360 100644 --- a/cocos/core/renderer/scene/shadows.ts +++ b/cocos/core/renderer/scene/shadows.ts @@ -23,6 +23,7 @@ THE SOFTWARE. */ +import { JSB } from 'internal:constants'; import { Material } from '../../assets/material'; import { Sphere } from '../../geometry'; import { Color, Mat4, Vec3, Vec2 } from '../../math'; @@ -111,13 +112,11 @@ export class Shadows { * @zh 是否启用平面阴影? */ get enabled (): boolean { - if (ShadowsPool.get(this._handle, ShadowsView.ENABLE)) { return true; } - return false; + return this._enabled; } set enabled (val: boolean) { - ShadowsPool.set(this._handle, ShadowsView.ENABLE, val ? 1 : 0); - if (!val) ShadowsPool.set(this._handle, ShadowsView.TYPE, SHADOW_TYPE_NONE); + this._setEnable(val); this.activate(); } @@ -131,7 +130,9 @@ export class Shadows { set normal (val: Vec3) { Vec3.copy(this._normal, val); - ShadowsPool.setVec3(this._handle, ShadowsView.NORMAL, this._normal); + if (JSB) { + ShadowsPool.setVec3(this._handle, ShadowsView.NORMAL, this._normal); + } } /** @@ -139,11 +140,14 @@ export class Shadows { * @zh 阴影接收平面与原点的距离。 */ get distance (): number { - return ShadowsPool.get(this._handle, ShadowsView.DISTANCE); + return this._distance; } set distance (val: number) { - ShadowsPool.set(this._handle, ShadowsView.DISTANCE, val); + this._distance = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.DISTANCE, val); + } } /** @@ -156,7 +160,9 @@ export class Shadows { set shadowColor (color: Color) { this._shadowColor = color; - ShadowsPool.setVec4(this._handle, ShadowsView.COLOR, color); + if (JSB) { + ShadowsPool.setVec4(this._handle, ShadowsView.COLOR, color); + } } /** @@ -164,10 +170,10 @@ export class Shadows { * @zh 阴影类型。 */ get type (): number { - return ShadowsPool.get(this._handle, ShadowsView.TYPE); + return this._type; } set type (val: number) { - ShadowsPool.set(this._handle, ShadowsView.TYPE, this.enabled ? val : SHADOW_TYPE_NONE); + this._setType(val); this.activate(); } @@ -176,10 +182,13 @@ export class Shadows { * @zh 获取或者设置阴影相机近裁剪面。 */ public get near (): number { - return ShadowsPool.get(this._handle, ShadowsView.NEAR); + return this._near; } public set near (val: number) { - ShadowsPool.set(this._handle, ShadowsView.NEAR, val); + this._near = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.NEAR, val); + } } /** @@ -187,10 +196,13 @@ export class Shadows { * @zh 获取或者设置阴影相机远裁剪面。 */ public get far (): number { - return ShadowsPool.get(this._handle, ShadowsView.FAR); + return this._far; } public set far (val: number) { - ShadowsPool.set(this._handle, ShadowsView.FAR, val); + this._far = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.FAR, val); + } } /** @@ -198,10 +210,13 @@ export class Shadows { * @zh 获取或者设置阴影相机的宽高比。 */ public get aspect (): number { - return ShadowsPool.get(this._handle, ShadowsView.ASPECT); + return this._aspect; } public set aspect (val: number) { - ShadowsPool.set(this._handle, ShadowsView.ASPECT, val); + this._aspect = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.ASPECT, val); + } } /** @@ -209,10 +224,13 @@ export class Shadows { * @zh 获取或者设置阴影相机正交大小。 */ public get orthoSize (): number { - return ShadowsPool.get(this._handle, ShadowsView.ORTHO_SIZE); + return this._orthoSize; } public set orthoSize (val: number) { - ShadowsPool.set(this._handle, ShadowsView.ORTHO_SIZE, val); + this._orthoSize = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.ORTHO_SIZE, val); + } } /** @@ -224,7 +242,9 @@ export class Shadows { } public set size (val: Vec2) { this._size = val; - ShadowsPool.setVec2(this._handle, ShadowsView.SIZE, this._size); + if (JSB) { + ShadowsPool.setVec2(this._handle, ShadowsView.SIZE, this._size); + } } /** @@ -232,10 +252,13 @@ export class Shadows { * @zh 获取或者设置阴影pcf等级。 */ public get pcf (): number { - return ShadowsPool.get(this._handle, ShadowsView.PCF_TYPE); + return this._pcf; } public set pcf (val: number) { - ShadowsPool.set(this._handle, ShadowsView.PCF_TYPE, val); + this._pcf = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.PCF_TYPE, val); + } } /** @@ -243,11 +266,13 @@ export class Shadows { * @zh 阴影贴图大小是否被修改。 */ public get shadowMapDirty (): boolean { - if (ShadowsPool.get(this._handle, ShadowsView.SHADOW_MAP_DIRTY)) { return true; } - return false; + return this._shadowMapDirty; } public set shadowMapDirty (val: boolean) { - ShadowsPool.set(this._handle, ShadowsView.SHADOW_MAP_DIRTY, val ? 1 : 0); + this._shadowMapDirty = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.SHADOW_MAP_DIRTY, val ? 1 : 0); + } } /** @@ -255,10 +280,13 @@ export class Shadows { * @zh 获取或者设置阴影偏移量。 */ public get bias (): number { - return ShadowsPool.get(this._handle, ShadowsView.BIAS); + return this._bias; } public set bias (val: number) { - ShadowsPool.set(this._handle, ShadowsView.BIAS, val); + this._bias = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.BIAS, val); + } } /** @@ -266,11 +294,13 @@ export class Shadows { * @zh 打开或者关闭深度压缩。 */ public get packing (): boolean { - if (ShadowsPool.get(this._handle, ShadowsView.PACKING)) { return true; } - return false; + return this._packing; } public set packing (val: boolean) { - ShadowsPool.set(this._handle, ShadowsView.PACKING, val ? 1 : 0); + this._packing = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.PACKING, val ? 1 : 0); + } } /** @@ -278,11 +308,13 @@ export class Shadows { * @zh 打开或者关闭线性深度。 */ public get linear (): boolean { - if (ShadowsPool.get(this._handle, ShadowsView.LINEAR)) { return true; } - return false; + return this._linear; } public set linear (val: boolean) { - ShadowsPool.set(this._handle, ShadowsView.LINEAR, val ? 1 : 0); + this._linear = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.LINEAR, val ? 1 : 0); + } } /** @@ -290,11 +322,13 @@ export class Shadows { * @zh 打开或者关闭自阴影。 */ public get selfShadow (): boolean { - if (ShadowsPool.get(this._handle, ShadowsView.SELF_SHADOW)) { return true; } - return false; + return this._selfShadow; } public set selfShadow (val: boolean) { - ShadowsPool.set(this._handle, ShadowsView.SELF_SHADOW, val ? 1 : 0); + this._selfShadow = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.SELF_SHADOW, val ? 1 : 0); + } } /** @@ -302,10 +336,13 @@ export class Shadows { * @zh 设置或者获取法线偏移。 */ public get normalBias (): number { - return ShadowsPool.get(this._handle, ShadowsView.NORMAL_BIAS); + return this._normalBias; } public set normalBias (val: number) { - ShadowsPool.set(this._handle, ShadowsView.NORMAL_BIAS, val); + this._normalBias = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.NORMAL_BIAS, val); + } } /** @@ -313,11 +350,13 @@ export class Shadows { * @zh 获取或者设置阴影是否自动控制。 */ public get autoAdapt (): boolean { - if (ShadowsPool.get(this._handle, ShadowsView.AUTO_ADAPT)) { return true; } - return false; + return this._autoAdapt; } public set autoAdapt (val: boolean) { - ShadowsPool.set(this._handle, ShadowsView.AUTO_ADAPT, val ? 1 : 0); + this._autoAdapt = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.AUTO_ADAPT, val ? 1 : 0); + } } public get matLight () { @@ -355,16 +394,35 @@ export class Shadows { protected _instancingMaterial: Material | null = null; protected _size: Vec2 = new Vec2(512, 512); protected _handle: ShadowsHandle = NULL_HANDLE; + protected _enabled = false; + protected _distance = 0; + protected _type = SHADOW_TYPE_NONE; + protected _near = 0; + protected _far = 0; + protected _aspect = 1; + protected _orthoSize = 1; + protected _pcf = 0; + protected _shadowMapDirty = false; + protected _packing = false; + protected _bias = 0; + protected _linear = false; + protected _selfShadow = false; + protected _normalBias = 0; + protected _autoAdapt = false; constructor () { - this._handle = ShadowsPool.alloc(); + if (JSB) { + this._handle = ShadowsPool.alloc(); + } } public getPlanarShader (patches: IMacroPatch[] | null): ShaderHandle { if (!this._material) { this._material = new Material(); this._material.initialize({ effectName: 'planar-shadow' }); - ShadowsPool.set(this._handle, ShadowsView.PLANAR_PASS, this._material.passes[0].handle); + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.PLANAR_PASS, this._material.passes[0].handle); + } } return this._material.passes[0].getShaderVariant(patches); @@ -374,34 +432,48 @@ export class Shadows { if (!this._instancingMaterial) { this._instancingMaterial = new Material(); this._instancingMaterial.initialize({ effectName: 'planar-shadow', defines: { USE_INSTANCING: true } }); - ShadowsPool.set(this._handle, ShadowsView.INSTANCE_PASS, this._instancingMaterial.passes[0].handle); + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.INSTANCE_PASS, this._instancingMaterial.passes[0].handle); + } } return this._instancingMaterial.passes[0].getShaderVariant(patches); } + private _setEnable (val) { + this._enabled = val; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.ENABLE, val ? 1 : 0); + if (!val) this._setType(SHADOW_TYPE_NONE); + } + } + + private _setType (val) { + this._type = this.enabled ? val : SHADOW_TYPE_NONE; + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.TYPE, this._type); + } + } + public initialize (shadowsInfo: ShadowsInfo) { - ShadowsPool.set(this._handle, ShadowsView.TYPE, shadowsInfo.enabled ? shadowsInfo.type : SHADOW_TYPE_NONE); - ShadowsPool.set(this._handle, ShadowsView.NEAR, shadowsInfo.near); - ShadowsPool.set(this._handle, ShadowsView.FAR, shadowsInfo.far); - ShadowsPool.set(this._handle, ShadowsView.ASPECT, shadowsInfo.aspect); - ShadowsPool.set(this._handle, ShadowsView.ORTHO_SIZE, shadowsInfo.orthoSize); - this._size = shadowsInfo.shadowMapSize; - ShadowsPool.setVec2(this._handle, ShadowsView.SIZE, this._size); - ShadowsPool.set(this._handle, ShadowsView.PCF_TYPE, shadowsInfo.pcf); - Vec3.copy(this._normal, shadowsInfo.normal); - ShadowsPool.setVec3(this._handle, ShadowsView.NORMAL, this._normal); - ShadowsPool.set(this._handle, ShadowsView.DISTANCE, shadowsInfo.distance); - this._shadowColor.set(shadowsInfo.shadowColor); - ShadowsPool.setVec4(this._handle, ShadowsView.COLOR, this._shadowColor); - ShadowsPool.set(this._handle, ShadowsView.BIAS, shadowsInfo.bias); - ShadowsPool.set(this._handle, ShadowsView.PACKING, shadowsInfo.packing ? 1 : 0); - ShadowsPool.set(this._handle, ShadowsView.LINEAR, shadowsInfo.linear ? 1 : 0); - ShadowsPool.set(this._handle, ShadowsView.SELF_SHADOW, shadowsInfo.selfShadow ? 1 : 0); - ShadowsPool.set(this._handle, ShadowsView.NORMAL_BIAS, shadowsInfo.normalBias); - ShadowsPool.set(this._handle, ShadowsView.ENABLE, shadowsInfo.enabled ? 1 : 0); + this.near = shadowsInfo.near; + this.far = shadowsInfo.far; + this.aspect = shadowsInfo.aspect; + this.orthoSize = shadowsInfo.orthoSize; + this.size = shadowsInfo.shadowMapSize; + this.pcf = shadowsInfo.pcf; + this.normal = shadowsInfo.normal; + this.distance = shadowsInfo.distance; + this.shadowColor = shadowsInfo.shadowColor; + this.bias = shadowsInfo.bias; + this.packing = shadowsInfo.packing; + this.linear = shadowsInfo.linear; + this.selfShadow = shadowsInfo.selfShadow; + this.normalBias = shadowsInfo.normalBias; this.maxReceived = shadowsInfo.maxReceived; - ShadowsPool.set(this._handle, ShadowsView.AUTO_ADAPT, shadowsInfo.autoAdapt ? 1 : 0); + this.autoAdapt = shadowsInfo.autoAdapt; + this._setEnable(shadowsInfo.enabled); + this._setType(shadowsInfo.type); } public activate () { @@ -423,12 +495,16 @@ export class Shadows { if (!this._material) { this._material = new Material(); this._material.initialize({ effectName: 'planar-shadow' }); - ShadowsPool.set(this._handle, ShadowsView.PLANAR_PASS, this._material.passes[0].handle); + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.PLANAR_PASS, this._material.passes[0].handle); + } } if (!this._instancingMaterial) { this._instancingMaterial = new Material(); this._instancingMaterial.initialize({ effectName: 'planar-shadow', defines: { USE_INSTANCING: true } }); - ShadowsPool.set(this._handle, ShadowsView.INSTANCE_PASS, this._instancingMaterial.passes[0].handle); + if (JSB) { + ShadowsPool.set(this._handle, ShadowsView.INSTANCE_PASS, this._instancingMaterial.passes[0].handle); + } } const root = legacyCC.director.root; @@ -444,7 +520,15 @@ export class Shadows { root.onGlobalPipelineStateChanged(); } + protected _destroy () { + if (JSB && this._handle) { + ShadowsPool.free(this._handle); + this._handle = NULL_HANDLE; + } + } + public destroy () { + this._destroy(); if (this._material) { this._material.destroy(); } @@ -453,11 +537,6 @@ export class Shadows { this._instancingMaterial.destroy(); } - if (this._handle) { - ShadowsPool.free(this._handle); - this._handle = NULL_HANDLE; - } - this.sphere.destroy(); } } diff --git a/cocos/core/renderer/scene/skybox.ts b/cocos/core/renderer/scene/skybox.ts index eddf531375..19e586bdbd 100644 --- a/cocos/core/renderer/scene/skybox.ts +++ b/cocos/core/renderer/scene/skybox.ts @@ -23,6 +23,7 @@ THE SOFTWARE. */ +import { JSB } from 'internal:constants'; import { builtinResMgr } from '../../builtin'; import { Material } from '../../assets/material'; import { Mesh } from '../../../3d/assets/mesh'; @@ -50,24 +51,23 @@ export class Skybox { * @zh 是否启用天空盒? */ get enabled (): boolean { - return SkyboxPool.get(this._handle, SkyboxView.ENABLE) as unknown as boolean; + return this._enabled; } set enabled (val: boolean) { if (val) this.activate(); else this._updatePipeline(); - SkyboxPool.set(this._handle, SkyboxView.ENABLE, val ? 1 : 0); + this._setEnabled(val); } - /** * @en Whether use environment lighting * @zh 是否启用环境光照? */ get useIBL (): boolean { - return SkyboxPool.get(this._handle, SkyboxView.USE_IBL) as unknown as boolean; + return this._useIBL; } set useIBL (val: boolean) { - SkyboxPool.set(this._handle, SkyboxView.USE_IBL, val ? 1 : 0); + this._setUseIBL(val); this._updatePipeline(); } @@ -76,7 +76,7 @@ export class Skybox { * @zh 是否需要开启 shader 内的 RGBE 数据支持? */ get isRGBE (): boolean { - return SkyboxPool.get(this._handle, SkyboxView.IS_RGBE) as unknown as boolean; + return this._isRGBE; } set isRGBE (val: boolean) { @@ -89,7 +89,7 @@ export class Skybox { this._model.setSubModelMaterial(0, skybox_material!); } } - SkyboxPool.set(this._handle, SkyboxView.IS_RGBE, val ? 1 : 0); + this._setIsRGBE(val); this._updatePipeline(); } @@ -114,7 +114,9 @@ export class Skybox { protected _model: Model | null = null; protected _default: TextureCube | null = null; protected _handle: SkyboxHandle = NULL_HANDLE; - + protected _enabled = false; + protected _useIBL = false; + protected _isRGBE = false; get handle () : SkyboxHandle { return this._handle; } @@ -123,10 +125,31 @@ export class Skybox { this._handle = SkyboxPool.alloc(); } + private _setEnabled (val) { + this._enabled = val; + if (JSB) { + SkyboxPool.set(this._handle, SkyboxView.ENABLE, val ? 1 : 0); + } + } + + private _setUseIBL (val) { + this._useIBL = val; + if (JSB) { + SkyboxPool.set(this._handle, SkyboxView.USE_IBL, val ? 1 : 0); + } + } + + private _setIsRGBE (val) { + this._isRGBE = val; + if (JSB) { + SkyboxPool.set(this._handle, SkyboxView.IS_RGBE, val ? 1 : 0); + } + } + public initialize (skyboxInfo: SkyboxInfo) { - SkyboxPool.set(this._handle, SkyboxView.ENABLE, skyboxInfo.enabled ? 1 : 0); - SkyboxPool.set(this._handle, SkyboxView.USE_IBL, skyboxInfo.useIBL ? 1 : 0); - SkyboxPool.set(this._handle, SkyboxView.IS_RGBE, skyboxInfo.isRGBE ? 1 : 0); + this._setEnabled(skyboxInfo.enabled); + this._setUseIBL(skyboxInfo.useIBL); + this._setIsRGBE(skyboxInfo.isRGBE); this._envmap = skyboxInfo.envmap; } @@ -141,8 +164,9 @@ export class Skybox { // @ts-expect-error private member access this._model._initLocalDescriptors = () => {}; } - - SkyboxPool.set(this._handle, SkyboxView.MODEL, this._model.handle); + if (JSB) { + SkyboxPool.set(this._handle, SkyboxView.MODEL, this._model.handle); + } if (!this._envmap) { this._envmap = this._default; } @@ -181,12 +205,16 @@ export class Skybox { this._globalDescriptorSet!.bindTexture(UNIFORM_ENVIRONMENT_BINDING, texture); } - public destroy () { - if (this._handle) { + protected _destroy () { + if (JSB && this._handle) { SkyboxPool.free(this._handle); this._handle = NULL_HANDLE; } } + + public destroy () { + this._destroy(); + } } legacyCC.Skybox = Skybox; diff --git a/cocos/core/scene-graph/node.ts b/cocos/core/scene-graph/node.ts index 0f5b6e1ece..f69a8d2f65 100644 --- a/cocos/core/scene-graph/node.ts +++ b/cocos/core/scene-graph/node.ts @@ -147,10 +147,15 @@ export class Node extends BaseNode { protected _poolHandle: NodeHandle = NULL_HANDLE; + protected _init () { + if (JSB) { + this._poolHandle = NodePool.alloc(); + NodePool.set(this._poolHandle, NodeView.LAYER, this._layer); + } + } constructor (name?: string) { super(name); - this._poolHandle = NodePool.alloc(); - NodePool.set(this._poolHandle, NodeView.LAYER, this._layer); + this._init(); } /** @@ -161,11 +166,15 @@ export class Node extends BaseNode { return obj instanceof Node && (obj.constructor === Node || !(obj instanceof legacyCC.Scene)); } - public destroy () { - if (this._poolHandle) { + protected _destroy () { + if (JSB && this._poolHandle) { NodePool.free(this._poolHandle); this._poolHandle = NULL_HANDLE; } + } + + public destroy () { + this._destroy(); return super.destroy(); } @@ -336,7 +345,9 @@ export class Node extends BaseNode { @editable set layer (l) { this._layer = l; - NodePool.set(this._poolHandle, NodeView.LAYER, this._layer); + if (JSB) { + NodePool.set(this._poolHandle, NodeView.LAYER, this._layer); + } this.emit(SystemEventType.LAYER_CHANGED, this._layer); } @@ -354,7 +365,9 @@ export class Node extends BaseNode { set hasChangedFlags (val: number) { bookOfChange.set(this, val); - NodePool.set(this._poolHandle, NodeView.FLAGS_CHANGED, val); + if (JSB) { + NodePool.set(this._poolHandle, NodeView.FLAGS_CHANGED, val); + } } // =============================== @@ -397,9 +410,10 @@ export class Node extends BaseNode { } public _onBatchCreated (dontSyncChildPrefab: boolean) { - NodePool.set(this._poolHandle, NodeView.LAYER, this._layer); - NodePool.setVec3(this._poolHandle, NodeView.WORLD_SCALE, this._scale); - + if (JSB) { + NodePool.set(this._poolHandle, NodeView.LAYER, this._layer); + NodePool.setVec3(this._poolHandle, NodeView.WORLD_SCALE, this._scale); + } const prefabInstance = this._prefab?.instance; if (!dontSyncChildPrefab && prefabInstance) { createNodeWithPrefab(this); @@ -478,7 +492,9 @@ export class Node extends BaseNode { if (this._eventMask & TRANSFORM_ON) { this.emit(SystemEventType.TRANSFORM_CHANGED, TransformBit.POSITION); } - NodePool.setVec3(this._poolHandle, NodeView.WORLD_POSITION, this.worldPosition); + if (JSB) { + NodePool.setVec3(this._poolHandle, NodeView.WORLD_POSITION, this.worldPosition); + } } /** @@ -505,7 +521,9 @@ export class Node extends BaseNode { if (this._eventMask & TRANSFORM_ON) { this.emit(SystemEventType.TRANSFORM_CHANGED, TransformBit.ROTATION); } - NodePool.setVec4(this._poolHandle, NodeView.WORLD_ROTATION, this.worldRotation); + if (JSB) { + NodePool.setVec4(this._poolHandle, NodeView.WORLD_ROTATION, this.worldRotation); + } } /** @@ -571,21 +589,27 @@ export class Node extends BaseNode { child._mat.m12 = child._pos.x; child._mat.m13 = child._pos.y; child._mat.m14 = child._pos.z; - NodePool.setVec3(child._poolHandle, NodeView.WORLD_POSITION, child._pos); + if (JSB) { + NodePool.setVec3(child._poolHandle, NodeView.WORLD_POSITION, child._pos); + } } if (dirtyBits & TransformBit.RS) { Mat4.fromRTS(child._mat, child._lrot, child._lpos, child._lscale); Mat4.multiply(child._mat, cur._mat, child._mat); if (dirtyBits & TransformBit.ROTATION) { Quat.multiply(child._rot, cur._rot, child._lrot); - NodePool.setVec4(child._poolHandle, NodeView.WORLD_ROTATION, child._rot); + if (JSB) { + NodePool.setVec4(child._poolHandle, NodeView.WORLD_ROTATION, child._rot); + } } Mat3.fromQuat(m3_1, Quat.conjugate(qt_1, child._rot)); Mat3.multiplyMat4(m3_1, m3_1, child._mat); child._scale.x = m3_1.m00; child._scale.y = m3_1.m04; child._scale.z = m3_1.m08; - NodePool.setVec3(child._poolHandle, NodeView.WORLD_SCALE, child._scale); + if (JSB) { + NodePool.setVec3(child._poolHandle, NodeView.WORLD_SCALE, child._scale); + } } } else { if (dirtyBits & TransformBit.POSITION) { @@ -593,22 +617,28 @@ export class Node extends BaseNode { child._mat.m12 = child._pos.x; child._mat.m13 = child._pos.y; child._mat.m14 = child._pos.z; - NodePool.setVec3(child._poolHandle, NodeView.WORLD_POSITION, child._pos); + if (JSB) { + NodePool.setVec3(child._poolHandle, NodeView.WORLD_POSITION, child._pos); + } } if (dirtyBits & TransformBit.RS) { if (dirtyBits & TransformBit.ROTATION) { Quat.copy(child._rot, child._lrot); - NodePool.setVec4(child._poolHandle, NodeView.WORLD_ROTATION, child._rot); + if (JSB) { + NodePool.setVec4(child._poolHandle, NodeView.WORLD_ROTATION, child._rot); + } } if (dirtyBits & TransformBit.SCALE) { Vec3.copy(child._scale, child._lscale); - NodePool.setVec3(child._poolHandle, NodeView.WORLD_SCALE, child._scale); + if (JSB) { + NodePool.setVec3(child._poolHandle, NodeView.WORLD_SCALE, child._scale); + } Mat4.fromRTS(child._mat, child._rot, child._pos, child._scale); } } } - if (dirtyBits !== TransformBit.NONE) { + if (dirtyBits !== TransformBit.NONE && JSB) { NodePool.setMat4(child._poolHandle, NodeView.WORLD_MATRIX, child._mat); } @@ -833,7 +863,9 @@ export class Node extends BaseNode { } else { Vec3.set(this._pos, val as number, y, z); } - NodePool.setVec3(this._poolHandle, NodeView.WORLD_POSITION, this._pos); + if (JSB) { + NodePool.setVec3(this._poolHandle, NodeView.WORLD_POSITION, this._pos); + } const parent = this._parent; const local = this._lpos; if (parent) { @@ -891,7 +923,9 @@ export class Node extends BaseNode { } else { Quat.set(this._rot, val as number, y, z, w); } - NodePool.setVec4(this._poolHandle, NodeView.WORLD_ROTATION, this._rot); + if (JSB) { + NodePool.setVec4(this._poolHandle, NodeView.WORLD_ROTATION, this._rot); + } if (this._parent) { this._parent.updateWorldTransform(); Quat.multiply(this._lrot, Quat.conjugate(this._lrot, this._parent._rot), this._rot); @@ -965,7 +999,9 @@ export class Node extends BaseNode { } else { Vec3.set(this._scale, val as number, y, z); } - NodePool.setVec3(this._poolHandle, NodeView.WORLD_SCALE, this._scale); + if (JSB) { + NodePool.setVec3(this._poolHandle, NodeView.WORLD_SCALE, this._scale); + } const parent = this._parent; if (parent) { parent.updateWorldTransform(); @@ -1124,7 +1160,7 @@ export class Node extends BaseNode { */ public syncToNativeTransform () { const v = this.hasChangedFlags; - if (v) { + if (v && JSB) { if (v & TransformBit.POSITION) { NodePool.setVec3(this._poolHandle, NodeView.WORLD_POSITION, this.worldPosition); } if (v & TransformBit.ROTATION) { NodePool.setVec3(this._poolHandle, NodeView.WORLD_ROTATION, this.worldRotation); } if (v & TransformBit.SCALE) { NodePool.setVec3(this._poolHandle, NodeView.WORLD_SCALE, this.worldScale); }