2 registered members (AndrewAMD, Lapsa),
1,331
guests, and 1
spider. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: 8 BIT SHOOTER
[Re: 3run]
#460228
06/20/16 14:41
06/20/16 14:41
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
here's an example object shader, I've marked the important parts with ***
//==========
#define ANGLE_SURFACE_DARKEN 0.75 // *** strength of the darkening effect; range [0 - 1]
//==========
const float fAmbient; // used as per-entity brightness factor here
const float4 vecViewPos;
const float4x4 matWorld;
const float4x4 matViewProj;
//==========
const texture entSkin1;
sampler2D smpTex = sampler_state
{
Texture = <entSkin1>;
MipFilter = Linear;
MagFilter = Point;
MinFilter = Point;
AddressU = Wrap;
AddressV = Wrap;
};
//==========
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 Tex : TEXCOORD0;
float3 Normal : NORMAL; // *** contains the object space normals of the block
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 Tex : TEXCOORD0;
float3 Light : TEXCOORD1; // *** used to store the calculated light value for the object
};
//==========
VertexShaderOutput VShader(VertexShaderInput input)
{
VertexShaderOutput output;
//
float4 vWorldPos = mul(input.Position, matWorld); // transform the coordinates in two steps (matWorld & matViewProj instead of matWorldViewProj) because the world position is needed
output.Position = mul(vWorldPos, matViewProj); // transform to the final screen-space coordinates
output.Tex = input.Tex; // pass the texture coordinates
float3 vPixelToViewDir = normalize(vecViewPos.xyz - vWorldPos.xyz); // *** direction vector from the surface to the camera
float3 vNormal = normalize(mul(float4(input.Normal, 0), matWorld).xyz); // *** transform surface normal from object space to world space
float dot_result = dot(vPixelToViewDir, vNormal); // *** get the angle ( cos(angle) ) between these vectors; both vectors in the dot product have to be normalized (length = 1)
output.Light = fAmbient.xxx; // *** light (= entity brightness) is defined by ent->ambient
output.Light *= saturate(1.0 - (1.0 - dot_result) * ANGLE_SURFACE_DARKEN); // *** apply the darkening factor with adjustable intensity; saturate() to prevent negative numbers (and numbers > 1)
//
return output;
}
//==========
float4 PShader(VertexShaderOutput In) : COLOR0
{
float4 Color = tex2D(smpTex, In.Tex);
Color.rgb *= In.Light; // *** simply apply the Light value recieved from the vertex shader to the texture
return Color;
}
//==========
technique Tech_PP
{
pass Pass_1
{
Lighting = False;
AlphaBlendEnable = False;
AlphaTestEnable = True;
VertexShader = compile vs_3_0 VShader();
PixelShader = compile ps_3_0 PShader();
}
}
if you're already using an object shader for your level it shouldn't be too hard to implement it. I've put the light-calculation in the vertexshader for performance reasons. It wouldn't be that much slower when it's in the pixel shader but there are way fewer vertices than pixels in your scene so it's a free preformance boost ![laugh laugh](/ubb7/images/graemlins/default_dark/laugh.gif) edit: it doesn't use fog though. if you're using the default shader, fog still needs to be implemented.
Last edited by Kartoffel; 06/20/16 14:55.
POTATO-MAN saves the day! - Random
|
|
|
Re: 8 BIT SHOOTER
[Re: 3run]
#460230
06/20/16 14:55
06/20/16 14:55
|
Malice
Unregistered
|
Malice
Unregistered
|
I didn't get this one to be honest. Maybe you meant something like 'bumpmapping' to 'faux it's shape', or what? Not really,Bump-mapping is to change normals to cause a 3d distortion. I'm talking about more like per-frame shadow mapping. Right now the whole sprite gets a uniformed shadow. A flat shadow across the flat surface. Which just adjust the surface color by a gray-scale(or color-scale). But the shadow can make the shape pop up if using a map. Pictures - QaD for example From top left - Hand and Gun - Shadow map - Shadow mapped hand 100% -Shadowmapped 50% ![](http://i.imgur.com/5iJmswd.png) Hand and Gun http://i.imgur.com/AmYh425.pngShadow map http://i.imgur.com/2AYDYAv.pngShadow mapped hand 100% http://i.imgur.com/F4tt9KM.pngShadowmapped 50% http://i.imgur.com/Sx5eilJ.pngSo you can get d-shadows with intensity by light distance and using 4 mapping per -frame you could lerp/blend (whatever) by light-source direction. So the shadow will faux the shaped of the sprite object. Anyway just an idea and post just for clarification sake. Great work man !
Last edited by Malice; 06/20/16 14:57.
|
|
|
Re: 8 BIT SHOOTER
[Re: ]
#460231
06/20/16 14:57
06/20/16 14:57
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
OP
Senior Expert
|
OP
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
I didn't get this one to be honest. Maybe you meant something like 'bumpmapping' to 'faux it's shape', or what? Not really,Bump-mapping is to change normals to cause a 3d distortion. I'm talking about more like per-frame shadow mapping. Right now the whole sprite gets a uniformed shadow. A flat shadow across the flat surface. Which just adjust the surface color by a gray-scale(or color-scale). But the shadow can make the shape pop up it using a map. Pictures - QaD for example From top left - Hand and Gun - Shadow map - Shadow mapped hand 100% -Shadowmapped 50% ![](http://i.imgur.com/5iJmswd.png) Hand and Gun http://i.imgur.com/AmYh425.pngShadow map http://i.imgur.com/2AYDYAv.pngShadow mapped hand 100% http://i.imgur.com/F4tt9KM.pngShadowmapped 50% http://i.imgur.com/Sx5eilJ.pngSo you can get d-shadows with intensity by light distance and using 4 mapping per -frame you could lerp/blend (whatever) by light-source direction. So the shadow will faux the shaped of the sprite object. Anyway just an idea and post just for clarification sake. Great work man ! This looks sweet. I got the idea now, but I guess I'm not going to implement this one, cause it's going to be out of place (to my taste), but it looks like a nice idea to make sprites less 'flat'. Thank you for kind words, your time and the idea man! My best regards!
|
|
|
Re: 8 BIT SHOOTER
[Re: ]
#460232
06/20/16 14:59
06/20/16 14:59
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
@3run: ![frown frown](/ubb7/images/graemlins/default_dark/frown.gif) if you'd like, I could take a look at the object shader. you can also pm me if you don't want to post the code here. @Malice: It's a cool effect, but it would require some kind of dynamic lighting which makes things a lot more complicated :S
POTATO-MAN saves the day! - Random
|
|
|
Re: 8 BIT SHOOTER
[Re: Kartoffel]
#460241
06/20/16 20:11
06/20/16 20:11
|
Malice
Unregistered
|
Malice
Unregistered
|
@Kartoffel if you wanted to blend sprite_s_maps to do dynamic shading yes you'd need d-light to get a source direction. But using one sprite_s_map like the example above , you can just read the value of the level shadow map under the entity and use it as the blend factor for sprite and it's sprite_s_Map.
Last edited by Malice; 06/20/16 20:12.
|
|
|
|