Wally869 / DirectionalSpriteShader

Shader created using shadergraph which implements billboarding and sprite selection from a spritemap according to the position of the camera and its angle
https://www.noveltech.dev/sprite-change-camera-direction/
MIT License
14 stars 2 forks source link

Lock billboards horizontally #1

Open Stratium opened 2 years ago

Stratium commented 2 years ago

Something that I've been looking to do is to lock sprites to a horizontal axis. At the moment, if the camera gets above or below the sprite they'll lean to accommodate this.

This is fine for floating camera type games but doesn't look believable for some sprites, such as trees or fences, as well as first-person or virtual reality titles. Is it possible to lock individual axis, so that this can be prevented?

Wally869 commented 2 years ago

I took a quick look and it depends on the method used for billboarding.

The method outlined in the article also allows lit billboards so it's pretty nice, but it has the unfortunate side effect of reversing UVs so you need to handle it in your shadergraph.

Wally869 commented 2 years ago

The method for billboarding in the implementation in the repo differs from the one I mentionned in the previous comment since it seems to more or less disregard the Y-axis. Instead rotation seems to be purely driven by x and z vertex position so you can't use the previous trick.

I'll try to find a solution and upload cleaned and up-to-date versions for the shaders.

StatueTheShaman commented 2 years ago

For billboarding with locked y I like the following script:

using System.Collections; using System.Collections.Generic; using UnityEngine; //Thank you! https://youtu.be/_LRZcmX_xw0 public class Billboard_Sprite : MonoBehaviour { private Camera theCam;

public bool useStaticBillboard; //This here prevents the twisting of the trees

// Start is called before the first frame update
void Start()
{
    theCam = Camera.main;
}

// Update is called once per frame
void LateUpdate()
{
    //transform.LookAt(theCam.transform); ((This here causes the trees to twist))

    if (!useStaticBillboard)
    {
        transform.LookAt(theCam.transform);
    } else
    {
        transform.rotation = theCam.transform.rotation;
    }

    transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0f);
}

}