|
|
shader snippets (2): pp-specular/diffuse, textured
#131928
05/25/07 18:43
05/25/07 18:43
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
OP
Expert
|
OP
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
ok, here we go with another shader, as always, if you understand something about shaders please don't forget to comment on it. the shader supports one texture and 8 dynamic lights with colors, one texture (can easily be changed to support more than one). is now ps 2_a, not 3_0. attenuation added, play with the exponent to get various interesting results. it's not physically correct (i think you should use log somehow), but a nice approximation.Code:
float4x4 matWorldViewProj; float4x4 matWorld;
float4 vecLightPos[8]; float4 vecLightColor[8]; float4 vecViewPos;
//------------------------------------ struct vertexInput { float4 Position : POSITION0; float3 Normal : NORMAL0; float4 texCoord : TEXCOORD0; };
struct vertexOutput { float4 Position : POSITION0; float4 texCoord : TEXCOORD0; float4 pos : TEXCOORD1; float3 posVec : TEXCOORD2; float3 normalVec : TEXCOORD3; };
struct pixelOutput { float4 Color : COLOR0; };
texture entSkin1;
sampler2D tex_sampler = sampler_state { Texture = <entSkin1>; MinFilter = Linear; MagFilter = Linear; MipFilter = None; };
float4 ppLightCalc(float4 Color, float3 Position, float3 Normal, int i) { 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); } }
//------------------------------------ void VS(in vertexInput a, out vertexOutput b) { // transform position b.Position = mul(a.Position, matWorldViewProj); // position for pixel shader b.pos = a.Position; // get the position of the vertex in the world b.posVec = mul(a.Position, matWorld).xyz; // get normal b.normalVec = mul(a.Normal, matWorld).xyz; // texture coordinate b.texCoord = a.texCoord; }
void PS(in vertexOutput b, out pixelOutput c) { int i; // ambient value c.Color = float4(0, 0, 0, 0); // per pixel diffuse and specular for (i = 0; i < 8; i ++) { c.Color += ppLightCalc(tex2D(tex_sampler, b.texCoord), b.posVec, b.normalVec, i); } }
//----------------------------------- technique simple { pass p0 { VertexShader = compile vs_1_1 VS(); PixelShader = compile ps_2_a PS(); } }
screens in the next posting (still without attenuation, maybe you can post some shots...).
Last edited by Joey; 05/26/07 13:02.
|
|
|
Re: shader snippets (2): pp-specular/diffuse, text
[Re: Joey]
#131929
05/26/07 10:39
05/26/07 10:39
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
OP
Expert
|
OP
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
here are two shots. it's applied to mat_shaded and reduced to accept 4 dynamic lights (faster!). note that the level texture is on TEXCOORD1, not on TEXCOORD0 (shadowmap).   notice the perfectly smooth specular cones and the hard edges (due to a phong algorithm). greetings, joey. edit: next thing to do would be to adapt the lighting to attenuation. that's no problem though.
Last edited by Joey; 05/26/07 10:44.
|
|
|
Re: shader snippets (2): pp-specular/diffuse, text
[Re: Joey]
#131930
05/28/07 15:51
05/28/07 15:51
|
Joined: Mar 2006
Posts: 2,503 SC, United States
xXxGuitar511
Expert
|
Expert
Joined: Mar 2006
Posts: 2,503
SC, United States
|
So this shader is applied to geometry and reacts to dynamic lights, correct?
From your statements, would it not then be possible to pass the texture and pre-rendered shadowmap to the shader then? Allowing to create a softer-(precompiled)-shadow system?
xXxGuitar511 - Programmer
|
|
|
Re: shader snippets (2): pp-specular/diffuse, text
[Re: Machinery_Frank]
#131934
05/28/07 22:04
05/28/07 22:04
|
Joined: May 2005
Posts: 2,713 Lübeck
Slin
Expert
|
Expert
Joined: May 2005
Posts: 2,713
Lübeck
|
Quote:
Is there a second texture with second uv-set?
Yep, entSkin2 and TEXCOORD0.
|
|
|
Re: shader snippets (2): pp-specular/diffuse, text
[Re: Slin]
#131935
05/29/07 06:06
05/29/07 06:06
|
Joined: Mar 2006
Posts: 2,503 SC, United States
xXxGuitar511
Expert
|
Expert
Joined: Mar 2006
Posts: 2,503
SC, United States
|
As long as the shader is kept simple, it shouldn't impact the framerate too badly...
...Unlike the normal/parallax mapping shaders shown before... lol.
xXxGuitar511 - Programmer
|
|
|
Re: shader snippets (2): pp-specular/diffuse, text
[Re: Joey]
#131937
05/29/07 12:13
05/29/07 12:13
|
Joined: Nov 2004
Posts: 7,121 Potsdam, Brandenburg, Germany
Machinery_Frank
Senior Expert
|
Senior Expert
Joined: Nov 2004
Posts: 7,121
Potsdam, Brandenburg, Germany
|
Interesting. Which version of Gamestudio did you use for that?
I tested it not only with a small level - I checked a bigger bsp level and even simple FFP-materials via auto-material produced less than 10 fps. The reason for that is: A6 sends every single polygon seperated to the graphics card when you use blocks. Models are faster because there it sends the complete model with all polygons and material at once to the GPU.
That will change with A7 though when you use the new options to compile meshes out of blocks.
Models, Textures and Games from Dexsoft
|
|
|
|