Skip to content

Conversation

@shrinktofit
Copy link
Contributor

@shrinktofit shrinktofit commented Sep 20, 2025

Re: #

Changelog

Add following methods to normalize a vec3 and return its length.

  • Generic static form: Vec3.normalizeAndExtractLength(out, epsilon = 1e-8)
  • Instance form: Vec3.prototype.normalizeSelfAndExtractLength(epsilon = 1e-8)

Usage:

/**
 * Move `source` towards `target`.
 */
function moveTowards(source: Vec3, target: Vec3, maxMovement: number) {
  const dir = Vec3.sustract(new Vec3(), target, source);
  const distance = dir.normalizeSelfAndExtractLength();
  Vec3.scaleAndAdd(source, source, dir, Math.min(distance, maxMovement));
}

Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

@github-actions
Copy link

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1009805 bytes 1010039 bytes ⚠️ +234 bytes
2D All (legacy pipeline) 2675364 bytes 2675598 bytes ⚠️ +234 bytes
2D All (new pipeline) 2767100 bytes 2767334 bytes ⚠️ +234 bytes
(2D + 3D) All 10024070 bytes 10024304 bytes ⚠️ +234 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16843132 bytes 16843366 bytes ⚠️ +234 bytes

Interface Check Report

! WARNING this pull request has changed these public interfaces:

@@ -14732,8 +14732,16 @@
              * @zh 归一化向量,输入零向量将会返回零向量。
              */
             static normalize<Out extends IVec3Like>(out: Out, a: IVec3Like): Out;
             /**
+             * @en Normalizes the vector and returns the length of the vector before normalization.
+             * @zh 归一化向量并返回归一化前的长度。
+             * @param out 向量。
+             * @param epsilon 用于判定向量长度是否为 0 的阈值。
+             * @returns 归一化前的长度。
+             */
+            static normalizeAndExtractLength<Out extends IVec3Like>(out: Out, epsilon?: number): number;
+            /**
              * @en Calculates the dot product of the vector
              * @zh 向量点积(数量积)
              */
             static dot<Out extends IVec3Like>(a: Out, b: IVec3Like): number;
@@ -15123,10 +15131,17 @@
             /**
              * @en Normalize the current vector.
              * @zh 将当前向量归一化
              */
-            normalize(): Vec3;
+            normalize(epsilon?: number): Vec3;
             /**
+             * @en Normalize the current vector and return its length.
+             * @zh 将当前向量归一化并返回其长度。
+             * @param epsilon 用于判断向量是否为零的阈值。
+             * @returns 向量归一化前的长度(模)。
+             */
+            normalizeSelfAndExtractLength(epsilon?: number): number;
+            /**
              * @en Transforms the vec3 with a mat4. 4th vector component is implicitly '1'
              * @zh 将当前向量视为 w 分量为 1 的四维向量,应用四维矩阵变换到当前矩阵
              * @param matrix matrix to transform with
              */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant