Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Last active April 7, 2024 19:19
Show Gist options
  • Save shivaduke28/031cfb685303d43f952971f65bbc7a2c to your computer and use it in GitHub Desktop.
Save shivaduke28/031cfb685303d43f952971f65bbc7a2c to your computer and use it in GitHub Desktop.
Unity shader encode model matrix to RGBA * 4
/*
MIT License
Copyright (c) 2024 shivaduke28
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Shader "ParamToColor/TransformEncoder"
{
Properties
{
_MainTex("Param Tex", 2D) = "white" {}
_Offset("Position Constraint Offset", Vector) = (0, 0, 0)
_Index("Index", Int) = 0
[Header(Stencil)]
_StencilRef("Stencil Ref (default=0)", Float) = 0
[Enum(UnityEngine.Rendering.CompareFunction)]
_StencilComp("Comp (default=Disable)", Float) = 0
[Enum(UnityEngine.Rendering.StencilOp)]
_StencilPass("Pass (default=Keep)", Float) = 0
[Enum(UnityEngine.Rendering.StencilOp)]
_StencilFail("Fail (default=Keep)", Float) = 0
}
SubShader
{
Tags
{
"RenderType"="Opaque"
}
LOD 100
Pass
{
Blend One Zero
Cull Back
ZTest Off
ZWrite Off
Stencil
{
Ref [_StencilRef]
Comp [_StencilComp]
Pass [_StencilPass]
Pass [_StencilFail]
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _MainTex_TexelSize;
float3 _Offset;
uint _Index;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
uint vertexId :SV_VertexID;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
float2 uv;
const uint id = v.vertexId;
// full screen quad in [0,1]x[0,1]
uv.x = id % 2 == 0 ? 0 : 1;
uv.y = id <= 1 ? 0 : 1;
o.uv = v.uv;
uv.y = uv.y * _MainTex_TexelSize.y + _Index * _MainTex_TexelSize;
#if UNITY_UV_STARTS_AT_TOP
uv.y = 1 - uv.y;
#endif
// clip space is [-1,1]x[-1,1]
o.vertex = float4(uv * 2 - 1, 0, 1);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float u = i.uv;
u *= _MainTex_TexelSize.z; // [0, width]
u = floor(u);
uint index = min(3, u);
float4 column = float4(
unity_ObjectToWorld[0][index],
unity_ObjectToWorld[1][index],
unity_ObjectToWorld[2][index],
unity_ObjectToWorld[3][index]);
column.xyz += u == 3 ? -_Offset : 0;
return column;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment