Jump to content

  • Log In with Google      Sign In   
  • Create Account


We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

How to improve this dumb pixel shader?

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
1 reply to this topic

#1 lucky6969b   Members   -  Reputation: 636


Posted 08 January 2013 - 04:46 AM

//Pixel Shader
float4 ps_lighting(VS_OUTPUT IN) : COLOR0
	//float4 color = tex2D(DiffuseSampler, IN.tex0);
	float opacity = GlobalOpacity; //default is global opacity

	float4 color = float4(diffuseColor); //default diffusecolor

	color = lerp(color, color*IN.color, VertexDiffuse);

	//Create Light Calculations
	CreateLights( IN.worldSpacePos ); // fill lightstruct array DOES NOT WORK IN VERTEX SHADER, MUST HAPPEN IN PIXEL SHADER! ~0_o>

	float3 normal = IN.worldNormal; //default worldspace vertex normal

	normal = normalize( normal );

 	//Diffuse Fresnel
	float diffusefresnel = saturate(diffuseFresnelMult*fresnel(normal, IN.eyeVec, diffuseFresnelPower, 0));
	color = lerp(color, diffusefresnelColor, diffusefresnel);
	//Light Loop
	float4 ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
 	ambient = float4(ambientcolor, 1.0f);
 	float4 totaldiffuse =  ambient; //start off with ambient color

	for(int i = 0; i < numberOfActiveLights; ++i) //for loop to iterate over our 3 lights
		//	float4 emissive = lightsarray[i].ke;
		float diffuse;

	    //ambient = lightsarray[i].ka*ambientcolor;
		float3 L = normalize(-lightDirection);

		// Spot light
		diffuse = diffuselight(normal,lightsarray[i].LightVec);
		// Directional light
		//diffuse = diffuselight(normal,L);

		totaldiffuse += (diffuse*lightsarray[i].LightColor);
	//Apply Lighting
	float4 ret = color; // our final returned color starts as unlit diffuse color
	ret = float4(pow(ret.x,Gamma),pow(ret.y,Gamma),pow(ret.z,Gamma),ret.a);
 	ret.a = opacity; // finally set opacity
	ret.rgb*=(totaldiffuse); //multiply diffuse with color
	ret = pow(ret,1.0/Gamma);
	return ret; //all done, return result :)

The logic is this.
1) Setting global opacity to 1.0
2) Interpolate the color from the vertex shader (IN.color) with the global material color(diffuseColor)
3) Create lights and normal for each vertex
4) Calculate total diffuse by multiplying the normal of the vertex with the light direction
5) Multiply the interpolated color with the total diffuse value(light)
6) Output the pixel

It looks real dumb like this, and the image is really jagged

How do I improve it? Any good ideas?

Attached Thumbnails

  • dumbshader.png

Edited by lucky6969b, 08 January 2013 - 04:46 AM.


#2 kauna   Crossbones+   -  Reputation: 2856


Posted 08 January 2013 - 09:39 AM

It is hard to give you precise advices since you don't show the code for few of your functions.

However, there is something funny going on with the gamma correction. As far as I know, if you do your calculations in linear space you can skip all the gamma correction in the shader, assuming that the texture read gives you a texture color in linear space and that the back buffer is in SRGB format.


It seems that you don't calculate the specular factor or that you calculate it incorrectly.


Edited by kauna, 08 January 2013 - 09:41 AM.

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.