Posts

Showing posts with the label shortcut

CG shaders Alpha Channels and blending

Typical alpha channel usage - for a quad with a leaf texture that has alpha values. We make sure we're inthe Transparent Queue and also use the Blend function. It takes two arguments, the first multiplies the incoming data (what is on the surface) and the second multiplies the existing data on the framebuffer (whatever is behind the object - Unity renders from back to front). Then both values are added. In the code example, SrcAlpha x surface colour will give you the surface col where the Alpha is white & zero where the Alpha is black. By multiplying the framebuffer with 1-srcalpha we take the inverse of the alpha map and get inverted results. Now when you add these values they should sit together as if the quad has correct transparency. If for instance, you had Blend SrcAlpha One, then  you'd have an additive effect of the background on your leaf. I've pasted some info about it below.. Cull Off - tells the shader not to discard the back-face data, which it does by defa...

CG shader - World Position, if statement shorthand, frac

The shader below uses world position to create stripes on an object. It's as simple as including worldPos in the Input struct and using IN.worldPos.y or .x or .z. We're using the shorthand IF statement to assign a green (0,1,0) and red (1,0,0) to the surface where the worldPos.y is less than _rimThreshold. The shorthand is as follows -  condition ? true result : false result IN.worldPos.y>0.5 ? float3(0,1,0): float3(1,0,0); We're also using the frac function here on the worldPos.y - this returns just the fractional part of the number. eg. 3.23 would return just the 0.23 part. so in this example, 0.23 is NOT greather than 0.5, so would return false and therefore the (1,0,0) colour. By multiplying the worldPos.y by 5 (or any number) we're essentially able to repeat the pattern by 5 times. The *0.5 is just a further arbitrary scaling! Final point - just multiplying the colour by the dot product to give the surface a bit of shape, instead of being flat. Alternatively, yo...