Unity: Custom shader not obeying alpha (transparency) - c#

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.

Related

How to solve damaged cutout in my hair shader Unity?

This my hair shader
Heres my code of hair Shader:
Shader "Custom/Hair"
{
Properties
{
_MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
_Cutoff ("Alpha Cut-Off", range(0, 1)) = 0.95
_NormalTex ("Normal Map", 2D) = "bump" {}
_NormalPower ("NormalPower", range(0, 5)) = 0.75
_SpecularTex ("Specular (R) Spec Shift (G) Spec Mask (B)", 2D) = "gray" {}
_SpecularMultiplier ("Specular Multiplier", float) = 10.0
_SpecularColor ("Specular Color", Color) = (1,1,1,1)
_SpecularMultiplier2 ("Secondary Specular Multiplier", float) = 10.0
_SpecularColor2 ("Secondary Specular Color", Color) = (1,1,1,1)
_PrimaryShift ( "Specular Primary Shift", float) = -0.01
_SecondaryShift ( "Specular Secondary Shift", float) = -0.28
_RimColor ("Rim Color", Color) = (0,0,0,0)
_RimPower ("Rim Power", Range(1,3)) = 1.292
_TintColor ("Tint Color", Color) = (0,0,0)
_TintPower ("Tint Power", Range(0,1)) = 0.292
[HideInInspector]_LogLut ("_LogLut", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" "Queue" = "Geometry+1" "RequireOption" = "SoftVegetation" }
LOD 200
Blend Off
Cull Off
ZWrite on
CGPROGRAM
#pragma surface surf Hair vertex:vert finalcolor:tonemapping
#pragma target 3.0
#include "./Hair.cginc"
void surf (Input IN, inout SurfaceOutputHair o)
{
surf_base(IN, o);
if(o.Alpha < _Cutoff) {
clip(-1.0);
}
}
ENDCG
}
FallBack "Mobile/VertexLit"
}
i need hair like this, but in game this shader didn't match,
Can anyone to help me fix my hair shader?

How to add transparency to voxel shader

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

How to properly blend two RenderTextures?

I'm trying to create small paint app that uses RenderTexture for painting. My goal is to paint "Brush" texture with the color and alpha on the "Draw" RenderTexture (whole texture has the color (1, 1, 1, 0). I have written a shader for that, but it blends brush quads with black borders:
example
I'm using next shader for painting:
Shader "MyPaint/BrushShader"
{
Properties
{
_BrushTex ("Brush", 2D) = "white" {}
_DrawTex ("Draw Tex", 2D) = "white" {}
_Color ("Main Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags {
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
}
Cull Off
Lighting Off
ZWrite Off
ZTest Always
Pass
{
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
sampler2D _BrushTex;
half4 _BrushTex_ST;
sampler2D _DrawTex;
half4 _DrawTex_ST;
float4 _Color;
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPos = o.vertex;
o.uv = v.uv;
o.color = v.color;
return o;
}
float4 frag (v2f i) : SV_Target
{
float4 brush = tex2D(_BrushTex, i.uv) * _Color;
float2 grabTexcoord = i.screenPos.xy / i.screenPos.w;
grabTexcoord.x = (grabTexcoord.x + 1.0) * 0.5;
grabTexcoord.y = (grabTexcoord.y + 1.0) * 0.5;
#if UNITY_UV_STARTS_AT_TOP
grabTexcoord.y = 1.0 - grabTexcoord.y;
#endif
float4 draw = tex2D(_DrawTex, grabTexcoord);
float4 color = draw * (1.0f - brush.a) + brush * brush.a;
return color;
}
ENDCG
}
}
}
I create a Mesh and render it into "Draw" RenderTexture using CommandBuffer with
RenderTargetIdentifier and Material.
I don't understand how to avoid that black borders of the brush.
I'm using ARGB32 RenderTextures without depth and mip-maps, brush texture:
brush
My goal is to paint like in Photoshop, with constant color, that even works with alpha:
photoshop
If anyone can give me an advice on how to properly blend a RenderTexture I would greatly appreciate it!

How to create a shader that can display a texture made out of many small images in Unity

So what I'm trying to do is load satellite images from an SQL table and wrap them around a sphere to create a globe. I know I've got loading the images covered, I'm just not sure how to make my shader display the images in the correct orientation.
I've gone to the Unity Forums as well as checked out this code from the Unity Docs.
Using the linked shader code and the help I received on the forums, here's the code I've ended up with:
Properties
{
_MainTexArray("Tex", 2DArray) = "" {}
_SliceRange("Slices", Range(0,32)) = 6
_UVScale("UVScale", Float) = 1
_COLUMNS("Columns", Range(0, 5)) = 1
_ROWS("Rows", Range(0, 5)) = 1
_CELLS("Cells", Range(0, 32)) = 16
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// texture arrays are not available everywhere,
// only compile shader on platforms where they are
#pragma require 2darray
#include "UnityCG.cginc"
struct v2f
{
float3 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float _SliceRange;
float _UVScale;
v2f vert(float4 vertex : POSITION)
{
v2f o;
o.vertex = UnityObjectToClipPos(vertex);
o.uv.xy = (vertex.xy + 0.5) * _UVScale;
o.uv.z = (vertex.z + 0.5) * _SliceRange;
return o;
}
float _COLUMNS; //Columns and rows only go between 0 and 1
float _ROWS;
float _CELLS;
UNITY_DECLARE_TEX2DARRAY(_MainTexArray);
half4 frag(v2f i) : SV_Target
{
float3 uv = float3(i.uv.x * _CELLS, i.uv.y * _CELLS, 0);
uv.z = floor(i.uv.x / _COLUMNS) * floor(i.uv.y / _ROWS);
return UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, uv / _CELLS);
}
ENDCG
}
}
Using that I've gotten my materials to look like this:
Here's the code that I'm using to load the SQL images:
textures = new Texture2D[size];
for (int x = 0; x <= 7; x++)
{
for (int y = 0; y <= 3; y++)
{
textures[count] = tiler.Read(x, y, 2); //The z determines the zoom level, so I wouldn't want them all loaded at once
if (textures[count] != null) TextureScale.Bilinear(textures[count], 256, 256);
count++;
}
}
texArr = new Texture2DArray(256, 256, textures.Length, TextureFormat.RGBA32, true, true);
texArr.filterMode = FilterMode.Bilinear;
texArr.wrapMode = TextureWrapMode.Repeat;
for (int i = 0; i < textures.Length; i++)
{
if (textures[i] == null) continue;
texArr.SetPixels(textures[i].GetPixels(0), i, 0);
}
texArr.Apply();
mat.SetTexture("_MainTexArray", texArr);
In the SQL Table, the x and y determines the position of the tile and the z determines the zoom level. I'm just working with one zoom level for now.
Sorry for linking the whole shader class, but I'm not very experienced with shaders so I don't quite know where the problem lies.
If you can index into the array of photos such that you effectively have an equirectangular projection of the globe, you could try using a modified form of the shader code by Farfarer from the Unity forums copied and modified slightly below:
Shader "Custom/Equirectangular" {
Properties{
_MainTexArray("Tex", 2DArray) = "" {}
_COLUMNS("Columns", Int) = 2
_ROWS("Rows", Int) = 2
}
SubShader{
Pass {
Tags {"LightMode" = "Always"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma require 2darray
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 normal : TEXCOORD0;
};
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.normal = v.normal;
return o;
}
UNITY_DECLARE_TEX2DARRAY(_MainTexArray);
int _ROWS;
int _COLUMNS;
#define PI 3.141592653589793
inline float2 RadialCoords(float3 a_coords)
{
float3 a_coords_n = normalize(a_coords);
float lon = atan2(a_coords_n.z, a_coords_n.x);
float lat = acos(a_coords_n.y);
float2 sphereCoords = float2(lon, lat) * (1.0 / PI);
return float2(sphereCoords.x * 0.5 + 0.5, 1 - sphereCoords.y);
}
float4 frag(v2f IN) : COLOR
{
float2 equiUV = RadialCoords(IN.normal);
float2 texIndex;
float2 uvInTex = modf(equiUV * float2(_COLUMNS,_ROWS), texIndex);
int flatTexIndex = texIndex.x * _ROWS + texIndex.y;
return UNITY_SAMPLE_TEX2DARRAY(_MainTexArray,
float3(uvInTex, flatTexIndex));
}
ENDCG
}
}
FallBack "VertexLit"
}
You also need to use
texArr = new Texture2DArray(256, 256, textures.Length, TextureFormat.RGBA32, false, true);
instead of
texArr = new Texture2DArray(256, 256, textures.Length, TextureFormat.RGBA32, true, false);
It works for me if I attach this script to a sphere:
Material myMat;
public List<Texture2D> texes;
IEnumerator Start()
{
yield return null;
myMat = GetComponent<Renderer>().material;
Texture2DArray texArr = new Texture2DArray(256, 256, 9,
TextureFormat.RGBA32, false, true);
texArr.filterMode = FilterMode.Bilinear;
texArr.wrapMode = TextureWrapMode.Clamp;
for (int i = 0 ; i < texes.Count ; i++)
{
texArr.SetPixels(texes[i].GetPixels(), i, 0);
}
texArr.Apply();
myMat.SetTexture("_MainTexArray", texArr);
}
and in texes I add these textures in order:
0:
1:
2:
3:
4:
5:
6:
7:
8:
and set 3 for Rows and Columns, it produces decent results:
If bilinear filtering is enabled, there are still some artifacts at the borders of the textures. But these artifacts have to be zoomed in quite close to see. Due to the lack of adjacent pixels for bilinear filtering they mostly appear as improperly blended or missing pixels:
Of course this example doesn't properly tile so there is an obvious seam along one longitude line:
Since this setup expects normals from a sphere, this only works on things with normals that approximate those of a sphere. So it would not render properly on a plane, for instance.

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