> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/3d-nei-rong-ji-chu/special-types.md).

# 特殊类型

了解有哪些特殊类型，包括 Vector、Quaternion 等。

## Vector3

Decentraland 使用 *vector3* 数据来表示路径、空间中的点和方向。向量也可用于定义旋转方向，作为 *四元数*的更易用的替代方案。Vector3 对象包含以下各项的数值： *x*, *y*和 *z* 轴。

```ts
const myVector: Vector3 = { x: 8, y: 1, z: 8 }
```

&#x20;`Vector3`  命名空间包含一系列实用方法，调用这些方法可以省去大多数向量数学运算。输入 `Vector3.`，VS Studio 就会显示包含所有可用函数的下拉列表。

下面是几行代码，展示了一些基本向量运算的语法。

```ts
// 创建一个向量对象
let myVector = Vector3.create(3, 1, 5)

// 创建向量对象的另一种语法
let myOtherVector: Vector3 = { x: 8, y: 1, z: 8 }

// 修改其中一个值
myVector.x = 5

// 调用 Vector3 命名空间中的函数，
// 所有这些函数都要求在参数中传入 Vector3 对象

let normalizedVector = Vector3.normalize(myVector)

let distance = Vector3.distance(myVector, myOtherVector)

let midPoint = Vector3.lerp(myVector, myOtherVector, 0.5)
```

许多组件的字段中通常需要使用 Vector3 对象。例如， `Transform` 组件包含 `Vector3` 以下值： *位置* 和 *缩放* （用于该实体）。

要创建 [自定义组件](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jia-gou/custom-components.md) 对于需要 Vector3 值的参数，请将这些参数的类型设置为 `Schemas.Vector3`.

{% hint style="warning" %}
**📔 注意**: `Vector3` 必须通过以下方式导入：

> `import { Vector3 } from "@dcl/sdk/math"`

有关如何轻松处理这些内容，请参阅 [导入](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/ru-men/coding-scenes.md#imports) 。
{% endhint %}

### 编写方向向量的快捷方式

定义通用向量时，可以使用以下快捷方式：

* `Vector3.Zero()` 返回 *(0, 0, 0)*
* `Vector3.Up()` 返回 *(0, 1, 0)*
* `Vector3.Down()` 返回 *(0, -1, 0)*
* `Vector3.Left()` 返回 *(-1, 0, 0)*
* `Vector3.Right()` 返回 *(1, 0, 0)*
* `Vector3.Forward()` 返回 *(0, 0, 1)*
* `Vector3.Backward()` 返回 *(0, 0, -1)*

## 四元数

四元数用于存储 Transform 组件的旋转信息。一个四元数由四个介于 -1 和 1 之间的数值组成： *x*, *y*, *z*, *w*.

```ts
const myQuaternion: Quaternion = { x: 0, y: 0, z: 0, w: 1 }
```

四元数不同于 [*欧拉* 角](https://en.wikipedia.org/wiki/Euler_angles)，后者是更常见的 *x*, *y* 和 *z* 表示法，数值范围为 0 到 360，是大多数人熟悉的形式。引擎使用四元数表示所有旋转，因此应尽可能避免进行欧拉角与四元数之间的转换计算。

Quaternion `Quaternion` 命名空间包含一系列实用方法，可供调用，从而避免处理大量数学运算。输入 `Quaternion.`后，VS Studio 会显示包含所有可用函数的下拉列表。

下面用几行代码展示一些基本四元数运算的语法。

```ts
// 创建一个四元数对象
let myQuaternion = Quaternion.create(0, 0, 0, 1)

// 修改其中一个值
myQuaternion.x = 1

// 调用四元数命名空间中的函数
let midPoint = Quaternion.slerp(myQuaternion1, myQuaternion2, 0.5)

// 获取从一个朝向转到另一个朝向所需的旋转，
// 传入两个方向 Vector3 值
let rotation = Quaternion.fromToRotation(Vector3.Forward(), Vector3.Up())
```

由于用欧拉角度来思考要容易得多，SDK 提供了几个用于在四元数和欧拉角之间相互转换的函数。

{% hint style="info" %}
**💡 提示**：避免在每个 tick 都运行的系统的重复逻辑中执行这些转换，因为开销可能较大。这些转换主要适用于一次性操作，例如设置新实体的旋转。
{% endhint %}

```ts
// 从欧拉角转换为四元数
let myQuaternion = Quaternion.fromEulerDegrees(90, 0, 0)

// 从四元数转换为欧拉角
let myEuler = Quaternion.toEulerAngles(myQuaternion)
```

组件的字段中通常需要使用四元数对象。例如， `Transform` 组件包含 `Quaternion` 用于实体旋转的值。

要创建一个 [自定义组件](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jia-gou/custom-components.md) 其中包含需要使用 Quaternion 值的参数，请将这些参数的类型设置为 `Schemas.Quaternion`.

{% hint style="warning" %}
**📔 注意**: `Quaternion` 必须通过以下方式导入：

> `import { Quaternion } from "@dcl/sdk/math"`

有关如何轻松处理这些内容，请参阅 [导入](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/ru-men/coding-scenes.md#imports) 。
{% endhint %}

## 标量

标量不过是一个数字。因此，实例化一个 `Scalar` 对象来存储数据并没有太大意义，因为直接使用数字也能做到这一点。不过， `Scalar` 命名空间提供了若干实用函数（类似于 *Vector3* 命名空间中的函数），可用于处理数字。

```ts
// 调用 Scalar 类中的函数
let random = Scalar.randomRange(1, 100)

let midPoint = Scalar.lerp(number1, number2, 0.5)

let clampedValue = Scalar.clamp(myInput, 0, 100)
```

{% hint style="warning" %}
**📔 注意**: `Scalar` 必须通过以下方式导入：

> `import { Scalar } from "@dcl/sdk/math"`

有关如何轻松处理这些内容，请参阅 [导入](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/ru-men/coding-scenes.md#imports) 。
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/3d-nei-rong-ji-chu/special-types.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
