• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

adding attenuation to hlsl point light

15 posts in this topic

Hi,

I've been playing around with point lights in my d3d engine and got one working using my shader.

Although on the 'attenuation' side I have a challenge:

- I can now only control attenuation by changing "lightRange" from a range of 0.0 to 1.0 (0.1 being a 'big' range, 0.9 being a 'small' range), and with this, the range is far to high, meaning that meshes far away are also lit (a little).

 

What I want to achieve is that I can use "lightRange" to pass in a value/range in 'units' in my scene, up to which the light falls of and at 'lightRange' also ends. For now linear falloff is fine (or another falloff calculation which is relatively easiest).

 

Can someone help me in the right direction?

 

/***********************************************************/
/**  VARS NOT CONTROLLED BY ENGINE YET             *********/
/***********************************************************/

float4 diffuseColor = { 0.8f, 0.8f, 0.8f, 0.0f };    // NEW
float3 lightPosition = { 0.0f, 2.0f, 0.0f };   // NEW
float lightRange = 0.9f;    // NEW

// modelfile, just for effectedit
string XFile = "paving.x";   // model

/***********************************************************/
/**       VARS CONTROLLED BY ENGINE                *********/
/***********************************************************/

float4x4    World         : WORLD;
float4x4    WorldInvTransp: WORLDINVTRANSP;
float4x4    ViewProj      : VIEWPROJECTION;

float4      AmbientColor;
float       AmbientIntensity;

float4      MatAmb : MATERIALAMBIENT; 
float4      MatDiff: MATERIALDIFFUSE; 
            
texture     Tex0 < string name = "hungary009.jpg"; >;

/***********************************************************/
/**          SAMPLER STATES FOR TEXTURING          *********/
/***********************************************************/

sampler2D textureSampler = sampler_state
{
    Texture         = (Tex0);
                                 
    MinFilter       = ANISOTROPIC;
    MagFilter       = LINEAR;
    MipFilter       = LINEAR;    
    MaxAnisotropy   = 4;            
};

struct VS_INPUT
{
    float4    Pos      : POSITION0;
    float4    Normal   : NORMAL0;
    float2    TexCoord : TEXCOORD0;
};

struct VS_OUTPUT
{
    float4 Pos     : POSITION;
    float3 Normal  : TEXCOORD1;
    float2 TexCoord: TEXCOORD2;
    float3 LightPos: TEXCOORD3; 
    float3 Att     : TEXCOORD4;
};

/***********************************************************/
/**          THE VERTEXSHADER        PROGRAM       *********/
/***********************************************************/

VS_OUTPUT VS_function(VS_INPUT input)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    float4 worldPosition = mul(input.Pos, World);
    Out.Pos = mul(worldPosition, ViewProj);

    float4 normal = mul(input.Normal, WorldInvTransp);
    Out.Normal = normal;
    Out.TexCoord = input.TexCoord;

    Out.LightPos = lightPosition.xyz - worldPosition;   
    Out.LightPos = normalize(Out.LightPos);           

    Out.Att = Out.LightPos * lightRange;

    return Out;
}

/***********************************************************/
/**          THE PIXELSHADER        PROGRAM        *********/
/***********************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
    float4 textureColor = tex2D(textureSampler, input.TexCoord);

    float lightIntensity = saturate(dot(input.Normal, input.LightPos));    
    float4 color = diffuseColor * lightIntensity;          

    float4 amb = AmbientColor * AmbientIntensity * MatAmb;
    float4 attenuation = mul(input.Att, input.Att);

    return saturate((color + amb) * textureColor) * MatDiff * (1-attenuation); 
}

/***********************************************************/
/**          OPAUQE SHADER, REGULAR MESHES         *********/
/***********************************************************/

