Search Unity

Mask an image so only a part of it appears on an object...

Discussion in 'Shaders' started by Philip-ACN, Jan 16, 2019.

  1. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    Hello, forgive my cryptic sounding title but I have a strange issue.

    I'm trying to create a racing game where the player can create decals to apply to their car. (But I don't want to use the decal projector.)

    What I'd like is to have 2 properties exposed to the user. A color property for the vehicles main body colour, and a texture2D property which will contain the image of the decals. (Shown below)


    Its a transparent image where the black/green bits are the only bits visible. However when I create a Sample texture 2D node in Shader Graph it appears like this (But shows correctly in the inspector as transparency is on and set to 'use image alpha'):


    Is there a way I can use the alpha channel in the image to mask this image so I can create a shader which will take a color, then apply the texture on top?

    I've been trying all day trying channel masks, color masks, all kinds of things!

    Please Obi Wan, you're my only hope!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    This is all working as intended. A texture with alpha is 4 color channels, Red, Green, Blue, and Alpha. What you're seeing in the Shader Graph is the RGB channel without the A hiding what's actually there. While that looks like a mess, it's correct. The Alpha is still being output though, and can be used to blend between whatever else you're rendering and that texture. The node you want to use is a Lerp node.
    https://github.com/Unity-Technologies/ShaderGraph/wiki/Lerp-Node

    The inputs for A and B should be your base color and this texture's color respectively, and the T should be the alpha from this texture.
     
    id0 and Philip-ACN like this.
  3. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    You beautiful person you!

    That's got it working perfectly! Now I have shaders to amend!