Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
车家大少爷
three.js
提交
80063ffb
T
three.js
项目概览
车家大少爷
/
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,发现更多精彩内容 >>
提交
80063ffb
编写于
5月 13, 2019
作者:
M
Mugen87
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
3MFLoader: Added basic material support.
上级
cfef70f5
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
364 addition
and
18 deletion
+364
-18
examples/files.js
examples/files.js
+1
-0
examples/js/loaders/3MFLoader.js
examples/js/loaders/3MFLoader.js
+205
-18
examples/models/3mf/gear.3mf
examples/models/3mf/gear.3mf
+0
-0
examples/models/3mf/truck.3mf
examples/models/3mf/truck.3mf
+0
-0
examples/webgl_loader_3mf_materials.html
examples/webgl_loader_3mf_materials.html
+158
-0
未找到文件。
examples/files.js
浏览文件 @
80063ffb
...
@@ -75,6 +75,7 @@ var files = {
...
@@ -75,6 +75,7 @@ var files = {
"
webgl_lines_sphere
"
,
"
webgl_lines_sphere
"
,
"
webgl_loader_3ds
"
,
"
webgl_loader_3ds
"
,
"
webgl_loader_3mf
"
,
"
webgl_loader_3mf
"
,
"
webgl_loader_3mf_materials
"
,
"
webgl_loader_amf
"
,
"
webgl_loader_amf
"
,
"
webgl_loader_assimp
"
,
"
webgl_loader_assimp
"
,
"
webgl_loader_assimp2json
"
,
"
webgl_loader_assimp2json
"
,
...
...
examples/js/loaders/3MFLoader.js
浏览文件 @
80063ffb
...
@@ -205,7 +205,35 @@ THREE.ThreeMFLoader.prototype = {
...
@@ -205,7 +205,35 @@ THREE.ThreeMFLoader.prototype = {
}
}
function
parseBasematerialsNode
(
/* basematerialsNode */
)
{
function
parseBasematerialsNode
(
basematerialsNode
)
{
var
basematerialsData
=
{
id
:
basematerialsNode
.
getAttribute
(
'
id
'
),
// required
basematerials
:
[]
};
var
basematerialNodes
=
basematerialsNode
.
querySelectorAll
(
'
base
'
);
for
(
var
i
=
0
;
i
<
basematerialNodes
.
length
;
i
++
)
{
var
basematerialNode
=
basematerialNodes
[
i
];
var
basematerialData
=
parseBasematerialNode
(
basematerialNode
);
basematerialsData
.
basematerials
.
push
(
basematerialData
);
}
return
basematerialsData
;
}
function
parseBasematerialNode
(
basematerialNode
)
{
var
basematerialData
=
{};
basematerialData
[
'
name
'
]
=
basematerialNode
.
getAttribute
(
'
name
'
);
// required
basematerialData
[
'
displaycolor
'
]
=
basematerialNode
.
getAttribute
(
'
displaycolor
'
);
// required
return
basematerialData
;
}
}
...
@@ -437,7 +465,7 @@ THREE.ThreeMFLoader.prototype = {
...
@@ -437,7 +465,7 @@ THREE.ThreeMFLoader.prototype = {
if
(
basematerialsNode
)
{
if
(
basematerialsNode
)
{
resourcesData
[
'
basematerial
'
]
=
parseBasematerialsNode
(
basematerialsNode
);
resourcesData
[
'
basematerial
s
'
]
=
parseBasematerialsNode
(
basematerialsNode
);
}
}
...
@@ -514,39 +542,171 @@ THREE.ThreeMFLoader.prototype = {
...
@@ -514,39 +542,171 @@ THREE.ThreeMFLoader.prototype = {
}
}
function
buildMesh
(
meshData
)
{
function
buildMesh
(
meshData
,
objects
,
modelData
,
objectData
)
{
// geometry
var
geometry
=
new
THREE
.
BufferGeometry
();
var
geometry
=
new
THREE
.
BufferGeometry
();
geometry
.
setIndex
(
new
THREE
.
BufferAttribute
(
meshData
[
'
triangles
'
],
1
)
);
geometry
.
setIndex
(
new
THREE
.
BufferAttribute
(
meshData
[
'
triangles
'
],
1
)
);
geometry
.
addAttribute
(
'
position
'
,
new
THREE
.
BufferAttribute
(
meshData
[
'
vertices
'
],
3
)
);
geometry
.
addAttribute
(
'
position
'
,
new
THREE
.
BufferAttribute
(
meshData
[
'
vertices
'
],
3
)
);
if
(
meshData
[
'
colors
'
]
)
{
// groups
var
basematerialsData
=
modelData
[
'
resources
'
][
'
basematerials
'
];
var
triangleProperties
=
meshData
[
'
triangleProperties
'
];
var
start
=
0
;
var
count
=
0
;
var
currentMaterialIndex
=
-
1
;
for
(
var
i
=
0
,
l
=
triangleProperties
.
length
;
i
<
l
;
i
++
)
{
var
triangleProperty
=
triangleProperties
[
i
];
var
pid
=
triangleProperty
.
pid
;
// only proceed if the triangle refers to a basematerials definition
if
(
basematerialsData
&&
(
basematerialsData
.
id
===
pid
)
)
{
if
(
currentMaterialIndex
===
-
1
)
currentMaterialIndex
=
triangleProperty
.
p1
;
if
(
currentMaterialIndex
===
triangleProperty
.
p1
)
{
count
+=
3
;
// primitves per triangle
}
else
{
geometry
.
addGroup
(
start
,
count
,
currentMaterialIndex
);
start
+=
count
;
count
=
3
;
currentMaterialIndex
=
triangleProperty
.
p1
;
geometry
.
addAttribute
(
'
color
'
,
new
THREE
.
BufferAttribute
(
meshData
[
'
colors
'
],
3
)
);
}
}
}
}
if
(
geometry
.
groups
.
length
>
0
)
mergeGroups
(
geometry
);
geometry
.
computeBoundingSphere
();
geometry
.
computeBoundingSphere
();
var
materialOpts
=
{
// material
flatShading
:
true
};
var
material
;
if
(
basematerialsData
&&
(
basematerialsData
.
id
===
objectData
.
pid
)
)
{
var
materialIndex
=
objectData
.
pindex
;
var
basematerialData
=
basematerialsData
.
basematerials
[
materialIndex
];
material
=
getBuild
(
basematerialData
,
objects
,
modelData
,
objectData
,
buildBasematerial
);
}
else
if
(
geometry
.
groups
.
length
>
0
)
{
var
groups
=
geometry
.
groups
;
material
=
[];
if
(
meshData
[
'
colors
'
]
&&
0
<
meshData
[
'
colors
'
].
length
)
{
for
(
var
i
=
0
,
l
=
groups
.
length
;
i
<
l
;
i
++
)
{
materialOpts
[
'
vertexColors
'
]
=
THREE
.
VertexColors
;
var
group
=
groups
[
i
];
var
basematerialData
=
basematerialsData
.
basematerials
[
group
.
materialIndex
];
material
.
push
(
getBuild
(
basematerialData
,
objects
,
modelData
,
objectData
,
buildBasematerial
)
);
}
}
else
{
}
else
{
materialOpts
[
'
color
'
]
=
0xaaaaff
;
// default material
material
=
new
THREE
.
MeshPhongMaterial
(
{
color
:
0xaaaaff
,
flatShading
:
true
}
);
}
}
var
material
=
new
THREE
.
MeshPhongMaterial
(
materialOpts
);
return
new
THREE
.
Mesh
(
geometry
,
material
);
return
new
THREE
.
Mesh
(
geometry
,
material
);
}
}
function
mergeGroups
(
geometry
)
{
// sort by material index
var
groups
=
geometry
.
groups
.
sort
(
function
(
a
,
b
)
{
if
(
a
.
materialIndex
!==
b
.
materialIndex
)
return
a
.
materialIndex
-
b
.
materialIndex
;
return
a
.
start
-
b
.
start
;
}
);
// reorganize index buffer
var
index
=
geometry
.
index
;
var
itemSize
=
index
.
itemSize
;
var
srcArray
=
index
.
array
;
var
targetOffset
=
0
;
var
targetArray
=
new
srcArray
.
constructor
(
srcArray
.
length
);
for
(
var
i
=
0
;
i
<
groups
.
length
;
i
++
)
{
var
group
=
groups
[
i
];
var
groupLength
=
group
.
count
*
itemSize
;
var
groupStart
=
group
.
start
*
itemSize
;
var
sub
=
srcArray
.
subarray
(
groupStart
,
groupStart
+
groupLength
);
targetArray
.
set
(
sub
,
targetOffset
);
targetOffset
+=
groupLength
;
}
srcArray
.
set
(
targetArray
);
// update groups
var
start
=
0
;
for
(
i
=
0
;
i
<
groups
.
length
;
i
++
)
{
group
=
groups
[
i
];
group
.
start
=
start
;
start
+=
group
.
count
;
}
// merge groups
var
lastGroup
=
groups
[
0
];
geometry
.
groups
=
[
lastGroup
];
for
(
i
=
1
;
i
<
groups
.
length
;
i
++
)
{
group
=
groups
[
i
];
if
(
lastGroup
.
materialIndex
===
group
.
materialIndex
)
{
lastGroup
.
count
+=
group
.
count
;
}
else
{
lastGroup
=
group
;
geometry
.
groups
.
push
(
lastGroup
);
}
}
}
function
applyExtensions
(
extensions
,
meshData
,
modelXml
)
{
function
applyExtensions
(
extensions
,
meshData
,
modelXml
)
{
if
(
!
extensions
)
{
if
(
!
extensions
)
{
...
@@ -585,16 +745,42 @@ THREE.ThreeMFLoader.prototype = {
...
@@ -585,16 +745,42 @@ THREE.ThreeMFLoader.prototype = {
}
}
function
getBuild
(
data
,
objects
,
modelData
,
builder
)
{
function
getBuild
(
data
,
objects
,
modelData
,
objectData
,
builder
)
{
if
(
data
.
build
!==
undefined
)
return
data
.
build
;
if
(
data
.
build
!==
undefined
)
return
data
.
build
;
data
.
build
=
builder
(
data
,
objects
,
modelData
);
data
.
build
=
builder
(
data
,
objects
,
modelData
,
objectData
);
return
data
.
build
;
return
data
.
build
;
}
}
function
buildBasematerial
(
materialData
)
{
var
material
=
new
THREE
.
MeshPhongMaterial
(
{
flatShading
:
true
}
);
material
.
name
=
materialData
.
name
;
// displaycolor MUST be specified with a value of a 6 or 8 digit hexadecimal number, e.g. "#RRGGBB" or "#RRGGBBAA"
var
displaycolor
=
materialData
.
displaycolor
;
var
color
=
displaycolor
.
substring
(
0
,
7
);
material
.
color
.
setStyle
(
color
);
material
.
color
.
convertSRGBToLinear
();
// displaycolor is in sRGB
// process alpha if set
if
(
displaycolor
.
length
===
9
)
{
material
.
opacity
=
parseInt
(
displaycolor
.
charAt
(
7
)
+
displaycolor
.
charAt
(
8
),
16
)
/
255
;
}
return
material
;
}
function
buildComposite
(
compositeData
,
objects
,
modelData
)
{
function
buildComposite
(
compositeData
,
objects
,
modelData
)
{
var
composite
=
new
THREE
.
Group
();
var
composite
=
new
THREE
.
Group
();
...
@@ -634,22 +820,23 @@ THREE.ThreeMFLoader.prototype = {
...
@@ -634,22 +820,23 @@ THREE.ThreeMFLoader.prototype = {
function
buildObject
(
objectId
,
objects
,
modelData
)
{
function
buildObject
(
objectId
,
objects
,
modelData
)
{
var
objectData
=
modelData
[
'
resources
'
][
'
object
'
][
objectId
];
var
objectData
=
modelData
[
'
resources
'
][
'
object
'
][
objectId
];
var
meshData
=
objectData
[
'
mesh
'
];
if
(
meshData
)
{
if
(
objectData
[
'
mesh
'
]
)
{
var
meshData
=
objectData
[
'
mesh
'
];
var
extensions
=
modelData
[
'
extensions
'
];
var
extensions
=
modelData
[
'
extensions
'
];
var
modelXml
=
modelData
[
'
xml
'
];
var
modelXml
=
modelData
[
'
xml
'
];
applyExtensions
(
extensions
,
meshData
,
modelXml
);
applyExtensions
(
extensions
,
meshData
,
modelXml
);
objects
[
objectData
.
id
]
=
getBuild
(
meshData
,
objects
,
modelData
,
buildMesh
);
objects
[
objectData
.
id
]
=
getBuild
(
meshData
,
objects
,
modelData
,
objectData
,
buildMesh
);
}
else
{
}
else
{
var
compositeData
=
objectData
[
'
components
'
];
var
compositeData
=
objectData
[
'
components
'
];
objects
[
objectData
.
id
]
=
getBuild
(
compositeData
,
objects
,
modelData
,
buildComposite
);
objects
[
objectData
.
id
]
=
getBuild
(
compositeData
,
objects
,
modelData
,
objectData
,
buildComposite
);
}
}
...
...
examples/models/3mf/gear.3mf
已删除
100644 → 0
浏览文件 @
cfef70f5
文件已删除
examples/models/3mf/truck.3mf
0 → 100755
浏览文件 @
80063ffb
文件已添加
examples/webgl_loader_3mf_materials.html
0 → 100644
浏览文件 @
80063ffb
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<title>
three.js webgl - 3MF
</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
{
color
:
#fff
;
font-family
:
Monospace
;
font-size
:
13px
;
text-align
:
center
;
background-color
:
#fff
;
margin
:
0px
;
overflow
:
hidden
;
}
#info
{
position
:
absolute
;
top
:
0px
;
width
:
100%
;
padding
:
5px
;
}
a
{
color
:
#ff0000
;
}
</style>
</head>
<body>
<div
id=
"info"
>
<a
href=
"http://threejs.org"
target=
"_blank"
rel=
"noopener"
>
three.js
</a>
<a
href=
"http://3mf.io"
target=
"_blank"
rel=
"noopener"
>
3MF file with materials
</a>
</div>
<script
src=
"../build/three.js"
></script>
<script
src=
"js/loaders/3MFLoader.js"
></script>
<script
src=
"js/WebGL.js"
></script>
<script
src=
"js/controls/OrbitControls.js"
></script>
<script
src=
"js/libs/jszip.min.js"
></script>
<script>
if
(
WEBGL
.
isWebGLAvailable
()
===
false
)
{
document
.
body
.
appendChild
(
WEBGL
.
getWebGLErrorMessage
()
);
}
var
camera
,
scene
,
renderer
;
init
();
function
init
()
{
scene
=
new
THREE
.
Scene
();
scene
.
background
=
new
THREE
.
Color
(
0xa0a0a0
);
scene
.
fog
=
new
THREE
.
Fog
(
0xa0a0a0
,
10
,
500
);
scene
.
add
(
new
THREE
.
AmbientLight
(
0xffffff
,
0.2
)
);
camera
=
new
THREE
.
PerspectiveCamera
(
35
,
window
.
innerWidth
/
window
.
innerHeight
,
1
,
500
);
camera
.
position
.
set
(
-
50
,
40
,
50
);
scene
.
add
(
camera
);
//
var
hemiLight
=
new
THREE
.
HemisphereLight
(
0xffffff
,
0x444444
);
hemiLight
.
position
.
set
(
0
,
100
,
0
);
scene
.
add
(
hemiLight
);
var
dirLight
=
new
THREE
.
DirectionalLight
(
0xffffff
);
dirLight
.
position
.
set
(
-
0
,
40
,
50
);
dirLight
.
castShadow
=
true
;
dirLight
.
shadow
.
camera
.
top
=
50
;
dirLight
.
shadow
.
camera
.
bottom
=
-
25
;
dirLight
.
shadow
.
camera
.
left
=
-
25
;
dirLight
.
shadow
.
camera
.
right
=
25
;
dirLight
.
shadow
.
camera
.
near
=
0.1
;
dirLight
.
shadow
.
camera
.
far
=
200
;
dirLight
.
shadow
.
mapSize
.
set
(
1024
,
1024
);
scene
.
add
(
dirLight
);
// scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
//
var
loader
=
new
THREE
.
ThreeMFLoader
();
loader
.
load
(
'
./models/3mf/truck.3mf
'
,
function
(
object
)
{
object
.
quaternion
.
setFromEuler
(
new
THREE
.
Euler
(
-
Math
.
PI
/
2
,
0
,
0
)
);
// z-up conversion
object
.
traverse
(
function
(
child
)
{
child
.
castShadow
=
true
;
}
);
scene
.
add
(
object
);
render
();
}
);
//
var
ground
=
new
THREE
.
Mesh
(
new
THREE
.
PlaneBufferGeometry
(
1000
,
1000
),
new
THREE
.
MeshPhongMaterial
(
{
color
:
0x999999
,
depthWrite
:
false
}
)
);
ground
.
rotation
.
x
=
-
Math
.
PI
/
2
;
ground
.
position
.
y
=
11
;
ground
.
receiveShadow
=
true
;
scene
.
add
(
ground
);
//
renderer
=
new
THREE
.
WebGLRenderer
(
{
antialias
:
true
}
);
renderer
.
setPixelRatio
(
window
.
devicePixelRatio
);
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
renderer
.
gammaOutput
=
true
;
renderer
.
gammaFactor
=
2.2
;
renderer
.
shadowMap
.
enabled
=
true
;
renderer
.
shadowMap
.
type
=
THREE
.
PCFSoftShadowMap
;
document
.
body
.
appendChild
(
renderer
.
domElement
);
//
var
controls
=
new
THREE
.
OrbitControls
(
camera
,
renderer
.
domElement
);
controls
.
addEventListener
(
'
change
'
,
render
);
controls
.
minDistance
=
50
;
controls
.
maxDistance
=
200
;
controls
.
enablePan
=
false
;
controls
.
target
.
set
(
0
,
20
,
0
);
controls
.
update
();
window
.
addEventListener
(
'
resize
'
,
onWindowResize
,
false
);
render
();
}
function
onWindowResize
()
{
camera
.
aspect
=
window
.
innerWidth
/
window
.
innerHeight
;
camera
.
updateProjectionMatrix
();
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
render
();
}
function
render
()
{
renderer
.
render
(
scene
,
camera
);
}
</script>
</body>
</html>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录