Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define ObjSpaceViewDir in Fragment Shader #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TheMindVirus
Copy link

When calling ObjSpaceViewDir() Per Vertex from the Vertex Shader there is visible Warbling to Texture3D due to inaccuracies missed which are correct when calling the same method Per Fragment from the Fragment Shader.
Either allow calling of this method from both Shader Programs or provide a separate implementation for each.

Standard Method in the Vertex Shader before edit

ObjSpaceViewDirPerVertex

Self-Defined Method in the Fragment Shader after edit

ObjSpaceViewDirPerFragment

This is what I used to fix it in my own volumetric shader, though I shouldn't have to write it again as such.

            vec3 ObjSpaceViewDirFragment(vec4 pos)
            {
                vec3 objSpaceCameraPos = (unity_WorldToObject * vec4(_WorldSpaceCameraPos.xyz, 1.0)).xyz;
                return objSpaceCameraPos - pos.xyz;
            }

Pending an update with a correction to the Vertex Shader implementation of this method to remove the Warbling...

When calling `ObjSpaceViewDir()` Per Vertex from the Vertex Shader there is visible Warbling to Texture3D due to inaccuracies missed
which are correct when calling the same method Per Pixel in the Fragment Shader. Either allow calling of this method from both Shader Programs
or provide a separate implementation for each.
@TheMindVirus
Copy link
Author

TheMindVirus commented Mar 7, 2022

The Warbling issue seems to be caused by calling normalize() on the direction from the Vertex Shader instead of using the Standard Method from UnityCG.glslinc before edits, passing it as a varying vec3 and calling normalize() in the Fragment Shader.

The Edit still applies as calling it from the Fragment Shader and normalising it there and then is better to write and optimise.
However, writing it in the Vertex Shader and normalising it afterwards is also correct, so it is better to state in the documentation that it is un-normalised and any following calls to normalize() should be done only in the Fragment Shader.

There may also be an edit to normalize() itself to be had which detects whether it's running with #ifdef VERTEX
and defers or removes the normalisation out of the equation completely, then providing a separate implementation
for producing GL Characteristic Warbling if it is a desirable side-effect for certain applications and games.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant