Skip to content

Instantly share code, notes, and snippets.

@gamemachine
Last active July 21, 2022 14:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamemachine/2fe6e1f919f6fa6cf5b20eb942ddc0da to your computer and use it in GitHub Desktop.
Save gamemachine/2fe6e1f919f6fa6cf5b20eb942ddc0da to your computer and use it in GitHub Desktop.
// For the new DOTS animation. Although you need to use DrawMeshInstanced yourself, Hybrid renderer does not support instanced
// props for builtin.
Shader "Custom/VertexSkinning"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_SkinMatricesOffset("Bone Index Offset", Float) = 0
}
SubShader
{
Tags {
"RenderType"="Opaque"
}
CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
#pragma exclude_renderers gles
#pragma surface surf Standard addshadow fullforwardshadows vertex:vert
#pragma target 4.5
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
#pragma multi_compile_instancing
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
fixed4 _Color;
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 weights : BLENDWEIGHTS;
uint4 indices : BLENDINDICES;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
float2 texcoord2 : TEXCOORD2;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Input
{
float2 uv_MainTex;
};
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float, _SkinMatricesOffset_Array)
#define _SkinMatricesOffset UNITY_ACCESS_INSTANCED_PROP(Props, _SkinMatricesOffset_Array)
UNITY_INSTANCING_BUFFER_END(Props)
#ifdef SHADER_API_D3D11
uniform StructuredBuffer<float3x4> _SkinMatrices;
void Unity_LinearBlendSkinning_float(uint4 indices, int indexOffset, float4 weights, float3 positionIn, float3 normalIn, float3 tangentIn, out float3 positionOut, out float3 normalOut, out float3 tangentOut)
{
for (int i = 0; i < 4; i++)
{
float3x4 skinMatrix = _SkinMatrices[indices[i] + indexOffset];
float3 vtransformed = mul(skinMatrix, float4(positionIn, 1));
float3 ntransformed = mul(skinMatrix, float4(normalIn, 0));
float3 ttransformed = mul(skinMatrix, float4(tangentIn, 0));
positionOut += vtransformed * weights[i];
normalOut += ntransformed * weights[i];
tangentOut += ttransformed * weights[i];
}
}
#endif
void vert(inout appdata v)
{
#ifdef SHADER_API_D3D11
UNITY_SETUP_INSTANCE_ID(v);
int offset = (int)_SkinMatricesOffset;
float3 positionOut = 0;
float3 normalOut = 0;
float3 tangentOut = 0;
Unity_LinearBlendSkinning_float(v.indices, offset, v.weights, v.vertex.xyz, v.normal, v.tangent.xyz, positionOut, normalOut, tangentOut);
v.vertex = float4(positionOut.xyz, v.vertex.w);
v.normal = normalOut;
v.tangent = float4(tangentOut.xyz, v.tangent.w);
#endif
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
@Looooong
Copy link

Can you post the gist for the draw mesh instanced system? Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment