Skip to content

Instantly share code, notes, and snippets.

@kkolyan
Created May 6, 2020
Embed
What would you like to do?
Shader "OnFoot/TacticalObject" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_MainTexScale("Albedo Scale", Float) = 1
_TileCursorTex ("TileCursorTex (RGB)", 2D) = "green" {}
_TileCursorBoundsLength("_TileCursorBoundsLength", Int) = 0
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_CullingPlaneY("Culling Plane Y", Float) = 666
_GridEnabled("Grid Enabled", Int) = 0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows finalcolor:finalcolor vertex:vert addshadow
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
#include "UnityCG.cginc"
sampler2D _MainTex;
float _MainTexScale;
struct Input {
float2 uv_MainTex;
float3 worldPos;
float3 worldNormal;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
sampler3D _FogTransparency;
int _FogEnabled = 0;
float4 _FogGapOffset;
float4 _FogGapSize;
fixed4 _FogColor;
float _FogBaseTransparency;
float _CullingPlaneY;
int _GridEnabled;
float4 _TileCursorBounds[16];
int _TileCursorBoundsLength = 0;
sampler2D _TileCursorTex;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
#include "Grid.cginc"
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = globalTex(IN, o, _MainTex, _MainTexScale) * _Color;
if (_GridEnabled) {
// draw grid over objects
float grid = drawGrid(IN, o, float3(1, 1, 1) * 3, float3(1, 0.4, 1), float3(0, 0.2, 0));
//// this is for distinction between adjacent objects
//float2 uv = IN.uv_MainTex;
////uv = clamp(uv * 4, 0, 1);
//uv = pow(uv * 2, 1.0/3);
//float grad = uv[0] * uv[1];
float grad = 1;
o.Albedo = lerp(c.rgb, float3(1, 1, 1), grid) * grad;
} else {
o.Albedo = c.rgb;
}
o.Alpha = c.a;
o.Metallic = _Metallic;
o.Smoothness = 0;
}
void finalcolor(Input IN, SurfaceOutputStandard o, inout fixed4 color)
{
float3 norm = IN.worldNormal;
float3 wp = IN.worldPos;
if (norm.x == 0 && norm.z == 0) {
if (wp.y > 0) {
bool applied = false;
for (int i = 0; i < _TileCursorBoundsLength; i ++) {
float bx0 = _TileCursorBounds[i].x;
float bz0 = _TileCursorBounds[i].y;
float bx1 = _TileCursorBounds[i].z;
float bz1 = _TileCursorBounds[i].w;
if (!applied && wp.x > bx0 && wp.z > bz0 && wp.x < bx1 && wp.z < bz1) {
color = lerp(color, globalTex(IN, o,_TileCursorTex, 1) + 0.25, 0.5);
applied = true;
}
}
}
}
if (_FogEnabled) {
float3 wn = IN.worldPos + IN.worldNormal * 0.5;
float3 b0 = _FogGapOffset - float3(.505, 0.005, .505);
float3 b1 = b0 + _FogGapSize + float3(.01, .01, .01);
fixed4 fog = _FogColor;
float minVis = _FogBaseTransparency;
int3 f0 = _FogGapOffset;
if (wn.x < b0.x || wn.z < b0.z || wn.x > b1.x || wn.z > b1.z) {
color = color * minVis + fog;
} else {
int3 wp = int3(floor(wn.x), floor(wn.y), floor(wn.z));// - float3(0.5, 0, 0.5);
float3 f = IN.worldPos + float3(0, .005, 0) - (_FogGapOffset - (float3(.5, 0, .5) + 0.001 * IN.worldNormal));
float3 uv = float3(floor(f.x) + 0.5, floor(f.y) + 0.5, floor(f.z) + 0.5) / _FogGapSize;
fixed4 t = tex3D(_FogTransparency, uv);
color = color * minVis + color * (1 - minVis) * t.r + fog * (1 - t.r);
}
}
if (wp.y - 0.001 >= _CullingPlaneY) {
color = fixed4(.1, .1, .1, 0);
}
}
void vert (inout appdata_full data) {
float4 v = data.vertex;
v = mul(unity_ObjectToWorld, v);
if (v.y > _CullingPlaneY) {
v.y = _CullingPlaneY + 0.01;
}
v = mul(unity_WorldToObject, v);
data.vertex = v;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment