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
4 registered members (7th_zorro, Quad, VoroneTZ, 1 invisible), 623 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
dynamic per pixel lighting #163666
10/27/07 11:22
10/27/07 11:22
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
I have a huge optimized terrain (using a multitexture shader) with large faces that doesen't allow me to use vertex lghting for dynamic lights.
I'd be very glad if someone could post some code for using 3dgs dynamic lights as per pixel lights within my shader(hlsl).

Thanks!

Re: dynamic per pixel lighting [Re: Loopix] #163667
10/27/07 11:39
10/27/07 11:39
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
i've once posted such a shader. you might search this forum.

Re: dynamic per pixel lighting [Re: Joey] #163668
10/27/07 12:09
10/27/07 12:09
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
Thanks...I found it here but it would take me ages to implement that in my existing shader...I might be able to find out the phone-number of that girl you met last saturday...but only if you help me to replace the vertex-lighting with your pp-lighting into the following shader-code
Code:

material mat_terr_hlsl_2 {

flags = tangent;

skin1 = terr_blendtex_red_2;
skin2 = terr_blendtex_green_2;
skin3 = terr_blendtex_blue_2;
skin4 = terr_blendtex_black_2;

event=init_mat_terr_hlsl;

effect
"
/////////////////////////////////////////////////////////////////////////////////////////////
// Define your needed values
float4x4 matWorldViewProj;
float4x4 matWorld;
float4x4 matWorldInv;
float4x4 matWorldView;

float4 mtlSkill1;

float4 vecSkill41;

float4 vecSunDir;
float4 vecFog;
float4 vecLight;

float4 vecLightPos[8]; // preset this with light positions (xyz) and ranges (w)
float4 vecLightColor[8]; // preset this with light colors
float3 vecFalloff = float3(0.f, 0.f, 1.5f);
float4 vecSunDiffuse = {1.0f, 1.0f, 0.8f, 1.0f};

float sunStrength = 0.3f;


// Define your textures
texture mtlSkin1;
texture mtlSkin2;
texture mtlSkin3;
texture mtlSkin4;

texture entSkin1;
texture entSkin2;
texture entSkin3;
texture entSkin4;

/////////////////////////////////////////////////////////////////////////////////////////////
// Texture settings


sampler sTex1 = sampler_state
{
Texture = <entSkin1>; // rgb-blendmap
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex2 = sampler_state
{
Texture = <entSkin2>; // shadowmap
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex3 = sampler_state
{
Texture = <entSkin3>; // causticsA
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex4= sampler_state
{
Texture = <entSkin4>; // causticsB
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};


sampler sTex5 = sampler_state
{
Texture = <mtlSkin1>; // red
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex6 = sampler_state
{
Texture = <mtlSkin2>; // green
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex7 = sampler_state
{
Texture = <mtlSkin3>; // blue
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex8 = sampler_state
{
Texture = <mtlSkin4>; // black
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

//////////////////////////////////////////////////////////////////////
// return the sun light on the surface
float4 DoSunLight(float3 N)
{
// modulate the sunlight by the surface angle
return vecSunDiffuse * dot(N,-vecSunDir);
}

// return the dynamic light on the surface
float4 DoPointLight(float3 P, float3 N, int i)
{
// calculate the light ray pointing from the light to the surface
float3 D = vecLightPos[i].xyz - 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, DX uses a really strange formula here
float fac = 0.f;
if (NdotL >= 0.f && vecLightPos[i].w > 0.f)
{
// get the distance factor
float LD = length(D)/vecLightPos[i].w;
#ifdef DXLIGHTING
if (LD < 1.3f)
fac = 1.f/(vecFalloff.x + vecFalloff.y*LD + vecFalloff.z*LD*LD);
#else // linear Lighting
if (LD < 1.f)
fac = 1.f - LD;
#endif
}
return Color * fac;
}


float DoFog(float4 Pos)
{
// convert the vector position to view space to get it's depth (.z)
float3 P = mul(Pos,matWorldView);
// apply the linear fog formula
return saturate((vecFog.y-P.z) * vecFog.z);
}


struct TMULTI_VS_OUT // Output to the pixelshader fragment
{
float4 Pos : POSITION;
float Fog : FOG;

float4 tColor : COLOR0;

float2 Tex1 : TEXCOORD0;
float2 Tex2 : TEXCOORD1;

float2 Tex3 : TEXCOORD2;
float2 Tex4 : TEXCOORD3;

float2 Tex5 : TEXCOORD4;
float2 Tex6 : TEXCOORD5;
float2 Tex7 : TEXCOORD6;
float2 Tex8 : TEXCOORD7;
};

TMULTI_VS_OUT TMulti_VS(
float4 inPos : POSITION,
float3 inNormal : NORMAL,
float2 inTexCoord0 : TEXCOORD0)
{

TMULTI_VS_OUT Out;

// transform the vector position to screen coordinates
Out.Pos = mul(inPos, matWorldViewProj);
float3 N = normalize(mul(inNormal, matWorldInv));
float3 P = mul(inPos, matWorld);

// Add ambient and sun light
Out.tColor = ((DoSunLight(N) - 0.5f)*sunStrength)+0.7;

// Add 8 dynamic lights (maximum for vs 1.1)
for (int i=0; i<6; i++)
Out.tColor += DoPointLight(P,N,i);

// Add fog
Out.Fog = DoFog(inPos);

// scale the texture coordinates for the masked textures
Out.Tex1 = inTexCoord0.xy; // rgb-blendmap (not tiled)
Out.Tex2 = inTexCoord0.xy; // shadowmap (not tiled)

Out.Tex3 = (inTexCoord0.xy*vecSkill41.w+vecSkill41.w/50)+0.1* vecSkill41.xy;//sin(vecSkill41);//moving the caustics texture
Out.Tex4 = (inTexCoord0.xy*vecSkill41.w)-0.1* vecSkill41.y;//moving the caustics texture

Out.Tex5 = inTexCoord0.xy*40; // tiling texture red
Out.Tex6 = inTexCoord0.xy*60; // tiling texture green
Out.Tex7 = inTexCoord0.xy*20; // tiling texture blue
Out.Tex8 = inTexCoord0.xy*40; // tiling texture black



return Out;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
// pixelshader
float4 ps( TMULTI_VS_OUT In ) : COLOR
{
float4 BlendColor = tex2D(sTex1,In.Tex1);
float4 ShadowMap = tex2D(sTex2,In.Tex2);

float4 causticsA = tex2D(sTex3,In.Tex3);
float4 causticsB = tex2D(sTex4,In.Tex4);

float4 RedColor = tex2D(sTex5,In.Tex5);
float4 GreenColor = tex2D(sTex6,In.Tex6);
float4 BlueColor = tex2D(sTex7,In.Tex7);
float4 BlackColor = tex2D(sTex8,In.Tex8);


float4 BaseRed = lerp(BlackColor,RedColor,BlendColor.r);
float4 BaseGreen = lerp(BaseRed,GreenColor,BlendColor.g);


float4 FinalColor = lerp(BaseGreen,BlueColor,BlendColor.b);


float4 BaseCaustics = lerp(FinalColor,(FinalColor*(causticsA+causticsB)),ShadowMap.a);

//////////////////////////////////////////////////////////////////////////////////////////////


FinalColor = In.tColor*(ShadowMap*1.5)*BaseCaustics;

FinalColor = (FinalColor)*(mtlSkill1);

FinalColor.a = 1.0f; // prevent transparency

return FinalColor;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//
technique mytechnique
{
// Define your passes
pass p0
{
VertexShader = compile vs_2_0 TMulti_VS();
PixelShader = compile ps_2_0 ps();
}
}

technique fallback{pass p0{}}

";
}



Re: dynamic per pixel lighting [Re: Loopix] #163669
10/27/07 17:03
10/27/07 17:03
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
how can you know... last saturday... i thought nobody saw us

well, i'll have a look into it if i've some spare time left.

greetings, joey

Re: dynamic per pixel lighting [Re: Joey] #163670
10/27/07 18:58
10/27/07 18:58
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
hehe...thanks I'll start with the phone number investigation right now

Re: dynamic per pixel lighting [Re: Loopix] #163671
10/28/07 12:30
10/28/07 12:30
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
okay, i included my snippets... the code isn't very optimized, though, and i'm not quite sure it'll work. i haven't tested it. i've compressed your use of texture coordinates since i was in need of 2, too.

Code:
material mat_terr_hlsl_2 {

flags = tangent;

skin1 = terr_blendtex_red_2;
skin2 = terr_blendtex_green_2;
skin3 = terr_blendtex_blue_2;
skin4 = terr_blendtex_black_2;

event=init_mat_terr_hlsl;

effect
"
/////////////////////////////////////////////////////////////////////////////////////////////
// Define your needed values
float4x4 matWorldViewProj;
float4x4 matWorld;
float4x4 matWorldInv;
float4x4 matWorldView;

float4 mtlSkill1;

float4 vecSkill41;

float4 vecSunDir;
float4 vecFog;
float4 vecLight;

float4 vecLightPos[8]; // preset this with light positions (xyz) and ranges (w)
float4 vecLightColor[8]; // preset this with light colors
float3 vecFalloff = float3(0.f, 0.f, 1.5f);
float4 vecSunDiffuse = {1.0f, 1.0f, 0.8f, 1.0f};

float sunStrength = 0.3f;


// Define your textures
texture mtlSkin1;
texture mtlSkin2;
texture mtlSkin3;
texture mtlSkin4;

texture entSkin1;
texture entSkin2;
texture entSkin3;
texture entSkin4;

/////////////////////////////////////////////////////////////////////////////////////////////
// Texture settings


sampler sTex1 = sampler_state
{
Texture = <entSkin1>; // rgb-blendmap
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex2 = sampler_state
{
Texture = <entSkin2>; // shadowmap
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex3 = sampler_state
{
Texture = <entSkin3>; // causticsA
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex4= sampler_state
{
Texture = <entSkin4>; // causticsB
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};


sampler sTex5 = sampler_state
{
Texture = <mtlSkin1>; // red
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex6 = sampler_state
{
Texture = <mtlSkin2>; // green
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex7 = sampler_state
{
Texture = <mtlSkin3>; // blue
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler sTex8 = sampler_state
{
Texture = <mtlSkin4>; // black
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

//////////////////////////////////////////////////////////////////////
// return the sun light on the surface
float4 DoSunLight(float3 N)
{
// modulate the sunlight by the surface angle
return vecSunDiffuse * dot(N,-vecSunDir);
}

float4 ppLightCalc(float4 Color, float3 Position, float3 Normal, int i)
{
// direction towards light
float3 vecLight = vecLightPos[i].xyz - Position.xyz;
float3 dirLight = normalize(vecLight);

// diffuse lighting factor
float fDiff = clamp(dot(dirLight, Normal), 0, 1);

// direction towards view
float3 dirView = normalize(vecViewPos.xyz - Position.xyz);

// reflection vector for light
float3 reflection = reflect(-dirLight, Normal);

// specular lighting factor
float fSpec = pow(clamp(dot(reflection, dirView), 0, 1), 16);

// light color
float3 light = vecLightColor[i].rgb;

// attenuation
float attenuation = pow(clamp(1.0f - length(vecLight/vecLightPos[i].z), 0, 1), 0.5);

return float4(light*(fDiff*Color + fSpec)*attenuation, 1);
}

float DoFog(float4 Pos)
{
// convert the vector position to view space to get it's depth (.z)
float3 P = mul(Pos,matWorldView);
// apply the linear fog formula
return saturate((vecFog.y-P.z) * vecFog.z);
}


struct TMULTI_VS_OUT // Output to the pixelshader fragment
{
float4 Pos : POSITION;
float Fog : FOG;

float4 Light : COLOR0;

float4 Tex12 : TEXCOORD0;
float4 Tex34 : TEXCOORD1;

float4 Tex56 : TEXCOORD2;
float4 Tex78 : TEXCOORD3;

float3 WPos : TEXCOORD4;
float3 WNormal : TEXCOORD5;
};

TMULTI_VS_OUT TMulti_VS(
float4 inPos : POSITION,
float3 inNormal : NORMAL,
float2 inTexCoord0 : TEXCOORD0)
{

TMULTI_VS_OUT Out;

// transform the vector position to screen coordinates
Out.Pos = mul(inPos, matWorldViewProj);

// scale the texture coordinates for the masked textures
Out.Tex12 = float4(inTexCoord0.xy, inTexCoord0.xy); // rgb-blendmap (not tiled) and shadowmap

Out.Tex34 = float4((inTexCoord0.xy*vecSkill41.w+vecSkill41.w/50)+0.1* vecSkill41.xy,;//sin(vecSkill41);//moving the caustics texture
(inTexCoord0.xy*vecSkill41.w)-0.1* vecSkill41.y);//moving the caustics texture

Out.Tex56 = float4(inTexCoord0.xy*40, inTexCoord0.xy*60); // tiling texture red, green
Out.Tex78 = float4(inTexCoord0.xy*20, inTexCoord0.xy*40); // tiling texture blue, black

// normal, position for lighting
Out.WNormal = normalize(mul(inNormal, matWorldInv)).xyz;
Out.WPos = mul(inPos, matWorld).xyz;

// Add ambient and sun light
Out.Light = ((DoSunLight(N) - 0.5f)*sunStrength)+0.7;

// Add fog
Out.Fog = DoFog(inPos);

return Out;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
// pixelshader
float4 ps( TMULTI_VS_OUT In ) : COLOR
{
float4 BlendColor = tex2D(sTex1,In.Tex12.xy);
float4 ShadowMap = tex2D(sTex2,In.Tex12.zw);

float4 causticsA = tex2D(sTex3,In.Tex34.xy);
float4 causticsB = tex2D(sTex4,In.Tex34.zw);

float4 RedColor = tex2D(sTex5,In.Tex56.xy);
float4 GreenColor = tex2D(sTex6,In.Tex56.zw);
float4 BlueColor = tex2D(sTex7,In.Tex78.xy);
float4 BlackColor = tex2D(sTex8,In.Tex78.zw);


float4 BaseRed = lerp(BlackColor,RedColor,BlendColor.r);
float4 BaseGreen = lerp(BaseRed,GreenColor,BlendColor.g);


float4 FinalColor = lerp(BaseGreen,BlueColor,BlendColor.b);


float4 BaseCaustics = lerp(FinalColor,(FinalColor*(causticsA+causticsB)),ShadowMap.a);

//////////////////////////////////////////////////////////////////////////////////////////////

FinalColor = In.Light*(ShadowMap*1.5)*BaseCaustics;

FinalColor = (FinalColor)*(mtlSkill1);

int i;
float3 lighting = float3(0, 0, 0);
for (i = 0; i < 8; i ++) {
lighting += ppLightCalc(FinalColor, In.WPos, In.WNormal, i);
}

FinalColor += lighting;
FinalColor.a = 1.0f; // prevent transparency

return FinalColor;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//
technique mytechnique
{
// Define your passes
pass p0
{
VertexShader = compile vs_2_0 TMulti_VS();
PixelShader = compile ps_2_0 ps();
}
}

technique fallback{pass p0{}}

";
}



if you get some errors, just post them. you might be able to solve them by yourself, it's not that complicated code at all.

uum... any progress in her phone number?

joey

Re: dynamic per pixel lighting [Re: Joey] #163672
10/28/07 21:31
10/28/07 21:31
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
Big thanks...I'll try it asap!

hmm...I think I saw that girl fooling around with some other guy...let's just drop it

Re: dynamic per pixel lighting [Re: Loopix] #163673
10/29/07 14:05
10/29/07 14:05
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
ok..I have an error message "cannot implicitly convert (const float3) to float4"...and it points to that part of the code
Code:

...

float3 lighting = float3(0, 0, 0);
for (i = 0; i < 8; i ++) {
lighting += ppLightCalc(FinalColor, In.WPos, In.WNormal, i);

...
}

FinalColor += lighting;



any hints?

Re: dynamic per pixel lighting [Re: Loopix] #163674
10/29/07 15:19
10/29/07 15:19
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
just change the float3 lighting... by float4.

Re: dynamic per pixel lighting [Re: Joey] #163675
10/31/07 12:49
10/31/07 12:49
Joined: Mar 2005
Posts: 969
ch
Loopix Offline OP
User
Loopix  Offline OP
User

Joined: Mar 2005
Posts: 969
ch
hmm...I already tried that but it doesen't change anything

Page 1 of 2 1 2

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