Skip to content

Instantly share code, notes, and snippets.

@cdwfs
Created July 16, 2019 18:04
Show Gist options
  • Save cdwfs/d86d824fef354330a7a9586a2ee8de3a to your computer and use it in GitHub Desktop.
Save cdwfs/d86d824fef354330a7a9586a2ee8de3a to your computer and use it in GitHub Desktop.
Shader "ProcHexGridSurfShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_AlbedoMaps("Albedo (RGB)", 2DArray) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_HexScale("Hex Scale", Range(0,1)) = 1.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 addshadow vertex:my_vert_func
#pragma require 2darray
#pragma target 5.0
UNITY_DECLARE_TEX2DARRAY(_AlbedoMaps);
struct Input
{
float2 uv_AlbedoMaps;
float MainTexLayer;
};
half _Glossiness;
half _Metallic;
half _HexScale;
fixed4 _Color;
uniform float4x4 ObjectToWorld;
uniform float4 CellToXyAsFloat4;
struct Point {
float3 vertex;
float3 normal;
float2 uv;
};
#ifdef SHADER_API_D3D11
StructuredBuffer<Point> points;
StructuredBuffer<int3> cells; // xy = cell location as Q,R; z = texture layer
#endif
// 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)
struct my_appdata_custom {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
float4 texcoord2 : TEXCOORD2;
uint vid : SV_VertexID;
};
void my_vert_func(inout my_appdata_custom v, out Input o) {
#ifdef SHADER_API_D3D11
int instance = v.vid / 12;
int instanceVertId = v.vid % 12;
float2x2 CellToXy = float2x2(CellToXyAsFloat4.x, CellToXyAsFloat4.z,
CellToXyAsFloat4.y, CellToXyAsFloat4.w);
int3 cell = cells[instance];
float2 center = mul(CellToXy, float2(cell.xy));
float3 corner_pos = points[instanceVertId].vertex * _HexScale;
corner_pos.xy += center;
float3 vertex_position = mul(ObjectToWorld, float4(corner_pos, 1));
float3 vertex_normal = mul(ObjectToWorld, float4(points[instanceVertId].normal, 0));
float2 vertex_uv = points[instanceVertId].uv;
v.vertex.xyz = vertex_position;
v.normal = vertex_normal;
v.texcoord.xy = vertex_uv;
v.texcoord1.xy = vertex_uv;
v.texcoord2.xy = vertex_uv;
// custom struct fields in Input struct
UNITY_INITIALIZE_OUTPUT(Input, o);
o.MainTexLayer = float(cell.z);
#endif
}
void mycolor(Input IN, SurfaceOutputStandard o, inout fixed4 color)
{
//color += 0.5;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture array tinted by color
fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_AlbedoMaps, float3(IN.uv_AlbedoMaps.xy, IN.MainTexLayer))* _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment