Jump to content

  • Log In with Google      Sign In   
  • Create Account

adding attenuation to hlsl point light


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 12 January 2013 - 05:28 PM

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();
    }  
}

 



Sponsor:

#2 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 04 February 2013 - 01:42 PM

no one? maybe some pointers to a good tutorial/ article?



#3 phil_t   Crossbones+   -  Reputation: 4093

Like
0Likes
Like

Posted 04 February 2013 - 03:18 PM

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)?

#4 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 08 February 2013 - 01:18 PM

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

#5 phil_t   Crossbones+   -  Reputation: 4093

Like
0Likes
Like

Posted 08 February 2013 - 02:03 PM

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);

 


 



#6 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 12 February 2013 - 04:18 PM

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?



#7 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 12 February 2013 - 04:36 PM

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, 12 February 2013 - 04:49 PM.


#8 phil_t   Crossbones+   -  Reputation: 4093

Like
0Likes
Like

Posted 13 February 2013 - 11:00 AM

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? 



#9 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 13 February 2013 - 01:29 PM

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();
    }  
}



#10 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 13 February 2013 - 01:30 PM

Here's the tutorial I tried to use before:

http://digitalerr0r.wordpress.com/2009/05/06/xna-shader-programming-tutorial-17-point-light-and-self-shadowing/



#11 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 13 February 2013 - 01:44 PM

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 :)



#12 phil_t   Crossbones+   -  Reputation: 4093

Like
0Likes
Like

Posted 13 February 2013 - 01:45 PM

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.



#13 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 13 February 2013 - 01:55 PM

thanks, not what I hoped for but sounds logical :)



#14 phil_t   Crossbones+   -  Reputation: 4093

Like
0Likes
Like

Posted 13 February 2013 - 03:17 PM

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



#15 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 13 February 2013 - 04:51 PM

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.



#16 cozzie   Members   -  Reputation: 1761

Like
0Likes
Like

Posted 14 February 2013 - 01:29 PM

Just for fun, here's a screenshot of all 3 types of lightsources in action (ambient, diffuse and point):

 

lightingcombi.jpg






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS