| /****************************************************************************** | |
| * Copyright 2024 NVIDIA Corporation. All rights reserved. * | |
| ****************************************************************************** | |
| Permission is hereby granted by NVIDIA Corporation ("NVIDIA"), free of charge, | |
| to any person obtaining a copy of the sample definition code that uses our | |
| Material Definition Language (the "MDL Materials"), to reproduce and distribute | |
| the MDL Materials, including without limitation the rights to use, copy, merge, | |
| publish, distribute, and sell modified and unmodified copies of the MDL | |
| Materials, and to permit persons to whom the MDL Materials is furnished to do | |
| so, in all cases solely for use with NVIDIA's Material Definition Language, | |
| subject to the following further conditions: | |
| 1. The above copyright notices, this list of conditions, and the disclaimer | |
| that follows shall be retained in all copies of one or more of the MDL | |
| Materials, including in any software with which the MDL Materials are bundled, | |
| redistributed, and/or sold, and included either as stand-alone text files, | |
| human-readable headers or in the appropriate machine-readable metadata fields | |
| within text or binary files as long as those fields can be easily viewed by the | |
| user, as applicable. | |
| 2. The name of NVIDIA shall not be used to promote, endorse or advertise any | |
| Modified Version without specific prior written permission, except a) to comply | |
| with the notice requirements otherwise contained herein; or b) to acknowledge | |
| the contribution(s) of NVIDIA. | |
| THE MDL MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, | |
| TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR | |
| ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, | |
| INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF | |
| CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE | |
| THE MDL MATERIALS OR FROM OTHER DEALINGS IN THE MDL MATERIALS. | |
| */ | |
| mdl 1.6; | |
| import ::anno::*; | |
| import ::base::*; | |
| import ::df::*; | |
| import ::math::*; | |
| import ::state::*; | |
| import ::tex::*; | |
| import ::nvidia::core_definitions::blend_colors; | |
| import ::nvidia::core_definitions::dimension; | |
| const string COPYRIGHT = | |
| " Copyright 2024 NVIDIA Corporation. All rights reserved.\n" | |
| " MDL MATERIALS ARE PROVIDED PURSUANT TO AN END USER LICENSE AGREEMENT,\n" | |
| " WHICH WAS ACCEPTED IN ORDER TO GAIN ACCESS TO THIS FILE. IN PARTICULAR,\n" | |
| " THE MDL MATERIALS ARE PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" | |
| " EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF\n" | |
| " MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF\n" | |
| " COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL NVIDIA\n" | |
| " CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY\n" | |
| " GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN\n" | |
| " AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR\n" | |
| " INABILITY TO USE THE MDL MATERIALS OR FROM OTHER DEALINGS IN THE MDL MATERIALS.\n"; | |
| const string DESCRIPTION = "Clear PET (Polyethylene terephthalate) material with IOR, Abbe Number and volumetric exhaustion"; | |
| export enum unit_scale | |
| [[ | |
| ::anno::hidden() | |
| ]] | |
| { | |
| unit_mm = 1, | |
| unit_cm = 2, | |
| unit_m = 3 | |
| }; | |
| struct volume_info | |
| [[ | |
| ::anno::hidden() | |
| ]] | |
| { | |
| color absorption_coefficient; | |
| color scattering_coefficient; | |
| }; | |
| // simplified volume coefficients | |
| // This function takes a transmittance value and and albedo and translates it into meaningful | |
| // scattering and volume coefficients that are userfriendly | |
| // | |
| volume_info volume_transmittance_albedo( | |
| uniform float density_scale = 1.0, | |
| uniform color transmittance = color(0.5f), // transmittance color after unit distance | |
| uniform color albedo = color(1.0f) | |
| ) | |
| { | |
| color sigma_t = -::math::log(::math::saturate(transmittance)) * density_scale; | |
| color sigma_s = sigma_t * ::math::saturate(albedo); | |
| return volume_info( | |
| scattering_coefficient: sigma_s, | |
| absorption_coefficient: sigma_t - sigma_s); | |
| } | |
| // This function calculates the apropriate scattering and volume coefficients | |
| // for a material of a given thickness. | |
| // The user specifies the thickness of the material, e.g. 3mm and the amount | |
| // of light passing through. The rest is automatically calculated for the material | |
| // and the material_volume is returned. | |
| volume_info custom_volume_transmittance( | |
| uniform unit_scale unit_scale_select = unit_mm, | |
| uniform float absorption_thickness = 3.0f, | |
| uniform color transmittance = color(0.5f), | |
| uniform color albedo = color(0.0f) | |
| ) | |
| { | |
| absorption_thickness = (absorption_thickness <= 0.0f) ? 0.00001 : absorption_thickness; | |
| float scalefactor; | |
| switch(unit_scale_select){ | |
| case unit_mm: scalefactor = 0.001f; break; | |
| case unit_cm: scalefactor = 0.01f; break; | |
| case unit_m: scalefactor = 1.0f; break; | |
| default: scalefactor = 1.0f; | |
| } | |
| volume_info vol_coefficients = volume_transmittance_albedo( | |
| density_scale: 1.0f/(absorption_thickness * scalefactor), | |
| transmittance: transmittance, | |
| albedo: albedo | |
| ); | |
| return vol_coefficients; | |
| } | |
| ::base::texture_coordinate_info transform_coordinate_2( | |
| float4x4 transform | |
| [[ ::anno::description("A transformation to be applied to the source coordinates. rotation_translation_scale() is a suggested means to compute the transformation matrix.") ]], | |
| ::base::texture_coordinate_info coordinate = ::base::texture_coordinate_info() | |
| [[ ::anno::description("Coordinate, typically sourced from coordinate_source or coordinate_projection.") ]] | |
| ) [[ | |
| ::anno::description("Transform a texture coordinate by a matrix.") , | |
| ::anno::noinline() | |
| ]] | |
| { | |
| // Version 2 | |
| float4 r_position = transform * float4(coordinate.position.x,coordinate.position.y,coordinate.position.z,1); | |
| //Try aproximating it for the case that the rotation is only aroud z and assuming the texture layout is nice and z is ~constant. | |
| //just pretend there is no other rotation happening | |
| //get rid of scaling and translation. Then extract fields where sin and cos would be in a simple 2d transform around z. | |
| float4 u = transform[0]; | |
| float3 ru = ::math::normalize(float3(u.x,u.y,u.z)); | |
| float cos = ru.x; | |
| float sin = -ru.y; | |
| //TODO: at least also handle sign of z? | |
| //TODO: handle tangent becoming 0 | |
| return ::base::texture_coordinate_info( | |
| float3(r_position.x,r_position.y,r_position.z), | |
| ::math::normalize(cos * coordinate.tangent_u - sin * coordinate.tangent_v), | |
| ::math::normalize(cos * coordinate.tangent_v + sin * coordinate.tangent_u)); | |
| } | |
| // Takes the standard input that every material has. It combines a couple of | |
| // functions in one convenience function. | |
| ::base::texture_coordinate_info vmat_transform( | |
| float2 translation = float2(0.0, 0.0), | |
| float rotation = 0.0, // rotation in degrees | |
| float2 scaling = float2(1.0, 1.0), | |
| uniform ::base::texture_coordinate_system system = ::base::texture_coordinate_uvw, | |
| uniform int uv_space = 0 | |
| ) | |
| { | |
| float rotation_rad = (rotation * 3.1415926535897932384626433832f) / 180.f; | |
| float4x4 scale = | |
| float4x4(1.0 /scaling.x, 0. , 0. , 0., | |
| 0. , 1.0 /scaling.y , 0. , 0., | |
| 0. , 0. , 1.0, 0., | |
| translation.x , translation.y , 0.0, 1.); | |
| float s = ::math::sin(rotation_rad); | |
| float c = ::math::cos(rotation_rad); | |
| float4x4 rotate = | |
| float4x4( c , -s , 0.0 , 0.0, | |
| s , c , 0.0 , 0.0, | |
| 0.0, 0.0 , 1.0 , 0.0, | |
| 0. , 0.0 , 0.0 , 1.); | |
| return transform_coordinate_2(scale*rotate, ::base::coordinate_source(system, uv_space)); | |
| } | |
| float2x2 invert_2x2(float2x2 M) | |
| { | |
| float det = M[0][0]*M[1][1] - M[0][1]*M[1][0]; | |
| //https://www.chilimath.com/lessons/advanced-algebra/inverse-of-a-2x2-matrix/ | |
| return (1.0 / det) * float2x2(M[1][1], -M[0][1], -M[1][0], M[0][0]); | |
| } | |
| // https://nullprogram.com/blog/2018/07/31/ | |
| //bias: 0.17353355999581582 ( very probably the best of its kind ) | |
| // NOTE: To turn this back to a float, one must divide the value by 4294967296.f | |
| // which corresponds to 0xffffffff, however MDL seems to turn this into -1. | |
| int lowbias32(int x) | |
| { | |
| x ^= x >>> 16; | |
| x *= 0x7feb352d; | |
| x ^= x >>> 15; | |
| x *= 0x846ca68b; | |
| x ^= x >>> 16; | |
| return x; | |
| } | |
| float uint2float(int x) | |
| { | |
| return float(x & 0x7FFFFFFF) + (x < 0 ? 2147483648.0 : 0.0); | |
| } | |
| float2 rnd22(int2 p) { | |
| float2 ret_val = float2( | |
| uint2float(lowbias32(p[0] + lowbias32(p[1]))) / 4294967296.f, | |
| uint2float(lowbias32(p[0] + 32000 + lowbias32(p[1]))) / 4294967296.f | |
| ); | |
| return ret_val; | |
| } | |
| float3 srgb2rgb(float3 val) | |
| { | |
| return ::math::pow(::math::max(val, float3(0.0f)), 2.2); | |
| } | |
| float3 nonrepeat_lookup( | |
| uniform texture_2d texture = texture_2d(), | |
| ::base::texture_coordinate_info uvw = ::base::coordinate_source(), | |
| float texture_scale = 1.0, | |
| float3 average_color = float3(0.5), | |
| float patch_size = 8.0 | |
| ) | |
| { | |
| float2 uv_in = float2(uvw.position[0], uvw.position[1]) * texture_scale; | |
| float Z = patch_size; // patch scale inside example texture | |
| float3 O = float3(0.f); | |
| float2x2 M0 = float2x2(1.f,0.f, 0.5f, ::math::sqrt(3.f)/2.f); | |
| float2x2 M = invert_2x2(M0); // transform matrix <-> tilted space | |
| float2 U = uv_in; | |
| float2 V = M * uv_in; //pre-tilted hexa coordinates | |
| int2 I = int2(::math::floor(V)); // hexa-tile id | |
| // The mean color needs to be determined in Photoshop | |
| float3 m = average_color; | |
| float3 F = float3(::math::frac(V)[0], ::math::frac(V)[1], 0.f), W; | |
| F[2] = 1.0 - F[0] - F[1]; // local hexa coordinates | |
| if( F[2] > 0.f ) | |
| O = (W[0] = F[2]) * (( ::tex::lookup_float3(texture, U/Z-rnd22(I))) - m) | |
| + (W[1] = F[1]) * (( ::tex::lookup_float3(texture, U/Z-rnd22(I+int2(0,1)))) - m) | |
| + (W[2] = F[0]) * (( ::tex::lookup_float3(texture, U/Z-rnd22(I+int2(1,0)))) - m); | |
| else | |
| O = (W[0] = -F[2]) * (( ::tex::lookup_float3(texture, U/Z-rnd22(I+int2(1)))) - m) | |
| + (W[1] = 1.f - F[1]) * (( ::tex::lookup_float3(texture, U/Z-rnd22(I+int2(1, 0)))) - m) | |
| + (W[2] = 1.f - F[0]) * (( ::tex::lookup_float3(texture, U/Z-rnd22(I+int2(0, 1)))) - m); | |
| O = m + O/::math::length(W); | |
| O = ::math::clamp( (O), 0.0, 1.0); | |
| return float3(O); | |
| } | |
| // NOTE: tex_resource must be in linear space (ensure that ::tex::gamma_linear is used) | |
| color endless_texture( | |
| uniform texture_2d texture = texture_2d(), | |
| ::base::texture_coordinate_info uvw = ::base::coordinate_source(), | |
| float texture_scale = 10.0, | |
| float3 average_color = float3(0.5, 0.5, 1.0), | |
| float patch_size = 8.0, | |
| bool gamma_correct_lookup = true | |
| ) | |
| { | |
| return gamma_correct_lookup ? color(srgb2rgb( | |
| nonrepeat_lookup ( | |
| texture: texture, | |
| uvw: uvw, | |
| texture_scale: texture_scale, | |
| average_color: average_color, | |
| patch_size: patch_size | |
| )) | |
| ) : color(nonrepeat_lookup ( | |
| texture: texture, | |
| uvw: uvw, | |
| texture_scale: texture_scale, | |
| average_color: average_color, | |
| patch_size: patch_size | |
| )); | |
| } | |
| export material PET_Clear( | |
| // Appearance | |
| uniform bool thin_walled = false [[ | |
| ::anno::description("Makes the material thin-walled. This changes the behavior of the material in a way that light attenuation is not calculated volumetrically as light travels through the geometry but at the light surface level. Thin walled geometry mustbe modeled as a single sheet without any volume."), | |
| ::anno::display_name("Thin Walled"), | |
| ::anno::in_group("Appearance") | |
| ]], | |
| uniform float ior = 1.573f [[ | |
| ::anno::description("Index of refraction"), | |
| ::anno::display_name("IOR"), | |
| ::anno::in_group("Appearance"), | |
| ::anno::hard_range(0.f, 20.f), | |
| ::anno::soft_range(0.f, 2.f) | |
| ]], | |
| uniform float abbe_number = 27.897f [[ | |
| ::anno::description("Dispersion in relation to index of refraction"), | |
| ::anno::display_name("Abbe Number"), | |
| ::anno::in_group("Appearance"), | |
| ::anno::soft_range(0.f, 100.f) | |
| ]], | |
| float roughness = 0.f [[ | |
| ::anno::description("Creates a frosted look of the glass"), | |
| ::anno::display_name("Roughness"), | |
| ::anno::in_group("Appearance"), | |
| ::anno::hard_range(0.f, 1.f) | |
| ]], | |
| uniform unit_scale units_absorption_thickness = unit_cm [[ | |
| ::anno::description("Chooses the units that are used for setting the absorption thickness. Can be meters, centimeters or millimeters."), | |
| ::anno::display_name("Units Absorption Thickness"), | |
| ::anno::in_group("Appearance") | |
| ]], | |
| uniform float absorption_thickness = 10.0f [[ | |
| ::anno::description("The thickness for which the transmittance (color) is set. Example: If thickness is set to 3mm and transmittance to 0.8, then 80% of the light will pass through a 3mm thick material."), | |
| ::anno::display_name("Absorption Thickness"), | |
| ::anno::in_group("Appearance") | |
| ]], | |
| uniform color transmittance = color(1.0f) [[ | |
| ::anno::description("The transmittance sets the amount of light that is left after light has travelled through a given material thickness which can be set with the parameter \"absorption thickness\". The transmittance can be set for RGB components separately to achieve a tinted appearance of the material."), | |
| ::anno::display_name("Transmittance"), | |
| ::anno::in_group("Appearance") | |
| ]], | |
| // Smudges | |
| float smudges = 0.f [[ | |
| ::anno::description("Determines the amount of smudges that appears on the surface of the material"), | |
| ::anno::display_name("Smudges"), | |
| ::anno::in_group("Appearance", "Smudges"), | |
| ::anno::hard_range(0.f, 1.f) | |
| ]], | |
| float2 smudge_scale = float2(1.f) [[ | |
| ::anno::description("Larger numbers increase the size."), | |
| ::anno::display_name("Smudge Scale"), | |
| ::anno::in_group("Appearance", "Smudges"), | |
| ::anno::soft_range(float2(0.f), float2(2.f)) | |
| ]], | |
| // Transform | |
| float2 texture_translate = float2(0.f) [[ | |
| ::anno::description("Controls the position of the texture."), | |
| ::anno::display_name("Texture Translate"), | |
| ::anno::in_group("Transform") | |
| ]], | |
| float texture_rotate = 0.f [[ | |
| ::anno::description("Rotates angle of the texture in degrees."), | |
| ::anno::display_name("Texture Rotate"), | |
| ::anno::in_group("Transform"), | |
| ::anno::soft_range(0.f, 360.f) | |
| ]], | |
| float2 texture_scale = float2(1.f) [[ | |
| ::anno::description("Larger numbers increase the size."), | |
| ::anno::display_name("Texture Scale"), | |
| ::nvidia::core_definitions::dimension(float2(0.45f, .45f)), | |
| ::anno::in_group("Transform"), | |
| ::anno::soft_range(float2(0.f), float2(2.f)) | |
| ]], | |
| // Round Corners | |
| uniform bool round_corners = false [[ | |
| ::anno::description("Enables the round corner effect. Comes at a slight performance cost as additional raytracing calls are required to evaluate the round corner effect."), | |
| ::anno::display_name("Round Corners"), | |
| ::anno::in_group("Round Corners") | |
| ]], | |
| uniform float radius = 1.5f [[ | |
| ::anno::description("Radius of the rounded corners."), | |
| ::anno::display_name("Radius mm"), | |
| ::anno::in_group("Round Corners"), | |
| ::anno::soft_range(0.f, 10.f) | |
| ]], | |
| uniform bool across_materials = false [[ | |
| ::anno::description("Applies the round corner effect across different materials when enabled."), | |
| ::anno::display_name("Across Materials"), | |
| ::anno::in_group("Round Corners") | |
| ]], | |
| // Advanced | |
| uniform int uv_space_index = 0 [[ | |
| ::anno::description("uses the selected UV space index."), | |
| ::anno::display_name("UV Space Index"), | |
| ::anno::in_group("Advanced"), | |
| ::anno::soft_range(0, 4) | |
| ]], | |
| float3 normal = ::state::normal() [[ | |
| ::anno::description("Override this input to provide a custom normal for the material."), | |
| ::anno::display_name("Normal"), | |
| ::anno::in_group("Advanced") | |
| ]] | |
| ) | |
| [[ | |
| ::anno::display_name("Polyethylene - Clear"), | |
| ::anno::author("NVIDIA vMaterials"), | |
| ::anno::contributor("Ruediger Raab"), | |
| ::anno::contributor("Maik Rohland"), | |
| ::anno::description(DESCRIPTION), | |
| ::anno::key_words(string[]("plastic", "polyethylene", "terephthalate", "PET", "clear", "synthetic", "transparent", "design", "packagaing", "molded", "infinite tiling", "smooth")), | |
| ::anno::thumbnail("./.thumbs/PET_Clear.PET_Clear.png"), | |
| ::anno::copyright_notice(COPYRIGHT) | |
| ]] | |
| = | |
| let { | |
| bool tmp0 = thin_walled; | |
| texture_2d smudges_tex = texture_2d("./textures/plastic_smudges.jpg", ::tex::gamma_linear); | |
| color endless_tex_lookup = endless_texture(smudges_tex, vmat_transform(texture_translate, texture_rotate, texture_scale * smudge_scale, ::base::texture_coordinate_uvw, uv_space_index), 4.f, float3(0.0590000004f, 0.050999999f, 0.125f), 4.f, false); | |
| float roughness_smudges = ::math::pow(::math::max(float3(endless_tex_lookup)[0] * smudges, float3(endless_tex_lookup)[1] * smudges), ::math::lerp(2.5f, 0.5f, ::math::pow(smudges, 0.25f))); | |
| material_surface tmp1( | |
| ::df::fresnel_layer( | |
| ior, | |
| 1.f, | |
| ::df::microfacet_ggx_smith_bsdf( | |
| roughness_smudges * roughness_smudges, | |
| roughness_smudges * roughness_smudges, | |
| color(1.f, 1.f, 1.f), | |
| color(0.f, 0.f, 0.f), | |
| ::state::texture_tangent_u(0), | |
| ::df::scatter_reflect), | |
| ::df::tint(color(1.f, 1.f, 1.f), | |
| thin_walled ? transmittance : color(1.0f), | |
| ::df::weighted_layer( | |
| 1.f, | |
| ::df::microfacet_ggx_smith_bsdf( | |
| roughness * roughness, | |
| roughness * roughness, | |
| color(1.f, 1.f, 1.f), | |
| color(0.f, 0.f, 0.f), | |
| ::state::texture_tangent_u(0), | |
| ::df::scatter_reflect_transmit | |
| ), | |
| bsdf(), | |
| normal) | |
| ), | |
| normal), | |
| material_emission(emission: edf(), intensity: color(0.f, 0.f, 0.f), mode: intensity_radiant_exitance)); | |
| material_surface tmp2 = material_surface(scattering: bsdf(), emission: material_emission(emission: edf(), intensity: color(0.f, 0.f, 0.f), mode: intensity_radiant_exitance)); | |
| color tmp3 = ::base::abbe_number_ior(ior, abbe_number); | |
| material_volume tmp4 = thin_walled ? material_volume(scattering: vdf(), absorption_coefficient: color(0.f, 0.f, 0.f), scattering_coefficient: color(0.f, 0.f, 0.f)) : material_volume(vdf(), custom_volume_transmittance(units_absorption_thickness, absorption_thickness, transmittance, color(0.f, 0.f, 0.f)).absorption_coefficient, custom_volume_transmittance(units_absorption_thickness, absorption_thickness, transmittance, color(0.f, 0.f, 0.f)).scattering_coefficient); | |
| material_geometry tmp5(float3(0.f), 1.f, round_corners ? ::state::rounded_corner_normal(radius * 0.00100000005f, across_materials, 1.f) : ::state::normal()); | |
| hair_bsdf tmp6 = hair_bsdf(); | |
| } in | |
| material( | |
| thin_walled: tmp0, | |
| surface: tmp1, | |
| backface: tmp2, | |
| ior: tmp3, | |
| volume: tmp4, | |
| geometry: tmp5, | |
| hair: tmp6); | |
| export material PET_Smudged(*) | |
| [[ | |
| ::anno::display_name("Polyethylene - Smudges"), | |
| ::anno::author("NVIDIA vMaterials"), | |
| ::anno::contributor("Ruediger Raab"), | |
| ::anno::contributor("Maik Rohland"), | |
| ::anno::description(DESCRIPTION), | |
| ::anno::key_words(string[]("plastic", "polyethylene", "terephthalate", "PET", "clear", "synthetic", "transparent", "design", "packagaing", "molded", "smudged", "smudges", "infinite tiling", "smooth")), | |
| ::anno::thumbnail("./.thumbs/PET_Clear.PET_Smudged.png"), | |
| ::anno::copyright_notice(COPYRIGHT) | |
| ]] | |
| = PET_Clear ( | |
| thin_walled: false, | |
| ior: 1.573f, | |
| abbe_number: 27.897f, | |
| roughness: 0.0f, | |
| units_absorption_thickness: unit_cm, | |
| absorption_thickness: 10.0f, | |
| transmittance: color(.98f), | |
| smudges: 0.85f, | |
| smudge_scale: float2(1.0f), | |
| texture_translate: float2(0.0f), | |
| texture_rotate: 0.0f, | |
| texture_scale: float2(1.0f), | |
| round_corners: false, | |
| radius: 0.5f, | |
| across_materials: false, | |
| uv_space_index: 0, | |
| normal: ::state::normal() | |
| ); | |