Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
939d37e5
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,发现更多精彩内容 >>
未验证
提交
939d37e5
编写于
5月 16, 2018
作者:
M
Michael Herzog
提交者:
GitHub
5月 16, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14046 from Mugen87/dev7
ConvexObjectBreaker: Clean up
上级
cd97b90d
e0ec8599
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
77 addition
and
67 deletion
+77
-67
examples/js/ConvexObjectBreaker.js
examples/js/ConvexObjectBreaker.js
+77
-67
未找到文件。
examples/js/ConvexObjectBreaker.js
浏览文件 @
939d37e5
...
...
@@ -25,10 +25,10 @@
*
* @param {double} minSizeForBreak Min size a debris can have to break.
* @param {double} smallDelta Max distance to consider that a point belongs to a plane.
*
*
*/
THREE
.
ConvexObjectBreaker
=
function
(
minSizeForBreak
,
smallDelta
)
{
THREE
.
ConvexObjectBreaker
=
function
(
minSizeForBreak
,
smallDelta
)
{
this
.
minSizeForBreak
=
minSizeForBreak
||
1.4
;
this
.
smallDelta
=
smallDelta
||
0.0001
;
...
...
@@ -45,9 +45,7 @@ THREE.ConvexObjectBreaker = function( minSizeForBreak, smallDelta ) {
this
.
segments
=
[];
var
n
=
30
*
30
;
for
(
var
i
=
0
;
i
<
n
;
i
++
)
{
this
.
segments
[
i
]
=
false
;
}
for
(
var
i
=
0
;
i
<
n
;
i
++
)
this
.
segments
[
i
]
=
false
;
};
...
...
@@ -55,7 +53,7 @@ THREE.ConvexObjectBreaker.prototype = {
constructor
:
THREE
.
ConvexObjectBreaker
,
prepareBreakableObject
:
function
(
object
,
mass
,
velocity
,
angularVelocity
,
breakable
)
{
prepareBreakableObject
:
function
(
object
,
mass
,
velocity
,
angularVelocity
,
breakable
)
{
// object is a THREE.Object3d (normally a Mesh), must have a Geometry, and it must be convex.
// Its material property is propagated to its children (sub-pieces)
...
...
@@ -63,9 +61,7 @@ THREE.ConvexObjectBreaker.prototype = {
// Create vertices mark
var
vertices
=
object
.
geometry
.
vertices
;
for
(
var
i
=
0
,
il
=
vertices
.
length
;
i
<
il
;
i
++
)
{
vertices
[
i
].
mark
=
0
;
}
for
(
var
i
=
0
,
il
=
vertices
.
length
;
i
<
il
;
i
++
)
vertices
[
i
].
mark
=
0
;
var
userData
=
object
.
userData
;
userData
.
mass
=
mass
;
...
...
@@ -78,11 +74,10 @@ THREE.ConvexObjectBreaker.prototype = {
/*
* @param {int} maxRadialIterations Iterations for radial cuts.
* @param {int} maxRandomIterations Max random iterations for not-radial cuts
* @param {double} minSizeForRadialSubdivision Min size a debris can have to break in radial subdivision.
*
* Returns the array of pieces
*/
subdivideByImpact
:
function
(
object
,
pointOfImpact
,
normal
,
maxRadialIterations
,
maxRandomIterations
,
minSizeForRadialSubdivision
)
{
subdivideByImpact
:
function
(
object
,
pointOfImpact
,
normal
,
maxRadialIterations
,
maxRandomIterations
)
{
var
debris
=
[];
...
...
@@ -103,9 +98,9 @@ THREE.ConvexObjectBreaker.prototype = {
debris
.
push
(
subObject
);
return
;
}
var
angle
=
Math
.
PI
;
if
(
numIterations
===
0
)
{
...
...
@@ -113,19 +108,17 @@ THREE.ConvexObjectBreaker.prototype = {
tempPlane2
.
normal
.
copy
(
tempPlane1
.
normal
);
tempPlane2
.
constant
=
tempPlane1
.
constant
;
}
else
{
}
else
{
if
(
numIterations
<=
maxRadialIterations
)
{
angle
=
(
endAngle
-
startAngle
)
*
(
0.2
+
0.6
*
Math
.
random
()
)
+
startAngle
;
// Rotate tempPlane2 at impact point around normal axis and the angle
scope
.
tempVector3_2
.
copy
(
object
.
position
).
sub
(
pointOfImpact
).
applyAxisAngle
(
normal
,
angle
).
add
(
pointOfImpact
);
tempPlane2
.
setFromCoplanarPoints
(
pointOfImpact
,
scope
.
tempVector3
,
scope
.
tempVector3_2
);
}
else
{
}
else
{
angle
=
(
(
0.5
*
(
numIterations
&
1
)
)
+
0.2
*
(
2
-
Math
.
random
()
)
)
*
Math
.
PI
;
...
...
@@ -164,7 +157,7 @@ THREE.ConvexObjectBreaker.prototype = {
},
cutByPlane
:
function
(
object
,
plane
,
output
)
{
cutByPlane
:
function
(
object
,
plane
,
output
)
{
// Returns breakable objects in output.object1 and output.object2 members, the resulting 2 pieces of the cut.
// object2 can be null if the plane doesn't cut the object.
...
...
@@ -183,22 +176,18 @@ THREE.ConvexObjectBreaker.prototype = {
var
delta
=
this
.
smallDelta
;
// Reset vertices mark
for
(
var
i
=
0
;
i
<
numPoints
;
i
++
)
{
points
[
i
].
mark
=
0
;
}
for
(
var
i
=
0
;
i
<
numPoints
;
i
++
)
points
[
i
].
mark
=
0
;
// Reset segments mark
var
numPointPairs
=
numPoints
*
numPoints
;
for
(
var
i
=
0
;
i
<
numPointPairs
;
i
++
)
{
this
.
segments
[
i
]
=
false
;
}
for
(
var
i
=
0
;
i
<
numPointPairs
;
i
++
)
this
.
segments
[
i
]
=
false
;
// Iterate through the faces to mark edges shared by coplanar faces
for
(
var
i
=
0
,
il
=
faces
.
length
-
1
;
i
<
il
;
i
++
)
{
for
(
var
i
=
0
,
il
=
faces
.
length
-
1
;
i
<
il
;
i
++
)
{
var
face1
=
faces
[
i
];
for
(
var
j
=
i
+
1
,
jl
=
faces
.
length
;
j
<
jl
;
j
++
)
{
for
(
var
j
=
i
+
1
,
jl
=
faces
.
length
;
j
<
jl
;
j
++
)
{
var
face2
=
faces
[
j
];
...
...
@@ -215,18 +204,24 @@ THREE.ConvexObjectBreaker.prototype = {
if
(
a1
===
a2
||
a1
===
b2
||
a1
===
c2
)
{
if
(
b1
===
a2
||
b1
===
b2
||
b1
===
c2
)
{
this
.
segments
[
a1
*
numPoints
+
b1
]
=
true
;
this
.
segments
[
b1
*
numPoints
+
a1
]
=
true
;
}
else
{
}
else
{
this
.
segments
[
c1
*
numPoints
+
a1
]
=
true
;
this
.
segments
[
a1
*
numPoints
+
c1
]
=
true
;
}
}
else
if
(
b1
===
a2
||
b1
===
b2
||
b1
===
c2
)
{
}
else
if
(
b1
===
a2
||
b1
===
b2
||
b1
===
c2
)
{
this
.
segments
[
c1
*
numPoints
+
b1
]
=
true
;
this
.
segments
[
b1
*
numPoints
+
c1
]
=
true
;
}
}
...
...
@@ -245,17 +240,14 @@ THREE.ConvexObjectBreaker.prototype = {
var
face
=
faces
[
i
];
for
(
var
segment
=
0
;
segment
<
3
;
segment
++
)
{
for
(
var
segment
=
0
;
segment
<
3
;
segment
++
)
{
var
i0
=
segment
===
0
?
face
.
a
:
(
segment
===
1
?
face
.
b
:
face
.
c
);
var
i1
=
segment
===
0
?
face
.
b
:
(
segment
===
1
?
face
.
c
:
face
.
a
);
var
segmentState
=
this
.
segments
[
i0
*
numPoints
+
i1
];
if
(
segmentState
)
{
// The segment already has been processed in another face
continue
;
}
if
(
segmentState
)
continue
;
// The segment already has been processed in another face
// Mark segment as processed (also inverted segment)
this
.
segments
[
i0
*
numPoints
+
i1
]
=
true
;
...
...
@@ -270,19 +262,23 @@ THREE.ConvexObjectBreaker.prototype = {
// mark: 1 for negative side, 2 for positive side, 3 for coplanar point
if
(
d
>
delta
)
{
p0
.
mark
=
2
;
points2
.
push
(
p0
);
}
else
if
(
d
<
-
delta
)
{
}
else
if
(
d
<
-
delta
)
{
p0
.
mark
=
1
;
points1
.
push
(
p0
);
}
else
{
}
else
{
p0
.
mark
=
3
;
points1
.
push
(
p0
);
var
p0_2
=
p0
.
clone
();
p0_2
.
mark
=
3
;
points2
.
push
(
p0_2
);
}
}
...
...
@@ -293,19 +289,23 @@ THREE.ConvexObjectBreaker.prototype = {
// mark: 1 for negative side, 2 for positive side, 3 for coplanar point
if
(
d
>
delta
)
{
p1
.
mark
=
2
;
points2
.
push
(
p1
);
}
else
if
(
d
<
-
delta
)
{
}
else
if
(
d
<
-
delta
)
{
p1
.
mark
=
1
;
points1
.
push
(
p1
);
}
else
{
}
else
{
p1
.
mark
=
3
;
points1
.
push
(
p1
);
var
p1_2
=
p1
.
clone
();
p1_2
.
mark
=
3
;
points2
.
push
(
p1_2
);
}
}
...
...
@@ -319,13 +319,18 @@ THREE.ConvexObjectBreaker.prototype = {
this
.
tempLine1
.
start
.
copy
(
p0
);
this
.
tempLine1
.
end
.
copy
(
p1
);
var
intersection
=
localPlane
.
intersectLine
(
this
.
tempLine1
);
var
intersection
=
new
THREE
.
Vector3
();
intersection
=
localPlane
.
intersectLine
(
this
.
tempLine1
,
intersection
);
if
(
intersection
===
undefined
)
{
// Shouldn't happen
console
.
error
(
"
Internal error: segment does not intersect plane.
"
);
output
.
segmentedObject1
=
null
;
output
.
segmentedObject2
=
null
;
return
0
;
}
intersection
.
mark
=
1
;
...
...
@@ -347,33 +352,40 @@ THREE.ConvexObjectBreaker.prototype = {
this
.
tempCM1
.
set
(
0
,
0
,
0
);
var
radius1
=
0
;
var
numPoints1
=
points1
.
length
;
if
(
numPoints1
>
0
)
{
for
(
var
i
=
0
;
i
<
numPoints1
;
i
++
)
{
this
.
tempCM1
.
add
(
points1
[
i
]
);
}
for
(
var
i
=
0
;
i
<
numPoints1
;
i
++
)
this
.
tempCM1
.
add
(
points1
[
i
]
);
this
.
tempCM1
.
divideScalar
(
numPoints1
);
for
(
var
i
=
0
;
i
<
numPoints1
;
i
++
)
{
for
(
var
i
=
0
;
i
<
numPoints1
;
i
++
)
{
var
p
=
points1
[
i
];
p
.
sub
(
this
.
tempCM1
);
radius1
=
Math
.
max
(
radius1
,
p
.
x
,
p
.
y
,
p
.
z
);
}
this
.
tempCM1
.
add
(
object
.
position
);
}
this
.
tempCM2
.
set
(
0
,
0
,
0
);
var
radius2
=
0
;
var
numPoints2
=
points2
.
length
;
if
(
numPoints2
>
0
)
{
for
(
var
i
=
0
;
i
<
numPoints2
;
i
++
)
{
this
.
tempCM2
.
add
(
points2
[
i
]
);
}
for
(
var
i
=
0
;
i
<
numPoints2
;
i
++
)
this
.
tempCM2
.
add
(
points2
[
i
]
);
this
.
tempCM2
.
divideScalar
(
numPoints2
);
for
(
var
i
=
0
;
i
<
numPoints2
;
i
++
)
{
for
(
var
i
=
0
;
i
<
numPoints2
;
i
++
)
{
var
p
=
points2
[
i
];
p
.
sub
(
this
.
tempCM2
);
radius2
=
Math
.
max
(
radius2
,
p
.
x
,
p
.
y
,
p
.
z
);
}
this
.
tempCM2
.
add
(
object
.
position
);
}
var
object1
=
null
;
...
...
@@ -389,7 +401,7 @@ THREE.ConvexObjectBreaker.prototype = {
this
.
prepareBreakableObject
(
object1
,
newMass
,
object
.
userData
.
velocity
,
object
.
userData
.
angularVelocity
,
2
*
radius1
>
this
.
minSizeForBreak
);
numObjects
++
;
numObjects
++
;
}
...
...
@@ -401,11 +413,10 @@ THREE.ConvexObjectBreaker.prototype = {
this
.
prepareBreakableObject
(
object2
,
newMass
,
object
.
userData
.
velocity
,
object
.
userData
.
angularVelocity
,
2
*
radius2
>
this
.
minSizeForBreak
);
numObjects
++
;
numObjects
++
;
}
output
.
object1
=
object1
;
output
.
object2
=
object2
;
...
...
@@ -415,7 +426,7 @@ THREE.ConvexObjectBreaker.prototype = {
};
THREE
.
ConvexObjectBreaker
.
transformFreeVector
=
function
(
v
,
m
)
{
THREE
.
ConvexObjectBreaker
.
transformFreeVector
=
function
(
v
,
m
)
{
// input:
// vector interpreted as a free vector
...
...
@@ -424,15 +435,15 @@ THREE.ConvexObjectBreaker.transformFreeVector = function( v, m ) {
var
x
=
v
.
x
,
y
=
v
.
y
,
z
=
v
.
z
;
var
e
=
m
.
elements
;
v
.
x
=
e
[
0
]
*
x
+
e
[
4
]
*
y
+
e
[
8
]
*
z
;
v
.
y
=
e
[
1
]
*
x
+
e
[
5
]
*
y
+
e
[
9
]
*
z
;
v
.
x
=
e
[
0
]
*
x
+
e
[
4
]
*
y
+
e
[
8
]
*
z
;
v
.
y
=
e
[
1
]
*
x
+
e
[
5
]
*
y
+
e
[
9
]
*
z
;
v
.
z
=
e
[
2
]
*
x
+
e
[
6
]
*
y
+
e
[
10
]
*
z
;
return
v
;
};
THREE
.
ConvexObjectBreaker
.
transformFreeVectorInverse
=
function
(
v
,
m
)
{
THREE
.
ConvexObjectBreaker
.
transformFreeVectorInverse
=
function
(
v
,
m
)
{
// input:
// vector interpreted as a free vector
...
...
@@ -441,15 +452,15 @@ THREE.ConvexObjectBreaker.transformFreeVectorInverse = function( v, m ) {
var
x
=
v
.
x
,
y
=
v
.
y
,
z
=
v
.
z
;
var
e
=
m
.
elements
;
v
.
x
=
e
[
0
]
*
x
+
e
[
1
]
*
y
+
e
[
2
]
*
z
;
v
.
y
=
e
[
4
]
*
x
+
e
[
5
]
*
y
+
e
[
6
]
*
z
;
v
.
x
=
e
[
0
]
*
x
+
e
[
1
]
*
y
+
e
[
2
]
*
z
;
v
.
y
=
e
[
4
]
*
x
+
e
[
5
]
*
y
+
e
[
6
]
*
z
;
v
.
z
=
e
[
8
]
*
x
+
e
[
9
]
*
y
+
e
[
10
]
*
z
;
return
v
;
};
THREE
.
ConvexObjectBreaker
.
transformTiedVectorInverse
=
function
(
v
,
m
)
{
THREE
.
ConvexObjectBreaker
.
transformTiedVectorInverse
=
function
(
v
,
m
)
{
// input:
// vector interpreted as a tied (ordinary) vector
...
...
@@ -458,18 +469,17 @@ THREE.ConvexObjectBreaker.transformTiedVectorInverse = function( v, m ) {
var
x
=
v
.
x
,
y
=
v
.
y
,
z
=
v
.
z
;
var
e
=
m
.
elements
;
v
.
x
=
e
[
0
]
*
x
+
e
[
1
]
*
y
+
e
[
2
]
*
z
-
e
[
12
];
v
.
y
=
e
[
4
]
*
x
+
e
[
5
]
*
y
+
e
[
6
]
*
z
-
e
[
13
];
v
.
x
=
e
[
0
]
*
x
+
e
[
1
]
*
y
+
e
[
2
]
*
z
-
e
[
12
];
v
.
y
=
e
[
4
]
*
x
+
e
[
5
]
*
y
+
e
[
6
]
*
z
-
e
[
13
];
v
.
z
=
e
[
8
]
*
x
+
e
[
9
]
*
y
+
e
[
10
]
*
z
-
e
[
14
];
return
v
;
};
THREE
.
ConvexObjectBreaker
.
transformPlaneToLocalSpace
=
function
()
{
THREE
.
ConvexObjectBreaker
.
transformPlaneToLocalSpace
=
function
()
{
var
v1
=
new
THREE
.
Vector3
();
var
m1
=
new
THREE
.
Matrix3
();
return
function
transformPlaneToLocalSpace
(
plane
,
m
,
resultPlane
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录