Skip to content

Instantly share code, notes, and snippets.

@noir-neo
Created December 19, 2017 14:56
Show Gist options
  • Select an option

  • Save noir-neo/19a0fac773348491de9e74397b220611 to your computer and use it in GitHub Desktop.

Select an option

Save noir-neo/19a0fac773348491de9e74397b220611 to your computer and use it in GitHub Desktop.
Animate voxel models on Unity
using UnityEngine;
namespace VoxelModel
{
[ExecuteInEditMode]
public class PivotRotator : MonoBehaviour
{
private static readonly float gizmoSize = 0.1f;
private static readonly Color gizmoColor = Color.yellow;
[SerializeField] private Vector3 point;
public Vector3 rotation;
private Vector3 WorldPivot()
{
return transform.TransformPoint(point);
}
private Quaternion WorldRotation()
{
return transform.rotation * Quaternion.Inverse(transform.localRotation) * Quaternion.Euler(rotation);
}
void Update()
{
transform.SetRotationAround(WorldPivot(), WorldRotation());
}
void OnDrawGizmosSelected()
{
Gizmos.color = gizmoColor;
Gizmos.DrawWireSphere(WorldPivot(), gizmoSize);
}
}
public static class TransformExtensions
{
public static void SetRotationAround(this Transform transform, Vector3 point, Quaternion rotation)
{
var position = rotation * Quaternion.Inverse(transform.rotation) * (transform.position - point) + point;
transform.SetPositionAndRotation(position, rotation);
}
}
}
Shader "VoxelModel/VertexColor-Blend" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_SubTex ("Albedo (RGB)", 2D) = "white" {}
_Ratio ("Ratio", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
sampler2D _SubTex;
struct Input {
float2 uv_MainTex;
float2 uv_SubTex;
};
fixed4 _Color;
half _Ratio;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
half4 sub = tex2D(_SubTex, IN.uv_SubTex);
o.Albedo = lerp (c.rgb, sub.rgb, _Ratio);
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Shader "VoxelModel/VertexColor-DepthOffset" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_DepthOffset ("Depth Offset", Float) = 0.0
}
SubShader {
Offset [_DepthOffset] , [_DepthOffset]
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment