From 74c11e6e693797cd62ebce5cba74dee497b9b7fc Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Thu, 2 Sep 2021 16:19:34 +0800 Subject: [PATCH 01/17] Animator editor (#475) * fix: bugfix --- .../src/scene-loader/resources/AnimatorControllerResource.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/loader/src/scene-loader/resources/AnimatorControllerResource.ts b/packages/loader/src/scene-loader/resources/AnimatorControllerResource.ts index 1eb8b6e0c4..cb39e3765f 100644 --- a/packages/loader/src/scene-loader/resources/AnimatorControllerResource.ts +++ b/packages/loader/src/scene-loader/resources/AnimatorControllerResource.ts @@ -98,8 +98,9 @@ export class AnimatorControllerResource extends SchemaResource { } _initAnimatorController(animatorControllerData) { - const { animations } = this.gltf; + const { animations } = this.gltf || {}; const { layers } = animatorControllerData; + if (!animations || !layers) return; this._resource.clearLayers(); for (let i = 0, length = layers.length; i < length; ++i) { const { name, blending, weight, stateMachine: stateMachineData } = layers[i]; From df25f0312fbc9d025bb57e3cfe124ac141ef7c82 Mon Sep 17 00:00:00 2001 From: yangfengzzz Date: Tue, 7 Sep 2021 13:32:39 +0800 Subject: [PATCH 02/17] feat: solve math design circular dependency (#485) * feat: solve math design cirular dependency --- packages/design/package.json | 5 ++++- packages/math/package.json | 5 +---- packages/math/src/BoundingBox.ts | 2 +- packages/math/src/BoundingFrustum.ts | 2 +- packages/math/src/BoundingSphere.ts | 2 +- packages/math/src/Color.ts | 2 +- packages/math/src/IClone.ts | 16 ++++++++++++++++ packages/math/src/Matrix.ts | 2 +- packages/math/src/Matrix3x3.ts | 2 +- packages/math/src/Plane.ts | 2 +- packages/math/src/Quaternion.ts | 2 +- packages/math/src/Rect.ts | 2 +- packages/math/src/SphericalHarmonics3.ts | 5 +++-- packages/math/src/Vector2.ts | 2 +- packages/math/src/Vector3.ts | 2 +- packages/math/src/Vector4.ts | 2 +- 16 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 packages/math/src/IClone.ts diff --git a/packages/design/package.json b/packages/design/package.json index 659c27557f..6018bafd83 100755 --- a/packages/design/package.json +++ b/packages/design/package.json @@ -11,5 +11,8 @@ "dist/**/*", "types/**/*" ], - "types": "types/index.d.ts" + "types": "types/index.d.ts", + "dependencies": { + "@oasis-engine/math": "0.5.2" + } } diff --git a/packages/math/package.json b/packages/math/package.json index 647f70a8a2..c54b12d1d7 100755 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -12,8 +12,5 @@ "files": [ "dist/**/*", "types/**/*" - ], - "devDependencies": { - "@oasis-engine/design": "0.5.2" - } + ] } diff --git a/packages/math/src/BoundingBox.ts b/packages/math/src/BoundingBox.ts index 538ef5bcec..d991cbb8bc 100644 --- a/packages/math/src/BoundingBox.ts +++ b/packages/math/src/BoundingBox.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { BoundingSphere } from "./BoundingSphere"; import { Matrix } from "./Matrix"; import { Vector3 } from "./Vector3"; diff --git a/packages/math/src/BoundingFrustum.ts b/packages/math/src/BoundingFrustum.ts index 975e28ba6e..c1daa57c7b 100644 --- a/packages/math/src/BoundingFrustum.ts +++ b/packages/math/src/BoundingFrustum.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { BoundingBox } from "./BoundingBox"; import { BoundingSphere } from "./BoundingSphere"; import { CollisionUtil } from "./CollisionUtil"; diff --git a/packages/math/src/BoundingSphere.ts b/packages/math/src/BoundingSphere.ts index 628b6531cd..0db173ff03 100644 --- a/packages/math/src/BoundingSphere.ts +++ b/packages/math/src/BoundingSphere.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { BoundingBox } from "./BoundingBox"; import { Vector3 } from "./Vector3"; diff --git a/packages/math/src/Color.ts b/packages/math/src/Color.ts index ff5198c88e..fe0e959326 100644 --- a/packages/math/src/Color.ts +++ b/packages/math/src/Color.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { MathUtil } from "./MathUtil"; /** diff --git a/packages/math/src/IClone.ts b/packages/math/src/IClone.ts new file mode 100644 index 0000000000..88bf04a002 --- /dev/null +++ b/packages/math/src/IClone.ts @@ -0,0 +1,16 @@ +/** + * Clone interface. + */ +export interface IClone { + /** + * Clone and return object. + * @returns Clone object + */ + clone(): Object; + + /** + * Clone to the target object. + * @param target - Target object + */ + cloneTo(target: Object): Object; +} diff --git a/packages/math/src/Matrix.ts b/packages/math/src/Matrix.ts index d6a1924070..f4e9b88db7 100644 --- a/packages/math/src/Matrix.ts +++ b/packages/math/src/Matrix.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { MathUtil } from "./MathUtil"; import { Matrix3x3 } from "./Matrix3x3"; import { Quaternion } from "./Quaternion"; diff --git a/packages/math/src/Matrix3x3.ts b/packages/math/src/Matrix3x3.ts index 304520d716..e5ee0e607c 100644 --- a/packages/math/src/Matrix3x3.ts +++ b/packages/math/src/Matrix3x3.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { MathUtil } from "./MathUtil"; import { Matrix } from "./Matrix"; import { Quaternion } from "./Quaternion"; diff --git a/packages/math/src/Plane.ts b/packages/math/src/Plane.ts index 46d65db82b..eaff0163b3 100644 --- a/packages/math/src/Plane.ts +++ b/packages/math/src/Plane.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { Vector3 } from "./Vector3"; /** diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index e83e355c0e..8c5b28fba4 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { MathUtil } from "./MathUtil"; import { Matrix3x3 } from "./Matrix3x3"; import { Vector3 } from "./Vector3"; diff --git a/packages/math/src/Rect.ts b/packages/math/src/Rect.ts index 838bff501e..af371ebcf7 100644 --- a/packages/math/src/Rect.ts +++ b/packages/math/src/Rect.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; // A 2d rectangle defined by x and y position, width and height. export class Rect implements IClone { diff --git a/packages/math/src/SphericalHarmonics3.ts b/packages/math/src/SphericalHarmonics3.ts index c9fe995659..e60db6c0a9 100644 --- a/packages/math/src/SphericalHarmonics3.ts +++ b/packages/math/src/SphericalHarmonics3.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { Color } from "./Color"; import { Vector3 } from "./Vector3"; @@ -203,7 +203,8 @@ export class SphericalHarmonics3 implements IClone { * @param out - The specified SphericalHarmonics3 * @returns The specified SphericalHarmonics3 */ - cloneTo(out: SphericalHarmonics3): void { + cloneTo(out: SphericalHarmonics3): SphericalHarmonics3 { this.toArray(out.coefficients); + return out; } } diff --git a/packages/math/src/Vector2.ts b/packages/math/src/Vector2.ts index 12af206f42..a4ee1ec855 100644 --- a/packages/math/src/Vector2.ts +++ b/packages/math/src/Vector2.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { MathUtil } from "./MathUtil"; /** diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 381dd22a91..b238f9be8b 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { MathUtil } from "./MathUtil"; import { Matrix } from "./Matrix"; import { Quaternion } from "./Quaternion"; diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 60af696950..83f460daeb 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -1,4 +1,4 @@ -import { IClone } from "@oasis-engine/design"; +import { IClone } from "./IClone"; import { MathUtil } from "./MathUtil"; import { Matrix } from "./Matrix"; import { Quaternion } from "./Quaternion"; From f064cc887c35c938f4d4a87554c92845e91bae30 Mon Sep 17 00:00:00 2001 From: GuoLei Date: Tue, 7 Sep 2021 13:33:18 +0800 Subject: [PATCH 03/17] Revert "feat: solve math design circular dependency (#485)" (#487) This reverts commit df25f0312fbc9d025bb57e3cfe124ac141ef7c82. --- packages/design/package.json | 5 +---- packages/math/package.json | 5 ++++- packages/math/src/BoundingBox.ts | 2 +- packages/math/src/BoundingFrustum.ts | 2 +- packages/math/src/BoundingSphere.ts | 2 +- packages/math/src/Color.ts | 2 +- packages/math/src/IClone.ts | 16 ---------------- packages/math/src/Matrix.ts | 2 +- packages/math/src/Matrix3x3.ts | 2 +- packages/math/src/Plane.ts | 2 +- packages/math/src/Quaternion.ts | 2 +- packages/math/src/Rect.ts | 2 +- packages/math/src/SphericalHarmonics3.ts | 5 ++--- packages/math/src/Vector2.ts | 2 +- packages/math/src/Vector3.ts | 2 +- packages/math/src/Vector4.ts | 2 +- 16 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 packages/math/src/IClone.ts diff --git a/packages/design/package.json b/packages/design/package.json index 6018bafd83..659c27557f 100755 --- a/packages/design/package.json +++ b/packages/design/package.json @@ -11,8 +11,5 @@ "dist/**/*", "types/**/*" ], - "types": "types/index.d.ts", - "dependencies": { - "@oasis-engine/math": "0.5.2" - } + "types": "types/index.d.ts" } diff --git a/packages/math/package.json b/packages/math/package.json index c54b12d1d7..647f70a8a2 100755 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -12,5 +12,8 @@ "files": [ "dist/**/*", "types/**/*" - ] + ], + "devDependencies": { + "@oasis-engine/design": "0.5.2" + } } diff --git a/packages/math/src/BoundingBox.ts b/packages/math/src/BoundingBox.ts index d991cbb8bc..538ef5bcec 100644 --- a/packages/math/src/BoundingBox.ts +++ b/packages/math/src/BoundingBox.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { BoundingSphere } from "./BoundingSphere"; import { Matrix } from "./Matrix"; import { Vector3 } from "./Vector3"; diff --git a/packages/math/src/BoundingFrustum.ts b/packages/math/src/BoundingFrustum.ts index c1daa57c7b..975e28ba6e 100644 --- a/packages/math/src/BoundingFrustum.ts +++ b/packages/math/src/BoundingFrustum.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { BoundingBox } from "./BoundingBox"; import { BoundingSphere } from "./BoundingSphere"; import { CollisionUtil } from "./CollisionUtil"; diff --git a/packages/math/src/BoundingSphere.ts b/packages/math/src/BoundingSphere.ts index 0db173ff03..628b6531cd 100644 --- a/packages/math/src/BoundingSphere.ts +++ b/packages/math/src/BoundingSphere.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { BoundingBox } from "./BoundingBox"; import { Vector3 } from "./Vector3"; diff --git a/packages/math/src/Color.ts b/packages/math/src/Color.ts index fe0e959326..ff5198c88e 100644 --- a/packages/math/src/Color.ts +++ b/packages/math/src/Color.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { MathUtil } from "./MathUtil"; /** diff --git a/packages/math/src/IClone.ts b/packages/math/src/IClone.ts deleted file mode 100644 index 88bf04a002..0000000000 --- a/packages/math/src/IClone.ts +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Clone interface. - */ -export interface IClone { - /** - * Clone and return object. - * @returns Clone object - */ - clone(): Object; - - /** - * Clone to the target object. - * @param target - Target object - */ - cloneTo(target: Object): Object; -} diff --git a/packages/math/src/Matrix.ts b/packages/math/src/Matrix.ts index f4e9b88db7..d6a1924070 100644 --- a/packages/math/src/Matrix.ts +++ b/packages/math/src/Matrix.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { MathUtil } from "./MathUtil"; import { Matrix3x3 } from "./Matrix3x3"; import { Quaternion } from "./Quaternion"; diff --git a/packages/math/src/Matrix3x3.ts b/packages/math/src/Matrix3x3.ts index e5ee0e607c..304520d716 100644 --- a/packages/math/src/Matrix3x3.ts +++ b/packages/math/src/Matrix3x3.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { MathUtil } from "./MathUtil"; import { Matrix } from "./Matrix"; import { Quaternion } from "./Quaternion"; diff --git a/packages/math/src/Plane.ts b/packages/math/src/Plane.ts index eaff0163b3..46d65db82b 100644 --- a/packages/math/src/Plane.ts +++ b/packages/math/src/Plane.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { Vector3 } from "./Vector3"; /** diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 8c5b28fba4..e83e355c0e 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { MathUtil } from "./MathUtil"; import { Matrix3x3 } from "./Matrix3x3"; import { Vector3 } from "./Vector3"; diff --git a/packages/math/src/Rect.ts b/packages/math/src/Rect.ts index af371ebcf7..838bff501e 100644 --- a/packages/math/src/Rect.ts +++ b/packages/math/src/Rect.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; // A 2d rectangle defined by x and y position, width and height. export class Rect implements IClone { diff --git a/packages/math/src/SphericalHarmonics3.ts b/packages/math/src/SphericalHarmonics3.ts index e60db6c0a9..c9fe995659 100644 --- a/packages/math/src/SphericalHarmonics3.ts +++ b/packages/math/src/SphericalHarmonics3.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { Color } from "./Color"; import { Vector3 } from "./Vector3"; @@ -203,8 +203,7 @@ export class SphericalHarmonics3 implements IClone { * @param out - The specified SphericalHarmonics3 * @returns The specified SphericalHarmonics3 */ - cloneTo(out: SphericalHarmonics3): SphericalHarmonics3 { + cloneTo(out: SphericalHarmonics3): void { this.toArray(out.coefficients); - return out; } } diff --git a/packages/math/src/Vector2.ts b/packages/math/src/Vector2.ts index a4ee1ec855..12af206f42 100644 --- a/packages/math/src/Vector2.ts +++ b/packages/math/src/Vector2.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { MathUtil } from "./MathUtil"; /** diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index b238f9be8b..381dd22a91 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { MathUtil } from "./MathUtil"; import { Matrix } from "./Matrix"; import { Quaternion } from "./Quaternion"; diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 83f460daeb..60af696950 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -1,4 +1,4 @@ -import { IClone } from "./IClone"; +import { IClone } from "@oasis-engine/design"; import { MathUtil } from "./MathUtil"; import { Matrix } from "./Matrix"; import { Quaternion } from "./Quaternion"; From 2878bdf13b826f0db0a63f10cd44813c2b9dd306 Mon Sep 17 00:00:00 2001 From: JujieX <67930474+JujieX@users.noreply.github.com> Date: Tue, 7 Sep 2021 18:31:43 +0800 Subject: [PATCH 04/17] feat: add rotateAxisAngle for Quaternion (#480) * feat: add rotateAxisAngle for Quaternion --- packages/math/src/Quaternion.ts | 14 ++++++++++++++ packages/math/tests/Quaternion.test.ts | 10 +++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index e83e355c0e..a0f01dca77 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -9,6 +9,8 @@ import { Vector3 } from "./Vector3"; export class Quaternion implements IClone { /** @internal */ static readonly _tempVector3 = new Vector3(); + /** @internal */ + static readonly _tempQuat1 = new Quaternion(); /** * Determines the sum of two quaternions. @@ -697,4 +699,16 @@ export class Quaternion implements IClone { Quaternion.lerp(this, quat, t, this); return this; } + + /** + * Calculate this quaternion rotation around an arbitrary axis. + * @param axis - The axis + * @param rad - The rotation angle in radians + * @returns This quaternion + */ + rotateAxisAngle(axis: Vector3, rad: number): Quaternion { + Quaternion._tempQuat1.rotationAxisAngle(axis, rad); + this.multiply(Quaternion._tempQuat1); + return this; + } } diff --git a/packages/math/tests/Quaternion.test.ts b/packages/math/tests/Quaternion.test.ts index 3363889062..d8ae41090a 100644 --- a/packages/math/tests/Quaternion.test.ts +++ b/packages/math/tests/Quaternion.test.ts @@ -177,6 +177,14 @@ describe("Quaternion test", () => { expect(Quaternion.equals(out, b)).toEqual(true); }); + it("static rotatAxisAngle", () => { + const a = new Vector3(0, 5, 0); + const b = 0.5 * Math.PI; + const out = new Quaternion(0, 0, 0, 1); + out.rotateAxisAngle(a, b); + expect(Quaternion.equals(out, new Quaternion(0, 0.7071067811865475, 0, 0.7071067811865476))).toEqual(true); + }); + it("static scale", () => { const a = new Quaternion(3, 4, 5, 0); const out = new Quaternion(); @@ -200,7 +208,7 @@ describe("Quaternion test", () => { const b = new Quaternion(); b.setValueByArray([1, 1, 1, 1]); expect(Quaternion.equals(a, b)).toEqual(true); - + const c = []; b.toArray(c); const d = new Quaternion(); From 7d17191d508ca2e4da2a8452ecb52d89711ada82 Mon Sep 17 00:00:00 2001 From: Hu Song Date: Wed, 8 Sep 2021 11:49:39 +0800 Subject: [PATCH 05/17] refactor: delete useless scene raycast (#493) --- packages/core/src/Scene.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/core/src/Scene.ts b/packages/core/src/Scene.ts index 8effb4c860..4d6a7cf550 100644 --- a/packages/core/src/Scene.ts +++ b/packages/core/src/Scene.ts @@ -258,11 +258,4 @@ export class Scene extends EngineObject { } features: SceneFeature[] = []; - - /** - * Raycast. - * @deprecated - * @param ray - */ - public raycast(ray: { origin: Vector3; direction: Vector3 }, outPos?: Vector3, tag?: Layer): any {} } From aea42777b2067fa309e0bb77ece830a9e84df382 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 10 Sep 2021 18:05:22 +0800 Subject: [PATCH 06/17] fix: group other uniform block (#504) --- packages/core/src/RenderPipeline/BasicRenderPipeline.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts index 5f9da6a13b..961b918c11 100644 --- a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts +++ b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts @@ -257,6 +257,7 @@ export class BasicRenderPipeline { const program = shader._getShaderProgram(engine, compileMacros); program.bind(); + program.groupingOtherUniformBlock(); program.uploadAll(program.materialUniformBlock, shaderData); program.uploadUngroupTextures(); From 6b86f88682bdb40fb9ea8d9154dd7d28bee5af22 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Fri, 8 Oct 2021 16:25:45 +0800 Subject: [PATCH 07/17] fix(primitivemesh): fix cylinder error (#526) --- packages/core/src/mesh/PrimitiveMesh.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/src/mesh/PrimitiveMesh.ts b/packages/core/src/mesh/PrimitiveMesh.ts index 83e78b39d3..5d30d46591 100644 --- a/packages/core/src/mesh/PrimitiveMesh.ts +++ b/packages/core/src/mesh/PrimitiveMesh.ts @@ -340,7 +340,9 @@ export class PrimitiveMesh { // Create torso const thetaStart = Math.PI; const thetaRange = Math.PI * 2; - const slope = (radiusBottom - radiusTop) / height; + const radiusDiff = radiusBottom - radiusTop; + const slope = radiusDiff / height; + const radiusSlope = radiusDiff / heightSegments; for (let i = 0; i < torsoVertexCount; ++i) { const x = i % radialCount; @@ -350,7 +352,7 @@ export class PrimitiveMesh { const theta = thetaStart + u * thetaRange; const sinTheta = Math.sin(theta); const cosTheta = Math.cos(theta); - const radius = radiusBottom - y * (radiusBottom - radiusTop); + const radius = radiusBottom - y * radiusSlope; let posX = radius * sinTheta; let posY = y * unitHeight - halfHeight; From 750be3cb7b2087886030a0f6d418a567abb46884 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Sat, 9 Oct 2021 15:50:44 +0800 Subject: [PATCH 08/17] Perf: release gpu command (#503) * perf: release viewport gpu command * perf: release clearColor gpu command --- packages/rhi-webgl/src/WebGLRenderer.ts | 39 ++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/rhi-webgl/src/WebGLRenderer.ts b/packages/rhi-webgl/src/WebGLRenderer.ts index fb05cd1ec1..03c4f21ea0 100644 --- a/packages/rhi-webgl/src/WebGLRenderer.ts +++ b/packages/rhi-webgl/src/WebGLRenderer.ts @@ -21,7 +21,7 @@ import { } from "@oasis-engine/core"; import { CameraClearFlags } from "@oasis-engine/core"; import { IPlatformPrimitive } from "@oasis-engine/design"; -import { Color } from "@oasis-engine/math"; +import { Color, Vector4 } from "@oasis-engine/math"; import { GLCapability } from "./GLCapability"; import { GLExtensions } from "./GLExtensions"; import { GLPrimitive } from "./GLPrimitive"; @@ -71,6 +71,10 @@ export class WebGLRenderer implements IHardwareRenderer { private _activeTextureID: number = WebGLRenderingContext.TEXTURE0; private _activeTextures: GLTexture[] = new Array(32); + // cache value + private _lastViewport: Vector4 = new Vector4(undefined, undefined, undefined, undefined); + private _lastClearColor: Color = new Color(undefined, undefined, undefined, undefined); + get isWebGL2() { return this._isWebGL2; } @@ -181,11 +185,16 @@ export class WebGLRenderer implements IHardwareRenderer { return this.capability.canIUseCompressedTextureInternalFormat(type); } - viewport(x, y, width, height) { + viewport(x: number, y: number, width: number, height: number): void { // gl.enable(gl.SCISSOR_TEST); // gl.scissor(x, transformY, width, height); const gl = this._gl; - gl.viewport(x, gl.drawingBufferHeight - y - height, width, height); + const lv = this._lastViewport; + + if (x !== lv.x || y !== lv.y || width !== lv.z || height !== lv.w) { + gl.viewport(x, y, width, height); + lv.setValue(x, y, width, height); + } } colorMask(r, g, b, a) { @@ -205,11 +214,18 @@ export class WebGLRenderer implements IHardwareRenderer { } = engine._lastRenderState; let clearFlag = gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT; + if (clearFlags === CameraClearFlags.DepthColor) { - clearFlag = clearFlag | gl.COLOR_BUFFER_BIT; - if (clearColor) { - gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); + clearFlag |= gl.COLOR_BUFFER_BIT; + + const lc = this._lastClearColor; + const { r, g, b, a } = clearColor; + + if (clearColor && (r !== lc.r || g !== lc.g || b !== lc.b || a !== lc.a)) { + gl.clearColor(r, g, b, a); + lc.setValue(r, g, b, a); } + if (targetBlendState.colorWriteMask !== ColorWriteMask.All) { gl.colorMask(true, true, true, true); targetBlendState.colorWriteMask = ColorWriteMask.All; @@ -245,13 +261,16 @@ export class WebGLRenderer implements IHardwareRenderer { /** @ts-ignore */ (renderTarget._platformRenderTarget as GLRenderTarget)?._activeRenderTarget(); const { width, height } = renderTarget; - gl.viewport(0.0, 0.0, width, height); + this.viewport(0, 0, width, height); } else { gl.bindFramebuffer(gl.FRAMEBUFFER, null); const viewport = camera.viewport; - const width = gl.drawingBufferWidth; - const height = gl.drawingBufferHeight; - this.viewport(viewport.x * width, viewport.y * height, viewport.z * width, viewport.w * height); + const { drawingBufferWidth, drawingBufferHeight } = gl; + const width = drawingBufferWidth * viewport.z; + const height = drawingBufferHeight * viewport.w; + const x = viewport.x * drawingBufferWidth; + const y = drawingBufferHeight - viewport.y * drawingBufferHeight - height; + this.viewport(x, y, width, height); } } From 22bf7f65a70d9a3540c98a1c6497040f1695b0f3 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Sat, 9 Oct 2021 15:51:58 +0800 Subject: [PATCH 09/17] fix: reset clipTime --- packages/core/src/animation/internal/AnimatorStatePlayData.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/animation/internal/AnimatorStatePlayData.ts b/packages/core/src/animation/internal/AnimatorStatePlayData.ts index bfd5452e54..f34b8cd1e3 100644 --- a/packages/core/src/animation/internal/AnimatorStatePlayData.ts +++ b/packages/core/src/animation/internal/AnimatorStatePlayData.ts @@ -18,6 +18,7 @@ export class AnimatorStatePlayData { this.frameTime = offsetFrameTime; this.stateData = stateData; this.finished = false; + this.clipTime = this.state.clipStartTime; this.currentEventIndex = 0; } From bc9487de1c5ac3e04c71537dc69a7a6fc955fd3d Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Sat, 9 Oct 2021 16:22:40 +0800 Subject: [PATCH 10/17] fix: reset clipTime (#530) * fix: reset clipTime --- packages/core/src/animation/internal/AnimatorStatePlayData.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/animation/internal/AnimatorStatePlayData.ts b/packages/core/src/animation/internal/AnimatorStatePlayData.ts index bfd5452e54..f34b8cd1e3 100644 --- a/packages/core/src/animation/internal/AnimatorStatePlayData.ts +++ b/packages/core/src/animation/internal/AnimatorStatePlayData.ts @@ -18,6 +18,7 @@ export class AnimatorStatePlayData { this.frameTime = offsetFrameTime; this.stateData = stateData; this.finished = false; + this.clipTime = this.state.clipStartTime; this.currentEventIndex = 0; } From 0a82e5ff953075218c5b5d18065fa1b8bbd6c272 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Sat, 9 Oct 2021 17:58:56 +0800 Subject: [PATCH 11/17] fix: bugfix --- packages/core/src/animation/Animator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index 5196dde13a..c7ca3746fd 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -192,7 +192,7 @@ export class Animator extends Component { if (animatorController) { const layers = animatorController.layers; if (layerIndex === -1) { - for (let i = 0, n = layers.length; i < n; i--) { + for (let i = 0, n = layers.length; i < n; i++) { state = layers[i].stateMachine.findStateByName(stateName); if (state) { layerIndex = i; From 385fa8d92f9b0645e2eb85306718bf3b64f09094 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Sat, 9 Oct 2021 21:08:07 +0800 Subject: [PATCH 12/17] feat: add animator reset --- packages/core/src/animation/Animator.ts | 63 ++++++++++++++++++- .../animation/internal/AnimationCurveOwner.ts | 42 +++++++++++++ packages/loader/src/scene-loader/GLTFModel.ts | 32 ++++++---- 3 files changed, 125 insertions(+), 12 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index c7ca3746fd..92a51f3df5 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -118,6 +118,23 @@ export class Animator extends Component { this._saveDefaultValues(animatorStateData); } + reset() { + const { _animatorController: animatorController } = this; + if (animatorController) { + const layers = animatorController.layers; + for (let i = 0, n = layers.length; i < n; ++i) { + const {states} = layers[i].stateMachine + const animatorLayerData = this._getAnimatorLayerData(i); + for (let j = 0, m = states.length; j < m; ++j) { + const state = states[j]; + const animatorStateData = this._getAnimatorStateData(state.name, state, animatorLayerData); + this._revertOriginValue(state, animatorStateData); + } + } + } + this._clearPlayData(); + } + /** * Create a cross fade from the current state to another state. * @param stateName - The state name @@ -561,6 +578,12 @@ export class Animator extends Component { break; } } + } else if (owner.type === SkinnedMeshRenderer) { + switch (owner.property) { + case AnimationProperty.BlendShapeWeights: + (owner.component).blendShapeWeights = value; + break; + } } if (additive) { @@ -657,6 +680,42 @@ export class Animator extends Component { case AnimationProperty.Scale: transform.scale = owner.defaultValue; break; + case AnimationProperty.BlendShapeWeights: + const { blendShapeWeights } = owner.component; + for (let j = 0, length = blendShapeWeights.length; j < length; ++j) { + (owner.component).blendShapeWeights[j] = owner.defaultValue[j]; + } + break; + } + } + } + } + + private _revertOriginValue(state: AnimatorState, stateData: AnimatorStateData) { + const { clip } = state; + if (clip) { + const curves = clip._curveBindings; + const { curveOwners } = stateData; + for (let i = curves.length - 1; i >= 0; i--) { + const owner = curveOwners[i]; + if (!owner.hasSavedOriginValue) return; + const { transform } = owner.target; + switch (owner.property) { + case AnimationProperty.Position: + transform.position = owner.originValue; + break; + case AnimationProperty.Rotation: + transform.rotationQuaternion = owner.originValue; + break; + case AnimationProperty.Scale: + transform.scale = owner.originValue; + break; + case AnimationProperty.BlendShapeWeights: + const { blendShapeWeights } = owner.component; + for (let j = 0, length = blendShapeWeights.length; j < length; ++j) { + (owner.component).blendShapeWeights[j] = owner.originValue[j]; + } + break; } } } @@ -769,6 +828,8 @@ export class Animator extends Component { this._animatorLayersData.length = 0; this._crossCurveDataCollection.length = 0; this._animationCurveOwners.length = 0; - this._controllerUpdateFlag.flag = false; + if (this._controllerUpdateFlag) { + this._controllerUpdateFlag.flag = false; + } } } diff --git a/packages/core/src/animation/internal/AnimationCurveOwner.ts b/packages/core/src/animation/internal/AnimationCurveOwner.ts index 0bcaf59a1e..8ee9a0498e 100644 --- a/packages/core/src/animation/internal/AnimationCurveOwner.ts +++ b/packages/core/src/animation/internal/AnimationCurveOwner.ts @@ -16,8 +16,10 @@ export class AnimationCurveOwner { readonly type: new (entity: Entity) => Component; readonly property: AnimationProperty; readonly component: Component; + readonly originValue: InterpolableValue; readonly defaultValue: InterpolableValue; readonly fixedPoseValue: InterpolableValue; + readonly hasSavedOriginValue: boolean = false; constructor(target: Entity, type: new (entity: Entity) => Component, property: AnimationProperty) { this.target = target; @@ -25,21 +27,25 @@ export class AnimationCurveOwner { this.property = property; switch (property) { case AnimationProperty.Position: + this.originValue = new Vector3(); this.defaultValue = new Vector3(); this.fixedPoseValue = new Vector3(); this.component = target.transform; break; case AnimationProperty.Rotation: + this.originValue = new Quaternion(); this.defaultValue = new Quaternion(); this.fixedPoseValue = new Quaternion(); this.component = target.transform; break; case AnimationProperty.Scale: + this.originValue = new Vector3(); this.defaultValue = new Vector3(); this.fixedPoseValue = new Vector3(); this.component = target.transform; break; case AnimationProperty.BlendShapeWeights: + this.originValue = new Float32Array(4); this.defaultValue = new Float32Array(4); this.fixedPoseValue = new Float32Array(4); this.component = target.getComponent(SkinnedMeshRenderer); @@ -47,7 +53,31 @@ export class AnimationCurveOwner { } } + saveOriginValue(): void { + switch (this.property) { + case AnimationProperty.Position: + this.target.transform.position.cloneTo(this.originValue); + break; + case AnimationProperty.Rotation: + this.target.transform.rotationQuaternion.cloneTo(this.originValue); + break; + case AnimationProperty.Scale: + this.target.transform.scale.cloneTo(this.originValue); + break; + case AnimationProperty.BlendShapeWeights: + const { blendShapeWeights } = this.component; + for (let i = 0, length = blendShapeWeights.length; i < length; ++i) { + this.originValue[i] = (this.component).blendShapeWeights[i]; + } + break; + } + this.hasSavedOriginValue = true; + } + saveDefaultValue(): void { + if (!this.hasSavedOriginValue) { + this.saveOriginValue(); + } switch (this.property) { case AnimationProperty.Position: this.target.transform.position.cloneTo(this.defaultValue); @@ -58,6 +88,12 @@ export class AnimationCurveOwner { case AnimationProperty.Scale: this.target.transform.scale.cloneTo(this.defaultValue); break; + case AnimationProperty.BlendShapeWeights: + const { blendShapeWeights } = this.component; + for (let i = 0, length = blendShapeWeights.length; i < length; ++i) { + this.defaultValue[i] = (this.component).blendShapeWeights[i]; + } + break; } } @@ -72,6 +108,12 @@ export class AnimationCurveOwner { case AnimationProperty.Scale: this.target.transform.scale.cloneTo(this.fixedPoseValue); break; + case AnimationProperty.BlendShapeWeights: + const { blendShapeWeights } = this.component; + for (let i = 0, length = blendShapeWeights.length; i < length; ++i) { + this.fixedPoseValue[i] = (this.component).blendShapeWeights[i]; + } + break; } } } diff --git a/packages/loader/src/scene-loader/GLTFModel.ts b/packages/loader/src/scene-loader/GLTFModel.ts index 813cc9b498..f4a434639a 100644 --- a/packages/loader/src/scene-loader/GLTFModel.ts +++ b/packages/loader/src/scene-loader/GLTFModel.ts @@ -87,9 +87,13 @@ export class GLTFModel extends Component { set clipPreview(value: string) { if (this._animator) { if (value) { - this._animator.play(value, 0); + if (value === "_default") { + this._playDefaultState(); + } else { + this._animator.play(value, 0); + } } else { - this._playDefaultState(); + this._animator.reset(); } } this._clipPreview = value; @@ -104,7 +108,7 @@ export class GLTFModel extends Component { * @param props - Init props */ init(props): void { - const { asset = null, speed, animatorController, clipPreview, isClone } = props; + const { asset = null, speed, animatorController, clipPreview, isClone } = props; if (isClone) { const rootName = (props as any).gltfRootName; if (rootName) { @@ -153,12 +157,16 @@ export class GLTFModel extends Component { _playState() { const playStateName = this._clipPreview; if (playStateName) { - this._animator.play(playStateName, 0); - if (this._controllerUpdateFlag?.flag) { - this._controllerUpdateFlag.flag = false; + if (playStateName === "_default") { + this._playDefaultState(); + } else { + this._animator.play(playStateName, 0); } } else { - this._playDefaultState(); + this._animator.reset(); + } + if (this._controllerUpdateFlag?.flag) { + this._controllerUpdateFlag.flag = false; } } @@ -169,13 +177,15 @@ export class GLTFModel extends Component { const { layers } = animatorController; for (let i = 0, length = layers.length; i < length; ++i) { //@ts-ignore - const defaultState = layers[i]?.stateMachine?._defaultState ?? layers[i]?.stateMachine?.states[0]; + const defaultState = layers[i]?.stateMachine?._defaultState; const defaultStateName = defaultState?.name; if (defaultStateName) { animator.play(defaultStateName, i); - if (this._controllerUpdateFlag?.flag) { - this._controllerUpdateFlag.flag = false; - } + } else { + animator.reset(); + } + if (this._controllerUpdateFlag?.flag) { + this._controllerUpdateFlag.flag = false; } } } From e920d02aa8af8dae02629bcfd55a8c44183b4807 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Mon, 11 Oct 2021 10:29:18 +0800 Subject: [PATCH 13/17] feat: add animator reset --- packages/loader/src/scene-loader/GLTFModel.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/loader/src/scene-loader/GLTFModel.ts b/packages/loader/src/scene-loader/GLTFModel.ts index f4a434639a..0010e8cf92 100644 --- a/packages/loader/src/scene-loader/GLTFModel.ts +++ b/packages/loader/src/scene-loader/GLTFModel.ts @@ -12,7 +12,7 @@ export class GLTFModel extends Component { private _animator: Animator; private _asset: GLTFResource; private _glTFEntity: Entity; - private _clipPreview: string; + private _autoPlay: string; private _hasBuiltNode: boolean = false; private _controllerUpdateFlag: UpdateFlag; @@ -80,11 +80,11 @@ export class GLTFModel extends Component { return this._animator; } - get clipPreview() { - return this._clipPreview; + get autoPlay() { + return this._autoPlay; } - set clipPreview(value: string) { + set autoPlay(value: string) { if (this._animator) { if (value) { if (value === "_default") { @@ -96,7 +96,7 @@ export class GLTFModel extends Component { this._animator.reset(); } } - this._clipPreview = value; + this._autoPlay = value; } constructor(entity) { @@ -108,7 +108,7 @@ export class GLTFModel extends Component { * @param props - Init props */ init(props): void { - const { asset = null, speed, animatorController, clipPreview, isClone } = props; + const { asset = null, speed, animatorController, autoPlay, isClone } = props; if (isClone) { const rootName = (props as any).gltfRootName; if (rootName) { @@ -127,7 +127,7 @@ export class GLTFModel extends Component { this.asset = asset; this.animatorController = animatorController; this.speed = speed; - this.clipPreview = clipPreview; + this.autoPlay = autoPlay; } update() { @@ -155,7 +155,7 @@ export class GLTFModel extends Component { } _playState() { - const playStateName = this._clipPreview; + const playStateName = this._autoPlay; if (playStateName) { if (playStateName === "_default") { this._playDefaultState(); From 4d13d2a29909770eb8dc97786e54fefbeddaf628 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Mon, 11 Oct 2021 14:15:13 +0800 Subject: [PATCH 14/17] feat: add animator reset --- packages/loader/src/scene-loader/GLTFModel.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/loader/src/scene-loader/GLTFModel.ts b/packages/loader/src/scene-loader/GLTFModel.ts index 0010e8cf92..d928e82de9 100644 --- a/packages/loader/src/scene-loader/GLTFModel.ts +++ b/packages/loader/src/scene-loader/GLTFModel.ts @@ -12,7 +12,7 @@ export class GLTFModel extends Component { private _animator: Animator; private _asset: GLTFResource; private _glTFEntity: Entity; - private _autoPlay: string; + private _clipPreview: string; private _hasBuiltNode: boolean = false; private _controllerUpdateFlag: UpdateFlag; @@ -80,11 +80,11 @@ export class GLTFModel extends Component { return this._animator; } - get autoPlay() { - return this._autoPlay; + get clipPreview() { + return this._clipPreview; } - set autoPlay(value: string) { + set clipPreview(value: string) { if (this._animator) { if (value) { if (value === "_default") { @@ -96,7 +96,7 @@ export class GLTFModel extends Component { this._animator.reset(); } } - this._autoPlay = value; + this._clipPreview = value; } constructor(entity) { @@ -155,7 +155,7 @@ export class GLTFModel extends Component { } _playState() { - const playStateName = this._autoPlay; + const playStateName = this._clipPreview; if (playStateName) { if (playStateName === "_default") { this._playDefaultState(); From 2531e975a5d8eb57f851af9897e727f07ad7e9a5 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Mon, 11 Oct 2021 14:16:23 +0800 Subject: [PATCH 15/17] feat: add animator reset --- packages/loader/src/scene-loader/GLTFModel.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/loader/src/scene-loader/GLTFModel.ts b/packages/loader/src/scene-loader/GLTFModel.ts index d928e82de9..f4a434639a 100644 --- a/packages/loader/src/scene-loader/GLTFModel.ts +++ b/packages/loader/src/scene-loader/GLTFModel.ts @@ -108,7 +108,7 @@ export class GLTFModel extends Component { * @param props - Init props */ init(props): void { - const { asset = null, speed, animatorController, autoPlay, isClone } = props; + const { asset = null, speed, animatorController, clipPreview, isClone } = props; if (isClone) { const rootName = (props as any).gltfRootName; if (rootName) { @@ -127,7 +127,7 @@ export class GLTFModel extends Component { this.asset = asset; this.animatorController = animatorController; this.speed = speed; - this.autoPlay = autoPlay; + this.clipPreview = clipPreview; } update() { From fe23e038544801d7b9d9c6065b8382c0cab4f7bf Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Mon, 25 Oct 2021 16:32:59 +0800 Subject: [PATCH 16/17] feat: merge dev0.6 --- packages/rhi-webgl/src/WebGLRenderer.ts | 38 +++++++------------------ 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/packages/rhi-webgl/src/WebGLRenderer.ts b/packages/rhi-webgl/src/WebGLRenderer.ts index ea55079146..0f9a7c810e 100644 --- a/packages/rhi-webgl/src/WebGLRenderer.ts +++ b/packages/rhi-webgl/src/WebGLRenderer.ts @@ -21,7 +21,7 @@ import { } from "@oasis-engine/core"; import { CameraClearFlags } from "@oasis-engine/core"; import { IPlatformPrimitive } from "@oasis-engine/design"; -import { Color, Vector4 } from "@oasis-engine/math"; +import { Color } from "@oasis-engine/math"; import { GLCapability } from "./GLCapability"; import { GLExtensions } from "./GLExtensions"; import { GLPrimitive } from "./GLPrimitive"; @@ -71,10 +71,6 @@ export class WebGLRenderer implements IHardwareRenderer { private _activeTextureID: number = WebGLRenderingContext.TEXTURE0; private _activeTextures: GLTexture[] = new Array(32); - // cache value - private _lastViewport: Vector4 = new Vector4(undefined, undefined, undefined, undefined); - private _lastClearColor: Color = new Color(undefined, undefined, undefined, undefined); - get isWebGL2() { return this._isWebGL2; } @@ -185,16 +181,11 @@ export class WebGLRenderer implements IHardwareRenderer { return this.capability.canIUseCompressedTextureInternalFormat(type); } - viewport(x: number, y: number, width: number, height: number): void { + viewport(x, y, width, height) { // gl.enable(gl.SCISSOR_TEST); // gl.scissor(x, transformY, width, height); const gl = this._gl; - const lv = this._lastViewport; - - if (x !== lv.x || y !== lv.y || width !== lv.z || height !== lv.w) { - gl.viewport(x, y, width, height); - lv.setValue(x, y, width, height); - } + gl.viewport(x, gl.drawingBufferHeight - y - height, width, height); } colorMask(r, g, b, a) { @@ -214,18 +205,11 @@ export class WebGLRenderer implements IHardwareRenderer { } = engine._lastRenderState; let clearFlag = gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT; - if (clearFlags === CameraClearFlags.DepthColor) { - clearFlag |= gl.COLOR_BUFFER_BIT; - - const lc = this._lastClearColor; - const { r, g, b, a } = clearColor; - - if (clearColor && (r !== lc.r || g !== lc.g || b !== lc.b || a !== lc.a)) { - gl.clearColor(r, g, b, a); - lc.setValue(r, g, b, a); + clearFlag = clearFlag | gl.COLOR_BUFFER_BIT; + if (clearColor) { + gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); } - if (targetBlendState.colorWriteMask !== ColorWriteMask.All) { gl.colorMask(true, true, true, true); targetBlendState.colorWriteMask = ColorWriteMask.All; @@ -261,16 +245,14 @@ export class WebGLRenderer implements IHardwareRenderer { /** @ts-ignore */ (renderTarget._platformRenderTarget as GLRenderTarget)?._activeRenderTarget(); const { width, height } = renderTarget; + gl.viewport(0.0, 0.0, width >> mipLevel, height >> mipLevel); } else { gl.bindFramebuffer(gl.FRAMEBUFFER, null); const viewport = camera.viewport; - const { drawingBufferWidth, drawingBufferHeight } = gl; - const width = drawingBufferWidth * viewport.z; - const height = drawingBufferHeight * viewport.w; - const x = viewport.x * drawingBufferWidth; - const y = drawingBufferHeight - viewport.y * drawingBufferHeight - height; - this.viewport(x, y, width, height); + const width = gl.drawingBufferWidth; + const height = gl.drawingBufferHeight; + this.viewport(viewport.x * width, viewport.y * height, viewport.z * width, viewport.w * height); } } From de8d4d2cc31e25b197abed1de030f0ba5b7901e7 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Fri, 12 Nov 2021 14:11:37 +0800 Subject: [PATCH 17/17] refactor: opt code --- packages/core/src/animation/Animator.ts | 58 +++++-------------- .../animation/internal/AnimationCurveOwner.ts | 30 ---------- packages/loader/src/scene-loader/GLTFModel.ts | 9 ++- 3 files changed, 21 insertions(+), 76 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index 4c3c9597a3..c03fedc618 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -107,7 +107,7 @@ export class Animator extends Component { const { srcPlayData } = animatorLayerData; const { state: curState } = srcPlayData; if (curState && curState !== state) { - this._revertDefaultValue(srcPlayData); + this._revertDefaultValue(srcPlayData.state, srcPlayData.stateData); } //CM: Not consider same stateName, but different animation @@ -119,23 +119,26 @@ export class Animator extends Component { this._saveDefaultValues(animatorStateData); } - reset() { + /** + * @internal + */ + _reset(): void { const { _animatorController: animatorController } = this; if (animatorController) { const layers = animatorController.layers; for (let i = 0, n = layers.length; i < n; ++i) { - const {states} = layers[i].stateMachine + const { states } = layers[i].stateMachine; const animatorLayerData = this._getAnimatorLayerData(i); for (let j = 0, m = states.length; j < m; ++j) { const state = states[j]; const animatorStateData = this._getAnimatorStateData(state.name, state, animatorLayerData); - this._revertOriginValue(state, animatorStateData); + this._revertDefaultValue(state, animatorStateData); } } } this._clearPlayData(); } - + /** * Create a cross fade from the current state to another state. * @param stateName - The state name @@ -380,6 +383,7 @@ export class Animator extends Component { if (owner.crossCurveMark === crossCurveMark) { crossCurveData[owner.crossCurveIndex].destCurveIndex = i; } else { + owner.saveDefaultValue(); saveFixed && owner.saveFixedPoseValue(); owner.crossCurveMark = crossCurveMark; owner.crossCurveIndex = crossCurveData.length; @@ -515,7 +519,7 @@ export class Animator extends Component { let crossWeight = destPlayData.frameTime / (destState._getDuration() * layerData.crossFadeTransition.duration); crossWeight >= 1.0 && (crossWeight = 1.0); - + srcPlayData.update(); destPlayData.update(); @@ -740,11 +744,11 @@ export class Animator extends Component { } } - private _revertDefaultValue(playData: AnimatorStatePlayData) { - const { clip } = playData.state; + private _revertDefaultValue(state: AnimatorState, stateData: AnimatorStateData) { + const { clip } = state; if (clip) { const curves = clip._curveBindings; - const { curveOwners } = playData.stateData; + const { curveOwners } = stateData; for (let i = curves.length - 1; i >= 0; i--) { const owner = curveOwners[i]; const { transform } = owner.target; @@ -758,40 +762,10 @@ export class Animator extends Component { case AnimationProperty.Scale: transform.scale = owner.defaultValue; break; - case AnimationProperty.BlendShapeWeights: - const { blendShapeWeights } = owner.component; - for (let j = 0, length = blendShapeWeights.length; j < length; ++j) { - (owner.component).blendShapeWeights[j] = owner.defaultValue[j]; - } - break; - } - } - } - } - - private _revertOriginValue(state: AnimatorState, stateData: AnimatorStateData) { - const { clip } = state; - if (clip) { - const curves = clip._curveBindings; - const { curveOwners } = stateData; - for (let i = curves.length - 1; i >= 0; i--) { - const owner = curveOwners[i]; - if (!owner.hasSavedOriginValue) return; - const { transform } = owner.target; - switch (owner.property) { - case AnimationProperty.Position: - transform.position = owner.originValue; - break; - case AnimationProperty.Rotation: - transform.rotationQuaternion = owner.originValue; - break; - case AnimationProperty.Scale: - transform.scale = owner.originValue; - break; case AnimationProperty.BlendShapeWeights: const { blendShapeWeights } = owner.component; for (let j = 0, length = blendShapeWeights.length; j < length; ++j) { - (owner.component).blendShapeWeights[j] = owner.originValue[j]; + (owner.component).blendShapeWeights[j] = owner.defaultValue[j]; } break; } @@ -815,7 +789,7 @@ export class Animator extends Component { } } - private _crossFadeByTransition(transition: AnimatorStateTransition, layerIndex: number) { + private _crossFadeByTransition(transition: AnimatorStateTransition, layerIndex: number): void { const { name } = transition.destinationState; const animatorStateInfo = this._getAnimatorStateInfo(name, layerIndex, Animator._animatorInfo); const { state: crossState } = animatorStateInfo; @@ -836,8 +810,6 @@ export class Animator extends Component { const offset = duration * transition.offset; destPlayData.reset(crossState, animatorStateData, offset); - this._saveDefaultValues(animatorStateData); - switch (layerState) { // Maybe not play, maybe end. case LayerState.Standby: diff --git a/packages/core/src/animation/internal/AnimationCurveOwner.ts b/packages/core/src/animation/internal/AnimationCurveOwner.ts index 8ee9a0498e..6ac8d401b1 100644 --- a/packages/core/src/animation/internal/AnimationCurveOwner.ts +++ b/packages/core/src/animation/internal/AnimationCurveOwner.ts @@ -16,10 +16,8 @@ export class AnimationCurveOwner { readonly type: new (entity: Entity) => Component; readonly property: AnimationProperty; readonly component: Component; - readonly originValue: InterpolableValue; readonly defaultValue: InterpolableValue; readonly fixedPoseValue: InterpolableValue; - readonly hasSavedOriginValue: boolean = false; constructor(target: Entity, type: new (entity: Entity) => Component, property: AnimationProperty) { this.target = target; @@ -27,25 +25,21 @@ export class AnimationCurveOwner { this.property = property; switch (property) { case AnimationProperty.Position: - this.originValue = new Vector3(); this.defaultValue = new Vector3(); this.fixedPoseValue = new Vector3(); this.component = target.transform; break; case AnimationProperty.Rotation: - this.originValue = new Quaternion(); this.defaultValue = new Quaternion(); this.fixedPoseValue = new Quaternion(); this.component = target.transform; break; case AnimationProperty.Scale: - this.originValue = new Vector3(); this.defaultValue = new Vector3(); this.fixedPoseValue = new Vector3(); this.component = target.transform; break; case AnimationProperty.BlendShapeWeights: - this.originValue = new Float32Array(4); this.defaultValue = new Float32Array(4); this.fixedPoseValue = new Float32Array(4); this.component = target.getComponent(SkinnedMeshRenderer); @@ -53,31 +47,7 @@ export class AnimationCurveOwner { } } - saveOriginValue(): void { - switch (this.property) { - case AnimationProperty.Position: - this.target.transform.position.cloneTo(this.originValue); - break; - case AnimationProperty.Rotation: - this.target.transform.rotationQuaternion.cloneTo(this.originValue); - break; - case AnimationProperty.Scale: - this.target.transform.scale.cloneTo(this.originValue); - break; - case AnimationProperty.BlendShapeWeights: - const { blendShapeWeights } = this.component; - for (let i = 0, length = blendShapeWeights.length; i < length; ++i) { - this.originValue[i] = (this.component).blendShapeWeights[i]; - } - break; - } - this.hasSavedOriginValue = true; - } - saveDefaultValue(): void { - if (!this.hasSavedOriginValue) { - this.saveOriginValue(); - } switch (this.property) { case AnimationProperty.Position: this.target.transform.position.cloneTo(this.defaultValue); diff --git a/packages/loader/src/scene-loader/GLTFModel.ts b/packages/loader/src/scene-loader/GLTFModel.ts index f4a434639a..885076d48b 100644 --- a/packages/loader/src/scene-loader/GLTFModel.ts +++ b/packages/loader/src/scene-loader/GLTFModel.ts @@ -93,7 +93,8 @@ export class GLTFModel extends Component { this._animator.play(value, 0); } } else { - this._animator.reset(); + // @ts-ignore + this._animator._reset(); } } this._clipPreview = value; @@ -163,7 +164,8 @@ export class GLTFModel extends Component { this._animator.play(playStateName, 0); } } else { - this._animator.reset(); + // @ts-ignore + this._animator._reset(); } if (this._controllerUpdateFlag?.flag) { this._controllerUpdateFlag.flag = false; @@ -182,7 +184,8 @@ export class GLTFModel extends Component { if (defaultStateName) { animator.play(defaultStateName, i); } else { - animator.reset(); + // @ts-ignore + animator._reset(); } if (this._controllerUpdateFlag?.flag) { this._controllerUpdateFlag.flag = false;