Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Ablesons
three.js
提交
6485c641
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,发现更多精彩内容 >>
提交
6485c641
编写于
7月 04, 2017
作者:
M
Mr.doob
提交者:
GitHub
7月 04, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #11684 from Mugen87/dev2
STLLoader: Refactoring and clean up
上级
83cb6381
394d9bfb
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
133 addition
and
127 deletion
+133
-127
examples/js/loaders/STLLoader.js
examples/js/loaders/STLLoader.js
+133
-127
未找到文件。
examples/js/loaders/STLLoader.js
浏览文件 @
6485c641
...
...
@@ -56,10 +56,10 @@ THREE.STLLoader.prototype = {
parse
:
function
(
data
)
{
var
isBinary
=
function
(
)
{
function
isBinary
(
data
)
{
var
expect
,
face_size
,
n_faces
,
reader
;
reader
=
new
DataView
(
binD
ata
);
reader
=
new
DataView
(
d
ata
);
face_size
=
(
32
/
8
*
3
)
+
(
(
32
/
8
*
3
)
*
3
)
+
(
16
/
8
);
n_faces
=
reader
.
getUint32
(
80
,
true
);
expect
=
80
+
(
32
/
8
)
+
(
n_faces
*
face_size
);
...
...
@@ -75,6 +75,7 @@ THREE.STLLoader.prototype = {
// plentiful. So, check the first 5 bytes for 'solid'.
// US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd'
var
solid
=
[
115
,
111
,
108
,
105
,
100
];
for
(
var
i
=
0
;
i
<
5
;
i
++
)
{
...
...
@@ -87,230 +88,235 @@ THREE.STLLoader.prototype = {
}
// First 5 bytes read "solid"; declare it to be an ASCII STL
return
false
;
};
var
binData
=
this
.
ensureBinary
(
data
);
return
isBinary
()
?
this
.
parseBinary
(
binData
)
:
this
.
parseASCII
(
this
.
ensureString
(
data
)
);
}
},
function
parseBinary
(
data
)
{
parseBinary
:
function
(
data
)
{
var
reader
=
new
DataView
(
data
);
var
faces
=
reader
.
getUint32
(
80
,
true
);
var
reader
=
new
DataView
(
data
)
;
var
faces
=
reader
.
getUint32
(
80
,
true
)
;
var
r
,
g
,
b
,
hasColors
=
false
,
colors
;
var
defaultR
,
defaultG
,
defaultB
,
alpha
;
var
r
,
g
,
b
,
hasColors
=
false
,
colors
;
var
defaultR
,
defaultG
,
defaultB
,
alpha
;
// process STL header
// check for default color in header ("COLOR=rgba" sequence).
// process STL header
// check for default color in header ("COLOR=rgba" sequence).
for
(
var
index
=
0
;
index
<
80
-
10
;
index
++
)
{
for
(
var
index
=
0
;
index
<
80
-
10
;
index
++
)
{
if
(
(
reader
.
getUint32
(
index
,
false
)
==
0x434F4C4F
/*COLO*/
)
&&
(
reader
.
getUint8
(
index
+
4
)
==
0x52
/*'R'*/
)
&&
(
reader
.
getUint8
(
index
+
5
)
==
0x3D
/*'='*/
)
)
{
if
(
(
reader
.
getUint32
(
index
,
false
)
==
0x434F4C4F
/*COLO*/
)
&&
(
reader
.
getUint8
(
index
+
4
)
==
0x52
/*'R'*/
)
&&
(
reader
.
getUint8
(
index
+
5
)
==
0x3D
/*'='*/
)
)
{
hasColors
=
true
;
colors
=
[];
hasColors
=
true
;
colors
=
[];
defaultR
=
reader
.
getUint8
(
index
+
6
)
/
255
;
defaultG
=
reader
.
getUint8
(
index
+
7
)
/
255
;
defaultB
=
reader
.
getUint8
(
index
+
8
)
/
255
;
alpha
=
reader
.
getUint8
(
index
+
9
)
/
255
;
defaultR
=
reader
.
getUint8
(
index
+
6
)
/
255
;
defaultG
=
reader
.
getUint8
(
index
+
7
)
/
255
;
defaultB
=
reader
.
getUint8
(
index
+
8
)
/
255
;
alpha
=
reader
.
getUint8
(
index
+
9
)
/
255
;
}
}
}
var
dataOffset
=
84
;
var
faceLength
=
12
*
4
+
2
;
var
dataOffset
=
84
;
var
faceLength
=
12
*
4
+
2
;
var
geometry
=
new
THREE
.
BufferGeometry
();
var
geometry
=
new
THREE
.
BufferGeometry
();
var
vertices
=
[];
var
normals
=
[];
var
vertices
=
[];
var
normals
=
[];
for
(
var
face
=
0
;
face
<
faces
;
face
++
)
{
for
(
var
face
=
0
;
face
<
faces
;
face
++
)
{
var
start
=
dataOffset
+
face
*
faceLength
;
var
normalX
=
reader
.
getFloat32
(
start
,
true
);
var
normalY
=
reader
.
getFloat32
(
start
+
4
,
true
);
var
normalZ
=
reader
.
getFloat32
(
start
+
8
,
true
);
var
start
=
dataOffset
+
face
*
faceLength
;
var
normalX
=
reader
.
getFloat32
(
start
,
true
);
var
normalY
=
reader
.
getFloat32
(
start
+
4
,
true
);
var
normalZ
=
reader
.
getFloat32
(
start
+
8
,
true
);
if
(
hasColors
)
{
if
(
hasColors
)
{
var
packedColor
=
reader
.
getUint16
(
start
+
48
,
true
);
var
packedColor
=
reader
.
getUint16
(
start
+
48
,
true
);
if
(
(
packedColor
&
0x8000
)
===
0
)
{
if
(
(
packedColor
&
0x8000
)
===
0
)
{
// facet has its own unique color
// facet has its own unique color
r
=
(
packedColor
&
0x1F
)
/
31
;
g
=
(
(
packedColor
>>
5
)
&
0x1F
)
/
31
;
b
=
(
(
packedColor
>>
10
)
&
0x1F
)
/
31
;
r
=
(
packedColor
&
0x1F
)
/
31
;
g
=
(
(
packedColor
>>
5
)
&
0x1F
)
/
31
;
b
=
(
(
packedColor
>>
10
)
&
0x1F
)
/
31
;
}
else
{
}
else
{
r
=
defaultR
;
g
=
defaultG
;
b
=
defaultB
;
r
=
defaultR
;
g
=
defaultG
;
b
=
defaultB
;
}
}
}
for
(
var
i
=
1
;
i
<=
3
;
i
++
)
{
for
(
var
i
=
1
;
i
<=
3
;
i
++
)
{
var
vertexstart
=
start
+
i
*
12
;
var
vertexstart
=
start
+
i
*
12
;
vertices
.
push
(
reader
.
getFloat32
(
vertexstart
,
true
)
);
vertices
.
push
(
reader
.
getFloat32
(
vertexstart
+
4
,
true
)
);
vertices
.
push
(
reader
.
getFloat32
(
vertexstart
+
8
,
true
)
);
vertices
.
push
(
reader
.
getFloat32
(
vertexstart
,
true
)
);
vertices
.
push
(
reader
.
getFloat32
(
vertexstart
+
4
,
true
)
);
vertices
.
push
(
reader
.
getFloat32
(
vertexstart
+
8
,
true
)
);
normals
.
push
(
normalX
,
normalY
,
normalZ
);
normals
.
push
(
normalX
,
normalY
,
normalZ
);
if
(
hasColors
)
{
if
(
hasColors
)
{
colors
.
push
(
r
,
g
,
b
);
colors
.
push
(
r
,
g
,
b
);
}
}
}
}
geometry
.
addAttribute
(
'
position
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
vertices
),
3
)
);
geometry
.
addAttribute
(
'
normal
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
normals
),
3
)
);
geometry
.
addAttribute
(
'
position
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
vertices
),
3
)
);
geometry
.
addAttribute
(
'
normal
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
normals
),
3
)
);
if
(
hasColors
)
{
if
(
hasColors
)
{
geometry
.
addAttribute
(
'
color
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
colors
),
3
)
);
geometry
.
hasColors
=
true
;
geometry
.
alpha
=
alpha
;
}
geometry
.
addAttribute
(
'
color
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
colors
),
3
)
);
geometry
.
hasColors
=
true
;
geometry
.
alpha
=
alpha
;
return
geometry
;
}
return
geometry
;
function
parseASCII
(
data
)
{
},
var
geometry
=
new
THREE
.
BufferGeometry
();
var
patternFace
=
/facet
([\s\S]
*
?)
endfacet/g
;
var
faceCounter
=
0
;
parseASCII
:
function
(
data
)
{
var
patternFloat
=
/
[\s]
+
([
+-
]?(?:\d
+.
\d
+|
\d
+.|
\d
+|.
\d
+
)(?:[
eE
][
+-
]?\d
+
)?)
/
.
source
;
var
patternVertex
=
new
RegExp
(
'
vertex
'
+
patternFloat
+
patternFloat
+
patternFloat
,
'
g
'
);
var
patternNormal
=
new
RegExp
(
'
normal
'
+
patternFloat
+
patternFloat
+
patternFloat
,
'
g
'
);
var
geometry
=
new
THREE
.
BufferGeometry
();
var
patternFace
=
/facet
([\s\S]
*
?)
endfacet/g
;
var
faceCounter
=
0
;
var
vertices
=
[];
var
normals
=
[];
var
patternFloat
=
/
[\s]
+
([
+-
]?(?:\d
+.
\d
+|
\d
+.|
\d
+|.
\d
+
)(?:[
eE
][
+-
]?\d
+
)?)
/
.
source
;
var
patternVertex
=
new
RegExp
(
'
vertex
'
+
patternFloat
+
patternFloat
+
patternFloat
,
'
g
'
);
var
patternNormal
=
new
RegExp
(
'
normal
'
+
patternFloat
+
patternFloat
+
patternFloat
,
'
g
'
);
var
normal
=
new
THREE
.
Vector3
();
var
vertices
=
[];
var
normals
=
[];
var
result
;
var
normal
=
new
THREE
.
Vector3
();
while
(
(
result
=
patternFace
.
exec
(
data
)
)
!==
null
)
{
var
result
;
var
vertexCountPerFace
=
0
;
var
normalCountPerFace
=
0
;
while
(
(
result
=
patternFace
.
exec
(
data
)
)
!==
null
)
{
var
text
=
result
[
0
];
var
vertexCountPerFace
=
0
;
var
normalCountPerFace
=
0
;
while
(
(
result
=
patternNormal
.
exec
(
text
)
)
!==
null
)
{
var
text
=
result
[
0
];
normal
.
x
=
parseFloat
(
result
[
1
]
);
normal
.
y
=
parseFloat
(
result
[
2
]
);
normal
.
z
=
parseFloat
(
result
[
3
]
);
normalCountPerFace
++
;
while
(
(
result
=
patternNormal
.
exec
(
text
)
)
!==
null
)
{
}
normal
.
x
=
parseFloat
(
result
[
1
]
);
normal
.
y
=
parseFloat
(
result
[
2
]
);
normal
.
z
=
parseFloat
(
result
[
3
]
);
normalCountPerFace
++
;
while
(
(
result
=
patternVertex
.
exec
(
text
)
)
!==
null
)
{
}
vertices
.
push
(
parseFloat
(
result
[
1
]
),
parseFloat
(
result
[
2
]
),
parseFloat
(
result
[
3
]
)
);
normals
.
push
(
normal
.
x
,
normal
.
y
,
normal
.
z
);
vertexCountPerFace
++
;
while
(
(
result
=
patternVertex
.
exec
(
text
)
)
!==
null
)
{
}
vertices
.
push
(
parseFloat
(
result
[
1
]
),
parseFloat
(
result
[
2
]
),
parseFloat
(
result
[
3
]
)
);
normals
.
push
(
normal
.
x
,
normal
.
y
,
normal
.
z
);
vertexCountPerFace
++
;
// every face have to own ONE valid normal
}
if
(
normalCountPerFace
!==
1
)
{
// Every face have to own ONE valid normal
if
(
normalCountPerFace
!==
1
)
{
console
.
error
(
'
THREE.STLLoader: Something isn
\'
t right with the normal of face number
'
+
faceCounter
);
throw
new
Error
(
'
Something isn
\'
t right with the normal of face number
'
+
faceCounter
);
}
}
// each face have to own THREE valid vertices
if
(
vertexCountPerFace
!==
3
)
{
// Each face have to own THREE valid vertices
if
(
vertexCountPerFace
!==
3
)
{
console
.
error
(
'
THREE.STLLoader: Something isn
\'
t right with the vertices of face number
'
+
faceCounter
);
}
throw
new
Error
(
'
Something isn
\'
t right with the vertices of face number
'
+
faceCounter
)
;
faceCounter
++
;
}
faceCounter
++
;
geometry
.
addAttribute
(
'
position
'
,
new
THREE
.
Float32BufferAttribute
(
vertices
,
3
)
);
geometry
.
addAttribute
(
'
normal
'
,
new
THREE
.
Float32BufferAttribute
(
normals
,
3
)
);
return
geometry
;
}
geometry
.
addAttribute
(
'
position
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
vertices
),
3
)
);
geometry
.
addAttribute
(
'
normal
'
,
new
THREE
.
BufferAttribute
(
new
Float32Array
(
normals
),
3
)
);
function
ensureString
(
buffer
)
{
return
geometry
;
if
(
typeof
buffer
!==
'
string
'
)
{
},
var
array_buffer
=
new
Uint8Array
(
buffer
);
ensureString
:
function
(
buf
)
{
if
(
window
.
TextDecoder
!==
undefined
)
{
if
(
typeof
buf
!==
"
string
"
)
{
return
new
TextDecoder
().
decode
(
array_buffer
);
var
array_buffer
=
new
Uint8Array
(
buf
);
}
if
(
window
.
TextDecoder
!==
undefined
)
{
var
str
=
''
;
return
new
TextDecoder
().
decode
(
array_buffer
);
for
(
var
i
=
0
,
il
=
buffer
.
byteLength
;
i
<
il
;
i
++
)
{
}
str
+=
String
.
fromCharCode
(
array_buffer
[
i
]
);
// implicitly assumes little-endian
var
str
=
''
;
}
for
(
var
i
=
0
,
il
=
buf
.
byteLength
;
i
<
il
;
i
++
)
{
return
str
;
str
+=
String
.
fromCharCode
(
array_buffer
[
i
]
);
// implicitly assumes little-endian
}
else
{
return
buffer
;
}
return
str
;
}
}
else
{
function
ensureBinary
(
buffer
)
{
return
buf
;
if
(
typeof
buffer
===
'
string
'
)
{
}
},
var
array_buffer
=
new
Uint8Array
(
buffer
.
length
);
for
(
var
i
=
0
;
i
<
buffer
.
length
;
i
++
)
{
ensureBinary
:
function
(
buf
)
{
array_buffer
[
i
]
=
buffer
.
charCodeAt
(
i
)
&
0xff
;
// implicitly assumes little-endian
if
(
typeof
buf
===
"
string
"
)
{
}
return
array_buffer
.
buffer
||
array_buffer
;
var
array_buffer
=
new
Uint8Array
(
buf
.
length
);
for
(
var
i
=
0
;
i
<
buf
.
length
;
i
++
)
{
}
else
{
array_buffer
[
i
]
=
buf
.
charCodeAt
(
i
)
&
0xff
;
// implicitly assumes little-endian
return
buffer
;
}
return
array_buffer
.
buffer
||
array_buffer
;
}
else
{
}
// start
return
buf
;
var
binData
=
ensureBinary
(
data
)
;
}
return
isBinary
(
binData
)
?
parseBinary
(
binData
)
:
parseASCII
(
ensureString
(
data
)
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录