technique OpaqueShader
{
    pass P0              
    {
        AlphaBlendEnable    = FALSE;
                              
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

/***********************************************************/
/**   BLENDING SHADER, MESHES WITH BLENDED TEXTURES  *******/
/***********************************************************/

technique BlendingShader
{
    pass P0              
    {
        AlphaBlendEnable    = TRUE;
        SrcBlend            = SRCALPHA;
        DestBlend           = INVSRCALPHA;
        AlphaOp[0]          = SelectArg1;
        AlphaArg1[0]        = Texture;
                               
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

 

0

Share this post


Link to post
Share on other sites

What I want to achieve is that I can use "lightRange" to pass in a value/range in 'units' in my scene, up to which the light falls of and at 'lightRange' also ends. For now linear falloff is fine (or another falloff calculation which is relatively easiest).

Maybe no one answered because it's still unclear what you want. Do you to pass in just a single value that indicates the number of world units over which the light goes from full to zero (with linear falloff)?
0

Share this post


Link to post
Share on other sites
Hi phil. Thanks

That's the aimed result i want to achieve, yes.
With hopefully a little understanding how it's been calculated in the shader/math
0

Share this post


Link to post
Share on other sites

Well it's basically just the equation of a line, clamped to (0, 1).

Conceptually, I would think of it like this:

- If d is the distance between the light and the current point being shaded,
- And FallOffDistance is a shader constant for the fall off distance of the light in world units,
- Then (d / FallOffDistance) gives you a number that goes from (0 -> 1) as distance increases.
- That's the opposite of what you want, so invert it by subtracting it from 1.

float lightIntensity = 1f - (d / FallOffDistance);

Of course, you need to account for d being larger than FallOffDistance, so clamp the original term to (0, 1):

float lightIntensity = 1f - saturate(d / FallOffDistance);

 

 

If you want the light to remain full power for a certain distance before beginning to fall off, you can adjust distance by this value:

 

float lightIntensity = 1f - saturate((d - FullPowerDistance) / FallOffDistance);

 

And if you want a more realistic falloff (like 1 / R^2), since the lightIntensity is between (0, 1), it is easy to apply a power curve:

 

lightIntensity = pow(lightIntensity, 2);

 


 

0

Share this post


Link to post
Share on other sites

Hi Phil,

Thanks for your help. I've been playing around but unfortunately didn't get it to work yet.

 

Here's what I have now, based on your help/ explanation.

Resulting in a "error X4502: invalid reference to input semantic 'POSITION0'.

I found out that it's caused by the line where I calculatie 'distt' (distance from point light to shaded point in worldspace)

 

/***********************************************************/
/**  VARS NOT CONTROLLED BY ENGINE YET             *********/
/***********************************************************/

float4 	diffuseColor 	= { 0.8f, 0.8f, 0.8f, 0.0f };    	// NEW
//float3 	lightPosition 	= { 0.0f, 2.0f, 0.0f };   		// NEW
float4 	lightPosition 	= { 0.0f, 2.0f, 0.0f, 1.0f };   	// NEW
float 	lightRange 		= 5.0f;    					// NEW

// modelfile, just for effectedit
string XFile = "paving.x";   // model

/***********************************************************/
/**       VARS CONTROLLED BY ENGINE                *********/
/***********************************************************/

float4x4		World         : WORLD;
float4x4    	WorldInvTransp: WORLDINVTRANSP;
shared float4x4 	ViewProj      : VIEWPROJECTION;

float4      	AmbientColor;
float       	AmbientIntensity;

float4      	MatAmb : MATERIALAMBIENT; 
float4      	MatDiff: MATERIALDIFFUSE; 
            
texture     	Tex0 < string name = "hungary009.jpg"; >;

/***********************************************************/
/**          SAMPLER STATES FOR TEXTURING          *********/
/***********************************************************/

sampler2D textureSampler = sampler_state
{
	Texture         	= (Tex0);
                                 
    	MinFilter       	= ANISOTROPIC;
   	MagFilter       	= LINEAR;
    	MipFilter       	= LINEAR;    
    	MaxAnisotropy	= 4;            
};

struct VS_INPUT
{
	float4    Pos     : POSITION0;
    	float4    Normal  : NORMAL0;
    	float2    TexCoord: TEXCOORD0;
};

struct VS_OUTPUT
{
	float4 Pos     	: POSITION;
    	float3 Normal  	: TEXCOORD1;
    	float2 TexCoord	: TEXCOORD2;
//    float3 LightPos	: TEXCOORD3; 
    	float4 LightPos	: TEXCOORD3; 
    	float3 Att     	: TEXCOORD4;
};

/***********************************************************/
/**          THE VERTEXSHADER        PROGRAM       *********/
/***********************************************************/

VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	float4 worldPosition = mul(input.Pos, World);
    	Out.Pos = mul(worldPosition, ViewProj);

    	float4 normal = mul(input.Normal, WorldInvTransp);
    	Out.Normal = normal;
    	Out.TexCoord = input.TexCoord;

//    Out.LightPos = lightPosition.xyz - worldPosition;   
    	Out.LightPos = lightPosition - worldPosition;   
    	Out.LightPos = normalize(Out.LightPos);           

    	Out.Att = Out.LightPos * lightRange;

    return Out;
}

/***********************************************************/
/**          THE PIXELSHADER        PROGRAM        *********/
/***********************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);

//   	float lightIntensity = saturate(dot(input.Normal, input.LightPos));    
 
   	float distt = distance(input.LightPos, input.Pos);
    	float lightIntensity = 1 - saturate(distt / lightRange);    
    	
	float4 color = diffuseColor * lightIntensity;          
    	float4 amb = AmbientColor * AmbientIntensity * MatAmb;
    	float4 attenuation = mul(input.Att, input.Att);

    	return saturate((color + amb) * textureColor) * MatDiff * (1-attenuation); 
}

/***********************************************************/
/**          OPAUQE SHADER, REGULAR MESHES         *********/
/***********************************************************/

technique OpaqueShader
{
    pass P0              
    {
        AlphaBlendEnable    = FALSE;
                              
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

/***********************************************************/
/**   BLENDING SHADER, MESHES WITH BLENDED TEXTURES  *******/
/***********************************************************/

technique BlendingShader
{
    pass P0              
    {
        AlphaBlendEnable    = TRUE;
        SrcBlend            = SRCALPHA;
        DestBlend           = INVSRCALPHA;
        AlphaOp[0]          = SelectArg1;
        AlphaArg1[0]        = Texture;
                               
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

The commented lines of code are the ones which I used before (playing around with float3 / float4 to get them 'working together'.

I did manage to get a working point light with a 'range' of 0.0 to 1.0f, where 1.0f = completely dark, 0.1f being fully bright, and strangely having infinite distance.

 

Any suggestions?

0

Share this post


Link to post
Share on other sites

Update; I've cleaned up the fx file/ effect and removed the error.

I was trying to use the VS output 'POSITION' in the pixel shader, now fixed that.

 

I got a compiling effect now, but not giving any luminance/ light at all from the point light )-;

Any suggestions?

 

/***********************************************************/
/**  VARS NOT CONTROLLED BY ENGINE YET             *********/
/***********************************************************/

float4 	diffuseColor 	= { 0.8f, 0.8f, 0.8f, 0.0f };    	// NEW
float3 	lightPosition 	= { 0.0f, 2.0f, 0.0f };   		// NEW
float 	lightRange 		= 3.0f;    					// NEW

// modelfile, just for effectedit
string XFile = "paving.x";   // model

/***********************************************************/
/**       VARS CONTROLLED BY ENGINE                *********/
/***********************************************************/

float4x4		World         : WORLD;
float4x4    	WorldInvTransp: WORLDINVTRANSP;
shared float4x4 	ViewProj      : VIEWPROJECTION;

float4      	AmbientColor;
float       	AmbientIntensity;

float4      	MatAmb : MATERIALAMBIENT; 
float4      	MatDiff: MATERIALDIFFUSE; 
            
texture     	Tex0 < string name = "hungary009.jpg"; >;

/***********************************************************/
/**          SAMPLER STATES FOR TEXTURING          *********/
/***********************************************************/

sampler2D textureSampler = sampler_state
{
	Texture         	= (Tex0);
                                 
    	MinFilter       	= ANISOTROPIC;
   	MagFilter       	= LINEAR;
    	MipFilter       	= LINEAR;    
    	MaxAnisotropy	= 4;            
};

struct VS_INPUT
{
	float4    Pos     : POSITION0;
    	float4    Normal  : NORMAL0;
    	float2    TexCoord: TEXCOORD0;
};

struct VS_OUTPUT
{
	float4 Pos     	: POSITION0;
    	float3 Normal  	: TEXCOORD1;
    	float2 TexCoord	: TEXCOORD2;
	float3 LightPos	: TEXCOORD3; 
	float4 wPos		: TEXCOORD4;
};

/***********************************************************/
/**          THE VERTEXSHADER        PROGRAM       *********/
/***********************************************************/

VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	float4 worldPosition = mul(input.Pos, World);
    	Out.Pos = mul(worldPosition, ViewProj);

    	float4 normal = mul(input.Normal, WorldInvTransp);
    	Out.Normal = normal;
    	Out.TexCoord = input.TexCoord;

	Out.LightPos = lightPosition.xyz - worldPosition;   
    	Out.LightPos = normalize(Out.LightPos);           

	Out.wPos = Out.Pos;

	return Out;
}

/***********************************************************/
/**          THE PIXELSHADER        PROGRAM        *********/
/***********************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);
    	float4 amb = AmbientColor * AmbientIntensity * MatAmb;

  	float distt = distance(input.LightPos, input.wPos);
    	float attenuation = 1 - saturate(distt / lightRange);    
    	
    	return saturate((amb * textureColor) * MatDiff * attenuation); 
}

/***********************************************************/
/**          OPAUQE SHADER, REGULAR MESHES         *********/
/***********************************************************/

technique OpaqueShader
{
    pass P0              
    {
        AlphaBlendEnable    = FALSE;
                              
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

/***********************************************************/
/**   BLENDING SHADER, MESHES WITH BLENDED TEXTURES  *******/
/***********************************************************/

technique BlendingShader
{
    pass P0              
    {
        AlphaBlendEnable    = TRUE;
        SrcBlend            = SRCALPHA;
        DestBlend           = INVSRCALPHA;
        AlphaOp[0]          = SelectArg1;
        AlphaArg1[0]        = Texture;
                               
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

Edited by cozzie
0

Share this post


Link to post
Share on other sites

You're using the screenspace position for wPos, instead of the worldPosition. I assume all your lighting calculations are done in worldspace? wPos should be made equal to worldPosition (and you can make it a float3).

 

I also don't understand at all what you're doing with LightPos in your vertex shader. Why are you normalizing a position? Why are you subtracting worldPosition from lightPosition.xyz? 

0

Share this post


Link to post
Share on other sites

Thanks, I found that that was from an experiment to calculate attenuation in another way.

Cleaned it up and did what you explained.

 

Still darkness though, will play around to see what's wrong.

If you have any suggestions....

 

Here's the effect now:

 

/***********************************************************/
/**  VARS NOT CONTROLLED BY ENGINE YET             *********/
/***********************************************************/

float4 	diffuseColor 	= { 0.8f, 0.8f, 0.8f, 0.0f };    	// NEW
float3 	lightPosition 	= { 0.0f, 1.0f, 0.0f };   		// NEW
float 	lightRange 		= 1.0f;   					// NEW

// modelfile, just for effectedit
string XFile = "paving.x";   // model

/***********************************************************/
/**       VARS CONTROLLED BY ENGINE                *********/
/***********************************************************/

float4x4		World         : WORLD;
float4x4    	WorldInvTransp: WORLDINVTRANSP;
shared float4x4 	ViewProj      : VIEWPROJECTION;

float4      	AmbientColor;
float       	AmbientIntensity;

float4      	MatAmb : MATERIALAMBIENT; 
float4      	MatDiff: MATERIALDIFFUSE; 
            
texture     	Tex0 < string name = "hungary009.jpg"; >;

/***********************************************************/
/**          SAMPLER STATES FOR TEXTURING          *********/
/***********************************************************/

sampler2D textureSampler = sampler_state
{
	Texture         	= (Tex0);
                                 
    	MinFilter       	= ANISOTROPIC;
   	MagFilter       	= LINEAR;
    	MipFilter       	= LINEAR;    
    	MaxAnisotropy	= 4;            
};
				
struct VS_INPUT
{			
	float4 Pos     	: POSITION0;
   	float4 Normal  	: NORMAL0;
    	float2 TexCoord	: TEXCOORD0;
};			

struct VS_OUTPUT
{
	float4 Pos     	: POSITION0;
    	float3 Normal  	: TEXCOORD1;
    	float2 TexCoord	: TEXCOORD2;
	float3 wPos		: TEXCOORD4;
};
					
/***********************************************************/
/**          THE VERTEXSHADER        PROGRAM       *********/
/***********************************************************/

VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	float4 worldPosition = mul(input.Pos, World);
    	Out.Pos = mul(worldPosition, ViewProj);

    	float4 normal = mul(input.Normal, WorldInvTransp);

    	Out.Normal = normal;
    	Out.TexCoord = input.TexCoord;
	Out.wPos = worldPosition;

	return Out;
}

/***********************************************************/
/**          THE PIXELSHADER        PROGRAM        *********/
/***********************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);
    	float4 amb = AmbientColor * AmbientIntensity * MatAmb;

  	float distt = distance(lightPosition, input.wPos);
    	float attenuation = 1 - saturate(distt / lightRange);    
    	
    	return saturate((amb * textureColor) * MatDiff) * attenuation; 
}

/***********************************************************/
/**          OPAUQE SHADER, REGULAR MESHES         *********/
/***********************************************************/

technique OpaqueShader
{
    pass P0              
    {
        AlphaBlendEnable    = FALSE;
                              
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

/***********************************************************/
/**   BLENDING SHADER, MESHES WITH BLENDED TEXTURES  *******/
/***********************************************************/

technique BlendingShader
{
    pass P0              
    {
        AlphaBlendEnable    = TRUE;
        SrcBlend            = SRCALPHA;
        DestBlend           = INVSRCALPHA;
        AlphaOp[0]          = SelectArg1;
        AlphaArg1[0]        = Texture;
                               
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

0

Share this post


Link to post
Share on other sites

Addition;

I also got a sort off working situation with the shader/effect below. Here:

 

- 0.9f as lightRange looks OK / expected

- 1.0 and higher gives a point light at the given position but with a sort of infinite range in the scene


 

/***********************************************************/
/**  VARS NOT CONTROLLED BY ENGINE YET             *********/
/***********************************************************/

float4 diffuseColor = { 0.8f, 0.8f, 0.8f, 0.0f };    // NEW
float3 lightPosition = { 0.0f, 2.0f, 0.0f };   // NEW
float lightRange = 1.0f;    // NEW

// modelfile, just for effectedit
string XFile = "paving.x";   // model

/***********************************************************/
/**       VARS CONTROLLED BY ENGINE                *********/
/***********************************************************/

float4x4		World         : WORLD;
float4x4    	WorldInvTransp: WORLDINVTRANSP;
shared float4x4	ViewProj      : VIEWPROJECTION;

float4      	AmbientColor;
float       	AmbientIntensity;

float4      	MatAmb : MATERIALAMBIENT; 
float4      	MatDiff: MATERIALDIFFUSE; 
            
texture     	Tex0 < string name = "hungary009.jpg"; >;

/***********************************************************/
/**          SAMPLER STATES FOR TEXTURING          *********/
/***********************************************************/

sampler2D textureSampler = sampler_state
{
    Texture         	= (Tex0);
                                 
    MinFilter       	= ANISOTROPIC;
    MagFilter       	= LINEAR;
    MipFilter       	= LINEAR;    
    MaxAnisotropy   	= 4;            
};

struct VS_INPUT
{
    float4    Pos      	: POSITION0;
    float4    Normal   	: NORMAL0;
    float2    TexCoord 	: TEXCOORD0;
};

struct VS_OUTPUT
{
    float4 Pos     	: POSITION;
    float3 Normal  	: TEXCOORD1;
    float2 TexCoord	: TEXCOORD2;
    float3 LightPos	: TEXCOORD3; 
};

/***********************************************************/
/**          THE VERTEXSHADER        PROGRAM       *********/
/***********************************************************/

VS_OUTPUT VS_function(VS_INPUT input)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    float4 worldPosition = mul(input.Pos, World);
    Out.Pos = mul(worldPosition, ViewProj);

    float4 normal = mul(input.Normal, WorldInvTransp);
    Out.Normal = normal;
    Out.TexCoord = input.TexCoord;

    Out.LightPos = lightPosition.xyz - worldPosition;   
    Out.LightPos = normalize(Out.LightPos);           

    return Out;
}

/***********************************************************/
/**          THE PIXELSHADER        PROGRAM        *********/
/***********************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
    float4 textureColor = tex2D(textureSampler, input.TexCoord);

    float lightIntensity = saturate(dot(input.Normal, input.LightPos));    
    float4 color = diffuseColor * lightIntensity;          

    float4 amb = AmbientColor * AmbientIntensity * MatAmb;
    float4 attenuation = saturate(1 - dot(input.LightPos / lightRange, input.LightPos / lightRange));

    return saturate((color + amb) * textureColor) * MatDiff * attenuation; 
}

/***********************************************************/
/**          OPAUQE SHADER, REGULAR MESHES         *********/
/***********************************************************/

technique OpaqueShader
{
    pass P0              
    {
        AlphaBlendEnable    = FALSE;
                              
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

/***********************************************************/
/**   BLENDING SHADER, MESHES WITH BLENDED TEXTURES  *******/
/***********************************************************/

technique BlendingShader
{
    pass P0              
    {
        AlphaBlendEnable    = TRUE;
        SrcBlend            = SRCALPHA;
        DestBlend           = INVSRCALPHA;
        AlphaOp[0]          = SelectArg1;
        AlphaArg1[0]        = Texture;
                               
        VertexShader = compile vs_2_0 VS_function();
        PixelShader = compile ps_2_0 PS_function();
    }  
}

probably have to combine worlds :)

0

Share this post


Link to post
Share on other sites

My only suggestion is to debug it with PIX; or start with a simple pixel shader (e.g. that just returns 1), and add your 4 terms in until you see the problem happen.

0

Share this post


Link to post
Share on other sites

I also got a sort off working situation with the shader/effect below. Here:
 
- 0.9f as lightRange looks OK / expected
- 1.0 and higher gives a point light at the given position but with a sort of infinite range in the scene

 

Ok, that code has a number of problems. You're using LightPos as if it were both a position (for calculating attenuation) and a normal (for calculating lightIntensity). Those are two completely different things.

 

So, LightPos, as you have it passed out of the vertex shader in this latest bit of code, is the light direction. However, the GPU will be interpreting between two normals, and the result may no longer be a unit vector. So you'd need to re-normalize LightPos in the pixel shader to get proper lighting anyway.

 

However, there is no reason to even pass LightPos out of the vertex shader. Instead, since this is a point light, just calculate the direction to the light in the pixel shader using:

 

float3 directionToLight = normalize(lightPosition - input.wPos);

 

This should make intuitive sense. It is the position of the light, minus the world position for the current pixel, normalized.

 

I also don't understand your calculation for attenuation.

 

Summary:

- pass worldposition out of the vertex shader (e.g. as .wPos)

- use this for calculations in your pixel shader, along with the lightPosition shader constant

- pare down your shader until you have something simple working, then add features one by one

0

Share this post


Link to post
Share on other sites

Hi Phil.

I got it!!!

Not using the direction anymore. Thanks for your help, here's the working effect:

 

VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	float4 worldPosition = mul(input.Pos, World);
    	Out.Pos = mul(worldPosition, ViewProj);

    	float4 normal = mul(input.Normal, WorldInvTransp);

    	Out.Normal = normal;
    	Out.TexCoord = input.TexCoord;
	Out.wPos = worldPosition;

	return Out;
}

/***********************************************************/
/**          THE PIXELSHADER        PROGRAM        *********/
/***********************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);
    	float4 amb = AmbientColor * AmbientIntensity * MatAmb;

  	float distt = distance(lightPosition, input.wPos);
    	float attenuation = 1 - saturate(distt / lightRange);    
    	
//    	return saturate( (amb * textureColor) * MatDiff * attenuation); 
//    	return saturate( (amb * textureColor) + MatDiff * attenuation); 

//    	return saturate( (amb * textureColor) + MatDiff * attenuation); 
    	return saturate(attenuation * (MatDiff + amb) * textureColor); 

}


I had a lot of waste in the effect from earlier attempts.

In the working situation I've done exactly what you explained in the beginning, I'm now gonna play around with the fallof tricks you mentioned.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0