Advertisement

Directional lighting question

Started by April 11, 2020 09:45 AM
7 comments, last by DividedByZero 4 years, 9 months ago

Hi Guys,

I have a basic directional lighting shader that I am working on and all is looking pretty good so far.

I notice on a low poly sphere that you can faintly see lighter areas on the edges of where the triangles are (appearing as quads, as per the geometry).

I am wondering why this is and why the shading isn't uniformally smooth across the geometry? The sphere was generated in Maya and looks perfectly fine when rendered within it.

I have attached my shader.

// *** Vertex shader ***
attribute vec3 in_Position;
attribute vec3 in_Normal;
attribute vec2 in_TextureCoord;

varying vec3 v_vNormal;
varying vec3 v_vLightDirection;

void main()
{
	// Get object space position
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
	
	// Translate vertex and pass to fragment shader
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;

	// Normalise the input normals and place into transformed world space
	vec4 object_space_normal = gm_Matrices[MATRIX_WORLD] * vec4(normalize(in_Normal), 1.0);
	
	// Pass the world transformed normals to fragment shader	
	v_vNormal = normalize(object_space_normal.xyz);

	// Pass the normalised light position to the fragment shader
	// Light direction presently hard coded
	v_vLightDirection = normalize(vec3(-5.0, 7.0, -6.0));
}


// *** Fragment shader ***
varying vec3 v_vNormal;
varying vec3 v_vLightDirection;

void main()
{
	vec4 diffuse = vec4(1.0, 1.0, 1.0, 1.0);
	vec4 ambient = vec4(0.25, 0.25, 0.25, 1.0);
	
	float intensity = max(dot(v_vNormal, v_vLightDirection), 0.0);

	vec4 colorOut = max(intensity * diffuse, ambient);
		
    gl_FragColor = colorOut;
}

Is there something I am missing? Or is this just one of those quirky GPU situations?

Also from my understanding, this is a ‘per fragment’ implementation that I have made here, right?

Many thanks in advance.

 

I am a novice with shaders but I know that fragment shader interpolation can change the lengths of normals. Try normalizing v_vNormal again in the fragment shader.

Not renormalizing normals in the fragment shader is a common issue but I'm not sure if it is the problem here.

ED: I found an image for reference. You can see the interpolated normals are slightly shorter that the vertex normals.

http://glasnost.itcarlow.ie/~powerk/GeneralGraphicsNotes/LightingShadingandColour/lighting_images/img375.gif

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement

Normals interpolate wrongly between the pixel and vertex shaders.
It is the end position of the normal that interpolates. But not the direction of the normal vector.
It is an approximated solution. Your code is not wrong. But you need a heavier mesh or more coding inside the shader to deal with it. If your mesh has too few triangles, no technique(trick) can produce a perfect result. It is not your fault.

I guess in Maya, they use some trick to deal with this.

@fleabay is right too. Fix the length of the interpolated normals. It should improve the visuals a bit.

Thanks for your help guys!

Normalising in the fragment shader certainly helped.

There is a very slight occurance now. But I suspect you are right NikiTo, the resolution of the geometry might be pushing it a bit hard.

I normally use much higher detail geometry. So this is probably a non-issue now and we can call it solved.

Many thanks once again fleabay and NikiTo.  :D

Does look flawless with a higher resolution model though!

Thanks again :D

Advertisement

Thanks for showing us the results!

If you allow me, i will point you to the problems you will have with meshes made of too few triangles when you start casting shadows in the scene:
 

True geometry will reveal itself everywhere.

There are many advanced techniques to fake it, but always there will be artifacts, because it is fake-d.

In my opinion, with modern hardware, the count of triangles is still important, but not so important as it was ago. So, many of these advanced techniques could result computationally more expensive than bigger triangle count.

With skin animation low triangle count is still very important. But having a good 3D artist to create a rigged low poly model that tessellates well when animated, is prohibitively expensive.

Awesome example. I can see where low poly geometry can be a pain long term.

This topic is closed to new replies.

Advertisement