Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (Akow, TipmyPip, tomaslolo), 788 guests, and 11 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19055 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Human Skin Shader #419442
03/10/13 12:28
03/10/13 12:28
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187


Code:
//////////////////////////////
// Matrix
//////////////////////////////; 
float4x4 matWorldViewProj; 
float4x4 matWorld;
float4x4 matView;
float4x4 matWorldInv;

//////////////////////////////
// Lighting
//////////////////////////////
float4 vecLightPos[8];
float4 vecLightColor[8];

//////////////////////////////
// Coloring
//////////////////////////////
float4 DiffColor   = {0.9f, 1.0f, 0.9f, 1.0f};
float4 AmbiColor  = {0.1f, 0.1f, 0.1f, 1.0f};
float4 SubColor  = {1.0f, 0.2f, 0.2f, 1.0f};

float RollOff  = 0.2;

//////////////////////////////
// Texture Reference
//////////////////////////////
texture entSkin1;
texture entSkin2;
texture entSkin3;

sampler2D ColorSampler = sampler_state
{
	Texture = entSkin1;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = WRAP;
	AddressV = WRAP;
};

sampler NormalSampler = sampler_state
{ 
	Texture = entSkin2;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = WRAP;
	AddressV = WRAP;
};

sampler GlossSampler = sampler_state
{ 
	Texture = entSkin3;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = WRAP;
	AddressV = WRAP;
};

//////////////////////////////
// Data Structs
//////////////////////////////
struct appdata 
{
    float3 P			: POSITION;
    float4 UV			: TEXCOORD0;
    float4 N			: NORMAL;
    float3 T 			: TANGENT;
    float3 B 			: BINORMAL;
};

