Search Unity

Advice to achieve visibility close to mouse

Discussion in 'Shaders' started by Julian_Kraiz, Jan 12, 2022.

  1. Julian_Kraiz

    Julian_Kraiz

    Joined:
    Mar 9, 2017
    Posts:
    17
    Hello,
    I'm struggling to figure the best, or even a reasonable course of action to achieve some shader shenanigans.

    I have a plane, textured with a grid pattern( irrelevant), and I'd like to have the texture invisible whe only visible around the mouse. The plane has a mesh collider if it help in any form.
    I did install the URP and toyed around with the unlit shader graph, but I couldn't find anything related to position of the mouse, or world to screen point to use to calculate a distance between mouse (or ray from camera) to the vertex or area being painted.

    I tried to use or copy/paste some pass/grad script, but I'm at a complete loss with hlsl and the fragment thing. so if possible I'd rather no go that route.

    What technology available, be it graph or code, would be the most simple or elegant way to achieve this ?
    By the way, the plane I'm talking about has one 2D texture applied to it and never changes, so I'm not constraint to being able to swap it with game state or anything.

    Any word of wisdom is welcome.

    Thanks.
     
  2. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    119
    Is the plane in world space?

    If so I would probable approach it by raycasting the mouse position until it intersects with the plane. (in a c# spript)
    Then lerp the texture in using another black and white texture as a mask.

    Set the UV coordinates of the masking texture in world space (again in a c# script) edit (slight clarification. You pass the worldspace coords to the shader in a c# script, then convert them to uvs for the texture read in the shader.)
    You could probably do this in all three projections x.y and z if the id the masking texture was symetrical ie a circle.

    edit
    if it's screen space it would be easier, you wouldn't need to raycast to the 3d objectm you could just use the screen coordinates as uv coords
     
    Last edited: Jan 12, 2022
    Julian_Kraiz likes this.
  3. Julian_Kraiz

    Julian_Kraiz

    Joined:
    Mar 9, 2017
    Posts:
    17
    hi, thanks a lot.
    I think I got most of what you sugest and it does sounds reasonable enough for me to implement.
    as you guessed it's a world spaced. the plane is something of a build plane, to indicate on what x.z plane you build stuff.

    I'll update this post when I got to implement it for reference.