Hello Ventuzians!
THE FORUMS ARE CLOSED!

Please join our discord server HERE!! << click me :D

We are shutting our Ventuz Forum, but don't worry, it will all be archived for you to search in if you have a query. From now on, please add all your comments, questions or observations into our Discord Server

Thanks for the great time - see you on discord!!
Dee, Karol, Daniel and the whoooole Product and Support team!

simple hlsl shader

Q and A about functionality and how to solve a special task for your application.

Moderator: Support

Post Reply
pyrrr
Posts: 34
Joined: 13 May 2012, 15:50

simple hlsl shader

Post by pyrrr » 16 May 2012, 14:08

i create some simple material in mental mill and i exported it to ventuz.

ventuz shows no error during compiling the shader but in the viewport the object are still black.

in order to show you my setup i uploaded an image of my scene ( PLE )


Code: Select all

	add_default_states : true
	alpha_transparency : false
	annotation_map : default.axml
	depth_test : true
	depth_write : true
	face_forward : false
	flip_tex2d_v : false
	format : HLSL
	fragment_shader_profile : ps_3_0
	generate_sampler_states : true
	generate_samplers : true
	generate_umapped_annotations : false
	hlsl_sas_conformance : true
	internal_space : camera
	rotate_cube_map : false
	shader_type : surface
	two_sided : false
	use_oit : false
	vertex_shader_profile : vs_3_0
	vertex_stream_space : object
	vs_binding_binormal : BINORMAL
	vs_binding_tangent : TANGENT
*/

// shader type surface

//
// The state structure is used internally within the fragment shader to
// commonly used values.
//
struct State
{
	float3 tangent[1];
	float3 position;
	float3 origin;
	float3 normal;
	float3 direction;
	float ray_length;
};

//
// Values for the light_type parameter of light shaders
//
#define LIGHT_POINT    0
#define LIGHT_SPOT     1
#define LIGHT_INFINITE 2
#define LIGHT_PLANAR   3

//
// The light iterator structure holds the return values resulting from
// evaluating a light.
//
#ifndef __LIGHT_ITERATOR_STRUCT
#define __LIGHT_ITERATOR_STRUCT
struct Light_iterator {
float3 msl_point;
float3 msl_direction;
float  msl_distance;
float  msl_dot_nl;
float3 msl_contribution;
float3 msl_raw_contribution;
float3 msl_shadow;
int    msl_count;
};
#endif // __LIGHT_ITERATOR_STRUCT

//
// Ray
//
struct Ray {
float3 msl_origin;
float3 msl_direction;
};

bool Ray_equal(Ray a,Ray b) {
	return all(a.msl_origin == b.msl_origin)
		&& all(a.msl_direction == b.msl_direction);
}

Ray __Ray_ctor(float3 o, float3 d)
{
	Ray r;
	r.msl_origin = o;
	r.msl_direction = d;
	return r;
}

float2 __float2_ctor(float v)
{
	return v.xx;
}

float3 __float3_ctor(float v)
{
	return v.xxx;
}

float4 __float4_ctor(float v)
{
	return v.xxxx;
}

struct Trace_options
{
	int ray_type;
	bool enable_environment;
};

Trace_options __Trace_options_ctor(
	int ray_type,
	bool enable_environment)
{
	Trace_options to;
	to.ray_type = ray_type;
	to.enable_environment = enable_environment;
	return to;
}


//
// Scale parameter for ddx and ddy calls
//
uniform float __differential_scale = 1.0;

#ifndef __TEXTURE_LOOKUP
#define __TEXTURE_LOOKUP

#endif // __TEXTURE_LOOKUP

void __make_basis(const float3 n, const float3 u, const float3 v,
out float3 t, out float3 b)
{
b = normalize(cross(u,n));
t = cross(n,b);
if (dot(b,v) < 0.0)  b = -b;
}

float3 __perspective_divide(float4 pt)
{
	return pt.xyz/pt.w;
}

// 
// State dependant variable recalculation. 
// 
void __update_state_origin_or_position(inout State state) 
{ 
	state.direction = state.position - state.origin; 
	state.ray_length = length(state.direction); 
	state.direction = normalize(state.direction); 
} 

uniform int __object_label = 0; 
// 
// Encoding of Renderer::label internal state (int) as float4. 
// 
float4 _label_as_color() 
{ 
	int n = __object_label; 
	float4 c; 
	c.a = float(n / 16777216); 
	c.b = float((n % 16777216) / 65536); 
	c.g = float((n % 65536) / 256); 
	c.r = float(n % 256); 
	c /= 255.0;
	return c; 
} 

uniform float4 __clip_plane = float4(1, 1, 0, 0);


//
// The following are free parameters of the shader
// that should be set by the application at runtime.
//
float4 msl_Oren_nayar_basis_material_1_diffuse_color
<
	string UIName = "Diffuse Color";
	float4 UIMin = float4(0.000000f, 0.000000f, 0.000000f, 0.000000f);
	float4 UIMax = float4(1.000000f, 1.000000f, 1.000000f, 1.000000f);
	float UIStep = 0.01;
	string UIWidget = "slider";
> = float4(1.000000f, 1.000000f, 1.000000f, 1.000000f);
float msl_Oren_nayar_basis_material_1_diffuse_scalar
<
	string UIName = "Diffuse Intensity";
	float UIMin = 0.000000f;
	float UIMax = 1.000000f;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 0.700000f;
float4 msl_Oren_nayar_basis_material_1_glossy_color
<
	string UIName = "Glossy Color";
	float4 UIMin = float4(0.000000f, 0.000000f, 0.000000f, 0.000000f);
	float4 UIMax = float4(1.000000f, 1.000000f, 1.000000f, 1.000000f);
	float UIStep = 0.01;
	string UIWidget = "slider";
> = float4(1.000000f, 1.000000f, 1.000000f, 1.000000f);
float msl_Oren_nayar_basis_material_1_glossy_scalar
<
	string UIName = "Glossy Intensity";
	float UIMin = 0.000000f;
	float UIMax = 1.000000f;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 0.060000f;
float msl_Oren_nayar_basis_material_1_diffuse_deviation
<
	string UIName = "Diffuse Deviation";
	float UIMin = 0.000000f;
	float UIMax = 3.141590f;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 0.100000f;
float msl_Oren_nayar_basis_material_1_glossy_shininess
<
	string UIName = "Glossy Shininess";
	float UIMin = 0.000000f;
	float UIMax = 100.000000f;
	float UIStep = 0.01;
	string UIWidget = "slider";
> = 15.000000f;


//
// The following are parameters representing non-varying state variables 
// referenced by the shader. These should be set by the application at runtime.
// Note that vector parameters should be provided in camera space.
//
float4x4 __object_to_ndc : WorldViewProjection
<
	string UIWidget = "none";
>;
bool __orthographic : Orthographic= false;
float4x4 __object_to_world : World
<
	string UIWidget = "none";
>;
float4x4 __object_to_camera : WorldView
<
	string UIWidget = "none";
>;
float4x4 __camera_to_world : ViewInverse
<
	string UIWidget = "none";
>;
float4x4 __camera_to_object : WorldViewInverse
<
	string UIWidget = "none";
>;
//                                                   
// The following are parameters representing non-varying non-state variables      
// referenced by the shader. These should be set by the application at runtime.             
//                                                   
float4x4 __internal_to_light;
float4x4 __light_to_internal;


#ifndef mi_orennayar_diffuse__float__float3__float3__float3__float3__float
#define mi_orennayar_diffuse__float__float3__float3__float3__float3__float 1
float mi_orennayar_diffuse(
	inout State state,
	float3 msl_lightDir,
	float3 msl_eyeDir,
	float3 msl_n,
	float3 msl_tangent,
	float msl_diffuse_deviation)
{
	float msl_sigma2 = msl_diffuse_deviation * msl_diffuse_deviation;
	float msl_A = 1.0f - (msl_sigma2 / (2.0f * (msl_sigma2 + 0.33f)));
	float msl_B = (0.45f * msl_sigma2) / (msl_sigma2 + 0.09f);
	float msl_cosThetaI = dot(msl_lightDir, msl_n);
	float msl_cosThetaO = -(dot(msl_eyeDir, msl_n));
	float msl_sinThetaI = sqrt((max(0.0f, (1.0f - (msl_cosThetaI * msl_cosThetaI)))));
	float msl_sinThetaO = sqrt((max(0.0f, (1.0f - (msl_cosThetaO * msl_cosThetaO)))));
	float msl_cosPhiI = dot(msl_lightDir, msl_tangent);
	float msl_cosPhiO = -(dot(msl_eyeDir, msl_tangent));
	float msl_sinPhiI = sqrt((max(0.0f, (1.0f - (msl_cosPhiI * msl_cosPhiI)))));
	float msl_sinPhiO = sqrt((max(0.0f, (1.0f - (msl_cosPhiO * msl_cosPhiO)))));
	float msl_maxcos = max(0.0f, ((msl_cosPhiI * msl_cosPhiO) + (msl_sinPhiI * msl_sinPhiO)));
	float msl_sinAlpha, msl_tanBeta;
	if (msl_cosThetaI > msl_cosThetaO)
	{
		msl_sinAlpha = msl_sinThetaO;
		msl_tanBeta = (msl_sinThetaI / msl_cosThetaI);
	}
	else
	{
		msl_sinAlpha = msl_sinThetaI;
		msl_tanBeta = (msl_sinThetaO / msl_cosThetaO);
	}
	return saturate(((msl_A + (((msl_B * msl_maxcos) * msl_sinAlpha) * msl_tanBeta)) / 3.14159265358979323846f));
}
#endif

#ifndef mi_phong_glossy__float__float3__float3__float3__float
#define mi_phong_glossy__float__float3__float3__float3__float 1
float mi_phong_glossy(
	inout State state,
	float3 msl_lightDir,
	float3 msl_eyeDir,
	float3 msl_n,
	float msl_exponent)
{
	float3 msl_refl = ((2.f * (-(dot(msl_eyeDir, msl_n)))) * msl_n) + msl_eyeDir;
	float msl_h = dot(msl_refl, msl_lightDir);
	return ((pow((max(msl_h, 0.f)), msl_exponent)) * (msl_exponent + 2.f)) / (2.0f * 3.14159265358979323846f);
}
#endif

#ifndef mi_phong_specular__float__float3__float3__float3__float
#define mi_phong_specular__float__float3__float3__float3__float 1
float mi_phong_specular(
	inout State state,
	float3 msl_lightDir,
	float3 msl_eyeDir,
	float3 msl_n,
	float msl_exponent)
{
	return mi_phong_glossy(state, msl_lightDir, msl_eyeDir, msl_n, msl_exponent);
}
#endif



//
// The App2vert structure defines the vertex attributes used by the vertex
// shader. The application should supply a vertex stream containing these 
// elements.
//
struct App2vert
{
	float3 position : POSITION;
	float3 normal : NORMAL;
	float3 tex_du : TANGENT;
	float3 tex_dv : BINORMAL;
};

//
// The Vert2frag_out structure defines values output by the vertex shader.
//
struct Vert2frag_out
{
	float4 hpos : POSITION;
	float3 tex_du : TEXCOORD0;
	float3 tex_dv : TEXCOORD1;
	float3 position : TEXCOORD2;
	float3 normal : TEXCOORD3;
};

//
// The Vert2frag_in structure defines values used by the fragment shader.
//
struct Vert2frag_in
{
	float4 hpos : POSITION;
	float3 tex_du : TEXCOORD0;
	float3 tex_dv : TEXCOORD1;
	float3 position : TEXCOORD2;
	float3 normal : TEXCOORD3;
};

//
// This function is the main method of the vertex shader.
//
Vert2frag_out vertex_main(
	App2vert vs_in)
{
	Vert2frag_out vs_out;
	float4 position = float4(vs_in.position, 1);
	vs_out.hpos = mul(position, __object_to_ndc);
	vs_out.position = mul(position, __object_to_camera).xyz;
	vs_out.normal = mul((float3x3)__camera_to_object, vs_in.normal);
	vs_out.tex_du = mul(vs_in.tex_du, (float3x3)__object_to_camera);
	vs_out.tex_dv = mul(vs_in.tex_dv, (float3x3)__object_to_camera);
	return vs_out;
}

