Skip to content

Instantly share code, notes, and snippets.

@TheStoneBook
Last active September 10, 2018 03:01
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 TheStoneBook/89578338e1d3e00fea2086e9819045e8 to your computer and use it in GitHub Desktop.
Save TheStoneBook/89578338e1d3e00fea2086e9819045e8 to your computer and use it in GitHub Desktop.
【Unity】ポリゴンの拡大縮小【Shader】
Shader "Geometry/PolygonScaling"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MainColor("Color",Color)=(1,1,1,1)
_ScaleFactor("Scale Factor", Range(0.0, 1.0)) = 1.0
//_ScaleFactor("Scale Factor", Range(0.0, 2.0)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct g2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
fixed4 _MainColor;
fixed _ScaleFactor;
//float4 _MainTex_ST;
appdata vert (appdata v)
{
return v;
}
[maxvertexcount(3)]
void geom(triangle appdata input[3], inout TriangleStream<g2f> outStream)
{
//ポリゴンの中心を計算。
float3 center = (input[0].vertex + input[1].vertex + input[2].vertex).xyz / 3;
[unroll]
for(int i=0;i<3;i++)
{
appdata v=input[i];
g2f o;
//v.vertex.xyz = (v.vertex.xyz - center) * (1.0 - _ScaleFactor) + center;
v.vertex.xyz = (v.vertex.xyz - center) * _ScaleFactor + center;
o.vertex=UnityObjectToClipPos(v.vertex);
o.uv=v.uv;
outStream.Append(o);
}
outStream.RestartStrip();
}
fixed4 frag (g2f i) : SV_Target
{
fixed4 col=tex2D(_MainTex, i.uv)*_MainColor;
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment