Search Unity

Question Switch shape key values depending of the camera view inside a shader ?

Discussion in 'Shaders' started by DreamingWorld, Nov 17, 2022.

  1. DreamingWorld

    DreamingWorld

    Joined:
    Dec 29, 2013
    Posts:
    30
    Hi everyone!
    I'm going deeper in the shader rabbit hole!
    I'm doing NPR rendering that I want to look 2D, and for achieving that I thought that I could modify the mesh depending from the viewpoint it's seen.
    Like for example a manga face, the rotation doesn't make sense in a rigid 3D space.
    upload_2022-11-17_9-31-23.png
    (take a look at the mouth for exemple)
    Would it be possible ? Would it be a shader (I'm using built-in) ? Or maybe a script accessing camera angle and apply the shape key to the model ? Would it be better to use another function that the shape key ? (Like a deformation map of some sort ?) Same for the nose lines that would display in one way in an angle, and another way in another angle...

    Another question I have is :
    If I want to have eyebrows for exemple to show on top of the hair.. but not on top of another character's hair.. how should I go about this ?

    Thank you a lot for your time!
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Shape key would work fine. You can't access the shape keys (blendshapes) easily within a shader, Unity runs code (or compute shader when GPU skinning is enabled) that applies the shapes to the mesh, then the mesh is rendered and shaders have no information about the blendshapes, they just see the final deformed mesh.

    So the ideal route is to attach scripts to different bones, such as one to the head, that compares the direction of the head to the camera direction and adjusts the blendshape values on the head parts accordingly.

    For eyebrows, use the Stencil Buffer and a unique stencil value for each character's head materials to use. The shader that runs on the eyebrows will have a Stencil compare for that value, while the other materials of the head will write that value. So the eyebrows will only render if that stencil value is there. If you plan to have the eyebrows extend off the head a bit too, then you can add another regular (non-stencil) depth-tested pass of the eyebrows to handle the off-mesh rendering cases.
     
    DreamingWorld likes this.