Earth glas shader

From GameStudio Wiki

Jump to: navigation, search

Contents

Earth glas shader with refraction and normalmapping for reflections

The earth_glas.fx file:

float4 vecAmbient;
float4 vecDiffuse;
float4 vecSpecular;
float4 vecEmissive;
// transformations
float4x4 matWorld;
float4x4 matWorldViewProj;
float3 vecViewPos;

float4 vecFog;
float4 vecTime;



texture entSkin1;
texture entSkin2;
texture mtlSkin1;

samplerCUBE EnvironmentSampler = sampler_state
{ 
	Texture = <mtlSkin1>;
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
};

sampler ColorSampler =sampler_state
{
	Texture=<entSkin1>;
	
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
};

sampler NormalSampler =sampler_state
{
	Texture=<entSkin2>;
	
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
};






// vertex shader output structure
struct VS_OUTPUT
{
	float4 Pos  : POSITION;
	float2 Tex : TEXCOORD0;      
	float3 Eye : TEXCOORD1;
	float3 Norm: TEXCOORD2;
	float3 m1: TEXCOORD3;
	float3 m2: TEXCOORD4;
	float3 m3: TEXCOORD5;
	float  Fog:  FOG;
};


VS_OUTPUT VS(
float4 inPos  : POSITION,
float3 inNormal : NORMAL,
float2  inTex  : TEXCOORD0,
float3 inTangent : TEXCOORD2) 
{
	VS_OUTPUT Out = (VS_OUTPUT)0;
	float4 Pos = mul(inPos,matWorld);
	float3 Normal = normalize(mul(inNormal, (float3x3)matWorld));
	Out.Pos  = mul(inPos, matWorldViewProj);
	Out.Fog = 1 - (distance(Pos, vecViewPos) - vecFog.x) * vecFog.z;
	Out.Tex = inTex;	
	Out.Eye=Pos - vecViewPos;
	Out.Norm=Normal;
	float3x3 matTangent;
	matTangent[0] = mul(inTangent,matWorld);
	matTangent[1] = mul(cross(inTangent,inNormal),matWorld);	// binormal
	matTangent[2] = mul(inNormal,matWorld);
	Out.m1=matTangent[0];
	Out.m2=matTangent[1];
	Out.m3=matTangent[2];
	return Out;
}

float4 PS ( VS_OUTPUT In) : COLOR
{
	float4 OutColor;
	float4 color = tex2D(ColorSampler, In.Tex);
	float4 normal = tex2D(NormalSampler, In.Tex)*2-1;
	float3x3 toWorld;
	toWorld[0]=In.m1;
	toWorld[1]=In.m2;
	toWorld[2]=In.m3;
	float3 norm=mul(normal.xyz,toWorld);
	float3 vec = reflect(normalize(In.Eye), norm);
	float4 Environment = texCUBE(EnvironmentSampler,vec);	
	float3 vec2 = refract(normalize(In.Eye), norm, 0.95);
	float3 vec3 = refract(normalize(In.Eye), norm, 0.9525);
	float3 vec4 = refract(normalize(In.Eye), norm, 0.955);
	float4 EnvRefract = float4(texCUBE(EnvironmentSampler,vec2).r,texCUBE(EnvironmentSampler,vec3).g,texCUBE(EnvironmentSampler,vec4).b,1);
	float fresnel = 1-(dot(norm,-normalize(In.Eye)));
	fresnel=pow(fresnel,1.5);
	OutColor.rgb=lerp(color.rgb,EnvRefract,color.a)+Environment.rgb*1*fresnel;
	OutColor.a=(1-color.a)+pow(fresnel,0.25);
	return OutColor;
}

float4 PS_back( VS_OUTPUT In) : COLOR
{
	float4 OutColor;
	float4 color = tex2D(ColorSampler, In.Tex);
	float3 norm=In.Norm;
	float3 vec = reflect(normalize(In.Eye), norm);
	float4 Environment = texCUBE(EnvironmentSampler,vec);	
	float fresnel = 1-abs(dot(norm,-normalize(In.Eye)));
	fresnel=0.4+pow(fresnel,1.5);
	OutColor.rgb=vecAmbient.rgb*Environment.rgb*1*fresnel;
	OutColor.a=(1-color.a);
	return OutColor;
}



technique glas
{
	pass P0
	{
		CullMode=cw;
		Zenable=true;
		ZWriteEnable=true;
		VertexShader = compile vs_2_0 VS();
		PixelShader  = compile ps_2_0 PS_back();
	}
	pass P1
	{
		CullMode=ccw;
		Zenable=true;
		ZWriteEnable=true;
		VertexShader = compile vs_2_0 VS();
		PixelShader  = compile ps_2_0 PS();
	}
}


C-Script example:


bmap bmp_cubemap=<mysky+6.tga>;
material mat_earth
{
	effect="earth_glas.fx";
	skin1=bmp_cubemap;
	flags=TRANSLUCENT;
	ambient_red=200;
	ambient_green=180;
	ambient_blue=150;	
}

action earth
{
	my.material=mat_earth;	
	bmap_to_cubemap(bmp_cubemap);
}


Adjustable through:


material parameter meaning
mtl.ambient_xxx color for the reflections on the back side


Remarks

First material skin is used for reflections and refraction. First model skin is color, alpha channel is used for transparency. Second model skin is a normalmap.

Uses ShaderModel: 2
Has fallback techniques: no
Uses fog: yes

--ChrisB 17:53, 27 September 2008 (CEST)

Personal tools