Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 552 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 3 1 2 3
Re: Hair Shader [Re: xbox] #417677
02/15/13 13:32
02/15/13 13:32
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
Yeah in the past i have counted on the kindness of other programmers like HeelX contributions to handle any Shaders I thought I would need, and spent my time learning c lite.

Thanks for the wish, I have managed to get the coloring down right, and working on alpha mask, and lighting now....


John C Leutz II

Re: Hair Shader [Re: lostzac] #419613
03/12/13 14:09
03/12/13 14:09
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
Ok so in working on this shader I am coming up with two issues I seek input on..





I can not figure out why the Hair is coming up with the blueish color,and its also causing my eye [Pic2] to display through the skin...(This only happens when I have the Hair material on)

Here is my code thus far
Code:
//////////////////////////////
// Hair Fx
//////////////////////////////

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

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

//////////////////////////////
// Coloring
//////////////////////////////
float specExp1 = 10;
float specExp2 = 0;

//////////////////////////////
// Tweakables
//////////////////////////////
float primeShift = 10;
float secondShift = 15;

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

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

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

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

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

//////////////////////////////
// Data Structs
//////////////////////////////
struct appdata 
{
    float3 Position	: POSITION;
    float4 UV		: TEXCOORD0;
    float4 Normal	: NORMAL;
    float4 Tangent	: TANGENT0;
    float4 Binormal	: BINORMAL0;
};

struct vertexOutput 
{
    float4 HPosition	: POSITION;
    float2 UV		: TEXCOORD0;
    float3 LightVec	: TEXCOORD1;
    float3 WorldNormal	: TEXCOORD2;
    float3 WorldTangent	: TEXCOORD3;
    float3 WorldBinormal : TEXCOORD4;
    float3 WorldView	: TEXCOORD5;
    float4 LightColor : COLOR0;
};

//////////////////////////////
// Additonal Functions
//////////////////////////////
float3 ShiftTangent(float3 T, float3 N, float shift)
{
	float3 shifttedT = T + shift * N;
	return normalize(shifttedT);
}

float StrandSpecular(float3 T, float3 V, float3 L, float exponet)
{
	float3 H = normalize(L+V);
	float dotTh = dot(T,H);
	float sinTh = sqrt(1.0 - dotTh * dotTh);
	float dirAtten = smoothstep(-1.0,0.0, dot(T,H));
	return dirAtten * pow(sinTh,exponet);
}

float4 DoPointLight(float3 P, float3 N, int i)
{
	// calculate the light ray pointing from the light to the surface
   float3 D = (float3)vecLightPos[i]-P;
	
	// calculate the angle between surface and light ray
   float NdotL = dot(N,normalize(D));

	// modulate the light by the surface angle
   float4 Color = vecLightColor[i] * NdotL;

	// calculate the light attenuation factor
   float fac = 0.f;
   if (NdotL >= 0.f && vecLightPos[i].w > 0.f)
   {
		// get the distance factor
      float LD = length(D)/vecLightPos[i].w;
      if (LD < 1.f)
        fac = 1.f - LD;
   }
   return Color * fac;
}


//////////////////////////////
// Vertex Shader
//////////////////////////////
vertexOutput simpleVS(appdata IN) 
{
   vertexOutput OUT = (vertexOutput)0;
    
   OUT.WorldNormal = mul(IN.Normal,matWorldInv).xyz;
    
   OUT.WorldTangent = mul(IN.Tangent,matWorldInv).xyz;
    
   OUT.WorldBinormal = mul(IN.Binormal,matWorldInv).xyz;
    
   float4 Po = float4(IN.Position.xyz,1); // homogeneous location
    
   float4 Pw = mul(Po,matWorld);	// convert to "world" space

   float3 N = normalize(IN.Normal);
   float3 P = mul(IN.Position,matWorld);

   for (int i=0; i<8; i++)
   {
      OUT.LightVec = vecLightPos[i] - Pw.xyz;
		OUT.LightColor += DoPointLight(P,N,i);  
   }
 
    OUT.UV = IN.UV.xy;

    OUT.WorldView = normalize(matViewInv[3].xyz - Pw.xyz);

    OUT.HPosition = mul(Po,matWorldViewProj);

    return OUT;
}

//////////////////////////////
// Pixle Shader
//////////////////////////////
float4 hairPS_t(vertexOutput IN) : COLOR 
{
	float shiftTex = tex2D(ShiftSampler, IN.UV) - 0.5;
	float t1 = ShiftTangent(IN.WorldTangent,IN.WorldNormal,primeShift+shiftTex);
	float t2 = ShiftTangent(IN.WorldTangent,IN.WorldNormal,secondShift+shiftTex);
	
	float3 diffuse = saturate(lerp(0.25,1.0,dot(IN.WorldNormal,IN.LightVec)));
	diffuse*=vecDiffuse.rgb;
	
	float3 specular = vecSpecular * StrandSpecular(t1,IN.WorldView,IN.LightVec,specExp1);
	float specMask = tex2D(SpecSampler,IN.UV);
	specular+= vecSpecular * specMask * StrandSpecular(t2,IN.WorldView,IN.LightVec,specExp2);

	float4 FinalColor = (0.0,0.0,0.0,1.0);
	float4 Color = tex2D(ColorSampler,IN.UV);
	FinalColor.rgb = (diffuse+specular) * Color.rgb * IN.LightColor;
	FinalColor.rgb*= tex2D(AmbOccSampler,IN.UV).rgb;
	FinalColor.a = Color.a;
	return Color;
}

///////////////////////////////////////
/// TECHNIQUES 
////////////////////////
technique Hair
 {		
 	pass p0
 	{
		ZEnable = true;
		ZWriteEnable = true;
		CullMode = ccw;
		ZFunc = Less;
		AlphaBlendEnable=true;
		//SrcBlend=srcalpha;
		//DestBlend=invsrcColor;
		AlphaTestEnable = True;
		BlendOpAlpha = ADD;	
		VertexShader = compile vs_2_0 simpleVS();
		PixelShader = compile ps_2_0 hairPS_t();
	}	
}



again seeking any ideas to these issues


John C Leutz II

Re: Hair Shader [Re: lostzac] #419618
03/12/13 14:54
03/12/13 14:54
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
Im not very farmilar with shader programming but the blue could be the same as the background color, try to chnage the color.
For the Z-Buffer sorting bug use this:

zWriteEnable = true;
alphaTestEnable = true;
alphaBlendEnable = false;

And remove BlendOpAlpha = ADD;

Last edited by Ch40zzC0d3r; 03/12/13 15:19.
Re: Hair Shader [Re: Ch40zzC0d3r] #419628
03/12/13 17:16
03/12/13 17:16
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
Originally Posted By: Ch40zzC0d3r
Im not very farmilar with shader programming but the blue could be the same as the background color, try to chnage the color.
For the Z-Buffer sorting bug use this:



zWriteEnable = true;
alphaTestEnable = true;
alphaBlendEnable = false;

And remove BlendOpAlpha = ADD;



the alphaBlendEnable change to false fixed both issues
Thank you

Last edited by lostzac; 03/12/13 17:20.

John C Leutz II

Re: Hair Shader [Re: lostzac] #419667
03/13/13 11:26
03/13/13 11:26
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
ok now I am running into a similar issue with the beards...



This is without AlphaBlending or AlphaTest as I wanted to make sure it was creating the shells



This one is AlphaBlendEnable = True



This one is AlphaTestEnable = True and AlphaBlendEnable = false

Here is the code I am using...(Fur Shader off wiki with less shells basicaly)

Code:
//////////////////////////////
// Hair Fx
//////////////////////////////
#define nrm normalize
#define sat saturate
#define dst distance

//////////////////////////////
// Matrix
//////////////////////////////
float4x4 matWorld;
float4x4 matViewProj;
float4x4 matWorldViewProj;
float4 vecViewPos;
float4 vecTime;

//////////////////////////////
// Lighting
//////////////////////////////
float4 vecSunDir;
float4 vecAmbient;
float4 vecLightColor[8];
float4 vecLightPos[8];
int iLights;

//////////////////////////////
// Coloring
//////////////////////////////


//////////////////////////////
// Tweakables
//////////////////////////////
float  vecSkill1;
float4 vecSkill41;
float4 vecSkill45;

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

sampler SKIN = sampler_state {texture = <entSkin1>;};
sampler FUR = sampler_state {texture = <entSkin2>;};


//////////////////////////////
// Data Structs
//////////////////////////////


//////////////////////////////
// Additonal Functions
//////////////////////////////



//////////////////////////////
// Vertex Shader
//////////////////////////////
float4 VS (	uniform float O,
		in float4 P : POSITION,
		in float2 T : TEXCOORD0,
		in float3 N : NORMAL,

		out float4 tex : TEXCOORD0,
		out float3 light : TEXCOORD1) : POSITION 
{		
	tex.xy = T;	
	tex.z = pow(O / 30,2);	
	tex.w = 0.5 - (dst(mul(P,matWorld),vecViewPos) > vecSkill1);
	
	P.xyz += vecSkill41.x * O * N + tex.z;
	float4 pos = mul(P,matWorld);
	//pos.y -= tex.z * vecSkill45.x;
	pos.z -= tex.z * vecSkill45.x;
	
	light = vecAmbient;
	for (int L=0; L<iLights; L++) 
		light += (1 - sat(length(pos.xyz - vecLightPos[L].xyz) / vecLightPos[L].w))
		* sat(dot(mul(N,matWorld),-nrm(pos - vecLightPos[L])))
		* vecLightColor[L] * 10;
	
	return mul(pos,matViewProj);
}

