From 182a29a869dcb73dfda41d0391e0ab7842a52fee Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Wed, 12 Jun 2019 17:06:13 +0800 Subject: [PATCH] skinning mesh bounds (#4591) * skinning mesh bounds * scene-utils docs * fix batching --- cocos/3d/assets/mesh.ts | 64 +- cocos/3d/assets/primitive.ts | 30 + cocos/3d/builtin/init.ts | 7 +- .../batched-skinning-model-component.ts | 19 +- .../3d/framework/skinning-model-component.ts | 19 +- cocos/3d/misc/utils.ts | 6 +- cocos/core/vmath/mat3.ts | 83 ++- cocos/core/vmath/quat.ts | 2 +- cocos/pipeline/render-pipeline.ts | 6 +- cocos/renderer/models/skinning-model.ts | 1 + cocos/renderer/scene/model.ts | 41 +- cocos/renderer/scene/render-scene.ts | 10 +- cocos/scene-graph/layers.ts | 48 +- cocos/scene-graph/node.ts | 561 +++++++++--------- cocos/scene-graph/scene-globals.ts | 47 ++ cocos/scene-graph/scene.ts | 19 +- 16 files changed, 528 insertions(+), 435 deletions(-) create mode 100644 cocos/3d/assets/primitive.ts diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index f80d120fe8..b4fc6b3944 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -33,6 +33,7 @@ import { IGFXAttribute } from '../../gfx/input-assembler'; import { BufferBlob } from '../misc/buffer-blob'; import { IBufferView } from './utils/buffer-view'; import { postLoadMesh } from './utils/mesh-utils'; +import { vec3 } from '../../core/vmath'; function getIndexStrideCtor (stride: number) { switch (stride) { @@ -373,8 +374,7 @@ export class Mesh extends Asset { if (prim.geometricInfo) { const info = prim.geometricInfo; - let positions: Float32Array | null = null; - positions = new Float32Array(buffer, info.view.offset, info.view.length / 4); + const positions = new Float32Array(buffer, info.view.offset, info.view.length / 4); subMesh.geometricInfo = { indices: ib, positions, @@ -498,33 +498,33 @@ export class Mesh extends Asset { vbView.set(srcVBView); - if (bundle.view.stride === dstBundle.view.stride) { - vbView.set(dstVBView, bundle.view.length); - } else { - srcAttrOffset = 0; - for (const attr of bundle.attributes) { - dstVBOffset = 0; - hasAttr = false; - for (const dstAttr of dstBundle.attributes) { - if (attr.name === dstAttr.name && attr.format === dstAttr.format) { - hasAttr = true; - break; - } - dstVBOffset += GFXFormatInfos[dstAttr.format].size; + // if (bundle.view.stride === dstBundle.view.stride) { + // vbView.set(dstVBView, bundle.view.length); + // } else { + srcAttrOffset = 0; + for (const attr of bundle.attributes) { + dstVBOffset = 0; + hasAttr = false; + for (const dstAttr of dstBundle.attributes) { + if (attr.name === dstAttr.name && attr.format === dstAttr.format) { + hasAttr = true; + break; } - if (hasAttr) { - attrSize = GFXFormatInfos[attr.format].size; - srcVBOffset = bundle.view.length + srcAttrOffset; - for (let v = 0; v < dstBundle.view.count; ++v) { - dstAttrView = dstVBView.subarray(dstVBOffset, dstVBOffset + attrSize); - vbView.set(dstAttrView, srcVBOffset); - srcVBOffset += bundle.view.stride; - dstVBOffset += dstBundle.view.stride; - } - } - srcAttrOffset += GFXFormatInfos[attr.format].size; + dstVBOffset += GFXFormatInfos[dstAttr.format].size; } + if (hasAttr) { + attrSize = GFXFormatInfos[attr.format].size; + srcVBOffset = bundle.view.length + srcAttrOffset; + for (let v = 0; v < dstBundle.view.count; ++v) { + dstAttrView = dstVBView.subarray(dstVBOffset, dstVBOffset + attrSize); + vbView.set(dstAttrView, srcVBOffset); + srcVBOffset += bundle.view.stride; + dstVBOffset += dstBundle.view.stride; + } + } + srcAttrOffset += GFXFormatInfos[attr.format].size; } + // } vertexBundles[i] = { attributes: bundle.attributes, @@ -642,7 +642,7 @@ export class Mesh extends Asset { view: { offset: bufferBlob.getLength(), length: geomBuffView.length, - count: prim.geometricInfo.view.count, + count: prim.geometricInfo.view.count + dstPrim.geometricInfo.view.count, stride: prim.geometricInfo.view.stride, }, }; @@ -659,19 +659,15 @@ export class Mesh extends Asset { }; if (meshStruct.minPosition && mesh._struct.minPosition) { - meshStruct.minPosition.x = Math.min(meshStruct.minPosition.x, mesh._struct.minPosition.x); - meshStruct.minPosition.y = Math.min(meshStruct.minPosition.y, mesh._struct.minPosition.y); - meshStruct.minPosition.z = Math.min(meshStruct.minPosition.z, mesh._struct.minPosition.z); + vec3.min(meshStruct.minPosition, meshStruct.minPosition, mesh._struct.minPosition); } if (meshStruct.maxPosition && mesh._struct.maxPosition) { - meshStruct.maxPosition.x = Math.max(meshStruct.maxPosition.x, mesh._struct.maxPosition.x); - meshStruct.maxPosition.y = Math.max(meshStruct.maxPosition.y, mesh._struct.maxPosition.y); - meshStruct.maxPosition.z = Math.max(meshStruct.maxPosition.z, mesh._struct.maxPosition.z); + vec3.max(meshStruct.maxPosition, meshStruct.maxPosition, mesh._struct.maxPosition); } // Create mesh. this.assign(meshStruct, new Uint8Array(bufferBlob.getCombined())); - this.initialize (); + this.initialize(); return true; } diff --git a/cocos/3d/assets/primitive.ts b/cocos/3d/assets/primitive.ts new file mode 100644 index 0000000000..79392c30d3 --- /dev/null +++ b/cocos/3d/assets/primitive.ts @@ -0,0 +1,30 @@ +import { ccclass, property } from '../../core/data/class-decorator'; +import { Enum } from '../../core/value-types'; +import { createMesh } from '../misc/utils'; +import * as primitives from '../primitive'; +import { Mesh } from './mesh'; + +const PrimitiveType = Enum({ + BOX: 0, + SPHERE: 1, + CYLINDER: 2, + CONE: 3, + CAPSULE: 4, + TORUS: 5, + PLANE: 6, + QUAD: 7, +}); + +@ccclass('cc.Primitive') +export class Primitive extends Mesh { + @property(PrimitiveType) + public type: number = PrimitiveType.BOX; + @property + public info: Record = {}; + + public onLoaded () { + createMesh(primitives[PrimitiveType[this.type]](this.info), this); + } +} + +cc.Primitive = Primitive; diff --git a/cocos/3d/builtin/init.ts b/cocos/3d/builtin/init.ts index 1fd0100401..419dfb0cf5 100644 --- a/cocos/3d/builtin/init.ts +++ b/cocos/3d/builtin/init.ts @@ -219,10 +219,9 @@ class BuiltinResMgr { const shadow = m.scene.planarShadow; m.updateTransform = () => { const bounds = m.worldBounds; - if (!m.node.hasChanged && !light.node.hasChanged) { return; } - m.node.updateWorldTransformFull(); - if (!bounds) { return; } - m.modelBounds!.transform(m.node._mat, null, null, null, bounds); + if (!m.transform.hasChanged && !light.node.hasChanged) { return; } + if (!bounds) { m.transform.updateWorldTransformFull(); return; } + m.modelBounds!.transform(m.transform.worldMatrix, null, null, null, bounds); bounds.transform(shadow.matLight, null, null, null, tmp); aabb.merge(bounds, bounds, tmp); }; diff --git a/cocos/3d/framework/batched-skinning-model-component.ts b/cocos/3d/framework/batched-skinning-model-component.ts index 94a4fb9015..8e90b39860 100644 --- a/cocos/3d/framework/batched-skinning-model-component.ts +++ b/cocos/3d/framework/batched-skinning-model-component.ts @@ -124,6 +124,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { return this._mesh; } set mesh (val) { + // @ts-ignore super.mesh = val; } @@ -132,6 +133,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { return this._skeleton; } set skeleton (val) { + // @ts-ignore super.skeleton = val; } @@ -140,6 +142,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { return this._skinningRoot; } set skinningRoot (val) { + // @ts-ignore super.skinningRoot = val; } @@ -232,6 +235,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { skeleton.joints = skeleton.joints.map((_, idx, arr) => arr[idxMap[idx]]); skeleton.bindposes = skeleton.bindposes.map((_, idx, arr) => arr[idxMap[idx]]); // apply + // @ts-ignore super.skeleton = skeleton; } @@ -278,13 +282,9 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { if (!unit || !unit.mesh || !unit.mesh.data) { continue; } // add batch ID to this temp mesh - const newMeshStruct: IMeshStruct = JSON.parse(JSON.stringify(unit.mesh.struct)); - const extraLength = unit.mesh.struct.vertexBundles.reduce((acc, cur) => acc + cur.view.count * batch_extras_size, 0); - const newMeshData = new Uint8Array(unit.mesh.data.byteLength + extraLength); - dataView = new DataView(newMeshData.buffer); - // first, update bookkeepping - let newOffset = 0; let oldOffset = 0; + const newMeshStruct: IMeshStruct = JSON.parse(JSON.stringify(unit.mesh.struct)); + let newOffset = 0; for (const vb of newMeshStruct.vertexBundles) { vb.attributes.push(batch_id); vb.attributes.push(batch_uv); @@ -299,14 +299,17 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { newOffset += pm.indexView.length; } if (pm.geometricInfo) { + newOffset = Math.ceil(newOffset / 4) * 4; pm.geometricInfo.view.offset = newOffset; newOffset += pm.geometricInfo.view.length; } } // now, we ride! - const src = unit.mesh.data; + const src = unit.mesh.data; let oldOffset = 0; + const newMeshData = new Uint8Array(newOffset); + dataView = new DataView(newMeshData.buffer); for (let k = 0; k < newMeshStruct.vertexBundles.length; k++) { - const uvs = unit.mesh.readAttribute(k, GFXAttributeName.ATTR_TEX_COORD)!; // FIXME: should be kth bundle instead of primitive + const uvs = unit.mesh.readAttribute(k, GFXAttributeName.ATTR_TEX_COORD)!; // FIXME: should be k-th bundle instead of primitive const oldView = unit.mesh.struct.vertexBundles[k].view; const newView = newMeshStruct.vertexBundles[k].view; const oldStride = oldView.stride; diff --git a/cocos/3d/framework/skinning-model-component.ts b/cocos/3d/framework/skinning-model-component.ts index 9673c15d1e..cd2778d259 100644 --- a/cocos/3d/framework/skinning-model-component.ts +++ b/cocos/3d/framework/skinning-model-component.ts @@ -53,7 +53,6 @@ class Joint { protected _lastUpdate = -1; constructor (node: Node) { this.node = node; } public update () { - // if (!this.node.hasChanged) { return; } const totalFrames = cc.director.totalFrames; if (this._lastUpdate >= totalFrames) { return; } this._lastUpdate = totalFrames; @@ -61,11 +60,11 @@ class Joint { const parent = this.parent; if (parent) { parent.update(); - vec3.multiply(this.position, this.node.localPosition, parent.scale); + vec3.multiply(this.position, this.node.position, parent.scale); vec3.transformQuat(this.position, this.position, parent.rotation); vec3.add(this.position, this.position, parent.position); - quat.multiply(this.rotation, parent.rotation, this.node.localRotation); - vec3.multiply(this.scale, parent.scale, this.node.localScale); + quat.multiply(this.rotation, parent.rotation, this.node.rotation); + vec3.multiply(this.scale, parent.scale, this.node.scale); } } } @@ -169,11 +168,11 @@ export class SkinningModelComponent extends ModelComponent { cur.update(); const bindpose = skeleton.bindposes[i]; - vec3.multiply(v3_1, bindpose.localPosition, cur.scale); + vec3.multiply(v3_1, bindpose.position, cur.scale); vec3.transformQuat(v3_1, v3_1, cur.rotation); vec3.add(v3_1, v3_1, cur.position); - quat.multiply(qt_1, cur.rotation, bindpose.localRotation); - vec3.multiply(v3_2, cur.scale, bindpose.localScale); + quat.multiply(qt_1, cur.rotation, bindpose.rotation); + vec3.multiply(v3_2, cur.scale, bindpose.scale); skinningModel.updateJointData(i, v3_1, qt_1, v3_2, i === 0); } @@ -210,12 +209,6 @@ export class SkinningModelComponent extends ModelComponent { super._updateModelParams(); } - protected _createModel () { - if (!this.node.scene) { return; } - const scene = this._getRenderScene(); - this._model = scene.createModel(this._getModelConstructor(), this.node.parent!); - } - protected _getModelConstructor () { return SkinningModel; } diff --git a/cocos/3d/misc/utils.ts b/cocos/3d/misc/utils.ts index c25f9930ca..040965e3ec 100644 --- a/cocos/3d/misc/utils.ts +++ b/cocos/3d/misc/utils.ts @@ -379,9 +379,9 @@ export function calculateBoneSpaceBounds (mesh: Mesh, skeleton: Skeleton) { const bindpose = skeleton.bindposes[refJointIndex]; const jointBounds = result[refJointIndex]; - vec3.multiply(transformedPos, pos, bindpose.localScale); - vec3.transformQuat(transformedPos, transformedPos, bindpose.localRotation); - vec3.add(transformedPos, transformedPos, bindpose.localPosition); + vec3.multiply(transformedPos, pos, bindpose.scale); + vec3.transformQuat(transformedPos, transformedPos, bindpose.rotation); + vec3.add(transformedPos, transformedPos, bindpose.position); jointBounds.hasValue = true; vec3.min(jointBounds.min, jointBounds.min, transformedPos); vec3.max(jointBounds.max, jointBounds.max, transformedPos); diff --git a/cocos/core/vmath/mat3.ts b/cocos/core/vmath/mat3.ts index d5f63aa7e8..812ccc1c1a 100644 --- a/cocos/core/vmath/mat3.ts +++ b/cocos/core/vmath/mat3.ts @@ -32,6 +32,46 @@ import vec3 from './vec3'; // tslint:disable-next-line:class-name class mat3 { + /** + * Calculates a 3x3 matrix from view direction and up direction. + * + * @param out - Matrix to store result. + * @param view - View direction (must be normalized). + * @param [up] - Up direction, default is (0,1,0) (must be normalized). + * + * @return out + */ + public static fromViewUp = (() => { + const default_up = vec3.create(0, 1, 0); + const x = vec3.create(0, 0, 0); + const y = vec3.create(0, 0, 0); + + return (out: mat3, view: vec3, up?: vec3) => { + if (vec3.sqrMag(view) < EPSILON * EPSILON) { + mat3.identity(out); + return out; + } + + up = up || default_up; + vec3.normalize(x, vec3.cross(x, up, view)); + + if (vec3.sqrMag(x) < EPSILON * EPSILON) { + mat3.identity(out); + return out; + } + + vec3.cross(y, view, x); + mat3.set( + out, + x.x, x.y, x.z, + y.x, y.y, y.z, + view.x, view.y, view.z, + ); + + return out; + }; + })(); + /** * Creates a matrix, with elements specified separately. * @@ -516,49 +556,6 @@ class mat3 { return out; } - /** - * Calculates a 3x3 matrix from view direction and up direction. - * - * @param out - Matrix to store result. - * @param view - View direction (must be normalized). - * @param [up] - Up direction, default is (0,1,0) (must be normalized). - * - * @return out - */ - public static fromViewUp (_out, _view, _up) { - const _fromViewUpIIFE = (() => { - const default_up = vec3.create(0, 1, 0); - const x = vec3.create(0, 0, 0); - const y = vec3.create(0, 0, 0); - - return (out, view, up) => { - if (vec3.sqrMag(view) < EPSILON * EPSILON) { - mat3.identity(out); - return out; - } - - up = up || default_up; - vec3.normalize(x, vec3.cross(x, up, view)); - - if (vec3.sqrMag(x) < EPSILON * EPSILON) { - mat3.identity(out); - return out; - } - - vec3.cross(y, view, x); - mat3.set( - out, - x.x, x.y, x.z, - y.x, y.y, y.z, - view.x, view.y, view.z, - ); - - return out; - }; - })(); - return _fromViewUpIIFE(_out, _view, _up); - } - /** * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix. * diff --git a/cocos/core/vmath/quat.ts b/cocos/core/vmath/quat.ts index 220d897da9..ba1414b098 100644 --- a/cocos/core/vmath/quat.ts +++ b/cocos/core/vmath/quat.ts @@ -546,7 +546,7 @@ export default class quat { * * @return out. */ - public static fromViewUp (out: Out, view: vec3, up: vec3) { + public static fromViewUp (out: Out, view: vec3, up?: vec3) { mat3.fromViewUp(tmpMat3, view, up); return quat.normalize(out, quat.fromMat3(out, tmpMat3)); } diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 41efa27713..654fc80792 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -989,7 +989,6 @@ export abstract class RenderPipeline { const planarShadow = scene.planarShadow; if (planarShadow.enabled) { - planarShadow.updateDirLight(scene.mainLight); this._globalBindings.get(UBOShadow.BLOCK.name)!.buffer!.update(planarShadow.data); } } @@ -1006,6 +1005,11 @@ export abstract class RenderPipeline { mainLight.update(); } + const planarShadow = scene.planarShadow; + if (planarShadow.enabled) { + planarShadow.updateDirLight(scene.mainLight); + } + if (scene.skybox.enabled) { this.addVisibleModel(scene.skybox, camera); } diff --git a/cocos/renderer/models/skinning-model.ts b/cocos/renderer/models/skinning-model.ts index 85b1591876..3cffb283e6 100644 --- a/cocos/renderer/models/skinning-model.ts +++ b/cocos/renderer/models/skinning-model.ts @@ -60,6 +60,7 @@ export class SkinningModel extends Model { constructor (scene: RenderScene, node: Node) { super(scene, node); this._type = 'skinning'; + if (node.parent) { this._transform = node.parent; } } public bindSkeleton (skeleton: Skeleton | null) { diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index 35f518ffbe..0a61e8d98d 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -17,8 +17,8 @@ import { customizationManager } from './customization-manager'; import { RenderScene } from './render-scene'; import { SubModel } from './submodel'; -const _temp_floatx16 = new Float32Array(16); -const _temp_mat4 = mat4.create(); +const f32_1 = new Float32Array(16); +const m4_1 = mat4.create(); const _subMeshPool = new Pool(() => { return new SubModel(); @@ -82,6 +82,14 @@ export class Model { this._node = node; } + get transform () { + return this._transform; + } + + set transform (transform: Node) { + this._transform = transform; + } + get worldBounds () { return this._worldBounds; } @@ -121,6 +129,7 @@ export class Model { protected _device: GFXDevice; protected _scene: RenderScene; protected _node: Node; + protected _transform: Node; protected _id: number; protected _enabled: boolean = false; protected _viewID: number = 1; @@ -143,8 +152,8 @@ export class Model { constructor (scene: RenderScene, node: Node) { this._device = cc.director.root.device; this._scene = scene; - this._node = node; this._id = this._scene.generateModelId(); + this._transform = this._node = node; this._matPSORecord = new Map(); this._matRefCount = new Map(); @@ -181,11 +190,12 @@ export class Model { } public updateTransform () { - if (!this._node.hasChanged) { return; } - this._node.updateWorldTransformFull(); + const node = this._transform; + if (!node.hasChanged) { return; } + node.updateWorldTransformFull(); if (!this._modelBounds) { return; } - this._modelBounds.transform(this._node._mat, this._node._pos, - this._node._rot, this._node._scale, this._worldBounds!); + // @ts-ignore + this._modelBounds.transform(node._mat, node._pos, node._rot, node._scale, this._worldBounds!); } public _resetUBOUpdateFlag () { @@ -197,11 +207,13 @@ export class Model { return; } this._uboUpdated = true; - mat4.array(_temp_floatx16, this._node._mat); - this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET); - mat4.normalMatrix(_temp_mat4, this._node._mat); - mat4.array(_temp_floatx16, _temp_mat4); - this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET); + // @ts-ignore + const worldMatrix = this._transform._mat; + mat4.array(f32_1, worldMatrix); + this._uboLocal.view.set(f32_1, UBOLocal.MAT_WORLD_OFFSET); + mat4.normalMatrix(m4_1, worldMatrix); + mat4.array(f32_1, m4_1); + this._uboLocal.view.set(f32_1, UBOLocal.MAT_WORLD_IT_OFFSET); const commonLocal = this._localBindings.get(UBOLocal.BLOCK.name); if (commonLocal && commonLocal.buffer) { @@ -227,8 +239,9 @@ export class Model { if (!minPos || !maxPos) { return; } this._modelBounds = aabb.fromPoints(aabb.create(), minPos, maxPos); this._worldBounds = aabb.clone(this._modelBounds); - this._modelBounds.transform(this._node._mat, this._node._pos, - this._node._rot, this._node._scale, this._worldBounds); + this._transform.updateWorldTransformFull(); + // @ts-ignore + this._modelBounds.transform(this._transform._mat, this._transform._pos, this._transform._rot, this._transform._scale, this._worldBounds); } public initSubModel (idx: number, subMeshData: IRenderingSubmesh, mat: Material) { diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 1de3d1b22d..253e604c59 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -221,10 +221,10 @@ export class RenderScene { public raycast (worldRay: ray, mask: number = Layers.RaycastMask): IRaycastResult[] { pool.reset(); for (const m of this._models) { - const node = m.node; - if (!node || !m.enabled || !cc.Layers.check(node.layer, mask) || !m.modelBounds) { continue; } + const transform = m.transform; + if (!transform || !m.enabled || !cc.Layers.check(m.node.layer, mask) || !m.modelBounds) { continue; } // transform ray back to model space - mat4.invert(m4, node.getWorldMatrix(m4)); + mat4.invert(m4, transform.getWorldMatrix(m4)); vec3.transformMat4(modelRay.o, worldRay.o, m4); vec3.normalize(modelRay.d, vec3.transformMat4Normal(modelRay.d, worldRay.d, m4)); // broadphase @@ -238,8 +238,8 @@ export class RenderScene { } if (distance < Infinity) { const r = pool.add(); - r.node = node; - r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale)); + r.node = m.node; + r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, transform.worldScale)); results[pool.length - 1] = r; } } diff --git a/cocos/scene-graph/layers.ts b/cocos/scene-graph/layers.ts index cc260d40c8..dde18879c4 100644 --- a/cocos/scene-graph/layers.ts +++ b/cocos/scene-graph/layers.ts @@ -1,23 +1,42 @@ /** - * Entity layer system + * 场景节点层管理器,用于射线检测、物理碰撞和用户自定义脚本逻辑。 + * 每个节点可属于一个或多个层,可通过 “包含式” 或 “排除式” 两种检测器进行层检测。 */ export class Layers { // built-in layers, reserved up to (1 << 7) + + /** + * @zh 默认层,所有节点的初始值 + */ public static Default = (1 << 0); + /** + * @zh 忽略射线检测 + */ public static IgnoreRaycast = (1 << 1); public static Gizmos = (1 << 2); public static Editor = (1 << 3); public static UI = (1 << 4); + // masks + + /** + * @zh 接受所有用户创建的节点 + */ public static All = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor]); + /** + * @zh 接受所有支持射线检测的节点 + */ public static RaycastMask = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor, Layers.IgnoreRaycast]); /** + * @en * Add a new layer - * @param {string} name name of the new layer - * @return {number} new layer's index + * @zh + * 添加一个新层 + * @param name 层名字 + * @return 新层的检测值 */ public static addLayer (name: string): number | undefined { if (Layers._nextAvailable > 31) { @@ -29,9 +48,12 @@ export class Layers { } /** + * @en * Make a layer mask accepting nothing but the listed layers - * @param {number[]} includes layers accepted by the mask - * @return {number} the specified layer mask + * @zh + * 创建一个包含式层检测器,只接受列表中的层 + * @param includes 可接受的层数组 + * @return 指定功能的层检测器 */ public static makeInclusiveMask (includes: number[]): number { let mask = 0; @@ -42,19 +64,25 @@ export class Layers { } /** + * @en * Make a layer mask accepting everything but the listed layers - * @param {number[]} excludes layers rejected by the mask - * @return {number} the specified layer mask + * @zh + * 创建一个排除式层检测器,只拒绝列表中的层 + * @param excludes 将拒绝的层数组 + * @return 指定功能的层检测器 */ public static makeExclusiveMask (excludes: number[]): number { return ~Layers.makeInclusiveMask(excludes); } /** + * @en * Check a layer is accepted by the mask or not - * @param {number} layer the layer number to be tested - * @param {number} mask the testing layer mask - * @return {boolean} true if accepted + * @zh + * 检查一个层是否被检测器接受 + * @param layer 待检测的层 + * @param mask 层检测器 + * @return 是否通过检测 */ public static check (layer: number, mask: number): boolean { return (layer & mask) === layer; diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 7b7df762de..5f777dae50 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -20,29 +20,48 @@ enum NodeSpace { } const TRANFORM_ON = 1 << 0; +/** + * @zh + * 场景树中的基本节点,基本特性有: + * * 具有层级关系 + * * 持有各类组件 + * * 维护空间变换(位移、旋转、缩放)信息 + */ @ccclass('cc.Node') -class Node extends BaseNode { +export class Node extends BaseNode { + /** + * @zh + * 节点可能发出的事件类型 + */ public static EventType = SystemEventType; + /** + * @zh + * 空间变换操作的坐标系 + */ public static NodeSpace = NodeSpace; - // is node but not scene + /** + * @zh + * 指定对象是否是普通的场景节点? + * @param obj 待测试的节点 + */ public static isNode (obj: object | null): obj is Node { return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cc.Scene)); } // world transform, don't access this directly - public _pos = new Vec3(); - public _rot = new Quat(); - public _scale = new Vec3(1, 1, 1); - public _mat = new Mat4(); + protected _pos = new Vec3(); + protected _rot = new Quat(); + protected _scale = new Vec3(1, 1, 1); + protected _mat = new Mat4(); // local transform @property - public _lpos = new Vec3(); + protected _lpos = new Vec3(); @property - public _lrot = new Quat(); + protected _lrot = new Quat(); @property - public _lscale = new Vec3(1, 1, 1); + protected _lscale = new Vec3(1, 1, 1); @property protected _layer = Layers.Default; // the layer this node belongs to @@ -60,6 +79,10 @@ class Node extends BaseNode { protected _eventMask = 0; private _uiTransfromComp: UITransformComponent | null = null; + /** + * @zh + * 以欧拉角表示的本地旋转值 + */ @property({ type: Vec3 }) set eulerAngles (val: Readonly) { this.setRotationFromEuler(val.x, val.y, val.z); @@ -72,6 +95,10 @@ class Node extends BaseNode { return this._euler; } + /** + * @zh + * 节点所属层,主要影响射线检测、物理碰撞等,参考 [[Layers]] + */ @property set layer (l) { this._layer = l; @@ -80,65 +107,23 @@ class Node extends BaseNode { return this._layer; } + /** + * @zh + * 这个节点的空间变换信息在当前帧内是否有变过? + */ get hasChanged () { return this._hasChanged; } - get uiTransfromComp () { - if (!this._uiTransfromComp){ - this._uiTransfromComp = this.getComponent('cc.UITransformComponent') as UITransformComponent; - } - - return this._uiTransfromComp; - } - - // NOTE: don't set it manually - set uiTransfromComp (value: UITransformComponent | null) { - this._uiTransfromComp = value; - } - - get width () { - return this.uiTransfromComp!.width; - } - - set width (value: number) { - this.uiTransfromComp!.width = value; - } - - get height () { - return this.uiTransfromComp!.height; - } - - set height (value: number) { - this.uiTransfromComp!.height = value; - } - - get anchorX () { - return this.uiTransfromComp!.anchorX; - } - - set anchorX (value) { - this.uiTransfromComp!.anchorX = value; - } - - get anchorY () { - return this.uiTransfromComp!.anchorY; - } - - set anchorY (value: number) { - this.uiTransfromComp!.anchorY = value; - } - - get eventProcessor () { - return this._eventProcessor; - } - // =============================== // hierarchy // =============================== /** - * hierarchical events + * @zh + * 设置父节点 + * @param value 父节点 + * @prama keepWorldTransform 是否保留当前世界变换 */ public setParent (value: this | null, keepWorldTransform: boolean = false) { if (keepWorldTransform) { this.updateWorldTransform(); } @@ -197,31 +182,27 @@ class Node extends BaseNode { // =============================== /** - * Translate the node - * @param trans - translation - * @param ns - the operating space + * @zh + * 移动节点 + * @param trans 位置增量 + * @param ns 操作空间 */ public translate (trans: Vec3, ns?: NodeSpace) { const space = ns || NodeSpace.LOCAL; - v3_a.set(this._lpos); + vec3.copy(v3_a, this._lpos); if (space === NodeSpace.LOCAL) { - vec3.transformQuat(v3_a, trans, this._lrot); + vec3.transformQuat(v3_a, trans, this.worldRotation); this.setPosition(vec3.add(v3_a, this._lpos, v3_a)); } else if (space === NodeSpace.WORLD) { this.setPosition(vec3.add(v3_a, this._lpos, trans)); } - - vec3.copy(this._pos, this._lpos); - this.invalidateChildren(); - if (this._eventMask & TRANFORM_ON) { - this.emit(SystemEventType.TRANSFORM_CHANGED, SystemEventType.POSITION_PART); - } } /** - * Rotate the node - * @param rot - rotation to apply - * @param ns - the operating space + * @zh + * 旋转节点 + * @param trans 旋转增量 + * @param ns 操作空间 */ public rotate (rot: Quat, ns?: NodeSpace) { const space = ns || NodeSpace.LOCAL; @@ -235,80 +216,31 @@ class Node extends BaseNode { } /** - * rotate the node around X axis - * @param rad - rotating angle - * @param ns - the operating space + * @zh + * 当前节点面向的前方方向 */ - public pitch (rad: number, ns?: NodeSpace) { - const space = ns || NodeSpace.LOCAL; - if (space === NodeSpace.LOCAL) { - this.getWorldRotation(q_a); - quat.toAxisX(v3_a, q_a); - quat.fromAxisAngle(q_a, v3_a, rad); - this.rotate(q_a, ns); - } else if (space === NodeSpace.WORLD) { - quat.fromAxisAngle(q_a, vec3.UNIT_X, rad); - this.rotate(q_a, ns); - } - } - - /** - * rotate the node around Y axis - * @param rad - rotating angle - * @param ns - the operating space - */ - public yaw (rad: number, ns?: NodeSpace) { - const space = ns || NodeSpace.LOCAL; - if (space === NodeSpace.LOCAL) { - this.getWorldRotation(q_a); - quat.toAxisY(v3_a, q_a); - quat.fromAxisAngle(q_a, v3_a, rad); - this.rotate(q_a, ns); - } else if (space === NodeSpace.WORLD) { - quat.fromAxisAngle(q_a, vec3.UNIT_Y, rad); - this.rotate(q_a, ns); - } - } - - /** - * rotate the node around Z axis - * @param rad - rotating angle - * @param ns - the operating space - */ - public roll (rad: number, ns?: NodeSpace) { - const space = (ns !== undefined ? ns : NodeSpace.LOCAL); - if (space === NodeSpace.LOCAL) { - this.getWorldRotation(q_a); - quat.toAxisZ(v3_a, q_a); - quat.fromAxisAngle(q_a, v3_a, rad); - this.rotate(q_a, ns); - } else if (space === NodeSpace.WORLD) { - quat.fromAxisAngle(q_a, vec3.UNIT_Z, rad); - this.rotate(q_a, ns); - } - } - - public get direction (): Vec3 { - this.getRotation(q_a); + get forward (): Vec3 { + this.getWorldRotation(q_a); return vec3.transformQuat(new Vec3(), vec3.UNIT_Z, q_a); } - - public set direction (dir: Vec3) { - quat.rotationTo(q_a, vec3.UNIT_Z, dir); - this.setRotation(q_a); + set forward (dir: Vec3) { + const len = vec3.magnitude(dir); + vec3.scale(v3_a, dir, -1 / len); // we use -z for view-dir + quat.fromViewUp(q_a, v3_a); + this.setWorldRotation(q_a); } /** - * Set rotation by lookAt target point - * @param pos - target position - * @param up - the up vector, default to (0,1,0) + * @zh + * 设置当前节点旋转为面向目标位置 + * @param pos 目标位置 + * @param up 坐标系的上方向 */ - public lookAt (pos: Vec3, up: Vec3) { + public lookAt (pos: Vec3, up?: Vec3) { this.getWorldPosition(v3_a); - vec3.sub(v3_a, v3_a, pos); // NOTE: we use -z for view-dir + vec3.sub(v3_a, v3_a, pos); // we use -z for view-dir vec3.normalize(v3_a, v3_a); quat.fromViewUp(q_a, v3_a, up); - this.setWorldRotation(q_a); } @@ -317,7 +249,10 @@ class Node extends BaseNode { // =============================== /** + * @en * Reset the `hasChanged` flag recursively + * @zh + * 递归重置节点的 hasChanged 标记为 false */ public resetHasChanged () { this._hasChanged = false; @@ -328,8 +263,11 @@ class Node extends BaseNode { } /** + * @en * invalidate the world transform information * for this node and all its children recursively + * @zh + * 递归标记节点世界变换为 dirty */ public invalidateChildren () { if (this._dirty && this._hasChanged) { return; } @@ -340,9 +278,12 @@ class Node extends BaseNode { } /** + * @en * update the world transform information if outdated * here we assume all nodes are children of a scene node, * which is always not dirty, has an identity transform and no parent. + * @zh + * 更新节点的世界变换信息 */ public updateWorldTransform () { if (!this._dirty) { return; } @@ -373,6 +314,10 @@ class Node extends BaseNode { } } + /** + * @zh + * 更新节点的完整世界变换信息 + */ public updateWorldTransformFull () { this.updateWorldTransform(); if (!this._matDirty) { return; } @@ -385,26 +330,22 @@ class Node extends BaseNode { // =============================== /** - * Sets local position. - * @param position - The new local position. + * @zh + * 设置本地位移 + * @param position 目标本地位移 */ public setPosition (position: Vec3): void; /** - * Sets local position. - * @param x - The x component of the new local position. - * @param y - The y component of the new local position. - * @param z - The z component of the new local position. - * @param w - The w component of the new local position. + * @zh + * 设置本地位移 + * @param x 目标本地位移的 X 分量 + * @param y 目标本地位移的 Y 分量 + * @param z 目标本地位移的 Z 分量 + * @param w 目标本地位移的 W 分量 */ public setPosition (x: number, y: number, z: number): void; - /** - * set local position - * @param val - the new local position, or the x component of it - * @param y - the y component of the new local position - * @param z - the z component of the new local position - */ public setPosition (val: Vec3 | number, y?: number, z?: number) { v3_a.set(this._lpos); if (y === undefined || z === undefined) { @@ -421,9 +362,9 @@ class Node extends BaseNode { } /** - * get local position - * @param out the receiving vector - * @returns the resulting vector + * @zh + * 获取本地位移 + * @param out 输出到此目标 vector */ public getPosition (out?: Vec3): Vec3 { if (out) { @@ -433,22 +374,31 @@ class Node extends BaseNode { } } - public get localPosition (): Readonly { + /** + * @zh + * 本地位移 + */ + public get position (): Readonly { return this._lpos; } + public set position (val: Readonly) { + this.setPosition(val); + } /** - * Sets local rotation. - * @param rotation - The new local rotation. + * @zh + * 设置本地旋转 + * @param rotation 目标本地旋转 */ public setRotation (rotation: Quat): void; /** - * Sets local rotation. - * @param x - The x component of the new local rotation. - * @param y - The y component of the new local rotation. - * @param z - The z component of the new local rotation. - * @param w - The w component of the new local rotation. + * @zh + * 设置本地旋转 + * @param x 目标本地旋转的 X 分量 + * @param y 目标本地旋转的 Y 分量 + * @param z 目标本地旋转的 Z 分量 + * @param w 目标本地旋转的 W 分量 */ public setRotation (x: number, y: number, z: number, w: number): void; @@ -468,10 +418,11 @@ class Node extends BaseNode { } /** - * set local rotation from euler angles - * @param x - Angle to rotate around X axis in degrees. - * @param y - Angle to rotate around Y axis in degrees. - * @param z - Angle to rotate around Z axis in degrees. + * @zh + * 通过欧拉角设置本地旋转 + * @param x - 目标欧拉角的 X 分量 + * @param y - 目标欧拉角的 Y 分量 + * @param z - 目标欧拉角的 Z 分量 */ public setRotationFromEuler (x: number, y: number, z: number) { vec3.set(this._euler, x, y, z); @@ -486,9 +437,9 @@ class Node extends BaseNode { } /** - * get local rotation - * @param out - the receiving quaternion - * @returns the resulting quaternion + * @zh + * 获取本地旋转 + * @param out 输出到此目标 quaternion */ public getRotation (out?: Quat): Quat { if (out) { @@ -498,30 +449,33 @@ class Node extends BaseNode { } } - public get localRotation (): Readonly { + /** + * @zh + * 本地旋转 + */ + public get rotation (): Readonly { return this._lrot; } + public set rotation (val: Readonly) { + this.setRotation(val); + } /** - * Sets local scale. - * @param scale - The new local scale. + * @zh + * 设置本地缩放 + * @param scale 目标本地缩放 */ public setScale (scale: Vec3): void; /** - * Sets local scale. - * @param x - The x component of the new local scale. - * @param y - The y component of the new local scale. - * @param z - The z component of the new local scale. + * @zh + * 设置本地缩放 + * @param x 目标本地缩放的 X 分量 + * @param y 目标本地缩放的 Y 分量 + * @param z 目标本地缩放的 Z 分量 */ public setScale (x: number, y: number, z: number): void; - /** - * set local scale - * @param val - the new local scale, or the x component of it - * @param y - the y component of the new local scale - * @param z - the z component of the new local scale - */ public setScale (val: Vec3 | number, y?: number, z?: number) { if (y === undefined || z === undefined) { vec3.copy(this._lscale, val as Vec3); @@ -537,9 +491,9 @@ class Node extends BaseNode { } /** - * get local scale - * @param out - the receiving vector - * @returns the resulting vector + * @zh + * 获取本地缩放 + * @param out 输出到此目标 vector */ public getScale (out?: Vec3): Vec3 { if (out) { @@ -549,30 +503,34 @@ class Node extends BaseNode { } } - public get localScale (): Readonly { + /** + * @zh + * 本地缩放 + */ + public get scale (): Readonly { return this._lscale; } + public set scale (val: Readonly) { + this.setScale(val); + } /** - * Sets world position. - * @param position - The new world position. + * @zh + * 设置世界位移 + * @param position 目标世界位移 */ public setWorldPosition (position: Vec3): void; /** - * Sets world position. - * @param x - The x component of the new world position. - * @param y - The y component of the new world position. - * @param z - The z component of the new world position. + * @zh + * 设置世界位移 + * @param x 目标世界位移的 X 分量 + * @param y 目标世界位移的 Y 分量 + * @param z 目标世界位移的 Z 分量 + * @param w 目标世界位移的 W 分量 */ public setWorldPosition (x: number, y: number, z: number): void; - /** - * set world position - * @param val - the new world position, or the x component of it - * @param y - the y component of the new world position - * @param z - the z component of the new world position - */ public setWorldPosition (val: Vec3 | number, y?: number, z?: number) { if (y === undefined || z === undefined) { vec3.copy(this._pos, val as Vec3); @@ -597,16 +555,12 @@ class Node extends BaseNode { } } - public getWorldPosition (out: Out): Out; - - public getWorldPosition (): Vec3; - /** - * get world position - * @param out - the receiving vector - * @returns the resulting vector + * @zh + * 获取世界位移 + * @param out 输出到此目标 vector */ - public getWorldPosition (out?: Out) { + public getWorldPosition (out?: Vec3): Vec3 { this.updateWorldTransform(); if (out) { return vec3.copy(out, this._pos); @@ -615,33 +569,35 @@ class Node extends BaseNode { } } + /** + * @zh + * 世界位移 + */ public get worldPosition (): Readonly { this.updateWorldTransform(); return this._pos; } + public set worldPosition (val: Readonly) { + this.setWorldPosition(val); + } /** - * Sets world rotation. - * @param rotation - The new world rotation. + * @zh + * 设置世界旋转 + * @param rotation 目标世界旋转 */ public setWorldRotation (rotation: Quat): void; /** - * Sets world rotation. - * @param x - The x component of the new world rotation. - * @param y - The y component of the new world rotation. - * @param z - The z component of the new world rotation. - * @param w - The w component of the new world rotation. + * @zh + * 设置世界旋转 + * @param x 目标世界旋转的 X 分量 + * @param y 目标世界旋转的 Y 分量 + * @param z 目标世界旋转的 Z 分量 + * @param w 目标世界旋转的 W 分量 */ public setWorldRotation (x: number, y: number, z: number, w: number): void; - /** - * set world rotation - * @param val - the new world rotation, or the x component of it - * @param y - the y component of the new world rotation - * @param z - the z component of the new world rotation - * @param w - the w component of the new world rotation - */ public setWorldRotation (val: Quat | number, y?: number, z?: number, w?: number) { if (y === undefined || z === undefined || w === undefined) { quat.copy(this._rot, val as Quat); @@ -663,10 +619,11 @@ class Node extends BaseNode { } /** - * set world rotation from euler angles - * @param x - Angle to rotate around X axis in degrees. - * @param y - Angle to rotate around Y axis in degrees. - * @param z - Angle to rotate around Z axis in degrees. + * @zh + * 通过欧拉角设置世界旋转 + * @param x - 目标欧拉角的 X 分量 + * @param y - 目标欧拉角的 Y 分量 + * @param z - 目标欧拉角的 Z 分量 */ public setWorldRotationFromEuler (x: number, y: number, z: number) { quat.fromEuler(this._rot, x, y, z); @@ -684,14 +641,10 @@ class Node extends BaseNode { } } - public getWorldRotation (out: Out): Out; - - public getWorldRotation (): Quat; - /** - * get world rotation - * @param out - the receiving quaternion - * @returns the resulting quaternion + * @zh + * 获取世界旋转 + * @param out 输出到此目标 quaternion */ public getWorldRotation (out?: Quat): Quat { this.updateWorldTransform(); @@ -702,31 +655,34 @@ class Node extends BaseNode { } } + /** + * @zh + * 世界旋转 + */ public get worldRotation (): Readonly { this.updateWorldTransform(); return this._rot; } + public set worldRotation (val: Readonly) { + this.setWorldRotation(val); + } /** - * Sets world scale. - * @param scale - The new world scale. + * @zh + * 设置世界缩放 + * @param scale 目标世界缩放 */ public setWorldScale (scale: Vec3): void; /** - * Sets world scale. - * @param x - The x component of the new world scale. - * @param y - The y component of the new world scale. - * @param z - The z component of the new world scale. + * @zh + * 设置世界缩放 + * @param x 目标世界缩放的 X 分量 + * @param y 目标世界缩放的 Y 分量 + * @param z 目标世界缩放的 Z 分量 */ public setWorldScale (x: number, y: number, z: number): void; - /** - * set world scale - * @param val - the new world scale, or the x component of it - * @param y - the y component of the new world scale - * @param z - the z component of the new world scale - */ public setWorldScale (val: Vec3 | number, y?: number, z?: number) { if (y === undefined || z === undefined) { vec3.copy(this._scale, val as Vec3); @@ -746,14 +702,10 @@ class Node extends BaseNode { } } - public getWorldScale (out: Out): Out; - - public getWorldScale (): Vec3; - /** - * get world scale - * @param out - the receiving vector - * @returns the resulting vector + * @zh + * 获取世界缩放 + * @param out 输出到此目标 vector */ public getWorldScale (out?: Vec3): Vec3 { this.updateWorldTransform(); @@ -764,65 +716,114 @@ class Node extends BaseNode { } } + /** + * @zh + * 世界缩放 + */ public get worldScale (): Readonly { this.updateWorldTransform(); return this._scale; } - - public getWorldMatrix (out?: Out): Out; - - public getWorldMatrix (): Mat4; + public set worldScale (val: Readonly) { + this.setWorldScale(val); + } /** - * get the matrix that transforms a point from local space into world space - * @param out - the receiving matrix - * @returns the - resulting matrix + * @zh + * 获取世界变换矩阵 + * @param out 输出到此目标矩阵 */ public getWorldMatrix (out?: Mat4) { this.updateWorldTransformFull(); - if (out) { - return mat4.copy(out, this._mat); - } else { - return mat4.copy(new Mat4(), this._mat); - } + if (!out) { out = new Mat4(); } + return mat4.copy(out, this._mat); } + /** + * @zh + * 世界变换矩阵 + */ public get worldMatrix (): Readonly { this.updateWorldTransformFull(); return this._mat; } /** - * get world transform matrix (with only rotation and scale) - * @param out - the receiving matrix - * @returns the - resulting matrix + * @zh + * 获取只包含旋转和缩放的世界变换矩阵 + * @param out 输出到此目标矩阵 */ public getWorldRS (out?: Mat4): Mat4 { this.updateWorldTransformFull(); - if (out === undefined) { out = new Mat4(); } + if (!out) { out = new Mat4(); } mat4.copy(out, this._mat); out.m12 = 0; out.m13 = 0; out.m14 = 0; return out; } /** - * get world transform matrix (with only rotation and translation) - * @param out - the receiving matrix - * @returns the - resulting matrix + * @zh + * 获取只包含位移和旋转的世界变换矩阵 + * @param out 输出到此目标矩阵 */ public getWorldRT (out?: Mat4): Mat4 { this.updateWorldTransform(); - if (!out) { - out = new Mat4(); - } + if (!out) { out = new Mat4(); } return mat4.fromRT(out, this._rot, this._pos); } + // =============================== + // creator-backward-compatible interfaces + // =============================== + + // NOTE: don't set it manually + get uiTransfromComp () { + if (!this._uiTransfromComp) { + this._uiTransfromComp = this.getComponent('cc.UITransformComponent') as UITransformComponent; + } + + return this._uiTransfromComp; + } + set uiTransfromComp (value: UITransformComponent | null) { + this._uiTransfromComp = value; + } + + get width () { + return this.uiTransfromComp!.width; + } + set width (value: number) { + this.uiTransfromComp!.width = value; + } + + get height () { + return this.uiTransfromComp!.height; + } + set height (value: number) { + this.uiTransfromComp!.height = value; + } + + get anchorX () { + return this.uiTransfromComp!.anchorX; + } + set anchorX (value) { + this.uiTransfromComp!.anchorX = value; + } + + get anchorY () { + return this.uiTransfromComp!.anchorY; + } + set anchorY (value: number) { + this.uiTransfromComp!.anchorY = value; + } + + get eventProcessor () { + return this._eventProcessor; + } + public getAnchorPoint (out?: Vec2) { if (!out) { out = new Vec2(); } - out.set(this.uiTransfromComp!.anchorPoint); return out; } @@ -902,20 +903,4 @@ class Node extends BaseNode { } } -Object.defineProperty(Node.prototype, 'position', { - get: Node.prototype.getPosition, - set: Node.prototype.setPosition, -}); - -Object.defineProperty(Node.prototype, 'rotation', { - get: Node.prototype.getRotation, - set: Node.prototype.setRotation, -}); - -Object.defineProperty(Node.prototype, 'scale', { - get: Node.prototype.getScale, - set: Node.prototype.setScale, -}); - cc.Node = Node; -export { Node }; diff --git a/cocos/scene-graph/scene-globals.ts b/cocos/scene-graph/scene-globals.ts index d53e5ad14f..653fc07626 100644 --- a/cocos/scene-graph/scene-globals.ts +++ b/cocos/scene-graph/scene-globals.ts @@ -37,6 +37,9 @@ const _up = new Vec3(0, 1, 0); const _v3 = new Vec3(); const _qt = new Quat(); +/** + * @zh 场景的环境光照相关信息 + */ @ccclass('cc.AmbientInfo') export class AmbientInfo { @property @@ -48,6 +51,9 @@ export class AmbientInfo { protected _resource: Ambient | null = null; + /** + * @zh 天空颜色 + */ @property({ type: Color }) set skyColor (val: Color) { this._skyColor.set(val); @@ -57,6 +63,9 @@ export class AmbientInfo { return this._skyColor; } + /** + * @zh 天空亮度 + */ @property({ type: Number }) set skyIllum (val: number) { this._skyIllum = val; @@ -66,6 +75,9 @@ export class AmbientInfo { return this._skyIllum; } + /** + * @zh 地面颜色 + */ @property({ type: Color }) set groundAlbedo (val: Color) { this._groundAlbedo.set(val); @@ -84,6 +96,9 @@ export class AmbientInfo { } cc.AmbientInfo = AmbientInfo; +/** + * @zh 天空盒相关信息 + */ @ccclass('cc.SkyboxInfo') export class SkyboxInfo { @property(TextureCube) @@ -95,6 +110,9 @@ export class SkyboxInfo { protected _resource: Skybox | null = null; + /** + * @zh 是否启用天空盒? + */ @property({ type: Boolean }) set enabled (val) { this._enabled = val; @@ -104,6 +122,9 @@ export class SkyboxInfo { return this._enabled; } + /** + * @zh 使用的立方体贴图 + */ @property({ type: TextureCube }) set cubemap (val) { this._cubemap = val; @@ -113,6 +134,9 @@ export class SkyboxInfo { return this._cubemap; } + /** + * @zh 是否需要开启 shader 内的 RGBE 数据支持? + */ @property({ type: Boolean }) set isRGBE (val) { this._isRGBE = val; @@ -131,6 +155,9 @@ export class SkyboxInfo { } cc.SkyboxInfo = SkyboxInfo; +/** + * @zh 平面阴影相关信息 + */ @ccclass('cc.PlanarShadowInfo') export class PlanarShadowInfo { @property @@ -144,6 +171,9 @@ export class PlanarShadowInfo { protected _resource: PlanarShadow | null = null; + /** + * @zh 是否启用平面阴影? + */ @property({ type: Boolean }) set enabled (val: boolean) { this._enabled = val; @@ -153,6 +183,9 @@ export class PlanarShadowInfo { return this._enabled; } + /** + * @zh 阴影接收平面的法线 + */ @property({ type: Vec3 }) set normal (val: Vec3) { vec3.copy(this._normal, val); @@ -162,6 +195,9 @@ export class PlanarShadowInfo { return this._normal; } + /** + * @zh 阴影接收平面与原点的距离 + */ @property({ type: Number }) set distance (val: number) { this._distance = val; @@ -171,6 +207,9 @@ export class PlanarShadowInfo { return this._distance; } + /** + * @zh 阴影颜色 + */ @property({ type: Color }) set shadowColor (val: Color) { this._shadowColor.set(val); @@ -180,6 +219,10 @@ export class PlanarShadowInfo { return this._shadowColor; } + /** + * @zh 根据指定节点的世界变换设置阴影接收平面的信息 + * @param node 阴影接收平面的世界变换 + */ public setPlaneFromNode (node: Node) { node.getWorldRotation(_qt); this.normal = vec3.transformQuat(_v3, _up, _qt); @@ -192,10 +235,14 @@ export class PlanarShadowInfo { this.normal = this._normal; this.distance = this._distance; this.shadowColor = this._shadowColor; + this.enabled = this._enabled; } } cc.PlanarShadowInfo = PlanarShadowInfo; +/** + * @zh 各类场景级别的渲染参数,将影响全场景的所有物体 + */ @ccclass('cc.SceneGlobals') export class SceneGlobals { @property({ type: AmbientInfo }) diff --git a/cocos/scene-graph/scene.ts b/cocos/scene-graph/scene.ts index f37b923c1d..dfeabd7780 100644 --- a/cocos/scene-graph/scene.ts +++ b/cocos/scene-graph/scene.ts @@ -30,14 +30,12 @@ import { Node } from './node'; import { SceneGlobals } from './scene-globals'; /** - * !#en + * @en * cc.Scene is a subclass of cc.Node that is used only as an abstract concept.
* cc.Scene and cc.Node are almost identical with the difference that users can not modify cc.Scene manually. - * !#zh + * @zh * cc.Scene 是 cc.Node 的子类,仅作为一个抽象的概念。
* cc.Scene 和 cc.Node 有点不同,用户不应直接修改 cc.Scene。 - * @class Scene - * @extends Node */ @ccclass('cc.Scene') export class Scene extends Node { @@ -49,22 +47,21 @@ export class Scene extends Node { get globals () { return this._globals; } + /** - * !#en Indicates whether all (directly or indirectly) static referenced assets of this scene are releasable by default after scene unloading. - * !#zh 指示该场景中直接或间接静态引用到的所有资源是否默认在场景切换后自动释放。 + * @en Indicates whether all (directly or indirectly) static referenced assets of this scene are releasable by default after scene unloading. + * @zh 指示该场景中直接或间接静态引用到的所有资源是否默认在场景切换后自动释放。 */ @property public autoReleaseAssets = false; + /** - * !#en Per-scene level rendering info - * !#zh 场景级别的渲染信息 + * @en Per-scene level rendering info + * @zh 场景级别的渲染信息 */ @property public _globals = new SceneGlobals(); - /** - * For internal usage. - */ public _renderScene: RenderScene | null = null; public dependAssets = null; // cache all depend assets for auto release