Hello everyone!
I’m currently developing a tile based City-Builder for fun and want to make each Tile have a different
fertility and now I want to change the Tile-Colour corresponding to the Fertility-Value.
I never created a Shader before, so please be patient.
Currently I’m trying to change the Saturation of each Tile corresponding to a Texture that is created at
Runtime with Saturation Values as the alpha Value. (Because a float array would be to big for a shader I learned)
I got it to the point where it would change the Saturation of the colours, but I couldn’t get the current corresponding Tile from the current colour (Pixel) or any other way.
I hope it is possible to do this with the current Implementation of Tilemaps Renderer (Shader) in Unity.
Shader "TileMapShader" // Name changed
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
[PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
[PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
[PerRendererData] _Saturations("Saturations", 2D) = "white" {}
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex SpriteVert
#pragma fragment CustomSpriteFrag // Originally SpriteFrag
#pragma multi_compile_instancing
#pragma multi_compile_local _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#pragma target 2.0
#include "UnitySprites.cginc"
#include "UnityCG.cginc"
// Custom properties
float2 _startPosition;
int _Width;
int _Height;
sampler2D _Saturations;
void Unity_Saturation_float(float3 In, float Saturation, out float3 Out)
{
float luma = dot(In, float3(0.2126729, 0.7151522, 0.0721750));
Out = luma.xxx + Saturation.xxx * (In - luma.xxx);
}
fixed4 CustomSpriteFrag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord);
c.rgb *= c.a;
float2 worldXY = mul(unity_ObjectToWorld, IN.texcoord).xy;
// Is needed because not the whole world is one Tilemap
// because I have multiple islands instead of one big landmass
float2 localXY = worldXY - _startPosition;
float2 pos = float2(floor(worldXY.x), floor(worldXY.y));
Unity_Saturation_float(c.rgb, tex2D(_Saturations, pos).a* tex2D(_Saturations, pos).a, c.rgb);
return c;
}
ENDCG
}
}
Fallback "Unlit/Color"
}