> 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).

# 特殊类型

了解有哪些特殊类型，包括向量、四元数等。

## Vector3

Decentraland 使用 *Vector3* 数据来表示路径、空间中的点和方向。向量也可用于定义旋转方向，作为一种更友好的替代方案，替代 *四元数*。一个 Vector3 对象包含每个 *x*, *y*，以及 *到* 轴。

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

该 `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` 用于 *位置* 和 *缩放* 实体的值。

要创建一个 [自定义组件](/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"`

参见 [导入](/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*, *到*, *w*.

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

四元数与 [*欧拉* 角](https://en.wikipedia.org/wiki/Euler_angles)，这是更常见的 *x*, *y* 和 *到* 以 0 到 360 的数字表示的记法不同，这是大多数人熟悉的方式。引擎会将所有旋转都表示为四元数，因此尽可能避免进行转换到欧拉角或从欧拉角转换的计算是有意义的。

该 `Quaternion` 命名空间包含一系列实用方法，你可以调用它们来避免处理许多数学运算。输入 `Quaternion.`，VS Studio 将显示一个包含所有可用函数的下拉菜单。

下面几行展示了四元数一些基本操作的语法。

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

// 编辑其中一个值
myQuaternion.x = 1

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

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

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

{% hint style="info" %}
**💡 提示**：避免将这些转换作为系统内循环逻辑的一部分反复执行，因为这会产生较高开销。这些转换主要适用于一次性操作，例如设置新实体的旋转。
{% endhint %}

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

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

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

要创建一个 [自定义组件](/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"`

参见 [导入](/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"`

参见 [导入](/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