/* data passed from vertex shader to pixel shader */
struct shadedVertexOutput 
{
    float4 P			: POSITION;
    float4 Tex			: TEXCOORD0;
    float4 Col			: COLOR0;
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput 
{
    half4 P					: POSITION;
    half4 Tex				: TEXCOORD0;
    half3 L					: TEXCOORD1;
    half3 wN				: TEXCOORD2;
    float3 V 				: TEXCOORD3;
    float3 Tangent 		: TEXCOORD4;
    float3 Binormal 		: TEXCOORD5;    
};

//////////////////////////////
// Vertex Shader
//////////////////////////////
void lambskin(float3 N,float3 L,out float4 Diffuse,out float4 Subsurface) 
{
    float ldn = dot(L,N);
    float diffComp = max(0,ldn);
    
    Diffuse = float4((diffComp * DiffColor).xyz,1);
   
    float subLamb = smoothstep(-RollOff,1.0,ldn) - smoothstep(0.0,1.0,ldn);
   
    subLamb = max(0.0,subLamb);
   
    Subsurface = subLamb * SubColor;
}

void lamb_ps_shared(vertexOutput IN,out float4 DiffuseContrib,out float4 SubContrib)
{
     
    half3 Ln = normalize(IN.L); 
    half3 Nn = normalize(IN.wN);
	
	 lambskin(Nn,Ln,DiffuseContrib,SubContrib);
}

vertexOutput simpleVS(appdata IN) 
{
	vertexOutput OUT;
	
	half4 Po = half4(IN.P.xyz,1);
	OUT.P = mul(Po, matWorldViewProj);

	OUT.Tex = IN.UV;

	float3 vP = mul(IN.P,matWorld);
	float4 vN = normalize(IN.N);

	OUT.L = float4(0.f,0.f,0.f,0.f);
   
   OUT.wN = mul(vN, matWorldInv).xyz;
   OUT.Tangent = normalize(mul(IN.T, matWorldInv));
   OUT.Binormal = normalize(mul(IN.B, matWorldInv));  
	
	half3 Pw = mul(Po, matWorld).xyz;
   for (int i=0; i<8; i++)
   {
      OUT.L+= normalize(vecLightPos[i] - Pw);
   }

	OUT.V = mul(vP, matView);
	
	return OUT;
}

//////////////////////////////
// Pixle Shader
//////////////////////////////
float4 lambPS_t(vertexOutput IN) : COLOR 
{
	float4 diffContrib;
	float4 subContrib;
	
	float4 Color = tex2D(ColorSampler, IN.Tex);
	float4 Gloss = tex2D(GlossSampler,IN.Tex);
	float4 Bump = tex2D(NormalSampler,IN.Tex);
	
	//LambSkin
	lamb_ps_shared(IN,diffContrib,subContrib);	
	float4 litC = diffContrib + AmbiColor + subContrib;

	//Normal Mapping
   float3 bumpNormal = IN.wN + (Bump.x * IN.Tangent + Bump.y * IN.Binormal);
   bumpNormal = normalize(bumpNormal);
   
	//Gloss
   float3 Normal = normalize(IN.wN);
   float3 LightDir = normalize(IN.L);
   float3 ViewDir = normalize(IN.V);
   float Diff = saturate(dot(Normal, LightDir));
   float3 Reflect = normalize(2 * dot(Diff, bumpNormal) * bumpNormal - LightDir);  
   float Specular = pow(saturate(dot(Reflect, ViewDir)), 20);   	
	Specular = Specular*Gloss.x;
	    	
	float4 FinalColor = (litC*Color)+Specular;	
	return FinalColor;
}

technique HumanSkin 
 {		
 	pass p0
 	{
		ZEnable = true;
		ZWriteEnable = true;
		CullMode = None;
		VertexShader = compile vs_2_0 simpleVS();
		PixelShader = compile ps_2_0 lambPS_t();
	}
}



It seems to be working correctly, however I am running into an issue...Whenever I add the Shadow Demo from the workshop The face becomes transparent...I do not know why this is happening as this is the first real shader I have tried to piece together
from reading on the net help here and all...so if any experts out there could take a glance at my code...point out a few things I am doing wrong...I would appreciate the critique.


John C Leutz II

Re: Human Skin Shader [Re: lostzac] #419463
03/10/13 15:41
03/10/13 15:41
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
Hm, throwing a shader together with shadow mapping is not so easy, because you have to write the depth of the mesh into the depthmap of the shadowmapping view -and- then you have to evaluate the visibility of the shader's current pixel in the depthmap to estimate, if the pixel is lit or shadowed...

Re: Human Skin Shader [Re: HeelX] #419519
03/11/13 10:33
03/11/13 10:33
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
I am not sure I am following...sorry for all the questions, but I am grateful for all the help.

So backtracking, I have taking the shadow map demo off from the workshop and the only thing implemented for shadows is I have set it to shadow_stencil = 2; The only thing in this picture casting a shadow is the hair



As you can see the back of the hairs shadows are showing on the face. When I set the CAST flag on the face the issue is not there...so I know it has to do with shadowing.....and the skin fx...Just not sure where to start looking....


John C Leutz II

Re: Human Skin Shader [Re: lostzac] #419521
03/11/13 10:50
03/11/13 10:50
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
I take it back even with no materials assigned to the models I am still having the issue.....Gonna have to think about this


John C Leutz II

Re: Human Skin Shader [Re: lostzac] #419612
03/12/13 13:59
03/12/13 13:59
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
ok fixed it....issue was in the hair...

Code:
//////////////////////////////
// Organic Skin Shader
// Converted John C Leutz II
//////////////////////////////


//////////////////////////////
// Matrix
//////////////////////////////; 
float4x4 matWorldViewProj; 
float4x4 matWorld;
float4x4 matView;
float4x4 matWorldInv;
float4x4 matViewInv;

//////////////////////////////
// Lighting
//////////////////////////////
float4 vecLightPos[8];
float4 vecLightColor[8];
float4 vecAmbient;

//////////////////////////////
// Coloring
//////////////////////////////
float4 DiffColor   = {0.9f, 1.0f, 0.9f, 1.0f};
float4 AmbiColor  = {0.1f, 0.1f, 0.1f, 1.0f};
float4 SubColor  = {1.0f, 0.2f, 0.2f, 1.0f};
float3 SpecColor  = {0.7f, 0.7f, 1.0f};

float GlossTopUI = 0.46;
float GlossBotUI  = 0.41;
float GlossDrop  = 0.25;

float RollOff  = 0.2;
float Ks = 0.5;
float SpecExpon = 6.0;

float PhongExp  = 128.0;

//////////////////////////////
// Texture Reference
//////////////////////////////
texture entSkin1;
texture entSkin2;
texture entSkin3;

sampler2D ColorSampler = sampler_state
{
	Texture = entSkin1;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = WRAP;
	AddressV = WRAP;
};

sampler NormalSampler = sampler_state
{ 
	Texture = entSkin2;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = WRAP;
	AddressV = WRAP;
};

sampler GlossSampler = sampler_state
{ 
	Texture = entSkin3;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = WRAP;
	AddressV = WRAP;
};

//////////////////////////////
// Data Structs
//////////////////////////////
struct appdata 
{
    float3 Pos			: POSITION;
    float4 UV			: TEXCOORD0;
    float4 Norm		: NORMAL;
    float4 Tangent	: TANGENT0;
    float4 Binormal	: BINORMAL0;
    float4 Color		: COLOR0;    
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput 
{
    half4 Pos				: POSITION;
    half4 Tex				: TEXCOORD0;
    half3 Light			: TEXCOORD1;
    half3 WorldNorm		: TEXCOORD2;
    float3 WorldTangent	: TEXCOORD3;
    float3 WorldBinormal: TEXCOORD4;
    float3 WorldView		: TEXCOORD5;
    float4 Color			: COLOR0;   
   
};

//////////////////////////////
// Vertex Shader
//////////////////////////////
void lambskin(float3 N,float3 L,out float4 Diffuse,out float4 Subsurface) 
{
    float ldn = dot(L,N);
    float diffComp = max(0,ldn);
    
    Diffuse = float4((diffComp * DiffColor).xyz,1);
   
    float subLamb = smoothstep(-RollOff,1.0,ldn) - smoothstep(0.0,1.0,ldn);
   
    subLamb = max(0.0,subLamb);
   
    Subsurface = subLamb * SubColor;
}

float glossy_drop(float v,
		    uniform float top,
		    uniform float bot,
		    uniform float drop)
{
    return (drop+smoothstep(bot,top,v)*(1.0-drop));
}

vertexOutput simpleVS(appdata IN) 
{
	vertexOutput OUT;
	
	half4 Po = half4(IN.Pos.xyz,1);
	OUT.Pos = mul(Po, matWorldViewProj);

	OUT.Tex = IN.UV;

	float4 vN = normalize(IN.Norm);

	OUT.Light = float4(0.f,0.f,0.f,0.f);
   
   OUT.WorldNorm = mul(vN, matWorldInv).xyz;
   OUT.WorldTangent = mul(IN.Tangent,matWorldInv).xyz;
   OUT.WorldBinormal = mul(IN.Binormal,matWorldInv).xyz;
	
	half3 Pw = mul(Po, matWorld).xyz;
   for (int i=0; i<8; i++)
   {
      OUT.Light+= normalize(vecLightPos[i] - Pw);
   }

	OUT.WorldView = normalize(matViewInv[3].xyz - Pw.xyz);
	OUT.Color = IN.Color;
	return OUT;
}

//////////////////////////////
// Pixle Shader
//////////////////////////////
void lamb_ps_shared(vertexOutput IN,out float4 DiffuseContrib,out float4 SubContrib)
{
     
    half3 Ln = normalize(IN.Light); 
    half3 Nn = normalize(IN.WorldNorm);
	
	 lambskin(Nn,Ln,DiffuseContrib,SubContrib);
}

void gloss_shared(vertexOutput IN,
	    uniform float3 SurfaceColor,
	    uniform float Ks,
	    uniform float SpecExpon,
	    uniform float3 SpecColor,
	    uniform float GlossTopUI,
	    uniform float GlossBotUI,
	    uniform float GlossDrop,
	    uniform float3 AmbiColor,
	    out float3 DiffuseContrib,
	    out float3 SpecularContrib)
{
    float3 Ln = normalize(IN.Light.xyz);
    float3 Nn = normalize(IN.WorldNorm);
    float3 Vn = normalize(IN.WorldView);
    float3 Hn = normalize(Vn + Ln);
    float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
    
    float spec = litV.y * litV.z;
    float GlossTop = max(GlossTopUI,GlossBotUI);
    float GlossBot = min(GlossTopUI,GlossBotUI);
    
    spec *= (Ks * glossy_drop(spec,GlossTop,GlossBot,GlossDrop));
    SpecularContrib = spec * SpecColor;
    
    DiffuseContrib = litV.y * SurfaceColor + AmbiColor;
}

float4 lambPS_t(vertexOutput IN) : COLOR 
{
	float4 diffContrib;
	float4 subContrib;
	float3 specContrib;
	
	float4 Color = tex2D(ColorSampler, IN.Tex);
	float4 Gloss = tex2D(GlossSampler,IN.Tex);
	float3 Bump = tex2D(NormalSampler,IN.Tex).xyz - float3(0.5,0.5,0.5);
	
	//LambSkin
	lamb_ps_shared(IN,diffContrib,subContrib);	
	float4 litC = diffContrib + AmbiColor + subContrib;
   	
   //Gloss	
 	gloss_shared(IN,Color.rgb,Ks,SpecExpon,Gloss.rgb,GlossTopUI,GlossBotUI,GlossDrop,AmbiColor,diffContrib.rgb,specContrib); 
   float3 FinalSpec = diffContrib + specContrib;
   
   //NormalMap
   // transform tNorm to world space
   Bump = normalize(Bump.x*IN.WorldTangent - Bump.y*IN.WorldBinormal + Bump.z*IN.WorldNorm);

   // view and light directions
   float3 Vn = normalize(IN.WorldView);
   float3 Ln = normalize(IN.Light.xyz-IN.WorldView);

 	// compute diffuse and specular terms
 	FinalSpec = saturate(dot(normalize(Ln-Vn),Bump));
	FinalSpec = pow(FinalSpec,PhongExp);
       
	float4 FinalColor = (litC*Color)*(FinalSpec,1);
	FinalColor.a=1;
	return FinalColor;
}

technique HumanSkin 
 {		
 	pass p0
 	{
		ZEnable = true;
		ZWriteEnable = true;
		CullMode = None;
		ZFunc = LessEqual;
		AlphaBlendEnable = false;		
		VertexShader = compile vs_2_0 simpleVS();
		PixelShader = compile ps_2_0 lambPS_t();
	}
}




any additional input would be welocomed

Last edited by lostzac; 03/12/13 14:03.

John C Leutz II


Moderated by  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1