@lijuhong1981/three.utils 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +9 -0
- package/API.md +179 -3
- package/README.md +22 -0
- package/index.js +6 -0
- package/package.json +37 -0
- package/src/intersect.js +90 -0
- package/src/setColorValue.js +25 -0
- package/src/setValues.js +36 -0
- package/src/setVectorValue.js +26 -0
- package/src/traverse.js +19 -0
- package/src/traverseVisible.js +21 -0
package/API.md
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
## Functions
|
|
2
|
+
|
|
3
|
+
<dl>
|
|
4
|
+
<dt><a href="#intersect">intersect(object, raycaster, [intersects], [recursive], [ignoreInvisible], [checkIgnore])</a> ⇒ <code><a href="#Intersection">Array.<Intersection></a></code></dt>
|
|
5
|
+
<dd><p>检测对象与射线的相交情况,并将结果存储到相交结果数组中</p>
|
|
6
|
+
<ul>
|
|
7
|
+
<li>该方法基于threejs内部的intersect方法实现,增加了ignoreInvisible参数和checkIgnore回调函数参数,用于在检测前检查是否需要跳过该对象,以提升性能。</li>
|
|
8
|
+
</ul>
|
|
9
|
+
</dd>
|
|
10
|
+
<dt><a href="#intersectObject">intersectObject(object, raycaster, [intersects], [recursive], [ignoreInvisible], [checkIgnore])</a> ⇒ <code><a href="#Intersection">Array.<Intersection></a></code></dt>
|
|
11
|
+
<dd><p>检查一个对象与射线的相交情况,并按由近到远顺序返回所有相交结果数组</p>
|
|
12
|
+
</dd>
|
|
13
|
+
<dt><a href="#intersectObjects">intersectObjects(objects, raycaster, [intersects], [recursive], [ignoreInvisible], [checkIgnore])</a> ⇒ <code><a href="#Intersection">Array.<Intersection></a></code></dt>
|
|
14
|
+
<dd><p>检查多个对象与射线的相交情况,并按由近到远顺序返回所有相交结果数组</p>
|
|
15
|
+
</dd>
|
|
16
|
+
<dt><a href="#setColorValue">setColorValue(color, value, [colorSpace])</a> ⇒ <code>Color</code></dt>
|
|
17
|
+
<dd><p>为threejs的Color对象赋值</p>
|
|
18
|
+
</dd>
|
|
19
|
+
<dt><a href="#setValues">setValues(target, values)</a></dt>
|
|
20
|
+
<dd><p>设置threejs的Obejct3D对象参数</p>
|
|
21
|
+
</dd>
|
|
22
|
+
<dt><a href="#setVectorValue">setVectorValue(vector, value)</a> ⇒ <code>Vector2</code> | <code>Vector3</code> | <code>Vector4</code> | <code>Euler</code> | <code>Quaternion</code> | <code>Spherical</code></dt>
|
|
23
|
+
<dd><p>为threejs的Vector2或Vector3或Vector4或Euler或Quaternion或Spherical对象赋值</p>
|
|
24
|
+
</dd>
|
|
25
|
+
<dt><a href="#traverse">traverse(object, callback)</a></dt>
|
|
26
|
+
<dd><p>遍历对象及其子对象,执行回调函数</p>
|
|
27
|
+
<ul>
|
|
28
|
+
<li>与threejs的Object3D.traverse方法类似,但增加了一个回调函数返回参数,用于在遍历过程中检查是否需要停止遍历该对象的子对象,以提升性能。</li>
|
|
29
|
+
</ul>
|
|
30
|
+
</dd>
|
|
31
|
+
<dt><a href="#traverseVisible">traverseVisible(object, callback)</a></dt>
|
|
32
|
+
<dd><p>遍历对象及其子对象中可见的对象,执行回调函数</p>
|
|
33
|
+
<ul>
|
|
34
|
+
<li>与threejs的Object3D.traverse方法类似,但增加了一个回调函数返回参数,用于在遍历过程中检查是否需要停止遍历该对象的子对象,以提升性能。</li>
|
|
35
|
+
</ul>
|
|
36
|
+
</dd>
|
|
37
|
+
</dl>
|
|
38
|
+
|
|
39
|
+
## Typedefs
|
|
40
|
+
|
|
41
|
+
<dl>
|
|
42
|
+
<dt><a href="#Intersection">Intersection</a> : <code>Object</code></dt>
|
|
43
|
+
<dd><p>The intersection point of a raycaster intersection test.</p>
|
|
44
|
+
</dd>
|
|
45
|
+
</dl>
|
|
46
|
+
|
|
47
|
+
<a name="intersect"></a>
|
|
48
|
+
|
|
49
|
+
## intersect(object, raycaster, [intersects], [recursive], [ignoreInvisible], [checkIgnore]) ⇒ [<code>Array.<Intersection></code>](#Intersection)
|
|
50
|
+
检测对象与射线的相交情况,并将结果存储到相交结果数组中
|
|
-
|
|
|
51
|
+
|
|
52
|
+
**Kind**: global function
|
|
53
|
+
**Returns**: [<code>Array.<Intersection></code>](#Intersection) - 返回相交结果数组,如果没有相交对象,则返回空数组
|
|
54
|
+
|
|
55
|
+
| Param | Type | Default | Description |
|
|
56
|
+
| --- | --- | --- | --- |
|
|
57
|
+
| object | <code>Object3D</code> | | 要检测的对象实例 |
|
|
58
|
+
| raycaster | <code>Raycaster</code> | | 射线发射器对象 |
|
|
59
|
+
| [intersects] | <code>Array</code> | | 存储相交结果的数组,可不传 |
|
|
60
|
+
| [recursive] | <code>boolean</code> | <code>true</code> | 是否递归检测子对象,默认true |
|
|
61
|
+
| [ignoreInvisible] | <code>boolean</code> | <code>true</code> | 是否忽略不可见对象,默认true |
|
|
62
|
+
| [checkIgnore] | <code>function</code> | | 是否忽略对象的回调函数,参数为对象实例,返回true表示忽略该对象,false表示不忽略,可不传 |
|
|
63
|
+
|
|
64
|
+
<a name="intersectObject"></a>
|
|
65
|
+
|
|
66
|
+
## intersectObject(object, raycaster, [intersects], [recursive], [ignoreInvisible], [checkIgnore]) ⇒ [<code>Array.<Intersection></code>](#Intersection)
|
|
67
|
+
检查一个对象与射线的相交情况,并按由近到远顺序返回所有相交结果数组
|
|
68
|
+
|
|
69
|
+
**Kind**: global function
|
|
70
|
+
**Returns**: [<code>Array.<Intersection></code>](#Intersection) - 返回相交结果数组,按照距离从近到远排序,如果没有相交对象,则返回空数组
|
|
71
|
+
|
|
72
|
+
| Param | Type | Description |
|
|
73
|
+
| --- | --- | --- |
|
|
74
|
+
| object | <code>Object3D</code> | 要检测的对象实例 |
|
|
75
|
+
| raycaster | <code>Raycaster</code> | 射线发射器对象 |
|
|
76
|
+
| [intersects] | <code>Array</code> | 存储相交结果的数组,可不传 |
|
|
77
|
+
| [recursive] | <code>boolean</code> | 是否递归检测子对象,默认true |
|
|
78
|
+
| [ignoreInvisible] | <code>boolean</code> | 是否忽略不可见对象,默认true |
|
|
79
|
+
| [checkIgnore] | <code>function</code> | 是否忽略对象的回调函数,参数为对象实例,返回true表示忽略该对象,false表示不忽略,可不传 |
|
|
80
|
+
|
|
81
|
+
<a name="intersectObjects"></a>
|
|
82
|
+
|
|
83
|
+
## intersectObjects(objects, raycaster, [intersects], [recursive], [ignoreInvisible], [checkIgnore]) ⇒ [<code>Array.<Intersection></code>](#Intersection)
|
|
84
|
+
检查多个对象与射线的相交情况,并按由近到远顺序返回所有相交结果数组
|
|
85
|
+
|
|
86
|
+
**Kind**: global function
|
|
87
|
+
**Returns**: [<code>Array.<Intersection></code>](#Intersection) - 返回相交结果数组,按照距离从近到远排序,如果没有相交对象,则返回空数组
|
|
88
|
+
|
|
89
|
+
| Param | Type | Default | Description |
|
|
90
|
+
| --- | --- | --- | --- |
|
|
91
|
+
| objects | <code>Array.<Object3D></code> | | 要检测的对象实例数组 |
|
|
92
|
+
| raycaster | <code>Raycaster</code> | | 射线发射器对象 |
|
|
93
|
+
| [intersects] | <code>Array</code> | | 存储相交结果的数组,可不传 |
|
|
94
|
+
| [recursive] | <code>boolean</code> | <code>true</code> | 是否递归检测子对象,默认true |
|
|
95
|
+
| [ignoreInvisible] | <code>boolean</code> | <code>true</code> | 是否忽略不可见对象,默认true |
|
|
96
|
+
| [checkIgnore] | <code>function</code> | | 是否忽略对象的回调函数,参数为对象实例,返回true表示忽略该对象,false表示不忽略,可不传 |
|
|
97
|
+
|
|
98
|
+
<a name="setColorValue"></a>
|
|
99
|
+
|
|
100
|
+
## setColorValue(color, value, [colorSpace]) ⇒ <code>Color</code>
|
|
101
|
+
为threejs的Color对象赋值
|
|
102
|
+
|
|
103
|
+
**Kind**: global function
|
|
104
|
+
|
|
105
|
+
| Param | Type | Description |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| color | <code>Color</code> | 要赋值的Color对象实例 |
|
|
108
|
+
| value | <code>Array.<number></code> \| <code>number</code> \| <code>string</code> \| <code>Color</code> | 颜色值,可以是一个包含RGB分量的数组(如[1, 0, 0]表示红色),一个十六进制数(如0xff0000表示红色),一个CSS颜色字符串(如"red"或"#ff0000"表示红色),或者另一个Color对象实例 |
|
|
109
|
+
| [colorSpace] | <code>string</code> | 颜色空间,可选值为"srgb"或"srgb-linear",仅当value为number或string时有效,表示输入的颜色值是sRGB空间还是线性空间,默认为"sRGB" |
|
|
110
|
+
|
|
111
|
+
<a name="setValues"></a>
|
|
112
|
+
|
|
113
|
+
## setValues(target, values)
|
|
114
|
+
设置threejs的Obejct3D对象参数
|
|
115
|
+
|
|
116
|
+
**Kind**: global function
|
|
117
|
+
|
|
118
|
+
| Param | Type | Description |
|
|
119
|
+
| --- | --- | --- |
|
|
120
|
+
| target | <code>Object3D</code> | 要设置参数的Object3D对象实例 |
|
|
121
|
+
| values | <code>object</code> | 参数对象,包含要设置的参数名称和对应的值 |
|
|
122
|
+
|
|
123
|
+
<a name="setVectorValue"></a>
|
|
124
|
+
|
|
125
|
+
## setVectorValue(vector, value) ⇒ <code>Vector2</code> \| <code>Vector3</code> \| <code>Vector4</code> \| <code>Euler</code> \| <code>Quaternion</code> \| <code>Spherical</code>
|
|
126
|
+
为threejs的Vector2或Vector3或Vector4或Euler或Quaternion或Spherical对象赋值
|
|
127
|
+
|
|
128
|
+
**Kind**: global function
|
|
129
|
+
|
|
130
|
+
| Param | Type | Description |
|
|
131
|
+
| --- | --- | --- |
|
|
132
|
+
| vector | <code>Vector2</code> \| <code>Vector3</code> \| <code>Vector4</code> \| <code>Euler</code> \| <code>Quaternion</code> \| <code>Spherical</code> | 要赋值的Vector2或Vector3或Vector4或Euler或Quaternion或Spherical对象实例 |
|
|
133
|
+
| value | <code>Array.<number></code> \| <code>object</code> \| <code>number</code> | 向量值,可以是一个包含分量的数组(如[1, 2, 3]表示一个三维向量),或者另一个具有相同属性的对象实例(如{ x: 1, y: 2, z: 3 }表示一个三维向量),或者一个单一的数字(如0表示所有分量都设置为0) |
|
|
134
|
+
|
|
135
|
+
<a name="traverse"></a>
|
|
136
|
+
|
|
137
|
+
## traverse(object, callback)
|
|
138
|
+
遍历对象及其子对象,执行回调函数
|
|
1
|
-
|
|
139
|
+
|
|
140
|
+
**Kind**: global function
|
|
141
|
+
|
|
142
|
+
| Param | Type | Description |
|
|
143
|
+
| --- | --- | --- |
|
|
144
|
+
| object | <code>Object3D</code> | 要遍历的对象实例 |
|
|
145
|
+
| callback | <code>function</code> | 回调函数,参数为当前遍历的对象实例,返回true表示停止遍历该对象的子对象 |
|
|
146
|
+
|
|
147
|
+
<a name="traverseVisible"></a>
|
|
148
|
+
|
|
149
|
+
## traverseVisible(object, callback)
|
|
150
|
+
遍历对象及其子对象中可见的对象,执行回调函数
|
|
2
|
-
|
|
151
|
+
|
|
152
|
+
**Kind**: global function
|
|
153
|
+
|
|
154
|
+
| Param | Type | Description |
|
|
155
|
+
| --- | --- | --- |
|
|
156
|
+
| object | <code>Object3D</code> | 要遍历的对象实例 |
|
|
157
|
+
| callback | <code>function</code> | 回调函数,参数为当前遍历的对象实例,返回true表示停止遍历该对象的子对象 |
|
|
158
|
+
|
|
159
|
+
<a name="Intersection"></a>
|
|
160
|
+
|
|
161
|
+
## Intersection : <code>Object</code>
|
|
162
|
+
The intersection point of a raycaster intersection test.
|
|
163
|
+
|
|
164
|
+
**Kind**: global typedef
|
|
165
|
+
**Properties**
|
|
166
|
+
|
|
167
|
+
| Name | Type | Description |
|
|
168
|
+
| --- | --- | --- |
|
|
169
|
+
| distance | <code>number</code> | The distance from the ray's origin to the intersection point. |
|
|
170
|
+
| distanceToRay | <code>number</code> | Some 3D objects e.g. [Points](Points) provide the distance of the intersection to the nearest point on the ray. For other objects it will be `undefined`. |
|
|
171
|
+
| point | <code>Vector3</code> | The intersection point, in world coordinates. |
|
|
172
|
+
| face | <code>Object</code> | The face that has been intersected. |
|
|
173
|
+
| faceIndex | <code>number</code> | The face index. |
|
|
174
|
+
| object | <code>Object3D</code> | The 3D object that has been intersected. |
|
|
175
|
+
| uv | <code>Vector2</code> | U,V coordinates at point of intersection. |
|
|
176
|
+
| uv1 | <code>Vector2</code> | Second set of U,V coordinates at point of intersection. |
|
|
177
|
+
| normal | <code>Vector3</code> | Interpolated normal vector at point of intersection. |
|
|
178
|
+
| instanceId | <code>number</code> | The index number of the instance where the ray intersects the [InstancedMesh](InstancedMesh). |
|
|
179
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# threeUtils
|
|
2
|
+
|
|
3
|
+
用来存放开发threejs过程中编写的一些小工具函数。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lijuhong1981/three.utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { traverse, setColorValue } from "@lijuhong1981/three.utils";
|
|
15
|
+
...
|
|
16
|
+
traverse(scene, (child)=>{
|
|
17
|
+
if (child.visible === false)
|
|
18
|
+
return true; //返回true表示中止遍历该对象的子对象
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## [API文档](./API.md)
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lijuhong1981/three.utils",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
13
|
+
"docs": "jsdoc2md --files src/*.js > API.md"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/lijuhong1981/threeUtils.git"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"three",
|
|
21
|
+
"utils",
|
|
22
|
+
"intersect",
|
|
23
|
+
"traverse"
|
|
24
|
+
],
|
|
25
|
+
"author": "lijuhong1981",
|
|
26
|
+
"license": "ISC",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/lijuhong1981/threeUtils/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/lijuhong1981/threeUtils#readme",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"three": ">=0.171.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"jsdoc-to-markdown": "^9.1.3"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/intersect.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { InstancedMesh, Object3D, Points, Raycaster } from "three";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The intersection point of a raycaster intersection test.
|
|
5
|
+
* @typedef {Object} Intersection
|
|
6
|
+
* @property {number} distance - The distance from the ray's origin to the intersection point.
|
|
7
|
+
* @property {number} distanceToRay - Some 3D objects e.g. {@link Points} provide the distance of the intersection to the nearest point on the ray. For other objects it will be `undefined`.
|
|
8
|
+
* @property {Vector3} point - The intersection point, in world coordinates.
|
|
9
|
+
* @property {Object} face - The face that has been intersected.
|
|
10
|
+
* @property {number} faceIndex - The face index.
|
|
11
|
+
* @property {Object3D} object - The 3D object that has been intersected.
|
|
12
|
+
* @property {Vector2} uv - U,V coordinates at point of intersection.
|
|
13
|
+
* @property {Vector2} uv1 - Second set of U,V coordinates at point of intersection.
|
|
14
|
+
* @property {Vector3} normal - Interpolated normal vector at point of intersection.
|
|
15
|
+
* @property {number} instanceId - The index number of the instance where the ray intersects the {@link InstancedMesh}.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 检测对象与射线的相交情况,并将结果存储到相交结果数组中
|
|
20
|
+
* - 该方法基于threejs内部的intersect方法实现,增加了ignoreInvisible参数和checkIgnore回调函数参数,用于在检测前检查是否需要跳过该对象,以提升性能。
|
|
21
|
+
* @param {Object3D} object - 要检测的对象实例
|
|
22
|
+
* @param {Raycaster} raycaster - 射线发射器对象
|
|
23
|
+
* @param {Array} [intersects] - 存储相交结果的数组,可不传
|
|
24
|
+
* @param {boolean} [recursive] - 是否递归检测子对象,默认true
|
|
25
|
+
* @param {boolean} [ignoreInvisible] - 是否忽略不可见对象,默认true
|
|
26
|
+
* @param {Function} [checkIgnore] - 是否忽略对象的回调函数,参数为对象实例,返回true表示忽略该对象,false表示不忽略,可不传
|
|
27
|
+
* @returns {Array<Intersection>} 返回相交结果数组,如果没有相交对象,则返回空数组
|
|
28
|
+
*/
|
|
29
|
+
function intersect(object, raycaster, intersects = [], recursive = true, ignoreInvisible = true, checkIgnore) {
|
|
30
|
+
if ((ignoreInvisible && !object.visible) ||
|
|
31
|
+
(typeof checkIgnore === 'function' && checkIgnore(object)))
|
|
32
|
+
return;
|
|
33
|
+
|
|
34
|
+
let propagate = true;
|
|
35
|
+
if (object.layers.test(raycaster.layers)) {
|
|
36
|
+
const result = object.raycast(raycaster, intersects);
|
|
37
|
+
if (result === false) propagate = false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (propagate === true && recursive === true) {
|
|
41
|
+
const children = object.children;
|
|
42
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
43
|
+
intersect(children[i], raycaster, intersects, true, ignoreInvisible, checkIgnore);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return intersects;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function ascSort(a, b) {
|
|
51
|
+
return a.distance - b.distance;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 检查一个对象与射线的相交情况,并按由近到远顺序返回所有相交结果数组
|
|
56
|
+
* @param {Object3D} object - 要检测的对象实例
|
|
57
|
+
* @param {Raycaster} raycaster - 射线发射器对象
|
|
58
|
+
* @param {Array} [intersects] - 存储相交结果的数组,可不传
|
|
59
|
+
* @param {boolean} [recursive] - 是否递归检测子对象,默认true
|
|
60
|
+
* @param {boolean} [ignoreInvisible] - 是否忽略不可见对象,默认true
|
|
61
|
+
* @param {Function} [checkIgnore] - 是否忽略对象的回调函数,参数为对象实例,返回true表示忽略该对象,false表示不忽略,可不传
|
|
62
|
+
* @returns {Array<Intersection>} 返回相交结果数组,按照距离从近到远排序,如果没有相交对象,则返回空数组
|
|
63
|
+
*/
|
|
64
|
+
function intersectObject(object, raycaster, intersects, recursive, ignoreInvisible, checkIgnore) {
|
|
65
|
+
intersect(object, raycaster, intersects, recursive, ignoreInvisible, checkIgnore);
|
|
66
|
+
intersects.sort(ascSort);
|
|
67
|
+
return intersects;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 检查多个对象与射线的相交情况,并按由近到远顺序返回所有相交结果数组
|
|
73
|
+
* @param {Array<Object3D>} objects - 要检测的对象实例数组
|
|
74
|
+
* @param {Raycaster} raycaster - 射线发射器对象
|
|
75
|
+
* @param {Array} [intersects] - 存储相交结果的数组,可不传
|
|
76
|
+
* @param {boolean} [recursive] - 是否递归检测子对象,默认true
|
|
77
|
+
* @param {boolean} [ignoreInvisible] - 是否忽略不可见对象,默认true
|
|
78
|
+
* @param {Function} [checkIgnore] - 是否忽略对象的回调函数,参数为对象实例,返回true表示忽略该对象,false表示不忽略,可不传
|
|
79
|
+
* @returns {Array<Intersection>} 返回相交结果数组,按照距离从近到远排序,如果没有相交对象,则返回空数组
|
|
80
|
+
*/
|
|
81
|
+
function intersectObjects(objects, raycaster, intersects = [], recursive = true, ignoreInvisible = true, checkIgnore) {
|
|
82
|
+
for (let i = 0, l = objects.length; i < l; i++) {
|
|
83
|
+
intersect(objects[i], raycaster, intersects, recursive, ignoreInvisible, checkIgnore);
|
|
84
|
+
}
|
|
85
|
+
intersects.sort(ascSort);
|
|
86
|
+
return intersects;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export default intersect;
|
|
90
|
+
export { intersect, intersectObject, intersectObjects };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Color } from "three";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 为threejs的Color对象赋值
|
|
5
|
+
* @param {Color} color - 要赋值的Color对象实例
|
|
6
|
+
* @param {Array<number>|number|string|Color} value - 颜色值,可以是一个包含RGB分量的数组(如[1, 0, 0]表示红色),一个十六进制数(如0xff0000表示红色),一个CSS颜色字符串(如"red"或"#ff0000"表示红色),或者另一个Color对象实例
|
|
7
|
+
* @param {string} [colorSpace] - 颜色空间,可选值为"srgb"或"srgb-linear",仅当value为number或string时有效,表示输入的颜色值是sRGB空间还是线性空间,默认为"sRGB"
|
|
8
|
+
* @returns {Color}
|
|
9
|
+
*/
|
|
10
|
+
function setColorValue(color, value, colorSpace) {
|
|
11
|
+
if (Array.isArray(value))
|
|
12
|
+
color.fromArray(value);
|
|
13
|
+
else if (typeof value === 'number')
|
|
14
|
+
color.setHex(value, colorSpace);
|
|
15
|
+
else if (typeof value === 'string')
|
|
16
|
+
color.setStyle(value, colorSpace);
|
|
17
|
+
else if (typeof value === 'object')
|
|
18
|
+
color.copy(value);
|
|
19
|
+
else
|
|
20
|
+
console.warn("The setColorValue error, unsupported value.", value);
|
|
21
|
+
return color;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default setColorValue;
|
|
25
|
+
export { setColorValue };
|
package/src/setValues.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Object3D } from "three";
|
|
2
|
+
import setColorValue from "./setColorValue.js";
|
|
3
|
+
import setVectorValue from "./setVectorValue.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 设置threejs的Obejct3D对象参数
|
|
7
|
+
* @param {Object3D} target - 要设置参数的Object3D对象实例
|
|
8
|
+
* @param {object} values - 参数对象,包含要设置的参数名称和对应的值
|
|
9
|
+
*/
|
|
10
|
+
function setValues(target, values) {
|
|
11
|
+
for (const key in values) {
|
|
12
|
+
if (values.hasOwnProperty(key) === false)
|
|
13
|
+
continue;
|
|
14
|
+
if (key in target) {
|
|
15
|
+
const newValue = values[key];
|
|
16
|
+
if (newValue === undefined) {
|
|
17
|
+
console.warn(`${values}: parameter '${key}' value is undefined.`);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const currentValue = target[key];
|
|
21
|
+
if (currentValue && currentValue.isColor) {
|
|
22
|
+
setColorValue(currentValue, newValue);
|
|
23
|
+
} else if (currentValue && (currentValue.isVector2 || currentValue.isVector3 || currentValue.isVector4 || currentValue.isEuler || currentValue.isQuaternion || currentValue.isSpherical)) {
|
|
24
|
+
setVectorValue(currentValue, newValue);
|
|
25
|
+
} else {
|
|
26
|
+
target[key] = newValue;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
console.warn(`${target}: parameter '${key}' is not exist.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default setValues;
|
|
35
|
+
export { setValues };
|
|
36
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Euler, Quaternion, Spherical, Vector2, Vector3, Vector4 } from "three";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 为threejs的Vector2或Vector3或Vector4或Euler或Quaternion或Spherical对象赋值
|
|
5
|
+
* @param {Vector2|Vector3|Vector4|Euler|Quaternion|Spherical} vector - 要赋值的Vector2或Vector3或Vector4或Euler或Quaternion或Spherical对象实例
|
|
6
|
+
* @param {Array<number>|object|number} value - 向量值,可以是一个包含分量的数组(如[1, 2, 3]表示一个三维向量),或者另一个具有相同属性的对象实例(如{ x: 1, y: 2, z: 3 }表示一个三维向量),或者一个单一的数字(如0表示所有分量都设置为0)
|
|
7
|
+
* @returns {Vector2|Vector3|Vector4|Euler|Quaternion|Spherical}
|
|
8
|
+
*/
|
|
9
|
+
function setVectorValue(vector, value) {
|
|
10
|
+
if (Array.isArray(value))
|
|
11
|
+
vector.set(...value);
|
|
12
|
+
else if (typeof value === 'number') {
|
|
13
|
+
if (vector.isEuler)
|
|
14
|
+
vector.set(value, value, value);
|
|
15
|
+
else
|
|
16
|
+
vector.set(value, value, value, value);
|
|
17
|
+
} else if (typeof value === 'object')
|
|
18
|
+
vector.copy(value);
|
|
19
|
+
else
|
|
20
|
+
console.warn("The setVectorValue error, unsupported value.", value);
|
|
21
|
+
return vector;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default setVectorValue;
|
|
25
|
+
export { setVectorValue };
|
|
26
|
+
|
package/src/traverse.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Object3D } from "three";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 遍历对象及其子对象,执行回调函数
|
|
5
|
+
* - 与threejs的Object3D.traverse方法类似,但增加了一个回调函数返回参数,用于在遍历过程中检查是否需要停止遍历该对象的子对象,以提升性能。
|
|
6
|
+
* @param {Object3D} object - 要遍历的对象实例
|
|
7
|
+
* @param {Function} callback - 回调函数,参数为当前遍历的对象实例,返回true表示停止遍历该对象的子对象
|
|
8
|
+
*/
|
|
9
|
+
function traverse(object, callback) {
|
|
10
|
+
if (callback(object) === true)
|
|
11
|
+
return;
|
|
12
|
+
const children = object.children;
|
|
13
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
14
|
+
traverse(children[i], callback);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default traverse;
|
|
19
|
+
export { traverse };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Object3D } from "three";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 遍历对象及其子对象中可见的对象,执行回调函数
|
|
5
|
+
* - 与threejs的Object3D.traverse方法类似,但增加了一个回调函数返回参数,用于在遍历过程中检查是否需要停止遍历该对象的子对象,以提升性能。
|
|
6
|
+
* @param {Object3D} object - 要遍历的对象实例
|
|
7
|
+
* @param {Function} callback - 回调函数,参数为当前遍历的对象实例,返回true表示停止遍历该对象的子对象
|
|
8
|
+
*/
|
|
9
|
+
function traverseVisible(object, callback) {
|
|
10
|
+
if (object.visible === false)
|
|
11
|
+
return;
|
|
12
|
+
if (callback(object) === true)
|
|
13
|
+
return;
|
|
14
|
+
const children = object.children;
|
|
15
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
16
|
+
traverseVisible(children[i], callback);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default traverseVisible;
|
|
21
|
+
export { traverseVisible };
|