Last active
March 5, 2023 05:15
-
-
Save laicasaane/4aeba98de592bfa42932969c8d7dcacf to your computer and use it in GitHub Desktop.
A WIP shader that is intended to work with NSprites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "NSprites/RegularNSprites_Builtin_Shader" | |
{ | |
Properties | |
{ | |
_MainTex("_MainTex", 2D) = "white" {} | |
} | |
SubShader | |
{ | |
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } | |
LOD 100 | |
ZTest LEqual //Default | |
// ZTest Less | Greater | GEqual | Equal | NotEqual | Always | |
ZWrite On //Default | |
Cull Off | |
Lighting Off | |
Blend One OneMinusSrcAlpha | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex UnlitVertex | |
#pragma fragment UnlitFragment | |
#pragma target 2.0 | |
#pragma multi_compile_instancing | |
#pragma instancing_options procedural:setup | |
#include "UnityCG.cginc" | |
struct Attributes | |
{ | |
float3 positionOS : POSITION; | |
float2 uv : TEXCOORD0; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct Varyings | |
{ | |
float4 positionCS : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
sampler2D _MainTex; | |
// 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) | |
UNITY_DEFINE_INSTANCED_PROP(float4, _mainTexSTBuffer) | |
UNITY_DEFINE_INSTANCED_PROP(float4, _boundsBuffer) // height-width-pivot | |
UNITY_DEFINE_INSTANCED_PROP(float4, _transformBuffer) // position-flip | |
UNITY_DEFINE_INSTANCED_PROP(float, _sortingValueBuffer) | |
UNITY_INSTANCING_BUFFER_END(Props) | |
void setup() | |
{ | |
#if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) || defined(UNITY_STEREO_INSTANCING_ENABLED) | |
float4 bounds = UNITY_ACCESS_INSTANCED_PROP(Props, _boundsBuffer); | |
float4 transform = UNITY_ACCESS_INSTANCED_PROP(Props, _transformBuffer); | |
float2 scale = float2(bounds.x, bounds.y); | |
float2 pivot = float2(bounds.z, bounds.w); | |
float2 position = float2(transform.x, transform.y) - scale * pivot; | |
unity_ObjectToWorld = half4x4 | |
( | |
scale.x, 0, 0, position.x, | |
0, scale.y, 0, position.y, | |
0, 0, 1, 0, | |
0, 0, 0, 1 | |
); | |
#endif | |
} | |
float2 TilingAndOffset(float2 uv, float2 tiling, float2 offset) | |
{ | |
return uv * tiling + offset; | |
} | |
Varyings UnlitVertex(Attributes attributes) | |
{ | |
Varyings varyings = (Varyings)0; | |
UNITY_SETUP_INSTANCE_ID(attributes); | |
UNITY_TRANSFER_INSTANCE_ID(attributes, varyings); | |
#if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) || defined(UNITY_STEREO_INSTANCING_ENABLED) | |
float4 mainTexST = UNITY_ACCESS_INSTANCED_PROP(Props, _mainTexSTBuffer); | |
float sortingValue = UNITY_ACCESS_INSTANCED_PROP(Props, _sortingValueBuffer); | |
float4 transform = UNITY_ACCESS_INSTANCED_PROP(Props, _transformBuffer); | |
int2 flipValue = int2(int(transform.z), int(transform.w)); | |
#else | |
float4 mainTexST = float4(1, 1, 0, 0); | |
float sortingValue = 0; | |
int2 flipValue = int2(0, 0); | |
#endif | |
attributes.uv.x = flipValue.x >= 0 ? attributes.uv.x : (1.0 - attributes.uv.x); | |
attributes.uv.y = flipValue.y >= 0 ? attributes.uv.y : (1.0 - attributes.uv.y); | |
varyings.positionCS = UnityObjectToClipPos(attributes.positionOS); | |
varyings.positionCS.z = sortingValue; | |
varyings.uv = TilingAndOffset(attributes.uv, mainTexST.xy, mainTexST.zw); | |
return varyings; | |
} | |
fixed4 UnlitFragment(Varyings varyings) : SV_Target | |
{ | |
UNITY_SETUP_INSTANCE_ID(varyings); | |
// sample the texture | |
fixed4 texColor = tex2D(_MainTex, varyings.uv); | |
return texColor; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment