Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
4f74c90d
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,发现更多精彩内容 >>
未验证
提交
4f74c90d
编写于
10月 25, 2018
作者:
M
Mr.doob
提交者:
GitHub
10月 25, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14508 from HypnosNova/BoxSelection
Add boxselection demo
上级
152d5e33
b651de5c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
394 addition
and
0 deletion
+394
-0
examples/files.js
examples/files.js
+1
-0
examples/js/interactive/SelectionBox.js
examples/js/interactive/SelectionBox.js
+116
-0
examples/js/interactive/SelectionHelper.js
examples/js/interactive/SelectionHelper.js
+73
-0
examples/webgl_interactive_boxselection.html
examples/webgl_interactive_boxselection.html
+204
-0
未找到文件。
examples/files.js
浏览文件 @
4f74c90d
...
...
@@ -54,6 +54,7 @@ var files = {
"
webgl_interactive_lines
"
,
"
webgl_interactive_points
"
,
"
webgl_interactive_raycasting_points
"
,
"
webgl_interactive_boxselection
"
,
"
webgl_interactive_voxelpainter
"
,
"
webgl_kinect
"
,
"
webgl_layers
"
,
...
...
examples/js/interactive/SelectionBox.js
0 → 100644
浏览文件 @
4f74c90d
/**
* @author HypnosNova / https://www.threejs.org.cn/gallery
* This is a class to check whether objects are in a selection area in 3D space
*/
function
SelectionBox
(
camera
,
scene
,
deep
)
{
this
.
camera
=
camera
;
this
.
scene
=
scene
;
this
.
startPoint
=
new
THREE
.
Vector3
();
this
.
endPoint
=
new
THREE
.
Vector3
();
this
.
collection
=
[];
this
.
deep
=
deep
||
Number
.
MAX_VALUE
;
}
SelectionBox
.
prototype
.
select
=
function
(
startPoint
,
endPoint
)
{
this
.
startPoint
=
startPoint
||
this
.
startPoint
;
this
.
endPoint
=
endPoint
||
this
.
endPoint
;
this
.
collection
=
[];
var
boxSelectionFrustum
=
this
.
createFrustum
(
this
.
startPoint
,
this
.
endPoint
);
this
.
searchChildInFrustum
(
boxSelectionFrustum
,
this
.
scene
);
return
this
.
collection
;
}
SelectionBox
.
prototype
.
createFrustum
=
function
(
startPoint
,
endPoint
)
{
startPoint
=
startPoint
||
this
.
startPoint
;
endPoint
=
endPoint
||
this
.
endPoint
this
.
camera
.
updateProjectionMatrix
();
this
.
camera
.
updateMatrixWorld
();
this
.
camera
.
updateMatrix
();
var
tmpPoint
=
startPoint
.
clone
();
tmpPoint
.
x
=
Math
.
min
(
startPoint
.
x
,
endPoint
.
x
);
tmpPoint
.
y
=
Math
.
max
(
startPoint
.
y
,
endPoint
.
y
);
endPoint
.
x
=
Math
.
max
(
startPoint
.
x
,
endPoint
.
x
);
endPoint
.
y
=
Math
.
min
(
startPoint
.
y
,
endPoint
.
y
);
var
vecNear
=
this
.
camera
.
position
.
clone
();
var
vecTopLeft
=
tmpPoint
.
clone
();
var
vecTopRight
=
new
THREE
.
Vector3
(
endPoint
.
x
,
tmpPoint
.
y
,
0
);
var
vecDownRight
=
endPoint
.
clone
();
var
vecDownLeft
=
new
THREE
.
Vector3
(
tmpPoint
.
x
,
endPoint
.
y
,
0
);
vecTopLeft
.
unproject
(
this
.
camera
);
vecTopRight
.
unproject
(
this
.
camera
);
vecDownRight
.
unproject
(
this
.
camera
);
vecDownLeft
.
unproject
(
this
.
camera
);
var
vectemp1
=
vecTopLeft
.
clone
().
sub
(
vecNear
);
var
vectemp2
=
vecTopRight
.
clone
().
sub
(
vecNear
);
var
vectemp3
=
vecDownRight
.
clone
().
sub
(
vecNear
);
vectemp1
.
normalize
();
vectemp2
.
normalize
();
vectemp3
.
normalize
();
vectemp1
.
multiplyScalar
(
this
.
deep
);
vectemp2
.
multiplyScalar
(
this
.
deep
);
vectemp3
.
multiplyScalar
(
this
.
deep
);
vectemp1
.
add
(
vecNear
);
vectemp2
.
add
(
vecNear
);
vectemp3
.
add
(
vecNear
);
var
planeTop
=
new
THREE
.
Plane
();
planeTop
.
setFromCoplanarPoints
(
vecNear
,
vecTopLeft
,
vecTopRight
);
var
planeRight
=
new
THREE
.
Plane
();
planeRight
.
setFromCoplanarPoints
(
vecNear
,
vecTopRight
,
vecDownRight
);
var
planeDown
=
new
THREE
.
Plane
();
planeDown
.
setFromCoplanarPoints
(
vecDownRight
,
vecDownLeft
,
vecNear
);
var
planeLeft
=
new
THREE
.
Plane
();
planeLeft
.
setFromCoplanarPoints
(
vecDownLeft
,
vecTopLeft
,
vecNear
);
var
planeFront
=
new
THREE
.
Plane
();
planeFront
.
setFromCoplanarPoints
(
vecTopRight
,
vecDownRight
,
vecDownLeft
);
var
planeBack
=
new
THREE
.
Plane
();
planeBack
.
setFromCoplanarPoints
(
vectemp3
,
vectemp2
,
vectemp1
);
planeBack
.
normal
=
planeBack
.
normal
.
multiplyScalar
(
-
1
);
return
new
THREE
.
Frustum
(
planeTop
,
planeRight
,
planeDown
,
planeLeft
,
planeFront
,
planeBack
);
}
SelectionBox
.
prototype
.
searchChildInFrustum
=
function
(
frustum
,
object
)
{
if
(
object
instanceof
THREE
.
Mesh
)
{
if
(
object
.
material
!==
undefined
)
{
object
.
geometry
.
computeBoundingSphere
();
var
center
=
object
.
geometry
.
boundingSphere
.
center
.
clone
().
applyMatrix4
(
object
.
matrixWorld
);
if
(
frustum
.
containsPoint
(
center
)
)
{
this
.
collection
.
push
(
object
);
}
}
}
if
(
object
.
children
.
length
>
0
)
{
for
(
var
x
=
0
;
x
<
object
.
children
.
length
;
x
++
)
{
this
.
searchChildInFrustum
(
frustum
,
object
.
children
[
x
]
);
}
}
}
\ No newline at end of file
examples/js/interactive/SelectionHelper.js
0 → 100644
浏览文件 @
4f74c90d
function
SelectionHelper
(
selectionBox
,
renderer
,
cssClassName
)
{
this
.
element
=
document
.
createElement
(
"
div
"
);
this
.
element
.
classList
.
add
(
cssClassName
);
this
.
element
.
style
.
pointerEvents
=
"
none
"
;
this
.
renderer
=
renderer
;
this
.
startPoint
=
{
x
:
0
,
y
:
0
};
this
.
pointTopLeft
=
{
x
:
0
,
y
:
0
};
this
.
pointBottomRight
=
{
x
:
0
,
y
:
0
};
this
.
isDown
=
false
;
this
.
renderer
.
domElement
.
addEventListener
(
"
mousedown
"
,
function
(
event
)
{
this
.
isDown
=
true
;
this
.
onSelectStart
(
event
);
}.
bind
(
this
),
false
);
this
.
renderer
.
domElement
.
addEventListener
(
"
mousemove
"
,
function
(
event
)
{
if
(
this
.
isDown
)
{
this
.
onSelectMove
(
event
);
}
}.
bind
(
this
),
false
);
this
.
renderer
.
domElement
.
addEventListener
(
"
mouseup
"
,
function
(
event
)
{
this
.
isDown
=
false
;
this
.
onSelectOver
(
event
);
}.
bind
(
this
),
false
);
}
SelectionHelper
.
prototype
.
onSelectStart
=
function
(
event
)
{
this
.
renderer
.
domElement
.
parentElement
.
appendChild
(
this
.
element
);
this
.
element
.
style
.
left
=
event
.
clientX
+
"
px
"
;
this
.
element
.
style
.
top
=
event
.
clientY
+
"
px
"
;
this
.
element
.
style
.
width
=
"
0px
"
;
this
.
element
.
style
.
height
=
"
0px
"
;
this
.
startPoint
.
x
=
event
.
clientX
;
this
.
startPoint
.
y
=
event
.
clientY
;
}
SelectionHelper
.
prototype
.
onSelectMove
=
function
(
event
)
{
this
.
pointBottomRight
.
x
=
Math
.
max
(
this
.
startPoint
.
x
,
event
.
clientX
);
this
.
pointBottomRight
.
y
=
Math
.
max
(
this
.
startPoint
.
y
,
event
.
clientY
);
this
.
pointTopLeft
.
x
=
Math
.
min
(
this
.
startPoint
.
x
,
event
.
clientX
);
this
.
pointTopLeft
.
y
=
Math
.
min
(
this
.
startPoint
.
y
,
event
.
clientY
);
this
.
element
.
style
.
left
=
this
.
pointTopLeft
.
x
+
"
px
"
;
this
.
element
.
style
.
top
=
this
.
pointTopLeft
.
y
+
"
px
"
;
this
.
element
.
style
.
width
=
(
this
.
pointBottomRight
.
x
-
this
.
pointTopLeft
.
x
)
+
"
px
"
;
this
.
element
.
style
.
height
=
(
this
.
pointBottomRight
.
y
-
this
.
pointTopLeft
.
y
)
+
"
px
"
;
}
SelectionHelper
.
prototype
.
onSelectOver
=
function
(
event
)
{
this
.
element
.
parentElement
.
removeChild
(
this
.
element
);
}
examples/webgl_interactive_boxselection.html
0 → 100644
浏览文件 @
4f74c90d
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<title>
three.js webgl - draggable cubes
</title>
<meta
charset=
"utf-8"
>
<meta
name=
"viewport"
content=
"width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
>
<style>
body
{
font-family
:
Monospace
;
background-color
:
#f0f0f0
;
margin
:
0px
;
overflow
:
hidden
;
}
.selectBox
{
border
:
1px
solid
#55aaff
;
background-color
:
rgba
(
75
,
160
,
255
,
0.3
);
position
:
fixed
;
}
</style>
</head>
<body>
<script
src=
"../build/three.js"
></script>
<script
src=
"js/libs/stats.min.js"
></script>
<script
src=
"js/interactive/SelectionBox.js"
></script>
<script
src=
"js/interactive/SelectionHelper.js"
></script>
<script>
var
container
,
stats
;
var
camera
,
controls
,
scene
,
renderer
;
var
objects
=
[];
init
();
animate
();
function
init
()
{
container
=
document
.
createElement
(
'
div
'
);
document
.
body
.
appendChild
(
container
);
camera
=
new
THREE
.
PerspectiveCamera
(
70
,
window
.
innerWidth
/
window
.
innerHeight
,
1
,
5000
);
camera
.
position
.
z
=
1000
;
scene
=
new
THREE
.
Scene
();
scene
.
background
=
new
THREE
.
Color
(
0xf0f0f0
);
scene
.
add
(
new
THREE
.
AmbientLight
(
0x505050
)
);
var
light
=
new
THREE
.
SpotLight
(
0xffffff
,
1.5
);
light
.
position
.
set
(
0
,
500
,
2000
);
light
.
angle
=
Math
.
PI
/
9
;
light
.
castShadow
=
true
;
light
.
shadow
.
camera
.
near
=
1000
;
light
.
shadow
.
camera
.
far
=
4000
;
light
.
shadow
.
mapSize
.
width
=
1024
;
light
.
shadow
.
mapSize
.
height
=
1024
;
scene
.
add
(
light
);
var
geometry
=
new
THREE
.
BoxBufferGeometry
(
20
,
20
,
20
);
for
(
var
i
=
0
;
i
<
200
;
i
++
)
{
var
object
=
new
THREE
.
Mesh
(
geometry
,
new
THREE
.
MeshLambertMaterial
(
{
color
:
Math
.
random
()
*
0xffffff
}
)
);
object
.
position
.
x
=
Math
.
random
()
*
1600
-
800
;
object
.
position
.
y
=
Math
.
random
()
*
900
-
450
;
object
.
position
.
z
=
Math
.
random
()
*
900
-
500
;
object
.
rotation
.
x
=
Math
.
random
()
*
2
*
Math
.
PI
;
object
.
rotation
.
y
=
Math
.
random
()
*
2
*
Math
.
PI
;
object
.
rotation
.
z
=
Math
.
random
()
*
2
*
Math
.
PI
;
object
.
scale
.
x
=
Math
.
random
()
*
2
+
1
;
object
.
scale
.
y
=
Math
.
random
()
*
2
+
1
;
object
.
scale
.
z
=
Math
.
random
()
*
2
+
1
;
object
.
castShadow
=
true
;
object
.
receiveShadow
=
true
;
scene
.
add
(
object
);
objects
.
push
(
object
);
}
renderer
=
new
THREE
.
WebGLRenderer
(
{
antialias
:
true
}
);
renderer
.
setPixelRatio
(
window
.
devicePixelRatio
);
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
renderer
.
shadowMap
.
enabled
=
true
;
renderer
.
shadowMap
.
type
=
THREE
.
PCFShadowMap
;
container
.
appendChild
(
renderer
.
domElement
);
var
info
=
document
.
createElement
(
'
div
'
);
info
.
style
.
position
=
'
absolute
'
;
info
.
style
.
top
=
'
10px
'
;
info
.
style
.
width
=
'
100%
'
;
info
.
style
.
textAlign
=
'
center
'
;
info
.
innerHTML
=
'
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - box selection
'
;
container
.
appendChild
(
info
);
stats
=
new
Stats
();
container
.
appendChild
(
stats
.
dom
);
window
.
addEventListener
(
'
resize
'
,
onWindowResize
,
false
);
}
function
onWindowResize
()
{
camera
.
aspect
=
window
.
innerWidth
/
window
.
innerHeight
;
camera
.
updateProjectionMatrix
();
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
}
//
function
animate
()
{
requestAnimationFrame
(
animate
);
render
();
stats
.
update
();
}
function
render
()
{
renderer
.
render
(
scene
,
camera
);
}
var
selectionBox
=
new
SelectionBox
(
camera
,
scene
);
var
helper
=
new
SelectionHelper
(
selectionBox
,
renderer
,
"
selectBox
"
);
document
.
addEventListener
(
"
mousedown
"
,
function
(
e
)
{
for
(
var
item
of
selectionBox
.
collection
)
{
item
.
material
.
emissive
=
new
THREE
.
Color
(
0x000000
);
}
selectionBox
.
startPoint
.
set
(
(
event
.
clientX
/
window
.
innerWidth
)
*
2
-
1
,
-
(
event
.
clientY
/
window
.
innerHeight
)
*
2
+
1
,
0.5
);
}
);
document
.
addEventListener
(
"
mousemove
"
,
function
(
e
)
{
if
(
helper
.
isDown
)
{
for
(
var
i
=
0
;
i
<
selectionBox
.
collection
.
length
;
i
++
)
{
selectionBox
.
collection
[
i
].
material
.
emissive
=
new
THREE
.
Color
(
0x000000
);
}
selectionBox
.
endPoint
.
set
(
(
event
.
clientX
/
window
.
innerWidth
)
*
2
-
1
,
-
(
event
.
clientY
/
window
.
innerHeight
)
*
2
+
1
,
0.5
);
var
allSelected
=
selectionBox
.
select
();
for
(
var
i
=
0
;
i
<
allSelected
.
length
;
i
++
)
{
allSelected
[
i
].
material
.
emissive
=
new
THREE
.
Color
(
0x0000ff
);
}
}
}
);
document
.
addEventListener
(
"
mouseup
"
,
function
(
e
)
{
selectionBox
.
endPoint
.
set
(
(
event
.
clientX
/
window
.
innerWidth
)
*
2
-
1
,
-
(
event
.
clientY
/
window
.
innerHeight
)
*
2
+
1
,
0.5
);
var
allSelected
=
selectionBox
.
select
();
for
(
var
i
=
0
;
i
<
allSelected
.
length
;
i
++
)
{
allSelected
[
i
].
material
.
emissive
=
new
THREE
.
Color
(
0x0000ff
);
}
});
</script>
</body>
</html>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录