Talk:GLSL Programming/Blender/Diffuse Reflection

=Shader Code for One Directional Light Source=

"In which coordinate system should the equation be implemented? We try view space by default in Blender. (Which turns out to be a good choice here because Blender provides the light direction in world space.)"

Apparently this is a semi-copy-paste from the Unity page - but I don't know if we should write view or world in both places? :) Beuc (discuss • contribs) 18:27, 28 August 2011 (UTC)


 * It should be world space in Unity (because Unity provides positions of light sources in world space) and view space in Blender (because Blender provides positions of light source in view space). If one chooses another space one just has to convert the light position to the other space, which is just one matrix-vector multiplication for each light source. Since there are potentially a few light sources, it appears to be better to avoid that multiplication and perform the lighting computation always in the space in which the light sources are specified. --Martin Kraus (discuss • contribs) 12:31, 29 August 2011 (UTC)


 * No, I meant that in the sentence quoted above, from the Blender page, both view space and world space are used in a contradictory way, while (from your answer), it should be view space in both occurrences. I didn't meant to change anything in the Unity page. Beuc (discuss • contribs) 12:59, 29 August 2011 (UTC)


 * Oh, OK. You are right, that was a mistake. Thanks! --Martin Kraus (discuss • contribs) 13:11, 29 August 2011 (UTC)

Not sure about lightDirection = ... * one_over_distance; in optimized snippet

 * Also note that this code is unlikely to give you the best performance because any  is usually quite costly. Since   is either 0 or 1, it is actually not too hard to rewrite the code to avoid the use of   and optimize a bit further:

I don't understand why lightDirection is multiplied by one_over_distance in that version? 90.61.177.124 (discuss) 19:55, 4 September 2011 (UTC)


 * Just to normalize it to unit length. Using  would require to compute another square root; thus, reusing   is likely to be faster. --Martin Kraus (discuss • contribs) 07:41, 5 September 2011 (UTC)


 * Thanks! I overlooked the need to normalize lightDirection :)