diff --git a/cocos/core/assets/asset.jsb.ts b/cocos/core/assets/asset.jsb.ts index c0451a1535e..1b2c3be2abc 100644 --- a/cocos/core/assets/asset.jsb.ts +++ b/cocos/core/assets/asset.jsb.ts @@ -23,18 +23,26 @@ THE SOFTWARE. */ -import { legacyCC } from "../global-exports"; -import { js } from "../utils"; -import { CallbacksInvoker } from "../event/callbacks-invoker"; +import { legacyCC } from '../global-exports'; +import { CallbacksInvoker } from '../event/callbacks-invoker'; +import { applyMixins } from '../event/event-target-factory'; +import { createMap } from "../utils/js-typed"; /** * @param error - null or the error info * @param node - the created node or null */ -type CreateNodeCallback = (error: Error | null, node: Node) => void; +export type CreateNodeCallback = (error: Error | null, node: Node) => void; -js.mixin(jsb.Asset, CallbacksInvoker); -jsb.Asset.prototype.createNode = null!; +applyMixins(jsb.Asset, [CallbacksInvoker]); + +const assetProto: any = jsb.Asset.prototype; + +assetProto._ctor = function() { + this._callbackTable = createMap(true); +}; + +assetProto.createNode = null!; export type Asset = jsb.Asset; export const Asset = jsb.Asset; diff --git a/cocos/core/assets/material.jsb.ts b/cocos/core/assets/material.jsb.ts index 856e82522b9..acfd56fb981 100644 --- a/cocos/core/assets/material.jsb.ts +++ b/cocos/core/assets/material.jsb.ts @@ -31,8 +31,10 @@ import { EffectAsset } from './effect-asset'; import { Texture } from '../gfx'; import { TextureBase } from './texture-base'; import { legacyCC } from '../global-exports'; -import { PassOverrides } from '../renderer/core/pass'; -import { MacroRecord, MaterialProperty, PropertyType } from '../renderer/core/pass-utils'; +import { PassOverrides, MacroRecord, MaterialProperty } from '../renderer'; + +import { Color, Mat3, Mat4, Quat, Vec2, Vec3, Vec4 } from '../math'; +import {wrap} from "yargs"; /** * @en The basic infos for material initialization. @@ -78,6 +80,162 @@ interface IMaterialInfo { type MaterialPropertyFull = MaterialProperty | TextureBase | Texture | null; +const matProto: any = jsb.Material.prototype; + +type setProperyCB = (name: string, val: MaterialPropertyFull | MaterialPropertyFull[], passIdx?: number) => void; +function wrapSetProperty(cb: setProperyCB, target: Material, name: string, val: MaterialPropertyFull | MaterialPropertyFull[], passIdx?: number) { + if (passIdx) { + cb.call(target, name, val, passIdx); + } else { + cb.call(target, name, val); + } +} + +matProto.setProperty = function (name: string, val: MaterialPropertyFull | MaterialPropertyFull[], passIdx?: number) { + if (Array.isArray(val)) { + const first = val[0]; + if (typeof first === 'number') { + if (Number.isInteger(first)) { + wrapSetProperty(this.setPropertyInt32Array, this, name, val, passIdx); + } else { + wrapSetProperty(this.setPropertyFloat32Array, this, name, val, passIdx); + } + } else if (first instanceof Vec2) { + wrapSetProperty(this.setPropertyVec2Array, this, name, val, passIdx); + } else if (first instanceof Vec3) { + wrapSetProperty(this.setPropertyVec3Array, this, name, val, passIdx); + } else if (first instanceof Vec4) { + wrapSetProperty(this.setPropertyVec4Array, this, name, val, passIdx); + } else if (first instanceof Color) { + wrapSetProperty(this.setPropertyColorArray, this, name, val, passIdx); + } else if (first instanceof Mat3) { + wrapSetProperty(this.setPropertyMat3Array, this, name, val, passIdx); + } else if (first instanceof Mat4) { + wrapSetProperty(this.setPropertyMat4Array, this, name, val, passIdx); + } else if (first instanceof Quat) { + wrapSetProperty(this.setPropertyQuatArray, this, name, val, passIdx); + } else if (first instanceof TextureBase) { + wrapSetProperty(this.setPropertyTextureBaseArray, this, name, val, passIdx); + } else if (first instanceof Texture) { + wrapSetProperty(this.setPropertyGFXTextureArray, this, name, val, passIdx); + } else { + legacyCC.error(`Material.setProperty Unknown type: ${val}`); + } + } else { + if (typeof val === 'number') { + if (Number.isInteger(val)) { + wrapSetProperty(this.setPropertyInt32, this, name, val, passIdx); + } else { + wrapSetProperty(this.setPropertyFloat32, this, name, val, passIdx); + } + } else if (val instanceof Vec2) { + wrapSetProperty(this.setPropertyVec2, this, name, val, passIdx); + } else if (val instanceof Vec3) { + wrapSetProperty(this.setPropertyVec3, this, name, val, passIdx); + } else if (val instanceof Vec4) { + wrapSetProperty(this.setPropertyVec4, this, name, val, passIdx); + } else if (val instanceof Color) { + wrapSetProperty(this.setPropertyColor, this, name, val, passIdx); + } else if (val instanceof Mat3) { + wrapSetProperty(this.setPropertyMat3, this, name, val, passIdx); + } else if (val instanceof Mat4) { + wrapSetProperty(this.setPropertyMat4, this, name, val, passIdx); + } else if (val instanceof Quat) { + wrapSetProperty(this.setPropertyQuat, this, name, val, passIdx); + } else if (val instanceof TextureBase) { + wrapSetProperty(this.setPropertyTextureBase, this, name, val, passIdx); + } else if (val instanceof Texture) { + wrapSetProperty(this.setPropertyGFXTexture, this, name, val, passIdx); + } else { + legacyCC.error(`Material.setProperty Unknown type: ${val}`); + } + } +}; + +matProto.getProperty = function (name: string, passIdx?: number) { + const val = this._getProperty(name, passIdx); + if (Array.isArray(val)) { + const first = val[0]; + const arr = []; // cjh TODO: optimize temporary gc objects being created + if (first instanceof Vec2) { + for (let i = 0, len = val.length; i < len; ++i) { + const e = val[i]; + arr.push(new Vec2(e.x, e.y)); + } + } else if (first instanceof Vec3) { + for (let i = 0, len = val.length; i < len; ++i) { + const e = val[i]; + arr.push(new Vec3(e.x, e.y, e.z)); + } + } else if (first instanceof Vec4) { + for (let i = 0, len = val.length; i < len; ++i) { + const e = val[i]; + arr.push(new Vec4(e.x, e.y, e.z, e.w)); + } + } else if (first instanceof Color) { + for (let i = 0, len = val.length; i < len; ++i) { + const e = val[i]; + arr.push(new Color(e.r, e.g, e.b, e.a)); + } + } else if (first instanceof Mat3) { + for (let i = 0, len = val.length; i < len; ++i) { + const e = val[i]; + arr.push(new Mat3( + e[0], e[1], e[2], + e[3], e[4], e[5], + e[6], e[7], e[8], + )); + } + } else if (first instanceof Mat4) { + for (let i = 0, len = val.length; i < len; ++i) { + let e = val[i]; + arr.push(new Mat4( + e[0], e[1], e[2], e[3], + e[4], e[5], e[6], e[7], + e[8], e[9], e[10], e[11], + e[12], e[13], e[14], e[15], + )); + } + } else if (first instanceof Quat) { + for (let i = 0, len = val.length; i < len; ++i) { + let e = val[i]; + arr.push(new Quat(e.x, e.y, e.z, e.w)); + } + } + + return arr ? arr : val; + } + + let ret; + const e = val; + if (val instanceof Vec2) { + ret = new Vec3(e.x, e.y); + } else if (val instanceof Vec3) { + ret = new Vec3(e.x, e.y, e.z); + } else if (val instanceof Vec4) { + ret = new Vec4(e.x, e.y, e.z, e.w); + } else if (val instanceof Color) { + ret = new Color(e.r, e.g, e.b, e.a); + } else if (val instanceof Mat3) { + ret = new Mat3( + e[0], e[1], e[2], + e[3], e[4], e[5], + e[6], e[7], e[8], + ); + } else if (val instanceof Mat4) { + ret = new Mat4( + e[0], e[1], e[2], e[3], + e[4], e[5], e[6], e[7], + e[8], e[9], e[10], e[11], + e[12], e[13], e[14], e[15], + ); + } else if (val instanceof Quat) { + ret = new Quat(e.x, e.y, e.z, e.w); + } + + return ret ? ret : val; +}; + export type Material = jsb.Material; export const Material = jsb.Material; diff --git a/cocos/core/components/component-event-handler.ts b/cocos/core/components/component-event-handler.ts index 05a02f75934..f3fe64cf1f2 100644 --- a/cocos/core/components/component-event-handler.ts +++ b/cocos/core/components/component-event-handler.ts @@ -30,7 +30,7 @@ */ import { ccclass, type, serializable, editable, tooltip } from 'cc.decorator'; -import { Node } from '../scene-graph'; +import type { Node } from '../scene-graph'; import { legacyCC } from '../global-exports'; /** @@ -191,5 +191,3 @@ export class EventHandler { } } } - -legacyCC.Component.EventHandler = EventHandler; diff --git a/cocos/core/components/component.ts b/cocos/core/components/component.ts index 87031ff0d0f..48662680cb8 100644 --- a/cocos/core/components/component.ts +++ b/cocos/core/components/component.ts @@ -42,6 +42,7 @@ import { Node } from '../scene-graph'; import { legacyCC } from '../global-exports'; import { errorID, warnID, assertID } from '../platform/debug'; import { CompPrefabInfo } from '../utils/prefab/prefab-info'; +import { EventHandler } from "./component-event-handler"; const idGenerator = new IDGenerator('Comp'); const IsOnLoadCalled = CCObject.Flags.IsOnLoadCalled; @@ -61,6 +62,8 @@ const NullNode = null as unknown as Node; */ @ccclass('cc.Component') class Component extends CCObject { + public static EventHandler = EventHandler; + get name () { if (this._name) { return this._name; diff --git a/cocos/core/pipeline/index.jsb.ts b/cocos/core/pipeline/index.jsb.ts index 36736cf8f0f..b3574423fa3 100644 --- a/cocos/core/pipeline/index.jsb.ts +++ b/cocos/core/pipeline/index.jsb.ts @@ -26,9 +26,8 @@ declare const nr: any; -import { getPhaseID } from './pass-phase' +import { getPhaseID } from './pass-phase'; import { setClassName, mixin } from '../../core/utils/js'; -import { PipelineSceneData } from './pipeline-scene-data'; import { DeferredPipelineSceneData } from './deferred/deferred-pipeline-scene-data'; import { legacyCC } from '../../core/global-exports'; import { Asset } from '../assets/asset'; @@ -40,14 +39,25 @@ export const RenderFlow = nr.RenderFlow; export const RenderStage = nr.RenderStage; export const InstancedBuffer = nr.InstancedBuffer; export const PipelineStateManager = nr.PipelineStateManager; +export const ForwardPipeline = nr.ForwardPipeline; +export const ForwardFlow = nr.ForwardFlow; +export const ShadowFlow = nr.ShadowFlow; +export const ForwardStage = nr.ForwardStage; +export const ShadowStage = nr.ShadowStage; +export const RenderQueueDesc = nr.RenderQueueDesc; +export const DeferredPipeline = nr.DeferredPipeline; +export const MainFlow = nr.MainFlow; +export const LightingStage = nr.LightingStage; +export const PostprocessStage = nr.PostprocessStage; +export const GbufferStage = nr.GbufferStage; let instancedBufferProto = nr.InstancedBuffer; let oldGetFunc = instancedBufferProto.get; let getOrCreatePipelineState = nr.PipelineStateManager.getOrCreatePipelineState; nr.PipelineStateManager.getOrCreatePipelineState = function(device, pass, shader, renderPass, ia) { - return getOrCreatePipelineState.call(device, pass.native, shader, renderPass, ia); -} + return getOrCreatePipelineState(pass, shader, renderPass, ia); //cjh TODO: remove hacking. c++ API doesn't access device argument. +}; export function createDefaultPipeline () { const pipeline = new ForwardPipeline(); @@ -56,44 +66,44 @@ export function createDefaultPipeline () { } // ForwardPipeline -export class ForwardPipeline extends nr.ForwardPipeline { - public pipelineSceneData = new PipelineSceneData(); - - constructor() { - super(); - this._tag = 0; - this._flows = []; - this.renderTextures = []; - this.materials = []; - } - - public init () { - this.setPipelineSharedSceneData(this.pipelineSceneData.native); - for (let i = 0; i < this._flows.length; i++) { - this._flows[i].init(); - } - const info = new nr.RenderPipelineInfo(this._tag, this._flows); - this.initialize(info); - } - - public activate () { - return super.activate() && this.pipelineSceneData.activate(legacyCC.director.root.device, this as any); - } - - public render (cameras) { - let nativeObjs = []; - for (let i = 0, len = cameras.length; i < len; ++i) { - nativeObjs.push(cameras[i].native) - } - super.render(nativeObjs); - } - - public destroy () { - this.pipelineSceneData.destroy(); - super.destroy(); - } -} - +// export class ForwardPipeline extends nr.ForwardPipeline { +// public pipelineSceneData = new nr.PipelineSceneData(); +// +// constructor() { +// super(); +// this._tag = 0; +// this._flows = []; +// this.renderTextures = []; +// this.materials = []; +// } +// +// public init () { +// this.setPipelineSharedSceneData(this.pipelineSceneData); +// for (let i = 0; i < this._flows.length; i++) { +// this._flows[i].init(); +// } +// const info = new nr.RenderPipelineInfo(this._tag, this._flows); +// this.initialize(info); +// } +// +// public activate () { +// return super.activate() && this.pipelineSceneData.activate(legacyCC.director.root.device, this as any); +// } +// +// public render (cameras) { +// let nativeObjs = []; +// for (let i = 0, len = cameras.length; i < len; ++i) { +// nativeObjs.push(cameras[i]) +// } +// super.render(nativeObjs); +// } +// +// public destroy () { +// this.pipelineSceneData.destroy(); +// super.destroy(); +// } +// } +// mixin(ForwardPipeline.prototype, Asset.prototype); const ForwardOnLoaded = ForwardPipeline.prototype.onLoaded; @@ -102,130 +112,130 @@ const ForwardOnLoaded = ForwardPipeline.prototype.onLoaded; ForwardPipeline.prototype.onLoaded = function () { if (ForwardOnLoaded) ForwardOnLoaded.call(this); this.init(); -} - -export class ForwardFlow extends nr.ForwardFlow { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this._stages = []; - } - init() { - for (let i = 0; i < this._stages.length; i++) { - this._stages[i].init(); - } - const info = new nr.RenderFlowInfo(this._name, this._priority, this._tag, this._stages); - this.initialize(info); - } -} - -export class ShadowFlow extends nr.ShadowFlow { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this._stages = []; - } - init() { - for (let i = 0; i < this._stages.length; i++) { - this._stages[i].init(); - } - const info = new nr.RenderFlowInfo(this._name, this._priority, this._tag, this._stages); - this.initialize(info); - } -} - -export class ForwardStage extends nr.ForwardStage { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this.renderQueues = []; - } - public init() { - const queues = []; - for (let i = 0; i < this.renderQueues.length; i++) { - queues.push(this.renderQueues[i].init()); - } - const info = new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); - this.initialize(info); - } -} - -export class ShadowStage extends nr.ShadowStage { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - } - public init() { - const info = new nr.RenderStageInfo(this._name, this._priority, this._tag, []); - this.initialize(info); - } -} - -export class RenderQueueDesc { - public isTransparent = false; - public sortMode = 0; - public stages = []; - - constructor() { - this.isTransparent = false; - this.sortMode = 0; - this.stages = []; - } - - public init() { - return new nr.RenderQueueDesc(this.isTransparent, this.sortMode, this.stages); - } -} - -export class DeferredPipeline extends nr.DeferredPipeline { - public pipelineSceneData = new DeferredPipelineSceneData(); - constructor() { - super(); - this._tag = 0; - this._flows = []; - this.renderTextures = []; - this.materials = []; - } - - init() { - this.setPipelineSharedSceneData(this.pipelineSceneData.native); - for (let i = 0; i < this._flows.length; i++) { - this._flows[i].init(this); - } - let info = new nr.RenderPipelineInfo(this._tag, this._flows); - this.initialize(info); - } - - public activate () { - return super.activate() && this.pipelineSceneData.activate(legacyCC.director.root.device, this as any); - } - - public render (cameras) { - let nativeObjs = []; - for (let i = 0, len = cameras.length; i < len; ++i) { - nativeObjs.push(cameras[i].native) - } - super.render(nativeObjs); - } - - destroy () { - this.fog.destroy(); - this.ambient.destroy(); - this.skybox.destroy(); - this.shadows.destroy(); - this.pipelineSceneData.destroy(); - super.destroy(); - } - -} +}; + +// export class ForwardFlow extends nr.ForwardFlow { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// this._stages = []; +// } +// init() { +// for (let i = 0; i < this._stages.length; i++) { +// this._stages[i].init(); +// } +// const info = new nr.RenderFlowInfo(this._name, this._priority, this._tag, this._stages); +// this.initialize(info); +// } +// } +// +// export class ShadowFlow extends nr.ShadowFlow { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// this._stages = []; +// } +// init() { +// for (let i = 0; i < this._stages.length; i++) { +// this._stages[i].init(); +// } +// const info = new nr.RenderFlowInfo(this._name, this._priority, this._tag, this._stages); +// this.initialize(info); +// } +// } +// +// export class ForwardStage extends nr.ForwardStage { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// this.renderQueues = []; +// } +// public init() { +// const queues = []; +// for (let i = 0; i < this.renderQueues.length; i++) { +// queues.push(this.renderQueues[i].init()); +// } +// const info = new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); +// this.initialize(info); +// } +// } +// +// export class ShadowStage extends nr.ShadowStage { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// } +// public init() { +// const info = new nr.RenderStageInfo(this._name, this._priority, this._tag, []); +// this.initialize(info); +// } +// } +// +// export class RenderQueueDesc { +// public isTransparent = false; +// public sortMode = 0; +// public stages = []; +// +// constructor() { +// this.isTransparent = false; +// this.sortMode = 0; +// this.stages = []; +// } +// +// public init() { +// return new nr.RenderQueueDesc(this.isTransparent, this.sortMode, this.stages); +// } +// } +// +// export class DeferredPipeline extends nr.DeferredPipeline { +// public pipelineSceneData = new DeferredPipelineSceneData(); +// constructor() { +// super(); +// this._tag = 0; +// this._flows = []; +// this.renderTextures = []; +// this.materials = []; +// } +// +// init() { +// this.setPipelineSharedSceneData(this.pipelineSceneData); +// for (let i = 0; i < this._flows.length; i++) { +// this._flows[i].init(this); +// } +// let info = new nr.RenderPipelineInfo(this._tag, this._flows); +// this.initialize(info); +// } +// +// public activate () { +// return super.activate() && this.pipelineSceneData.activate(legacyCC.director.root.device, this as any); +// } +// +// public render (cameras) { +// let nativeObjs = []; +// for (let i = 0, len = cameras.length; i < len; ++i) { +// nativeObjs.push(cameras[i].native) +// } +// super.render(nativeObjs); +// } +// +// destroy () { +// this.fog.destroy(); +// this.ambient.destroy(); +// this.skybox.destroy(); +// this.shadows.destroy(); +// this.pipelineSceneData.destroy(); +// super.destroy(); +// } +// +// } mixin(DeferredPipeline.prototype, Asset.prototype); @@ -235,94 +245,94 @@ const DeferredOnLoaded = DeferredPipeline.prototype.onLoaded; DeferredPipeline.prototype.onLoaded = function () { if (DeferredOnLoaded) DeferredOnLoaded.call(this); this.init(); -} - -export class MainFlow extends nr.MainFlow { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this._stages = []; - } - - init(pipeline) { - for (let i = 0; i < this._stages.length; i++) { - this._stages[i].init(pipeline); - } - let info = new nr.RenderFlowInfo( - this._name, this._priority, this._tag, this._stages); - this.initialize(info); - } -} - -export class GbufferStage extends nr.GbufferStage { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this.renderQueues = [] - } - - init(pipeline) { - const queues = []; - for (let i = 0; i < this.renderQueues.length; i++) { - queues.push(this.renderQueues[i].init()); - } - let info = - new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); - this.initialize(info); - } -} - -export class LightingStage extends nr.LightingStage { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this.renderQueues = []; - this._deferredMaterial = null; - } - init(pipeline) { - const queues = []; - for (let i = 0; i < this.renderQueues.length; i++) { - queues.push(this.renderQueues[i].init()); - } - pipeline.pipelineSceneData.deferredLightingMaterial = this._deferredMaterial; - let info = - new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); - this.initialize(info); - } -} - -export class PostprocessStage extends nr.PostprocessStage { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this.renderQueues = []; - this._postProcessMaterial = null; - } - init(pipeline) { - const queues = []; - for (let i = 0; i < this.renderQueues.length; i++) { - queues.push(this.renderQueues[i].init()); - } - pipeline.pipelineSceneData.deferredPostMaterial = this._postProcessMaterial; - let info = - new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); - this.initialize(info); - } -} +}; + +// export class MainFlow extends nr.MainFlow { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// this._stages = []; +// } +// +// init(pipeline) { +// for (let i = 0; i < this._stages.length; i++) { +// this._stages[i].init(pipeline); +// } +// let info = new nr.RenderFlowInfo( +// this._name, this._priority, this._tag, this._stages); +// this.initialize(info); +// } +// } +// +// export class GbufferStage extends nr.GbufferStage { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// this.renderQueues = [] +// } +// +// init(pipeline) { +// const queues = []; +// for (let i = 0; i < this.renderQueues.length; i++) { +// queues.push(this.renderQueues[i].init()); +// } +// let info = +// new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); +// this.initialize(info); +// } +// } +// +// export class LightingStage extends nr.LightingStage { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// this.renderQueues = []; +// this._deferredMaterial = null; +// } +// init(pipeline) { +// const queues = []; +// for (let i = 0; i < this.renderQueues.length; i++) { +// queues.push(this.renderQueues[i].init()); +// } +// pipeline.pipelineSceneData.deferredLightingMaterial = this._deferredMaterial; +// let info = +// new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); +// this.initialize(info); +// } +// } +// +// export class PostprocessStage extends nr.PostprocessStage { +// constructor() { +// super(); +// this._name = 0; +// this._priority = 0; +// this._tag = 0; +// this.renderQueues = []; +// this._postProcessMaterial = null; +// } +// init(pipeline) { +// const queues = []; +// for (let i = 0; i < this.renderQueues.length; i++) { +// queues.push(this.renderQueues[i].init()); +// } +// pipeline.pipelineSceneData.deferredPostMaterial = this._postProcessMaterial; +// let info = +// new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); +// this.initialize(info); +// } +// } setClassName('DeferredPipeline', DeferredPipeline); setClassName('MainFlow', MainFlow); setClassName('GbufferStage', GbufferStage); setClassName('LightingStage', LightingStage); -setClassName('PostprocessStage',PostprocessStage); +setClassName('PostprocessStage', PostprocessStage); setClassName('ForwardPipeline', ForwardPipeline); setClassName('ForwardFlow', ForwardFlow); setClassName('ShadowFlow', ShadowFlow); diff --git a/cocos/core/scene-graph/node.jsb.ts b/cocos/core/scene-graph/node.jsb.ts index 4285dce8cc7..ef51899ccd7 100644 --- a/cocos/core/scene-graph/node.jsb.ts +++ b/cocos/core/scene-graph/node.jsb.ts @@ -26,6 +26,36 @@ import { Component } from '../components/component'; import { NodeEventType } from './node-event'; import { CCObject } from '../data/object'; import { NodeUIProperties } from './node-ui-properties'; +import { NodeSpace, TransformBit } from './node-enum'; +import {Quat, Vec3} from "../math"; + +export const Node = jsb.Node; + +const NodeCls: any = Node; +/** + * @en Event types emitted by Node + * @zh 节点可能发出的事件类型 + */ +NodeCls.EventType = NodeEventType; + +/** + * @en Coordinates space + * @zh 空间变换操作的坐标系 + */ +NodeCls.NodeSpace = NodeSpace; + +/** + * @en Bit masks for Node transformation parts + * @zh 节点变换更新的具体部分 + * @deprecated please use [[Node.TransformBit]] + */ +NodeCls.TransformDirtyBit = TransformBit; + +/** + * @en Bit masks for Node transformation parts, can be used to determine which part changed in [[NodeEventType.TRANSFORM_CHANGED]] event + * @zh 节点变换更新的具体部分,可用于判断 [[NodeEventType.TRANSFORM_CHANGED]] 事件的具体类型 + */ +NodeCls.TransformBit = TransformBit; const nodeProto: any = jsb.Node.prototype; export const TRANSFORM_ON = 1 << 0; @@ -50,23 +80,10 @@ nodeProto._ctor = function (name?: string) { this._registerListeners(); }; -Object.defineProperties(jsb.Node.prototype, { - '_components': { - get: function () { - return this._components; - } - }, - '_eventProcessor': { - get: function () { - return this._eventProcessor; - } - } -}); - nodeProto.getComponent = function (typeOrClassName) { const constructor = getConstructor(typeOrClassName); if (constructor) { - return jsb.Node._findComponent(this, constructor); + return NodeCls._findComponent(this, constructor); } return null; }; @@ -75,7 +92,7 @@ nodeProto.getComponents = function (typeOrClassName) { const constructor = getConstructor(typeOrClassName); const components = []; if (constructor) { - jsb.Node._findComponents(this, constructor, components); + NodeCls._findComponents(this, constructor, components); } return components; }; @@ -83,7 +100,7 @@ nodeProto.getComponents = function (typeOrClassName) { nodeProto.getComponentInChildren = function (typeOrClassName) { const constructor = getConstructor(typeOrClassName); if (constructor) { - return jsb.Node._findChildComponent(this._children, constructor); + return NodeCls._findChildComponent(this._children, constructor); } return null; }; @@ -92,8 +109,8 @@ nodeProto.getComponentsInChildren = function (typeOrClassName) { const constructor = getConstructor(typeOrClassName); const components = []; if (constructor) { - jsb.Node._findComponents(this, constructor, components); - jsb.Node._findChildComponents(this.getChildren(), constructor, components); + NodeCls._findComponents(this, constructor, components); + NodeCls._findChildComponents(this.getChildren(), constructor, components); } return components; }; @@ -228,7 +245,7 @@ nodeProto.hasEventListener = function (type: string, callback?, target?: unknown nodeProto.targetOff = function (target: string | unknown) { // Check for event mask reset - let eventMask = this.getEventMask(); + const eventMask = this.getEventMask(); if ((eventMask & TRANSFORM_ON) && !this._eventProcessor.hasEventListener(NodeEventType.TRANSFORM_CHANGED)) { // this._eventMask &= ~TRANSFORM_ON; this.setEventMask(eventMask & ~TRANSFORM_ON); @@ -257,14 +274,14 @@ nodeProto._removeComponent = function (component: Component) { // These functions are invoked by native Node object. nodeProto._onTransformChanged = function (transformType) { - this._eventProcessor.dispatchEvent(NodeEventType.TRANSFORM_CHANGED, transformType); + this.emit(NodeEventType.TRANSFORM_CHANGED, transformType); }; nodeProto._onParentChanged = function (oldParent) { - this._eventProcessor(NodeEventType.PARENT_CHANGED, oldParent); + this.emit(NodeEventType.PARENT_CHANGED, oldParent); }; -nodeProto._onReattach = function () { +nodeProto._onReAttach = function () { this._eventProcessor.reattach(); }; @@ -311,19 +328,19 @@ nodeProto._onActivateNode = function (shouldActiveNow) { // Static functions. -jsb.Node._findComponent = function (node, constructor) { +NodeCls._findComponent = function (node, constructor) { const cls = constructor as any; const comps = node._components; if (cls._sealed) { for (let i = 0; i < comps.length; ++i) { - const comp = comps[i]; + const comp: Component = comps[i]; if (comp.constructor === constructor) { return comp; } } } else { for (let i = 0; i < comps.length; ++i) { - const comp = comps[i]; + const comp: Component = comps[i]; if (comp instanceof constructor) { return comp; } @@ -332,7 +349,7 @@ jsb.Node._findComponent = function (node, constructor) { return null; }; -jsb.Node._findComponents = function (node, constructor, components) { +NodeCls._findComponents = function (node, constructor, components) { const cls = constructor as any; const comps = node._components; if (cls._sealed) { @@ -352,17 +369,17 @@ jsb.Node._findComponents = function (node, constructor, components) { } }; -jsb.Node._findChildComponent = function (children, constructor) { +NodeCls._findChildComponent = function (children, constructor) { for (let i = 0; i < children.length; ++i) { const node = children[i]; - let comp = jsb.Node._findComponent(node, constructor); + let comp: Component = NodeCls._findComponent(node, constructor); if (comp) { return comp; } - let childChildren = node.getChildren(); + const childChildren = node.getChildren(); if (childChildren.length > 0) { - comp = jsb.Node._findChildComponent(childChildren, constructor); + comp = NodeCls._findChildComponent(childChildren, constructor); if (comp) { return comp; } @@ -371,16 +388,87 @@ jsb.Node._findChildComponent = function (children, constructor) { return null; }; -jsb.Node._findChildComponents = function (children, constructor, components) { +NodeCls._findChildComponents = function (children, constructor, components) { for (let i = 0; i < children.length; ++i) { const node = children[i]; - jsb.Node._findComponents(node, constructor, components); + NodeCls._findComponents(node, constructor, components); - let childChildren = node.getChildren(); + const childChildren = node.getChildren(); if (childChildren.length > 0) { - jsb.Node._findChildComponents(childChildren, constructor, components); + NodeCls._findChildComponents(childChildren, constructor, components); } } }; -export const Node = jsb.Node; +/** + * @en Determine whether the given object is a normal Node. Will return false if [[Scene]] given. + * @zh 指定对象是否是普通的节点?如果传入 [[Scene]] 会返回 false。 + */ +NodeCls.isNode = function (obj: unknown): obj is jsb.Node { + return obj instanceof jsb.Node && (obj.constructor === jsb.Node || !(obj instanceof legacyCC.Scene)); +}; + +const oldGetPosition = nodeProto.getPosition; +const oldGetRotation = nodeProto.getRotation; +const oldGetScale = nodeProto.getScale; + +nodeProto.getPosition = function (out?: Vec3) : Vec3 { + const r = oldGetPosition.call(this); + if (out) { + return Vec3.set(out, r.x, r.y, r.z); + } + return Vec3.copy(new Vec3(), r); +}; + +nodeProto.getRotation = function(out?: Quat): Quat { + const r = oldGetRotation.call(this); + if (out) { + return Quat.set(out, r.x, r.y, r.z, r.w); + } + return Quat.copy(new Quat(), r); +}; + +nodeProto.getScale = function (out?: Vec3) : Vec3 { + const r = oldGetScale.call(this); + if (out) { + return Vec3.set(out, r.x, r.y, r.z); + } + return Vec3.copy(new Vec3(), r); +}; + +Object.defineProperty(nodeProto, 'position', { + configurable: true, + enumerable: true, + get () : Vec3 { + return this.getPosition(); + }, + set (v: Vec3) { + this.setPosition(v); + }, +}); + +Object.defineProperty(nodeProto, 'rotation', { + configurable: true, + enumerable: true, + get () : Quat { + return this.getRotation(); + }, + set (v: Quat) { + this.setRotation(v); + }, +}); + +Object.defineProperty(nodeProto, 'scale', { + configurable: true, + enumerable: true, + get () : Vec3 { + return this.getScale(); + }, + set (v: Vec3) { + this.setScale(v); + }, +}); + +export type Node = jsb.Node; + +legacyCC.Node = Node; diff --git a/cocos/core/utils/prefab/prefab-info.ts b/cocos/core/utils/prefab/prefab-info.ts index 0749b24da48..2c8b142f509 100644 --- a/cocos/core/utils/prefab/prefab-info.ts +++ b/cocos/core/utils/prefab/prefab-info.ts @@ -2,9 +2,9 @@ import { ccclass, serializable, editable, type } from 'cc.decorator'; import { EDITOR } from 'internal:constants'; import { legacyCC } from '../../global-exports'; import { Prefab } from '../../assets'; -import { CCObject } from '../../data/object'; -import { Component } from '../../components/component'; -import { Node } from '../../scene-graph/node'; +import { CCObject } from '../../data'; +import { Component } from '../../components'; +import { Node } from '../../scene-graph'; function compareStringArray (array1: string[]|undefined, array2: string[]|undefined) { if (!array1 || !array2) {