Datasets:

License:
File size: 8,535 Bytes
b58f4d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/***************************************************************************************************
 * Copyright 2020 NVIDIA Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of NVIDIA CORPORATION nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 **************************************************************************************************/

//* 1.0.0 - first version
//* 1.0.1 - Emissive color affected by opacity
//        - Support opacity mask
//* 1.0.2 - Unlit translucent
//* 1.0.3 - specular bsdf instead of microfacet ggx smith bsdf
//* 1.0.4 - using absolute import paths when importing standard modules

mdl 1.3;

import ::df::*;
import ::state::*;
import ::math::*;
import ::tex::*;
import ::anno::*;

float emissive_multiplier()
[[
    anno::description("the multiplier to convert UE4 emissive to raw data"),
    anno::noinline()
]]
{
    return 20.0f * 128.0f;
}

color get_translucent_tint(color base_color, float opacity)
[[
    anno::description("base color of UE4 translucent"),
    anno::noinline()
]]
{
	return math::lerp(color(1.0), base_color, opacity);
}

// Just for UE4 distilling
float get_translucent_opacity(float opacity)
[[
	anno::noinline()
]]
{
	return opacity;
}

color get_emissive_intensity(color emissive, float opacity)
[[
    anno::description("emissive color of UE4 translucent"),
    anno::noinline()
]]
{
	return emissive * opacity;
}

float3 tangent_space_normal(
    float3 normal = float3(0.0,0.0,1.0),
    float3 tangent_u = state::texture_tangent_u(0),
	float3 tangent_v = state::texture_tangent_v(0)
)
[[
    anno::description("Interprets the vector in tangent space"),
    anno::noinline()
]]
{
    return math::normalize(
        tangent_u * normal.x -
        tangent_v * normal.y + /* flip_tangent_v */
        state::normal() * (normal.z));
}

float3 world_space_normal(
    float3 normal = float3(0.0,0.0,1.0),
    float3 tangent_u = state::texture_tangent_u(0),
    float3 tangent_v = state::texture_tangent_v(0)
)
[[
    anno::description("Interprets the vector in world space"),
    anno::noinline()
]]
{
    return tangent_space_normal(
        math::normalize(
        normal.x * float3(tangent_u.x, tangent_v.x, state::normal().x) -
        normal.y * float3(tangent_u.y, tangent_v.y, state::normal().y) +
        normal.z * float3(tangent_u.z, tangent_v.z, state::normal().z)),
        tangent_u,
        tangent_v
    );
}

export material OmniUe4Translucent(
	float3 base_color = float3(0.0, 0.0, 0.0),
	float metallic = 0.0,
	float roughness = 0.5,
	float specular = 0.5,
	float3 normal = float3(0.0,0.0,1.0),
    uniform bool enable_opacity = true,
	float opacity = 1.0,
    float opacity_mask = 1.0,
	float3 emissive_color = float3(0.0, 0.0, 0.0),
	float3 displacement = float3(0.0),
	uniform float refraction = 1.0,
	uniform bool two_sided = false,
	uniform bool is_tangent_space_normal = true,
	uniform bool is_unlit = false
)
[[
	anno::display_name("Omni UE4 Translucent"),
	anno::description("Omni UE4 Translucent, supports UE4 Translucent shading model"),
	anno::version( 1, 0, 0),
	anno::author("NVIDIA CORPORATION"), 
	anno::key_words(string[]("omni", "UE4", "omniverse", "translucent"))
]]
 = let {     
	color final_base_color = math::saturate(base_color);
	float final_metallic = math::min(math::max(metallic, 0.0f), 0.99f);
	float final_roughness = math::saturate(roughness);
	float final_specular = math::saturate(specular);
	color final_emissive_color = math::max(emissive_color, 0.0f) * emissive_multiplier(); /*factor for converting ue4 emissive to raw value*/
	float final_opacity = math::saturate(opacity);
	float3 final_normal = math::normalize(normal);
 
 
  // - compute final roughness by squaring the "roughness" parameter
   float alpha = final_roughness * final_roughness;
   // reduce the reflectivity at grazing angles to avoid "dark edges" for high roughness due to the layering
   float grazing_refl = math::max((1.0 - final_roughness), 0.0);

	float3 the_normal =  is_unlit ? state::normal() : 
							(is_tangent_space_normal ? 
                            tangent_space_normal(
                                            normal:         final_normal,
                                            tangent_u:      state::texture_tangent_u(0),
                                            tangent_v:      state::texture_tangent_v(0)
                            ) : world_space_normal(
                                            normal:         final_normal,
                                            tangent_u:      state::texture_tangent_u(0),
                                            tangent_v:      state::texture_tangent_v(0)
                            ));

   // for the dielectric component we layer the glossy component on top of the diffuse one,
   // the glossy layer has no color tint
       
   bsdf dielectric_component = df::custom_curve_layer(
       weight: final_specular,
       normal_reflectivity: 0.08,
       grazing_reflectivity: grazing_refl,
       layer: df::microfacet_ggx_smith_bsdf(roughness_u: alpha),
       base: df::diffuse_reflection_bsdf(tint: final_base_color));

   // the metallic component doesn't have a diffuse component, it's only glossy
   // base_color is applied to tint it
   bsdf metallic_component = df::microfacet_ggx_smith_bsdf(tint: final_base_color, roughness_u: alpha);

   // final BSDF is a linear blend between dielectric and metallic component
   bsdf dielectric_metal_mix =
       df::normalized_mix(
           components:
               df::bsdf_component[](
                   df::bsdf_component(
                       component: metallic_component,
                       weight: final_metallic),
                   df::bsdf_component(
                       component: dielectric_component,
                       weight: 1.0-final_metallic)
               )
       );
 
	bsdf frosted_bsdf = df::specular_bsdf(
									tint: color(1),
									mode: df::scatter_reflect_transmit
									);							
	
	bsdf final_mix_bsdf =
	   is_unlit ? df::specular_bsdf(
							tint: get_translucent_tint(base_color: final_base_color, opacity: final_opacity),
							mode: df::scatter_reflect_transmit
							)
	   : df::normalized_mix(
           components:
               df::bsdf_component[](
                   df::bsdf_component(
                       component: dielectric_metal_mix,
                       weight: get_translucent_opacity(final_opacity)),
                   df::bsdf_component(
                       component: frosted_bsdf,
                       weight: 1.0-get_translucent_opacity(final_opacity))
               )
       );
} 
in material(
	thin_walled: two_sided, // Graphene?
	ior: color(refraction), //refraction
	surface: material_surface(
		scattering: final_mix_bsdf,
		emission:
            material_emission (
                emission:  df::diffuse_edf (),
                intensity: get_emissive_intensity(emissive: final_emissive_color, opacity: final_opacity)
                )
	),
	
	geometry: material_geometry(
		displacement: displacement,
		normal: the_normal,
        cutout_opacity: enable_opacity ? opacity_mask : 1.0
	)	
);