How to add transparency to voxel shader - c#

I have a shader for voxel rendering which do not support transparency.
Shader code:
Shader "Custom/Voxel"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_AOColor ("AO Color", Color) = (0,0,0,1)
_AOIntensity ("AO Intensity", Range(0, 1)) = 1.0
_AOPower ("AO Power", Range(1, 10)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma vertex vert
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float3 position;
float4 custom_uv;
float4 color : COLOR;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
int _AtlasX;
int _AtlasY;
fixed4 _AtlasRec;
half4 _AOColor;
float _AOIntensity;
float _AOPower;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.custom_uv = v.texcoord;
o.position = v.vertex;
v.color.rgb = _AOColor;
v.color.a = pow((1-v.color.a) * _AOIntensity, _AOPower );
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed2 atlasOffset = IN.custom_uv.zw;
fixed2 scaledUV = IN.custom_uv.xy;
fixed2 atlasUV = scaledUV;
atlasUV.x = (atlasOffset.x * _AtlasRec.x) + frac(atlasUV.x) * _AtlasRec.x;
atlasUV.y = (((_AtlasY - 1) - atlasOffset.y) * _AtlasRec.y) + frac(atlasUV.y) * _AtlasRec.y;
// Albedo comes from a texture tinted by color
fixed4 c = tex2Dgrad(_MainTex, atlasUV, ddx(atlasUV * _AtlasRec), ddy(atlasUV * _AtlasRec)) * _Color;
//fixed4 c = tex2D(_MainTex, atlasUV) * _Color;
o.Albedo = lerp(c.rgb, IN.color.rgb, IN.color.a);
o.Alpha = c.a;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
}
ENDCG
}
FallBack "Diffuse"
}
What I have changed/added by googling attempts:
Tags { "Queue"="Transparent" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
This did nothing.
Then I have tried to change:
#pragma surface surf Standard fullforwardshadows
with
#pragma surface surf Standard alpha
or
#pragma surface surf Standard fullforwardshadows alpha:fade
So in result those block that should are transparent, but the rest are kinda too (non transparent tiles works good in original version):
https://i.imgur.com/EwMIQnq.png

Related

How to make Shader(unity) interact with lights with specific intensity?

I am working on a cel shader for a uni project that I would like to only be affected by scene lights over x intensity. I am struggling in figuring out how to implement this within the code. The code I have been working with I found online, and have been using it to test and understand how shaders work. I am unsure if I need to implement some kind of conditional statement? Some of the information I have read has said that using conditional statements within shaders makes for poor performance.
I am new to shaders and any help would be greatly appreciated.
Thank you in advance :)
ps. I am limited to using Unity 2018.3.8
Properties {
[Header(Base Parameters)]
_Color ("Tint", Color) = (0, 0, 0, 1)
_MainTex ("Texture", 2D) = "white" {}
[HDR] _Emission ("Emission", color) = (0, 0, 0, 1)
[HDR]
_SpecularColor("Specular Color", Color) = (0.9, 0.9, 0.9, 1)
// Controls the size of the specular reflection.
_Glossiness("Glossiness", Float) = 32
[HDR]
_RimColor("Rim Color", Color) = (1,1,1,1)
_RimAmount("Rim Amount", Range(0, 1)) = 0.716
// Control how smoothly the rim blends when approaching unlit
// parts of the surface.
_RimThreshold("Rim Threshold", Range(0, 1)) = 0.1
}
SubShader {
Tags{ "RenderType"="Opaque" "Queue"="Geometry"}
CGPROGRAM
#pragma surface surf Stepped fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
fixed4 _Color;
half3 _Emission;
float4 _SpecularColor;
float _Glossiness;
float4 _RimColor;
float _RimAmount;
float _RimThreshold;
float3 worldPos;
float4 LightingStepped(SurfaceOutput s, float3 lightDir, half3 viewDir, float shadowAttenuation){
float shadow = shadowAttenuation;
s.Normal=normalize(s.Normal);
float diff = dot(s.Normal, lightDir);
float towardsLightChange = fwidth(diff);
float lightIntensity = smoothstep(0, towardsLightChange, diff);
float3 diffuse = _LightColor0.rgb * lightIntensity * s.Albedo;
float diffussAvg = (diffuse.r + diffuse.g + diffuse.b) / 3;
float3 halfVector = normalize(viewDir + lightDir);
float NdotH = dot(s.Normal, halfVector);
float specularIntensity = pow(NdotH * lightIntensity, _Glossiness * _Glossiness);
float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity);
float3 specular = specularIntensitySmooth * _SpecularColor.rgb * diffussAvg;
float rimDot = 1 - dot(viewDir, s.Normal);
float rimIntensity = rimDot * pow(dot(lightDir, s.Normal), _RimThreshold);
rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity);
float3 rim = rimIntensity * _RimColor.rgb * diffussAvg;
float4 color;
color.rgb = (diffuse + specular + rim) * shadow;
color.a = s.Alpha;
return color;
}
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
void surf (Input i, inout SurfaceOutput o) {
worldPos = i.worldPos;
fixed4 col = tex2D(_MainTex, i.uv_MainTex);
col *= _Color;
o.Albedo = col.rgb;
o.Alpha = col.a;
o.Emission = _Emission;
}
ENDCG
}
FallBack "Standard"
}

Unity: Custom shader not obeying alpha (transparency)

This is a custom shader in Unity, it is a 3-colored gradient shader:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "CustomShaders/ThreeColorsGradientShader" {
Properties {
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_ColorTop ("Top Color", Color) = (1,1,1,1)
_ColorMid ("Mid Color", Color) = (1,1,1,1)
_ColorBot ("Bot Color", Color) = (1,1,1,1)
_Middle ("Middle", Range(0.001, 0.999)) = 1
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
ZWrite Off
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 _ColorTop;
fixed4 _ColorMid;
fixed4 _ColorBot;
float _Middle;
struct v2f {
float4 pos : SV_POSITION;
float4 texcoord : TEXCOORD0;
};
v2f vert (appdata_full v) {
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
o.texcoord = v.texcoord;
return o;
}
fixed4 frag (v2f i) : COLOR {
fixed4 c = lerp(_ColorBot, _ColorMid, i.texcoord.y / _Middle) * step(i.texcoord.y, _Middle);
c += lerp(_ColorMid, _ColorTop, (i.texcoord.y - _Middle) / (1 - _Middle)) * (1 - step(i.texcoord.y, _Middle));
//c.a = 1;
return c;
}
ENDCG
}
}
}
After assigning it to a material and used a material on a UIButton, it works as expected, but if i want to change the color alpha of the material (ie. make it transparent), it does NOT respond to those changes.
What's missing in the code?
Edit tested copy this blurb remove ZWrite Off:
SubShader
{
Tags {"Queue" = "Transparent" "RenderType" = "Transparent"}
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Edit to apply to a ui button, uncheck image from your button component, remove sprite, set alpha of color to 0. Add a plane as a child to your button object, size the plane to how you want,apply the shader to the plane.

Add Alpha to Shader

I have the following shader,
I'm trying to add an alpha to the white, however all attempts have proven to be difficult.
This is the project from which I obtained the shader for reference - github
I think it has to do with one of the passes overwriting.
Shader "Suibokuga/Suibokuga" {
Properties {
_MainTex ("Water Texture", 2D) = "white" {}
_Alpha ("Transfer/Diffusion coefficient for water particles", Range(0.01, 1.5)) = 1.0
_Evaporation ("a unit quantity of water for evaporation", Range(0.0001, 0.005)) = 0.00015
_PaperTex ("Paper Texture", 2D) = "white" {}
_Brush ("brush", Vector) = (-1, -1, -1, -1)
_Prev ("previous brush position", Vector) = (-1, -1, -1, -1)
}
CGINCLUDE
#include "UnityCG.cginc"
#pragma target 3.0
/*
* r : water particles
* b : capacities of water
* a : the heights of the bottoms
*/
sampler2D _MainTex;
float4 _MainTex_TexelSize;
float _Alpha;
float _Evaporation;
sampler2D _PaperTex;
float2 _Prev;
float3 _Brush; // x,y : position, z : size
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (appdata IN) {
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.uv = IN.uv;
return OUT;
}
void sample (float2 uv, out float4 o, out float4 l, out float4 t, out float4 r, out float4 b) {
float2 texel = _MainTex_TexelSize.xy;
o = tex2D(_MainTex, uv);
l = tex2D(_MainTex, uv + float2(-texel.x, 0));
t = tex2D(_MainTex, uv + float2( 0, -texel.y));
r = tex2D(_MainTex, uv + float2( texel.x, 0));
b = tex2D(_MainTex, uv + float2( 0, texel.y));
}
float waterDelta (float4 k, float4 o) {
float ld = (k.w + k.x) - (o.w + o.x); // level difference
float transfer = (k.w + k.x) - max(o.w, k.w + k.z); // transferable water particles
return max(
0.0,
0.25 * _Alpha * min(ld, transfer)
);
}
float waterFlow (float2 uv) {
float4 o, l, t, r, b;
sample(uv, o, l, t, r, b);
float nw = o.r;
nw += (waterDelta(l, o) - waterDelta(o, l));
nw += (waterDelta(t, o) - waterDelta(o, t));
nw += (waterDelta(r, o) - waterDelta(o, r));
nw += (waterDelta(b, o) - waterDelta(o, b));
return max(nw, 0);
}
float evaporation (float wo) {
return max(wo - _Evaporation, 0.0);
}
float brush (float2 uv) {
const int count = 10;
float2 dir = _Brush.xy - _Prev.xy;
float l = length(dir);
if(l <= 0) {
float d = length(uv - _Brush.xy);
return smoothstep(0.0, _Brush.z, _Brush.z - d);
}
float ld = l / count;
float2 norm = normalize(dir);
float md = 100;
for(int i = 0; i < count; i++) {
float2 p = _Prev.xy + norm * ld * i;
float d = length(uv - p);
if(d < md) {
md = d;
}
}
return smoothstep(0.0, _Brush.z, _Brush.z - md);
// float d = length(uv - _Brush.xy);
// return smoothstep(0.0, _Brush.z, _Brush.z - d);
}
ENDCG
SubShader {
Cull Off ZWrite Off ZTest Always
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment init
float4 init (v2f IN) : SV_Target {
float4 paper = tex2D(_PaperTex, IN.uv);
return float4(
0,
0,
paper.r,
paper.r
);
}
ENDCG
}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment waterUpdate
float4 waterUpdate (v2f IN) : SV_Target {
float4 col = tex2D(_MainTex, IN.uv);
col.x = evaporation(waterFlow(IN.uv));
float dw = brush(IN.uv);
// if(dw > 0) {
col.x = min(col.x + brush(IN.uv), 1.0);
// }
return col;
}
ENDCG
}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment visualize
float4 visualize (v2f IN) : SV_Target {
float4 col = tex2D(_MainTex, IN.uv);
return float4(1.0 - col.xxx, 1.0);
}
ENDCG
}
}
}
You seem to miss a blending step.
Try adding this line to the end of the last pass
Blend SrcAlpha OneMinusSrcAlpha
Check this page to learn about alpha blending for Unity shaders.
The line I attached is for standard transparency, you might want to try different blending options.

