Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
车家大少爷
three.js
提交
afb032b2
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,发现更多精彩内容 >>
提交
afb032b2
编写于
5月 27, 2021
作者:
M
Mr.doob
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
USDZExporter: Refactored diffuse modulation code
上级
3e8694d2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
61 addition
and
92 deletion
+61
-92
examples/jsm/exporters/USDZExporter.js
examples/jsm/exporters/USDZExporter.js
+61
-92
未找到文件。
examples/jsm/exporters/USDZExporter.js
浏览文件 @
afb032b2
...
...
@@ -14,8 +14,6 @@ class USDZExporter {
const
materials
=
{};
const
textures
=
{};
const
materialsWithColor
=
new
Set
();
const
coloredTextures
=
new
Map
();
// material -> diffuse map file name
scene
.
traverseVisible
(
(
object
)
=>
{
...
...
@@ -36,20 +34,6 @@ class USDZExporter {
if
(
!
(
material
.
uuid
in
materials
)
)
{
materials
[
material
.
uuid
]
=
material
;
// note: material.colorWhite dows not work (always true for some reason)
const
materialIsNotWhite
=
material
.
color
.
r
!=
1
||
material
.
color
.
g
!=
1
||
material
.
color
.
b
!=
1
;
if
(
material
.
map
!==
null
)
{
if
(
materialIsNotWhite
)
{
materialsWithColor
.
add
(
material
);
}
else
{
textures
[
material
.
map
.
uuid
]
=
material
.
map
;
}
}
if
(
material
.
normalMap
!==
null
)
textures
[
material
.
normalMap
.
uuid
]
=
material
.
normalMap
;
if
(
material
.
aoMap
!==
null
)
textures
[
material
.
aoMap
.
uuid
]
=
material
.
aoMap
;
if
(
material
.
roughnessMap
!==
null
)
textures
[
material
.
roughnessMap
.
uuid
]
=
material
.
roughnessMap
;
if
(
material
.
metalnessMap
!==
null
)
textures
[
material
.
metalnessMap
.
uuid
]
=
material
.
metalnessMap
;
if
(
material
.
emissiveMap
!==
null
)
textures
[
material
.
emissiveMap
.
uuid
]
=
material
.
emissiveMap
;
}
...
...
@@ -59,34 +43,19 @@ class USDZExporter {
}
);
for
(
const
material
of
materialsWithColor
)
{
const
color256
=
{
r
:
Math
.
floor
(
material
.
color
.
r
*
256
),
g
:
Math
.
floor
(
material
.
color
.
g
*
256
),
b
:
Math
.
floor
(
material
.
color
.
b
*
256
),
};
const
textureSignature
=
`
${
material
.
map
.
id
}
_
${
color256
.
r
}
_
${
color256
.
g
}
_
${
color256
.
b
}
`
;
const
texturePath
=
`textures/Texture_
${
textureSignature
}
.jpg`
;
if
(
!
files
[
texturePath
])
{
files
[
texturePath
]
=
await
imgToU8
(
material
.
map
.
image
,
color256
);
}
coloredTextures
.
set
(
material
,
textureSignature
);
}
output
+=
buildMaterials
(
materials
,
coloredTextures
);
output
+=
buildMaterials
(
materials
,
textures
);
files
[
modelFileName
]
=
fflate
.
strToU8
(
output
);
output
=
null
;
for
(
const
uu
id
in
textures
)
{
for
(
const
id
in
textures
)
{
const
texture
=
textures
[
uu
id
];
files
[
'
textures/Texture_
'
+
texture
.
id
+
'
.jpg
'
]
=
await
imgToU8
(
texture
.
image
)
;
const
texture
=
textures
[
id
];
const
color
=
id
.
split
(
'
_
'
)[
1
]
;
}
files
[
'
textures/Texture_
'
+
id
+
'
.jpg
'
]
=
await
imgToU8
(
texture
.
image
,
color
);
}
// 64 byte alignment
// https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109
...
...
@@ -115,12 +84,7 @@ class USDZExporter {
}
return
await
new
Promise
((
resolve
,
reject
)
=>
{
fflate
.
zip
(
files
,
{
level
:
0
},
(
err
,
zipped
)
=>
{
if
(
err
)
reject
(
err
);
else
resolve
(
zipped
);
});
});
return
fflate
.
zipSync
(
files
,
{
level
:
0
}
);
}
...
...
@@ -142,10 +106,12 @@ async function imgToU8( image, color ) {
const
context
=
canvas
.
getContext
(
'
2d
'
);
context
.
drawImage
(
image
,
0
,
0
,
canvas
.
width
,
canvas
.
height
);
if
(
!!
color
)
{
context
.
globalCompositeOperation
=
"
multiply
"
;
context
.
fillStyle
=
`rgb(
${
color
.
r
}
,
${
color
.
g
}
,
${
color
.
b
}
)`
;
context
.
fillRect
(
0
,
0
,
image
.
width
,
image
.
height
);
if
(
color
!==
undefined
)
{
context
.
globalCompositeOperation
=
'
multiply
'
;
context
.
fillStyle
=
`#
${
color
}
`
;
context
.
fillRect
(
0
,
0
,
canvas
.
width
,
canvas
.
height
);
}
const
blob
=
await
new
Promise
(
resolve
=>
canvas
.
toBlob
(
resolve
,
'
image/jpeg
'
,
1
)
);
...
...
@@ -330,7 +296,7 @@ function buildVector2Array( attribute, count ) {
// Materials
function
buildMaterials
(
materials
,
coloredT
extures
)
{
function
buildMaterials
(
materials
,
t
extures
)
{
const
array
=
[];
...
...
@@ -338,7 +304,7 @@ function buildMaterials( materials, coloredTextures ) {
const
material
=
materials
[
uuid
];
array
.
push
(
buildMaterial
(
material
,
coloredT
extures
)
);
array
.
push
(
buildMaterial
(
material
,
t
extures
)
);
}
...
...
@@ -351,17 +317,20 @@ ${ array.join( '' ) }
}
function
buildMaterial
(
material
,
coloredT
extures
)
{
function
buildMaterial
(
material
,
t
extures
)
{
// https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
const
pad
=
'
'
;
const
parameters
=
[];
const
textures
=
[];
const
inputs
=
[];
const
samplers
=
[];
function
buildTexture
(
texture
,
mapType
,
color
)
{
const
id
=
texture
.
id
+
(
color
?
'
_
'
+
color
.
getHexString
()
:
''
);
function
buildTexture
(
texture
,
mapType
,
textureSignature
)
{
textures
[
id
]
=
texture
;
const
textureId
=
textureSignature
||
texture
.
id
;
return
`
def Shader "Transform2d_
${
mapType
}
" (
sdrMetadata = {
...
...
@@ -379,7 +348,7 @@ function buildMaterial( material, coloredTextures ) {
def Shader "Texture_
${
texture
.
id
}
_
${
mapType
}
"
{
uniform token info:id = "UsdUVTexture"
asset inputs:file = @textures/Texture_
${
textureI
d
}
.jpg@
asset inputs:file = @textures/Texture_
${
i
d
}
.jpg@
float2 inputs:st.connect = </Materials/Material_
${
material
.
id
}
/Transform2d_
${
mapType
}
.outputs:result>
token inputs:wrapS = "repeat"
token inputs:wrapT = "repeat"
...
...
@@ -393,95 +362,95 @@ function buildMaterial( material, coloredTextures ) {
if
(
material
.
map
!==
null
)
{
parameter
s
.
push
(
`
${
pad
}
color3f inputs:diffuseColor.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
map
.
id
}
_diffuse.outputs:rgb>`
);
input
s
.
push
(
`
${
pad
}
color3f inputs:diffuseColor.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
map
.
id
}
_diffuse.outputs:rgb>`
);
textures
.
push
(
buildTexture
(
material
.
map
,
'
diffuse
'
,
coloredTextures
.
get
(
material
)
)
);
samplers
.
push
(
buildTexture
(
material
.
map
,
'
diffuse
'
,
material
.
color
)
);
}
else
{
parameter
s
.
push
(
`
${
pad
}
color3f inputs:diffuseColor =
${
buildColor
(
material
.
color
)
}
`
);
input
s
.
push
(
`
${
pad
}
color3f inputs:diffuseColor =
${
buildColor
(
material
.
color
)
}
`
);
}
if
(
material
.
emissiveMap
!==
null
)
{
parameter
s
.
push
(
`
${
pad
}
color3f inputs:emissiveColor.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
emissiveMap
.
id
}
_emissive.outputs:rgb>`
);
input
s
.
push
(
`
${
pad
}
color3f inputs:emissiveColor.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
emissiveMap
.
id
}
_emissive.outputs:rgb>`
);
texture
s
.
push
(
buildTexture
(
material
.
emissiveMap
,
'
emissive
'
)
);
sampler
s
.
push
(
buildTexture
(
material
.
emissiveMap
,
'
emissive
'
)
);
}
else
if
(
material
.
emissive
.
getHex
()
>
0
)
{
parameter
s
.
push
(
`
${
pad
}
color3f inputs:emissiveColor =
${
buildColor
(
material
.
emissive
)
}
`
);
input
s
.
push
(
`
${
pad
}
color3f inputs:emissiveColor =
${
buildColor
(
material
.
emissive
)
}
`
);
}
if
(
material
.
normalMap
!==
null
)
{
parameter
s
.
push
(
`
${
pad
}
normal3f inputs:normal.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
normalMap
.
id
}
_normal.outputs:rgb>`
);
input
s
.
push
(
`
${
pad
}
normal3f inputs:normal.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
normalMap
.
id
}
_normal.outputs:rgb>`
);
texture
s
.
push
(
buildTexture
(
material
.
normalMap
,
'
normal
'
)
);
sampler
s
.
push
(
buildTexture
(
material
.
normalMap
,
'
normal
'
)
);
}
if
(
material
.
aoMap
!==
null
)
{
parameter
s
.
push
(
`
${
pad
}
float inputs:occlusion.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
aoMap
.
id
}
_occlusion.outputs:r>`
);
input
s
.
push
(
`
${
pad
}
float inputs:occlusion.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
aoMap
.
id
}
_occlusion.outputs:r>`
);
texture
s
.
push
(
buildTexture
(
material
.
aoMap
,
'
occlusion
'
)
);
sampler
s
.
push
(
buildTexture
(
material
.
aoMap
,
'
occlusion
'
)
);
}
if
(
material
.
roughnessMap
!==
null
)
{
parameter
s
.
push
(
`
${
pad
}
float inputs:roughness.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
roughnessMap
.
id
}
_roughness.outputs:g>`
);
input
s
.
push
(
`
${
pad
}
float inputs:roughness.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
roughnessMap
.
id
}
_roughness.outputs:g>`
);
texture
s
.
push
(
buildTexture
(
material
.
roughnessMap
,
'
roughness
'
)
);
sampler
s
.
push
(
buildTexture
(
material
.
roughnessMap
,
'
roughness
'
)
);
}
else
{
parameter
s
.
push
(
`
${
pad
}
float inputs:roughness =
${
material
.
roughness
}
`
);
input
s
.
push
(
`
${
pad
}
float inputs:roughness =
${
material
.
roughness
}
`
);
}
if
(
material
.
metalnessMap
!==
null
)
{
parameter
s
.
push
(
`
${
pad
}
float inputs:metallic.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
metalnessMap
.
id
}
_metallic.outputs:b>`
);
input
s
.
push
(
`
${
pad
}
float inputs:metallic.connect = </Materials/Material_
${
material
.
id
}
/Texture_
${
material
.
metalnessMap
.
id
}
_metallic.outputs:b>`
);
texture
s
.
push
(
buildTexture
(
material
.
metalnessMap
,
'
metallic
'
)
);
sampler
s
.
push
(
buildTexture
(
material
.
metalnessMap
,
'
metallic
'
)
);
}
else
{
parameter
s
.
push
(
`
${
pad
}
float inputs:metallic =
${
material
.
metalness
}
`
);
input
s
.
push
(
`
${
pad
}
float inputs:metallic =
${
material
.
metalness
}
`
);
}
parameter
s
.
push
(
`
${
pad
}
float inputs:opacity =
${
material
.
opacity
}
`
);
input
s
.
push
(
`
${
pad
}
float inputs:opacity =
${
material
.
opacity
}
`
);
return
`
def Material "Material_
${
material
.
id
}
"
{
def Shader "PreviewSurface"
{
uniform token info:id = "UsdPreviewSurface"
${
parameter
s
.
join
(
'
\n
'
)
}
int inputs:useSpecularWorkflow = 0
token outputs:surface
}
def Material "Material_
${
material
.
id
}
"
{
def Shader "PreviewSurface"
{
uniform token info:id = "UsdPreviewSurface"
${
input
s
.
join
(
'
\n
'
)
}
int inputs:useSpecularWorkflow = 0
token outputs:surface
}
token outputs:surface.connect = </Materials/Material_
${
material
.
id
}
/PreviewSurface.outputs:surface>
token inputs:frame:stPrimvarName = "st"
token outputs:surface.connect = </Materials/Material_
${
material
.
id
}
/PreviewSurface.outputs:surface>
token inputs:frame:stPrimvarName = "st"
def Shader "uvReader_st"
{
uniform token info:id = "UsdPrimvarReader_float2"
token inputs:varname.connect = </Materials/Material_
${
material
.
id
}
.inputs:frame:stPrimvarName>
float2 inputs:fallback = (0.0, 0.0)
float2 outputs:result
}
def Shader "uvReader_st"
{
uniform token info:id = "UsdPrimvarReader_float2"
token inputs:varname.connect = </Materials/Material_
${
material
.
id
}
.inputs:frame:stPrimvarName>
float2 inputs:fallback = (0.0, 0.0)
float2 outputs:result
}
${
texture
s
.
join
(
'
\n
'
)
}
${
sampler
s
.
join
(
'
\n
'
)
}
}
}
`
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录