//
// Declaration of final output type. For MRT, this is a struct.
//
typedef float4 FS_OUT;

// 
// The following functions are generated from the MetaSL implementation
// of the shaders that are part of the compiled shader graph.
// 

void Bsdf_oren_nayar_main(
	inout State state,
	float4 msl_diffuse_color,
	float msl_diffuse_scalar,
	float4 msl_glossy_color,
	float msl_glossy_scalar,
	float msl_diffuse_deviation,
	float msl_glossy_shininess,
	out float3 msl_result,
	out float3 msl_diffuse,
	out float3 msl_glossy)
{
	{
	msl_diffuse = (__float3_ctor(float(0)));
	msl_glossy = (__float3_ctor(float(0)));
	float4 msl_Rd = msl_diffuse_color * msl_diffuse_scalar;
	float4 msl_Rs = msl_glossy_color * msl_glossy_scalar;
	float4 msl_temp = msl_Rd + msl_Rs;
	float msl_maxc = max((max(msl_temp.r, msl_temp.g)), msl_temp.b);
	if (msl_maxc > 1.000000f)
	{
		msl_Rd /= __float4_ctor(msl_maxc);
		msl_Rs /= __float4_ctor(msl_maxc);
	}
	Light_iterator msl_light;
	;
	msl_diffuse *= (msl_Rd).xyz;
	msl_glossy *= (msl_Rs).xyz;
	msl_result = (msl_diffuse + msl_glossy);
}
	}

void surface_bsdf_brdfmain(
	inout State state,
	out float3 msl_Oren_nayar_basis_material_1_surface_bsdf_diffuse,
	out float3 msl_Oren_nayar_basis_material_1_surface_bsdf_specular,
	out float3 msl_Oren_nayar_basis_material_1_surface_bsdf_glossy)
{
	msl_Oren_nayar_basis_material_1_surface_bsdf_diffuse = (__float3_ctor(0.000000f));
	msl_Oren_nayar_basis_material_1_surface_bsdf_specular = (__float3_ctor(0.000000f));
	msl_Oren_nayar_basis_material_1_surface_bsdf_glossy = (__float3_ctor(0.000000f));
	float4 msl_Oren_nayar_basis_material_1_diffuse_color_interface = float4(1.000000f, 1.000000f, 1.000000f, 1.000000f);
	msl_Oren_nayar_basis_material_1_diffuse_color_interface = msl_Oren_nayar_basis_material_1_diffuse_color;
	float msl_Oren_nayar_basis_material_1_diffuse_scalar_interface = 0.700000f;
	msl_Oren_nayar_basis_material_1_diffuse_scalar_interface = msl_Oren_nayar_basis_material_1_diffuse_scalar;
	float4 msl_Oren_nayar_basis_material_1_glossy_color_interface = float4(1.000000f, 1.000000f, 1.000000f, 1.000000f);
	msl_Oren_nayar_basis_material_1_glossy_color_interface = msl_Oren_nayar_basis_material_1_glossy_color;
	float msl_Oren_nayar_basis_material_1_glossy_scalar_interface = 0.060000f;
	msl_Oren_nayar_basis_material_1_glossy_scalar_interface = msl_Oren_nayar_basis_material_1_glossy_scalar;
	float msl_Oren_nayar_basis_material_1_diffuse_deviation_interface = 0.100000f;
	msl_Oren_nayar_basis_material_1_diffuse_deviation_interface = msl_Oren_nayar_basis_material_1_diffuse_deviation;
	float msl_Oren_nayar_basis_material_1_glossy_shininess_interface = 15.000000f;
	msl_Oren_nayar_basis_material_1_glossy_shininess_interface = msl_Oren_nayar_basis_material_1_glossy_shininess;
	float3 msl_Oren_nayar_basis_material_1_surface_bsdf_result;
	Bsdf_oren_nayar_main(state, msl_Oren_nayar_basis_material_1_diffuse_color_interface, msl_Oren_nayar_basis_material_1_diffuse_scalar_interface, msl_Oren_nayar_basis_material_1_glossy_color_interface, msl_Oren_nayar_basis_material_1_glossy_scalar_interface, msl_Oren_nayar_basis_material_1_diffuse_deviation_interface, msl_Oren_nayar_basis_material_1_glossy_shininess_interface, msl_Oren_nayar_basis_material_1_surface_bsdf_result, msl_Oren_nayar_basis_material_1_surface_bsdf_diffuse, msl_Oren_nayar_basis_material_1_surface_bsdf_glossy);
}

