Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gltf2_blender_KHR_materials_clearcoat.py « imp « blender « io_scene_gltf2 - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e34683f2dc81ce4d175ea07369259fd8deb28074 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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


# [Texture] => [Separate R] => [Clearcoat Factor] =>
def clearcoat(mh, location, clearcoat_socket):
    x, y = location
    try:
        ext = mh.pymat.extensions['KHR_materials_clearcoat']
    except Exception:
        return
    clearcoat_factor = ext.get('clearcoatFactor', 0)
    tex_info = ext.get('clearcoatTexture')
    if tex_info is not None:
        tex_info = TextureInfo.from_dict(tex_info)

    if clearcoat_socket is None:
        return

    if tex_info is None:
        clearcoat_socket.default_value = clearcoat_factor
        return

    # Mix clearcoat factor
    if clearcoat_factor != 1:
        node = mh.node_tree.nodes.new('ShaderNodeMath')
        node.label = 'Clearcoat Factor'
        node.location = x - 140, y
        node.operation = 'MULTIPLY'
        # Outputs
        mh.node_tree.links.new(clearcoat_socket, node.outputs[0])
        # Inputs
        clearcoat_socket = node.inputs[0]
        node.inputs[1].default_value = clearcoat_factor

        x -= 200

    # Separate RGB
    node = mh.node_tree.nodes.new('ShaderNodeSeparateColor')
    node.location = x - 150, y - 75
    # Outputs
    mh.node_tree.links.new(clearcoat_socket, node.outputs['Red'])
    # Inputs
    clearcoat_socket = node.inputs[0]

    x -= 200

    texture(
        mh,
        tex_info=tex_info,
        label='CLEARCOAT',
        location=(x, y),
        is_data=True,
        color_socket=clearcoat_socket,
    )


# [Texture] => [Separate G] => [Roughness Factor] =>
def clearcoat_roughness(mh, location, roughness_socket):
    x, y = location
    try:
        ext = mh.pymat.extensions['KHR_materials_clearcoat']
    except Exception:
        return
    roughness_factor = ext.get('clearcoatRoughnessFactor', 0)
    tex_info = ext.get('clearcoatRoughnessTexture')
    if tex_info is not None:
        tex_info = TextureInfo.from_dict(tex_info)

    if roughness_socket is None:
        return

    if tex_info is None:
        roughness_socket.default_value = roughness_factor
        return

    # Mix roughness factor
    if roughness_factor != 1:
        node = mh.node_tree.nodes.new('ShaderNodeMath')
        node.label = 'Clearcoat Roughness Factor'
        node.location = x - 140, y
        node.operation = 'MULTIPLY'
        # Outputs
        mh.node_tree.links.new(roughness_socket, node.outputs[0])
        # Inputs
        roughness_socket = node.inputs[0]
        node.inputs[1].default_value = roughness_factor

        x -= 200

    # Separate RGB (roughness is in G)
    node = mh.node_tree.nodes.new('ShaderNodeSeparateColor')
    node.location = x - 150, y - 75
    # Outputs
    mh.node_tree.links.new(roughness_socket, node.outputs['Green'])
    # Inputs
    color_socket = node.inputs[0]

    x -= 200

    texture(
        mh,
        tex_info=tex_info,
        label='CLEARCOAT ROUGHNESS',
        location=(x, y),
        is_data=True,
        color_socket=color_socket,
    )


# [Texture] => [Normal Map] =>
def clearcoat_normal(mh, location, normal_socket):
    x,y = location
    try:
        ext = mh.pymat.extensions['KHR_materials_clearcoat']
    except Exception:
        return
    tex_info = ext.get('clearcoatNormalTexture')
    if tex_info is not None:
        tex_info = MaterialNormalTextureInfoClass.from_dict(tex_info)

    if tex_info is None:
        return

    # Normal map
    node = mh.node_tree.nodes.new('ShaderNodeNormalMap')
    node.location = x - 150, y - 40
    # Set UVMap
    uv_idx = tex_info.tex_coord or 0
    try:
        uv_idx = tex_info.extensions['KHR_texture_transform']['texCoord']
    except Exception:
        pass
    node.uv_map = 'UVMap' if uv_idx == 0 else 'UVMap.%03d' % uv_idx
    # Set strength
    scale = tex_info.scale
    scale = scale if scale is not None else 1
    node.inputs['Strength'].default_value = scale
    # Outputs
    mh.node_tree.links.new(normal_socket, node.outputs['Normal'])
    # Inputs
    color_socket = node.inputs['Color']

    x -= 200

    texture(
        mh,
        tex_info=tex_info,
        label='CLEARCOAT NORMAL',
        location=(x, y),
        is_data=True,
        color_socket=color_socket,
    )