Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
56df7724
T
three.js
项目概览
Ablesons
/
three.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
three.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
56df7724
编写于
7月 31, 2011
作者:
A
alteredq
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extended GeometryUtils with methods for getting random points.
上级
bafa6ec1
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
734 addition
and
482 deletion
+734
-482
build/Three.js
build/Three.js
+322
-318
build/custom/ThreeExtras.js
build/custom/ThreeExtras.js
+168
-164
src/extras/GeometryUtils.js
src/extras/GeometryUtils.js
+244
-0
未找到文件。
build/Three.js
浏览文件 @
56df7724
因为 它太大了无法显示 source diff 。你可以改为
查看blob
。
build/custom/ThreeExtras.js
浏览文件 @
56df7724
此差异已折叠。
点击以展开。
src/extras/GeometryUtils.js
浏览文件 @
56df7724
...
...
@@ -179,6 +179,250 @@ THREE.GeometryUtils = {
return
cloneGeo
;
},
// Get random point in triangle (via barycentric coordinates)
// (uniform distribution)
// http://www.cgafaq.info/wiki/Random_Point_In_Triangle
randomPointInTriangle
:
function
(
vectorA
,
vectorB
,
vectorC
)
{
var
a
,
b
,
c
,
point
=
new
THREE
.
Vector3
(),
tmp
=
THREE
.
GeometryUtils
.
__v1
;
a
=
Math
.
random
();
b
=
Math
.
random
();
if
(
(
a
+
b
)
>
1
)
{
a
=
1
-
a
;
b
=
1
-
b
;
}
c
=
1
-
a
-
b
;
point
.
copy
(
vectorA
);
point
.
multiplyScalar
(
a
);
tmp
.
copy
(
vectorB
);
tmp
.
multiplyScalar
(
b
);
point
.
addSelf
(
tmp
);
tmp
.
copy
(
vectorC
);
tmp
.
multiplyScalar
(
c
);
point
.
addSelf
(
tmp
);
return
point
;
},
// Get random point in face (triangle / quad)
// (uniform distribution)
randomPointInFace
:
function
(
face
,
geometry
,
useCachedAreas
)
{
var
vA
,
vB
,
vC
,
vD
;
if
(
face
instanceof
THREE
.
Face3
)
{
vA
=
geometry
.
vertices
[
face
.
a
].
position
;
vB
=
geometry
.
vertices
[
face
.
b
].
position
;
vC
=
geometry
.
vertices
[
face
.
c
].
position
;
return
THREE
.
GeometryUtils
.
randomPointInTriangle
(
vA
,
vB
,
vC
);
}
else
if
(
face
instanceof
THREE
.
Face4
)
{
vA
=
geometry
.
vertices
[
face
.
a
].
position
;
vB
=
geometry
.
vertices
[
face
.
b
].
position
;
vC
=
geometry
.
vertices
[
face
.
c
].
position
;
vD
=
geometry
.
vertices
[
face
.
d
].
position
;
var
area1
,
area2
;
if
(
useCachedAreas
)
{
if
(
face
.
_area1
&&
face
.
_area2
)
{
area1
=
face
.
_area1
;
area2
=
face
.
_area2
;
}
else
{
area1
=
THREE
.
GeometryUtils
.
triangleArea
(
vA
,
vB
,
vD
);
area2
=
THREE
.
GeometryUtils
.
triangleArea
(
vB
,
vC
,
vD
);
face
.
_area1
=
area1
;
face
.
_area2
=
area2
;
}
}
else
{
area1
=
THREE
.
GeometryUtils
.
triangleArea
(
vA
,
vB
,
vD
),
area2
=
THREE
.
GeometryUtils
.
triangleArea
(
vB
,
vC
,
vD
);
}
var
r
=
Math
.
random
()
*
(
area1
+
area2
);
if
(
r
<
area1
)
{
return
THREE
.
GeometryUtils
.
randomPointInTriangle
(
vA
,
vB
,
vD
);
}
else
{
return
THREE
.
GeometryUtils
.
randomPointInTriangle
(
vB
,
vC
,
vD
);
}
}
},
// Get uniformly distributed random points in mesh
// - create array with cumulative sums of face areas
// - pick random number from 0 to total area
// - find corresponding place in area array by binary search
// - get random point in face
randomPointsInGeometry
:
function
(
geometry
,
n
)
{
var
face
,
i
,
faces
=
geometry
.
faces
,
vertices
=
geometry
.
vertices
,
il
=
faces
.
length
,
totalArea
=
0
,
cumulativeAreas
=
[],
vA
,
vB
,
vC
,
vD
;
// precompute face areas
for
(
i
=
0
;
i
<
il
;
i
++
)
{
face
=
faces
[
i
];
if
(
face
instanceof
THREE
.
Face3
)
{
vA
=
vertices
[
face
.
a
].
position
;
vB
=
vertices
[
face
.
b
].
position
;
vC
=
vertices
[
face
.
c
].
position
;
face
.
_area
=
THREE
.
GeometryUtils
.
triangleArea
(
vA
,
vB
,
vC
);
}
else
if
(
face
instanceof
THREE
.
Face4
)
{
vA
=
vertices
[
face
.
a
].
position
;
vB
=
vertices
[
face
.
b
].
position
;
vC
=
vertices
[
face
.
c
].
position
;
vD
=
vertices
[
face
.
d
].
position
;
face
.
_area1
=
THREE
.
GeometryUtils
.
triangleArea
(
vA
,
vB
,
vD
);
face
.
_area2
=
THREE
.
GeometryUtils
.
triangleArea
(
vB
,
vC
,
vD
);
face
.
_area
=
face
.
_area1
+
face
.
_area2
;
}
totalArea
+=
face
.
_area
;
cumulativeAreas
[
i
]
=
totalArea
;
}
// binary search cumulative areas array
function
binarySearchIndices
(
value
)
{
function
binarySearch
(
start
,
end
)
{
// return closest larger index
// if exact number is not found
if
(
end
<
start
)
return
start
;
var
mid
=
start
+
Math
.
floor
(
(
end
-
start
)
/
2
);
if
(
cumulativeAreas
[
mid
]
>
value
)
{
return
binarySearch
(
start
,
mid
-
1
);
}
else
if
(
cumulativeAreas
[
mid
]
<
value
)
{
return
binarySearch
(
mid
+
1
,
end
);
}
else
{
return
mid
;
}
}
var
result
=
binarySearch
(
0
,
cumulativeAreas
.
length
-
1
)
return
result
;
}
// pick random face weighted by face area
var
r
,
index
,
result
=
[];
var
stats
=
{};
for
(
i
=
0
;
i
<
n
;
i
++
)
{
r
=
Math
.
random
()
*
totalArea
;
index
=
binarySearchIndices
(
r
);
result
[
i
]
=
THREE
.
GeometryUtils
.
randomPointInFace
(
faces
[
index
],
geometry
,
true
);
if
(
!
stats
[
index
]
)
{
stats
[
index
]
=
1
;
}
else
{
stats
[
index
]
+=
1
;
}
}
return
result
;
},
// Get triangle area (by Heron's formula)
// http://en.wikipedia.org/wiki/Heron%27s_formula
triangleArea
:
function
(
vectorA
,
vectorB
,
vectorC
)
{
var
s
,
a
,
b
,
c
,
tmp
=
THREE
.
GeometryUtils
.
__v1
;
tmp
.
sub
(
vectorA
,
vectorB
);
a
=
tmp
.
length
();
tmp
.
sub
(
vectorA
,
vectorC
);
b
=
tmp
.
length
();
tmp
.
sub
(
vectorB
,
vectorC
);
c
=
tmp
.
length
();
s
=
0.5
*
(
a
+
b
+
c
);
return
Math
.
sqrt
(
s
*
(
s
-
a
)
*
(
s
-
b
)
*
(
s
-
c
)
);
}
};
THREE
.
GeometryUtils
.
__v1
=
new
THREE
.
Vector3
();
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录