I'm doing a unity conversion from 4.6 to 5.2 "Too many texture interpolators would be used for ForwardBase pass (11 out of max 10"

here is my script
Shader "Custom/Eyeball" {
Properties
{
_Color ("Base Color", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB)", 2D) = "white" {}
_EyeTex ("Eye Texture (RGB)", 2D) = "white" {}
_EyeColor1 ("Eye Color 1", Color) = (1,1,1,0.5)
_EyeColor2 ("Eye Color 2", Color) = (1,1,1,0.5)
_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
_RimPower ("Rim Power", float) = 3.0
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
[MaterialToggle] RampColor ("Ramp Color", Float) = 0
[MaterialToggle] CreatePupil ("Create Pupil", Float) = 0
[HideInInspector]EyeSize ("Eye Size", float) = 3.0
[HideInInspector]PupilSize ("Pupil Size", float) = 3.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
‪#‎pragma‬ surface surf Lambert vertex:vert
#pragma multi_compile DUMMY RAMPCOLOR_ON
#pragma multi_compile DUMMY2 CREATEPUPIL_ON
sampler2D _MainTex;
sampler2D _EyeTex;
samplerCUBE _Cube;
fixed4 _Color;
fixed4 _EyeColor1;
fixed4 _EyeColor2;
fixed4 _ReflectColor;
uniform float4x4 EyeMatrix;
half _RimPower;
half EyeSize;
half PupilSize;
struct Input {
float2 uv_MainTex;
float4 eyeCoords;
float3 worldRefl;
float3 viewDir;
};
void vert (inout appdata_full v, out Input o)
{
o.eyeCoords = mul(EyeMatrix, mul(_Object2World, v.vertex));
}
void surf (Input IN, inout SurfaceOutput o)
{
half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
if (IN.eyeCoords.z > 0)
{
half4 irisTex = tex2D (_EyeTex, saturate(IN.eyeCoords.xy/EyeSize+0.5));
fixed irisBoundary = saturate((0.99 - irisTex.a)/0.05);
‪#‎ifdef‬ CREATEPUPIL_ON
fixed pupilBoundary = saturate(saturate(irisTex.a - PupilSize) / 0.05);
‪#‎endif‬
#ifdef RAMPCOLOR_ON
#ifdef CREATEPUPIL_ON
o.Albedo = lerp(fixed3(0,0,0), lerp(c.rgb, lerp(_EyeColor1.rgb, _EyeColor2.rgb, irisTex.r), irisBoundary), pupilBoundary) * c.a;
‪#‎else‬
o.Albedo = lerp(c.rgb, lerp(_EyeColor1.rgb, _EyeColor2.rgb, irisTex.r), irisBoundary)* c.a;
#endif
#else
#ifdef CREATEPUPIL_ON
o.Albedo = lerp(fixed3(0,0,0), lerp(c.rgb, irisTex, irisBoundary), pupilBoundary)* c.a;
#else
o.Albedo = lerp(c.rgb, irisTex, irisBoundary)* c.a;
#endif
#endif
}
else
o.Albedo = c.rgb;
//o.Albedo *= _Color.rgb;
fixed4 reflcol = texCUBE (_Cube, IN.worldRefl) * _ReflectColor * c.a * _Color.a;
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
//o.Emission = _ReflectColor.rgb * pow (rim, _RimPower);
o.Emission = (_Color.a * o.Albedo* c.a + reflcol.rgb) * pow (rim, _RimPower);
o.Alpha = 1;
}
ENDCG
}
FallBack "Diffuse"
}

Shader that wraps around as it offsets a texture

I'd like to create a tiled shader that wraps back onto itself as it offsets a transparent texture over time. The result will make a "moving walkway/travellator" across a mesh using a texture representing a single step.
This is what I have so far. But it doesn't wrap around (without using external dependencies as setting the texture's wrap mode to repeat)
Shader "Custom/ScrollingTextureUnlitShader"
{
Properties
{
_FadeValue ("Fade Value", Range(0, 1)) = 1
_ColorTint ("Color Tint", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_ScrollXSpeed ("X Scroll Speed", Range(-10, 10)) = -5
_ScrollYSpeed ("Y Scroll Speed", Range(-10, 10)) = 0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Unlit alpha
float _FadeValue;
float4 _ColorTint;
sampler2D _MainTex;
fixed _ScrollXSpeed;
fixed _ScrollYSpeed;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed2 scrolledUV = IN.uv_MainTex;
fixed xScrollValue = _ScrollXSpeed * _Time;
fixed yScrollValue = _ScrollYSpeed * _Time;
scrolledUV += fixed2 (xScrollValue, yScrollValue);
half4 c = tex2D (_MainTex, scrolledUV);
o.Albedo = c.rgb * _ColorTint;
o.Alpha = c.a * _FadeValue;
}
inline fixed4 LightingUnlit (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
{
fixed4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
ENDCG
}
FallBack "Transparent/Diffuse"
}

Categories