Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 10:32
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
6 registered members (AndrewAMD, alibaba, fairtrader, ozgur, TipmyPip, Quad), 604 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
shader snippets (1): diffuse (per-pixel)-lighting #131613
05/24/07 15:31
05/24/07 15:31
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline OP
Expert
Joey  Offline OP
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
hey!

since i'm about to learn shading, i'll post shader snippets for you to try out. you can of course comment them, i'm far away from being an professional so i'm grateful for any advice you may give me. all the snippets will be in hlsl and will need little to no adjustment to work with 3dgs, if so i'll state it in a comment. just save it in a .fx-file and load it. ok, here we go.

diffuse vertex lighting, beige coloured. only one light source, neither specular nor ambient light (i'll add that later on).

Code:
//------------------------------------
float4x4 matWVP : WorldViewProjection;
float4x4 matW : World;
float4 lightPos; // you need to set this to a proper light position

//------------------------------------
struct vertexInput {
float4 Position : POSITION0;
float3 Normal : NORMAL0;
};

struct vertexOutput {
float4 Position : POSITION0;
float4 Diffuse : COLOR0;
};

struct pixelOutput {
float4 Color : COLOR0;
};

//------------------------------------
void VS(in vertexInput a, out vertexOutput b)
{
// transform position
b.Position = mul(float4(a.Position.xyz , 1.0) , matWVP);

// get the position of the vertex in the world
float3 posWorld = mul(a.Position, matW).xyz;

// get normal
float3 normal = mul(a.Normal, matW).xyz;

// get light normal vector
float3 light = normalize(lightPos - posWorld);

b.Diffuse = float4(1, 0.9, 0.6, 1) * saturate(dot(light, normal));
}

void PS(in vertexOutput b, out pixelOutput c)
{
c.Color = b.Diffuse;
}

//-----------------------------------
technique simple
{
pass p0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_1_4 PS();
}
}



diffuse per pixel lighting, everything else is the same as above:
Code:
//------------------------------------
float4x4 matWVP : WorldViewProjection;
float4x4 matW : World;
float4 lightPos; // you need to set this to a proper light position

//------------------------------------
struct vertexInput {
float4 Position : POSITION0;
float3 Normal : NORMAL0;
};

struct vertexOutput {
float4 Position : POSITION0;
float3 lightVec : TEXCOORD0;
float3 normalVec : TEXCOORD1;
};

struct pixelOutput {
float4 Color : COLOR0;
};

//------------------------------------
void VS(in vertexInput a, out vertexOutput b)
{
// transform position
b.Position = mul(float4(a.Position.xyz , 1.0) , matWVP);

//getting the position of the vertex in the world
float3 posWorld = mul(a.Position, matW).xyz;

// get normal
b.normalVec = mul(a.Normal, matW).xyz;

// get light normal vector
b.lightVec = normalize(lightPos - posWorld);
}

void PS(in vertexOutput b, out pixelOutput c)
{
float intensity = saturate(dot(b.normalVec, b.lightVec));
c.Color = float4(1, 0.9, 0.6, 1) * intensity;
}

//-----------------------------------
technique simple
{
pass p0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_1_4 PS();
}
}



and the result:



per-pixel-lighting definately creates smoother shading.

joey.

Re: shader snippets (1): diffuse (per-pixel)-light [Re: Joey] #131614
05/24/07 17:04
05/24/07 17:04
Joined: Jun 2005
Posts: 4,875
broozar Offline
Expert
broozar  Offline
Expert

Joined: Jun 2005
Posts: 4,875
more of that, and i'm about to marry you thanks!

[edit] as i look at your code, may it be that no textures are supported right now?

Re: shader snippets (1): diffuse (per-pixel)-light [Re: broozar] #131615
05/24/07 17:19
05/24/07 17:19
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
Thank you very much!
So.., please Conitec, enable shaders for the beta version of LiteC

Re: shader snippets (1): diffuse (per-pixel)-light [Re: HeelX] #131616
05/25/07 06:54
05/25/07 06:54
Joined: Aug 2006
Posts: 652
Netherlands
bstudio Offline
User
bstudio  Offline
User

Joined: Aug 2006
Posts: 652
Netherlands
to add textures you'll just have to add them into the pixel en vertex shader and output them normally, no big deal


BASIC programmers never die, they GOSUB and don't RETURN.
Re: shader snippets (1): diffuse (per-pixel)-light [Re: bstudio] #131617
05/25/07 08:34
05/25/07 08:34
Joined: Oct 2002
Posts: 8,939
planet.earth
ello Offline
Senior Expert
ello  Offline
Senior Expert

Joined: Oct 2002
Posts: 8,939
planet.earth
Quote:

to add textures you'll just have to add them into the pixel en vertex shader and output them normally, no big deal




and dont forget to set up the state.. joey you can see how thats done in the shaders that come with the templates

Re: shader snippets (1): diffuse (per-pixel)-light [Re: ello] #131618
05/25/07 09:23
05/25/07 09:23
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline OP
Expert
Joey  Offline OP
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
or just add a texture lookup in the pixel shader. which of them is faster?

edit: and can anyone tell me why there's so various syntax supported? e.g. the return value of the pixel shader, either by out-argument, adding :COLOR0 or by return value. which is the most common one?

set your card supports 2.0 and the pixel shader is 1.4, might it run faster if i just set the version to 2.0 (keeping 1.4 as fallback) or will it be just as fast? fx composer at least tells me that it does not matter, is this a fact?

edit2: forgot: thank you and i'll post loads more stuff when your comments remain worth it.

Last edited by Joey; 05/25/07 09:31.
Re: shader snippets (1): diffuse (per-pixel)-light [Re: Joey] #131619
05/25/07 13:11
05/25/07 13:11
Joined: Sep 2002
Posts: 1,604
Deutschland
ChrisB Offline
Serious User
ChrisB  Offline
Serious User

Joined: Sep 2002
Posts: 1,604
Deutschland
Quote:

and can anyone tell me why there's so various syntax supported? e.g. the return value of the pixel shader, either by out-argument, adding :COLOR0 or by return value. which is the most common one?



I normaly do it this way:
Code:

float4 ps(VS_OUTPUT in) : COLOR
{
return color;
}


I think its easier to understand what the shader is returning when you have a return.^^

Quote:

set your card supports 2.0 and the pixel shader is 1.4, might it run faster if i just set the version to 2.0 (keeping 1.4 as fallback) or will it be just as fast? fx composer at least tells me that it does not matter, is this a fact?



It doesn't matter, at least thats what i know.


www.Swollen-Eyeballs.org
ICQ:169213431
#3dgs@quakenet
Re: shader snippets (1): diffuse (per-pixel)-light [Re: ChrisB] #131620
05/25/07 16:00
05/25/07 16:00
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline OP
Expert
Joey  Offline OP
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
okay, one question again, how can i calculate the direction of a point to the camera? the point is given.

Re: shader snippets (1): diffuse (per-pixel)-light [Re: Joey] #131621
05/25/07 16:05
05/25/07 16:05
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
dir = vecViewPos - point

Re: shader snippets (1): diffuse (per-pixel)-light [Re: Excessus] #131622
05/25/07 16:08
05/25/07 16:08
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline OP
Expert
Joey  Offline OP
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
omg i definately need a better reference than this crap here thanks, sorry for asking...

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