Evening, I'm learning about fragment and vertex shaders (beginner) and I kept scratching my head trying to get it right.
My goal is to make a shader that is tileable (for indoor tiles) and customizable grouts (color and width).
With the help of the Internet, I managed to learn some basic stuff about shaders and made minor tweaks to it. I used a ready made grid shader and tweaked the grid to not tile according to world
space.
Here's the code:
Shader "Test/Unlit Grid"
{
Properties
{
//GRID PROPERTIES
_GridColour ("Grid Colour", color) = (1, 1, 1, 1)
_BaseColour ("Base Colour", color) = (1, 1, 1, 0)
_GridSpacing ("Grid Spacing", float) = 0.1
_LineThickness ("Line Thickness", float) = 1
//2D TEXTURES
_MainTex ("Base Texture", 2D) = "white"{}
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
fixed4 _GridColour;
fixed4 _BaseColour;
float _GridSpacing;
float _LineThickness;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _NormalTex;
//VERT FUNCTION
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//o.uv = mul(unity_ObjectToWorld, v.vertex).xz / _GridSpacing;
o.uv = TRANSFORM_TEX(v.uv, _MainTex)*_GridSpacing;
return o;
}
//FRAG FUNCTION
fixed4 frag (v2f i) : SV_Target
{
float2 wrapped = frac(i.uv);
float2 range = abs(wrapped);
float2 speeds;
// Euclidean norm gives slightly more even thickness on diagonals
float4 deltas = float4(ddx(i.uv), ddy(i.uv));
speeds = sqrt(float2(
dot(deltas.xz, deltas.xz),
dot(deltas.yw, deltas.yw)
));
// Cheaper Manhattan norm in fwidth slightly exaggerates thickness of diagonals
//speeds = fwidth(i.uv)/2;
fixed4 col = tex2D(_MainTex, i.uv);
float2 pixelRange = range/speeds;
float lineWeight = saturate(min(pixelRange.x, pixelRange.y) /_LineThickness);
//float lineWeight = min(pixelRange.x, pixelRange.y) - _LineThickness;
return lerp(_GridColour, col, lineWeight);
}
ENDCG
}
}
}
Sample image:
![alt text][1]
The question is, am I able to mix a Surface Shader below the fragment shader and still retain the grouts from the fragment shader itself?
[1]: /storage/temp/100930-capture.jpg
↧