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

Try this for billboarding #4

Open
float3 opened this issue Sep 18, 2021 · 0 comments
Open

Try this for billboarding #4

float3 opened this issue Sep 18, 2021 · 0 comments

Comments

@float3
Copy link
Owner

float3 commented Sep 18, 2021

                //Check if we're in VR, if we are set the camera's position to between
                //the two eyes. This avoids bad stereo effects where the sprite is aiming
                //at each eye at the same time. Otherwise just use the normal camera pos.
                #if UNITY_SINGLE_PASS_STEREO
                    float4 cameraPos = float4((unity_StereoWorldSpaceCameraPos[0] + unity_StereoWorldSpaceCameraPos[1])*0.5, 1);
                #else
                    float4 cameraPos = float4(_WorldSpaceCameraPos,1);
                #endif
                
                float4 center = mul(unity_ObjectToWorld, float4(_rotPos.xyz, 1));
                cameraPos.xyz -= center.xyz;
                //cameraPos =  mul(unity_WorldToObject, cameraPos);
                float len = distance(float2(0,0) , cameraPos.xz);
                
                //Length of the hypotenuse of the triangle formed by the camera's
                //objectspace pos, the origin, and the projection of the camera on
                //to the xz plane
                float hyp = distance(float3(0, 0, 0), cameraPos.xyz);
                
                // rotate the vertices along the X-axis to face the Camera
                float cosa = len/hyp;
                float sina = (-cameraPos.y)/hyp;
                float4x4 R = float4x4(
                    1,    0,        0,        0,
                    0,  cosa,    -sina,    0,
                    0,    sina,    cosa,    0,
                    0,        0,    0,        1);
                pos = mul(R, pos);
                
                //Rotate the vertices around the y-axis
                cosa = (cameraPos.z)/len;
                sina = (cameraPos.x)/len;
                R = float4x4(
                    cosa,    0,    sina,    0,
                    0,        1,    0,        0,
                    -sina,    0,    cosa,    0,
                    0,        0,    0,        1);
                pos = mul(R, pos);
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

No branches or pull requests

1 participant