/*
*     Copyright 1986-2017 NVIDIA Corporation. All rights reserved.
******************************************************************************

 MDL MATERIALS ARE PROVIDED PURSUANT TO AN END USER LICENSE  AGREEMENT,
 WHICH WAS ACCEPTED IN ORDER TO GAIN ACCESS TO THIS FILE.  IN PARTICULAR, 
 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. 
*/
//notnv/epicgames/UE4/carbon/SourceArt/Omniverse/MDL/OmniPBR.mdl


//fixes: customIOR, AO mapping JIC, added bsdf dropdown
//fixes 10/12/2018 - converted metallic Bool into float for map.
//jjordan 10/18: disabled bsdf_type. imho makes material complicated without real benefit, but final call is for Daniela since she knows feedback
//jjordan 10/18: removed all ternaries on material structs and substituted with ternaries on bsdf. this works around https://moskito.nvidia-arc.com/show_bug.cgi?id=18719 but at the same time is shorter then original code
//danielaf  10/24/18: replacing ifm annotation with updated 1.4 anno for parameter ui grouping

//danielaf  10/24/18: removing all Booleans and using floats instead for dynamic material in RTX_OV
//ddanielaf  10/24/18: adding explicit texture input parameter and texture enable parameters for RTX_OV OMN-812 - unless tex::texture_isvalid implemented in RTX_OV
//danielaf  10/24/18: removed anisotropy,backscatter, thinfilms, custom_bsdf, abbe_number(things that don't seem to have any effect in RTX_OV ATM)


//danielaf 11/2/2018:  omni flex trimmed way down
//danielaf 11/06/2018: switch enables to bool! :D
mdl 1.4;
//switched back to 1.3, getting "mdl 1.4 not supported" from my iviewer version 2017 1.4 296300.6298
//switched to 1.2 - getting normalmap_texture parameter type error when switching to 1.4 - still troubleshooting
import ::df::*;
import ::state::*;
import ::math::*;
import ::base::*;
import ::tex::*;
import ::anno::*; 

import ::nvidia::core_definitions::file_texture;
import ::nvidia::core_definitions::normalmap_texture;
 
export using ::base import texture_return;
export using ::base import mono_mode;
export using ::base import color_layer_mode;


export material OmniPBR(



	color color_constant = color(0.2f)
	[[
		anno::display_name("Base color"),
		anno::in_group("Base Layer"),
		anno::description("The color of the material."),
		ui_position(1)
	]],
	uniform bool enable_basecolor_texture = false
	[[
		anno::display_name("Use Base Color Texture"),
		anno::in_group("Base Layer"),
		anno::hard_range(0.0,1.),
		ui_position(2),
		anno::description("Set to True to enable texture.")
	]],
	uniform texture_2d basecolor_texture = texture_2d()
	[[
		anno::display_name("Base Color Texture"),
		anno::in_group("Base Layer"),
		ui_position(3)
	]],
	uniform bool is_ao = false
	[[ 
		anno::display_name("Use AO Texture"),
		anno::in_group("Base Layer","AO"),
		anno::description("Set to True to enable texture."),
		ui_position(4)
	]],
	uniform texture_2d AO_map = texture_2d()
	[[
		anno::display_name("AO Map"),
		anno::in_group("Base Layer","AO"),
		anno::description("Input AO Map."),
		anno::enable_if("is_ao"),
		ui_position(5)
		//ifm::enableIf("is_ao","%value==True")
	]],
	float reflection_roughness_constant = 0.01f
	[[
		anno::display_name("Roughness"),
		anno::in_group("Reflectivity"),
		anno::hard_range(0.0,1.),
		anno::description("Higher roughness values lead to more blurry reflections"),
		ui_position(6)
	]],
	
	uniform bool enable_reflectionroughness_texture = false
	[[
		anno::display_name("Use Roughness Texture"),
		anno::in_group("Reflectivity"),
		anno::hard_range(0.0,1.),
		anno::description("Set to True to enable texture."),
		ui_position(7)
	]],
	uniform texture_2d reflectionroughness_texture = texture_2d()
	[[
		anno::display_name("Roughness Texture"),
		anno::in_group("Reflectivity"),
		ui_position(8)
	]],
	float specular = 1.f
	[[
		anno::display_name("Specular"),
		anno::in_group("Reflectivity"),
		anno::hard_range(0.f,1.f)
	]],
	float metallic_constant = 0.f
	[[
		anno::display_name("Metallic"),
		anno::in_group("Reflectivity"),
		anno::hard_range(0.0,1.),
		anno::description("Metallic Material"),
		ui_position(9)
	]],
	
	uniform bool enable_metallic_texture = false
	[[
		anno::display_name("Use Metallic Texture"),
		anno::in_group("Reflectivity"),
		anno::hard_range(0.0,1.),
		anno::description("Set to 1.f to enable texture."),
		ui_position(10)
	]],
	uniform texture_2d metallic_texture = texture_2d()
	[[
		anno::display_name("Metallic Texture"),
		anno::in_group("Reflectivity"),
		ui_position(11)
	]], 
	uniform bool enable_normalmap_texture = false
	[[
		anno::display_name("Use Normal Texture"),
		anno::in_group("Normal"),
		anno::hard_range(0.0,1.),
		anno::description("Set to 1.f to enable texture."),
		ui_position(12)
	]],
	uniform texture_2d normalmap_texture = texture_2d()
	[[
		anno::display_name("Normal Texture"),
		anno::in_group("Normal"),
		ui_position(13)
	]], 
	
	uniform float normal_factor = 1.f
	[[
		anno::display_name("Normal Strength"),
		anno::in_group("Normal"),
		anno::description("Strength of normal."),
		ui_position(34)
	]],
	uniform float texture_tiling = 1.f
	[[
		anno::display_name("Texture Tiling"),
		anno::in_group("Transforms")
	]],
	uniform float texture_rotation = 0.f
	[[
		anno::display_name("Texture Rotation"),
		anno::in_group("Transforms")
	]]


) 
[[
	anno::display_name("OmniPBR"),
	anno::description("PBR Material - MDL - Omniverse"),
	anno::author("NVIDIA CORPORATION"), 
	anno::key_words(string[]("omni","generic"))
]]
 = let{
	 
	float reflection_roughness =  enable_reflectionroughness_texture==true? ::nvidia::core_definitions::file_texture(
				texture: reflectionroughness_texture,scaling:float2(texture_tiling,texture_tiling),rotation:texture_rotation).mono : reflection_roughness_constant;			
 
 
	color base_color =  enable_basecolor_texture==true? ::nvidia::core_definitions::file_texture(
				texture: basecolor_texture,scaling:float2(texture_tiling,texture_tiling),rotation:texture_rotation).tint : color_constant;
				
	texture_return blend_color = base::blend_color_layers(
		layers: base::color_layer[](
            base::color_layer(
                layer_color: ::nvidia::core_definitions::file_texture(
				texture: AO_map,scaling:float2(texture_tiling,texture_tiling),rotation:texture_rotation).tint,
                weight: 1.f,
                mode: color_layer_multiply
            )),
        base: base_color
	 
    );			
				
	float metallic =  enable_metallic_texture==true? ::nvidia::core_definitions::file_texture(
				texture: metallic_texture,scaling:float2(texture_tiling,texture_tiling),rotation:texture_rotation).mono : metallic_constant;
			 
	
	color reflection_color =  metallic ==false? color(1.f,1.f,1.f): base_color;
	
	bsdf diffuse_bsdf = df::diffuse_reflection_bsdf(
		tint:is_ao==true?blend_color.tint:base_color,
		roughness: 0.f
	);
	/*
	float3 normal  = ::nvidia::core_definitions::normalmap_texture(
				texture: normalmap_texture, factor: normal_factor);
	
	*/
	float3 normal =  enable_normalmap_texture==true? ::nvidia::core_definitions::normalmap_texture(
				texture: normalmap_texture,scaling:float2(texture_tiling,texture_tiling),rotation:texture_rotation, factor: normal_factor): state::normal() ;				
	
	
	bsdf reflection_bsdf = df::microfacet_ggx_vcavities_bsdf(
		roughness_u:reflection_roughness,
		roughness_v:reflection_roughness,
		tint:reflection_color,
		mode: df::scatter_reflect
	); 
	/*
	bsdf reflection_bsdf = df::simple_glossy_bsdf(
		roughness_u:reflection_roughness,
		roughness_v:reflection_roughness,
		tint:reflection_color,
		tangent_u:reflection_roughness,
		mode: df::scatter_reflect
	);
	*/
	
	bsdf reflection_fresnel  = df::fresnel_layer(
				ior: 1.4f,
				weight: specular,
				layer: reflection_bsdf,
				base: diffuse_bsdf
	);
	
	bsdf reflection_metal  = df::weighted_layer(
		weight: metallic,
		layer: reflection_bsdf,
		base: reflection_fresnel
	);
	
	 bsdf add_normal = df::weighted_layer(
        normal: normal,
        weight: 1.,
        layer: reflection_metal
    );
	
	
	material_surface surfaceMat  = material_surface(
		scattering: add_normal
	);
	

} in material(
	ior: 1.4f,
	surface: surfaceMat
);

	


