void Default_surface_main(
	inout State state,
	out float4 msl_result)
{
	{
	float3 msl_direct_diffuse, msl_direct_specular, msl_direct_glossy;
	float3 msl_indirect_diffuse, msl_indirect_specular, msl_indirect_glossy;
	surface_bsdf_brdfmain(state, msl_direct_diffuse, msl_direct_specular, msl_direct_glossy);
	msl_indirect_diffuse=0, msl_indirect_specular=0, msl_indirect_glossy=0;
	msl_result = float4((((((msl_direct_diffuse + msl_indirect_diffuse) + msl_direct_specular) + msl_indirect_specular) + msl_direct_glossy) + msl_indirect_glossy),1.0);
}
	}

//
// The following method is the root function of the shader graph
//
float4 Oren_nayar_basis_material_1_surface_eval(State state, out FS_OUT eval_out)
{
	// primary_out: 
	float4 msl_result;
	Default_surface_main(state, msl_result);
	eval_out = float4(msl_result);
	return float4(msl_result);
}

//
// This function is the main method of the fragment shader. It initializes the
// values in the state structure that are used by nodes in the shader graph
// and produces the final result of the shader.
//
FS_OUT fragment_main(
	Vert2frag_in fs_in) : COLOR
{
	State state;
	state.position = fs_in.position;
	state.normal = normalize(fs_in.normal);
	state.origin = __orthographic ? float3(state.position.x, state.position.y, 0) : 0;
	state.ray_length = __orthographic ? abs(state.position.z) : length(state.position);
	state.direction = __orthographic ? float3(0,0,-1) : state.position/state.ray_length;
	float3 t_tex_tangent, t_tex_binormal;
	__make_basis(state.normal, fs_in.tex_du, fs_in.tex_dv, t_tex_tangent, t_tex_binormal);
	state.tangent[0] = t_tex_tangent;

	FS_OUT eval_out;  // multiple shader outputs
	float4 primary_out;  // primary shader output
	primary_out = Oren_nayar_basis_material_1_surface_eval(state, eval_out);
	FS_OUT fs_out;
	fs_out = primary_out;
	return fs_out;
}

//
// The following define the default technique and pass of the effect.
//
technique T0
{
	pass P0
	{
		ZEnable      = true;
		ZWriteEnable = true;
		CullMode     = none;
		VertexShader = compile vs_3_0 vertex_main();
		PixelShader  = compile ps_3_0 fragment_main();
	}
}



Attachments
tree.JPG
tree.JPG (14.44 KiB) Viewed 2842 times

pyrrr
Posts: 34
Joined: 13 May 2012, 15:50

Re: simple hlsl shader

Post by pyrrr » 19 May 2012, 11:16

i think i'm an advance 3d shader artist but have no shader programming experiences. i used mental mill to create metasl shaders in order to render in c4d / max the same image. so i have no idea what i'm doing ;)

GlareTechnologies

Re: simple hlsl shader

Post by GlareTechnologies » 25 May 2012, 11:19

it seems your are trying to implement the Oren Nayar Lighting Model. I suppose it is easier to adapt a human written implementation, rather then a generated one. These links might help:

http://content.gpwiki.org/index.php/D3D ... Oren-Nayar
http://tog.acm.org/resources/shaderx/In ... ectX_9.pdf

cheers, Glare-Technologies (Ventuz-VFX-Shader-Experts)

Post Reply