//////////////////////////////
// Pixle Shader
//////////////////////////////
float4 PS (	in float4 tex : TEXCOORD0,
		in float3 light : TEXCOORD1) : COLOR 
{
	clip(tex.w);

	float3 skin 	= tex2D(SKIN,tex.xy);
	float  len 	= tex2D(FUR,tex.xy).a;
	float4 fur 	= tex2D(FUR,tex.xy * vecSkill41.w);

	fur.a = (fur.rgb * vecSkill41.z - pow(tex.z,4)) * len;
	clip (fur.a);
	fur.rgb = lerp(fur,skin,vecSkill41.y);
	fur.rgb *= light;
	return fur;
}
	
///////////////////////////////////////
/// TECHNIQUES 
////////////////////////
technique t 
{	
	pass p 
	{
		ZEnable = true;
		ZWriteEnable = true;
		AlphaBlendEnable =false;
		AlphaTestEnable = TRUE;

		VertexShader = compile vs_2_0 VS(1);
		PixelShader = compile ps_2_0 PS();
	}
	
	pass p {VertexShader = compile vs_2_0 VS(2);PixelShader = compile ps_2_0 PS();}
	pass p {VertexShader = compile vs_2_0 VS(3);PixelShader = compile ps_2_0 PS();}
//	pass p {VertexShader = compile vs_2_0 VS(4);PixelShader = compile ps_2_0 PS();}
//	pass p {VertexShader = compile vs_2_0 VS(5);PixelShader = compile ps_2_0 PS();}
//	pass p {VertexShader = compile vs_2_0 VS(6);PixelShader = compile ps_2_0 PS();}
//	pass p {VertexShader = compile vs_2_0 VS(7);PixelShader = compile ps_2_0 PS();}
//	pass p {VertexShader = compile vs_2_0 VS(8);PixelShader = compile ps_2_0 PS();}
//	pass p {VertexShader = compile vs_2_0 VS(9);PixelShader = compile ps_2_0 PS();}
//	pass p {VertexShader = compile vs_2_0 VS(10);PixelShader = compile ps_2_0 PS();}
}



again any ideas would be helpful


John C Leutz II

Re: Hair Shader [Re: lostzac] #419671
03/13/13 11:54
03/13/13 11:54
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
I am assuming the reason I am running into these issues has to deal with a render order or something similar to do with the zDepth...Is that all possible ? The image should look like image 2 but with the whole face their ...


John C Leutz II

Re: Hair Shader [Re: lostzac] #419677
03/13/13 13:09
03/13/13 13:09
Joined: Jul 2005
Posts: 187
L
lostzac Offline OP
Member
lostzac  Offline OP
Member
L

Joined: Jul 2005
Posts: 187
solved...was in the way i was designing the maps to render the facial hair...





Still have to play around with the images and coloring to get the desired results...but at least I know where the problem is now


John C Leutz II

Re: Hair Shader [Re: lostzac] #419683
03/13/13 13:52
03/13/13 13:52
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
it's getting really cool!


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Hair Shader [Re: sivan] #419685
03/13/13 14:29
03/13/13 14:29
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
I'm not sure if the treatment of the alpha channel is right, because cutout alpha is a bad idea for realistic hair. I guess you will get sorting errors with alphablending, maybe applying an alpha-to-coverage PP step could solve it; read here: Covering New Ground: Foliage Rendering in Pure

Re: Hair Shader [Re: HeelX] #419687
03/13/13 14:52
03/13/13 14:52
Joined: Mar 2006
Posts: 2,252
Hummel Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
What they suggest in this paper is not alpha-to-coverage:
Originally Posted By: paper
Alpha-to-coverage converts the alpha value output by the pixel shader into a coverage
mask. This coverage mask is combined with the standard multisample coverage mask to
determine which samples should be updated.
Alpha-to-coverage, when combined with alpha testing, gives softer edges without
sacrificing the ability to use the z-buffer with unsorted primitives. Although this is an
improvement on simple alpha testing, the resulting alpha gradients can be of poor
quality compared to those obtained in alpha blending. This is particularly true when
using a low number of samples or on hardware that doesn't support flexible coverage
masks.
The cases where alpha-to-coverage delivers suitable results are rather rare, since it works best for translucent surfaces which cover a preferably moderately large and homogeneous colored/translucent screen space area. wink
The technique described in the paper should be worth a try, though.

Page 2 of 3 1 2 3

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