Skip to content

Try this for billboarding #4

Description

@float3
                //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);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions