1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# SPDX-License-Identifier: Apache-2.0
# Copyright 2018-2021 The glTF-Blender-IO authors.
from ...io.com.gltf2_io import TextureInfo, MaterialNormalTextureInfoClass
from .gltf2_blender_texture import texture
def volume(mh, location, volume_socket, thickness_socket):
# implementation based on https://github.com/KhronosGroup/glTF-Blender-IO/issues/1454#issuecomment-928319444
try:
ext = mh.pymat.extensions['KHR_materials_volume']
except Exception:
return
# Attenuation Color
attenuationColor = \
mh.pymat.extensions['KHR_materials_volume'] \
.get('attenuationColor')
# glTF is color3, Blender adds alpha
if attenuationColor is None:
attenuationColor = [1.0, 1.0, 1.0, 1.0]
else:
attenuationColor.extend([1.0])
volume_socket.node.inputs[0].default_value = attenuationColor
# Attenuation Distance / Density
attenuationDistance = mh.pymat.extensions['KHR_materials_volume'].get('attenuationDistance')
if attenuationDistance is None:
density = 0
else:
density = 1.0 / attenuationDistance
volume_socket.node.inputs[1].default_value = density
# thicknessFactor / thicknessTexture
x, y = location
try:
ext = mh.pymat.extensions['KHR_materials_volume']
except Exception:
return
thickness_factor = ext.get('thicknessFactor', 0)
tex_info = ext.get('thicknessTexture')
if tex_info is not None:
tex_info = TextureInfo.from_dict(tex_info)
if thickness_socket is None:
return
if tex_info is None:
thickness_socket.default_value = thickness_factor
return
# Mix thickness factor
if thickness_factor != 1:
node = mh.node_tree.nodes.new('ShaderNodeMath')
node.label = 'Thickness Factor'
node.location = x - 140, y
node.operation = 'MULTIPLY'
# Outputs
mh.node_tree.links.new(thickness_socket, node.outputs[0])
# Inputs
thickness_socket = node.inputs[0]
node.inputs[1].default_value = thickness_factor
x -= 200
# Separate RGB
node = mh.node_tree.nodes.new('ShaderNodeSeparateColor')
node.location = x - 150, y - 75
# Outputs
mh.node_tree.links.new(thickness_socket, node.outputs['Green'])
# Inputs
thickness_socket = node.inputs[0]
x -= 200
texture(
mh,
tex_info=tex_info,
label='THICKNESS',
location=(x, y),
is_data=True,
color_socket=thickness_socket,
)
|