Skip to content

Instantly share code, notes, and snippets.

@onotchi
Created May 16, 2017 14:51
Show Gist options
  • Save onotchi/eec99c44f41b83c2d7b56bb4c6fd7ed6 to your computer and use it in GitHub Desktop.
Save onotchi/eec99c44f41b83c2d7b56bb4c6fd7ed6 to your computer and use it in GitHub Desktop.
UV2利用で左右非対称デカール表現
Shader "PronamaChan/Toon/Lit Decal" {
Properties {
_Color("Main Color", Color) = (0.5,0.5,0.5,1)
_DecalColor ("Decal Color", Color) = (0.5,0.5,0.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_DecalTex("Decal (RGBA)", 2D) = "black" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
[Enum(OFF,0,FRONT,1,BACK,2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Cull[_CullMode]
CGPROGRAM
#pragma surface surf ToonRamp
sampler2D _Ramp;
// custom lighting function that uses a texture ramp based
// on angle between light direction and normal
#pragma lighting ToonRamp exclude_path:prepass
inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
{
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
half d = dot (s.Normal, lightDir) * 0.5 + 0.5;
half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * ramp * atten * 2;
c.a = 0;
return c;
}
sampler2D _MainTex;
sampler2D _DecalTex;
fixed4 _Color;
fixed4 _DecalColor;
struct Input {
float2 uv_MainTex : TEXCOORD0;
float2 uv2_DecalTex : TEXCOORD1;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
half4 decal = tex2D(_DecalTex, IN.uv2_DecalTex) * _DecalColor;
//half4 decal = tex2Dproj(_DecalTex, UNITY_PROJ_COORD(IN.uv_DecalTex));
c.rgb = lerp(c.rgb, decal.rgb, decal.a);
c *= _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Diffuse"
}
Shader "PronamaChan/Toon/Lit Decal Outline" {
Properties {
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
_DecalColor("Decal Color", Color) = (0.5,0.5,0.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_DecalTex("Decal (RGBA)", 2D) = "black" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
[Enum(OFF,0,FRONT,1,BACK,2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
_OutlineColor("Outline Color", Color) = (0,0,0,1)
_Outline("Outline width", Range(.0, 0.03)) = .005
_OutlineZOffest("Outline Z Offset", float) = .0
}
SubShader {
Tags { "RenderType"="Opaque" }
UsePass "PronamaChan/Toon/Lit Decal/FORWARD"
UsePass "PronamaChan/Toon/Basic Outline/OUTLINE"
}
Fallback "PronamaChan/Toon/Lit Decal"
}
using UnityEngine;
public class UV2Setter : MonoBehaviour
{
public SkinnedMeshRenderer SkinnedMeshRenderer;
private void Start()
{
if (this.SkinnedMeshRenderer == null)
{
this.SkinnedMeshRenderer = this.GetComponentInChildren<SkinnedMeshRenderer>();
}
//メッシュのクローンをSkinnedMeshRendererにセット
var mesh = this.SkinnedMeshRenderer.sharedMesh;
mesh = GameObject.Instantiate(mesh);
mesh.MarkDynamic();
var vertices = mesh.vertices;
var uvs = new Vector2[vertices.Length];
for (int i = 0; i < uvs.Length; i++)
{
uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
}
mesh.uv2 = uvs;
this.SkinnedMeshRenderer.sharedMesh = mesh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment