Skip to content

Instantly share code, notes, and snippets.

@RoyLab
Created June 23, 2017 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoyLab/7da01e012d04bcdd8355b2453830d861 to your computer and use it in GitHub Desktop.
Save RoyLab/7da01e012d04bcdd8355b2453830d861 to your computer and use it in GitHub Desktop.
instance rendering example
Shader "SpaceFramework/PlanetShaderInstance"
{
Properties
{
_MainTex("Color Texture", 2D) = "white" {}
}
CGINCLUDE
#include "SFCommon.cginc"
#include "UnityCG.cginc"
StructuredBuffer<SQuadNodeVertex> _Meshes;
sampler2D _MainTex;
sampler2D _NormalMap;
ENDCG
SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 100
Pass
{
//Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
UNITY_INSTANCING_CBUFFER_START(MyProperties)
UNITY_DEFINE_INSTANCED_PROP(float, _MeshId)
UNITY_INSTANCING_CBUFFER_END
struct appdata
{
float4 pos : POSITION;
uint vertexId: SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv: TEXCOORD0;
float3 uBase: TEXCOORD1;
float3 vBase: TEXCOORD2;
};
v2f vert(appdata v)
{
v2f o = (v2f)0;
UNITY_SETUP_INSTANCE_ID(v);
int meshId = (int)UNITY_ACCESS_INSTANCED_PROP(_MeshId);
int vId = v.vertexId + meshId * VERTEX_PITCH;
SQuadNodeVertex input = _Meshes[vId];
o.pos = UnityObjectToClipPos(input.pos);
o.uv = input.uv;
//float3 normal = applyRotation(float3(0, 0, 1), input.rot);
//o.normal = UnityObjectToWorldDir(normal);
o.rot = input.rot;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float3 rawN = tex2D(_NormalMap, i.uv);
rawN.xy = rawN.xy * 2 - 1;
float3 normal = normalize(applyRotation(rawN, i.rot));
return tex2D(_MainTex, i.uv) *saturate(dot(float3(0.732, 0, 0.732), UnityObjectToWorldDir(normal)));
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment