Skip to content

Commit

Permalink
Pivot baking example
Browse files Browse the repository at this point in the history
Added example of baking pivot at rotating in a shader..
Added option to bake rotation of pivot into uvs
Added option to bake local or world space pivot data (default is local)
  • Loading branch information
slipster216 committed Aug 23, 2016
1 parent 14c12c2 commit c995861
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 5 deletions.
55 changes: 51 additions & 4 deletions Editor/VertexPainterWindow_BakePivot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,53 @@ enum PivotTarget
}

PivotTarget pivotTarget = PivotTarget.UV2;
bool bakePivotUseLocal = true;
void BakeRotation()
{
switch (pivotTarget)
{
case PivotTarget.UV0:
{
InitBakeChannel(BakeChannel.UV0);
foreach (PaintJob job in jobs)
{
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localRotation.eulerAngles : job.meshFilter.transform.rotation.eulerAngles;
job.stream.SetUV0(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
}
case PivotTarget.UV1:
{
InitBakeChannel(BakeChannel.UV1);
foreach (PaintJob job in jobs)
{
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localRotation.eulerAngles : job.meshFilter.transform.rotation.eulerAngles;
job.stream.SetUV1(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
}
case PivotTarget.UV2:
{
InitBakeChannel(BakeChannel.UV2);
foreach (PaintJob job in jobs)
{
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localRotation.eulerAngles : job.meshFilter.transform.rotation.eulerAngles;
job.stream.SetUV2(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
}
case PivotTarget.UV3:
{
InitBakeChannel(BakeChannel.UV3);
foreach (PaintJob job in jobs)
{
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localRotation.eulerAngles : job.meshFilter.transform.rotation.eulerAngles;
job.stream.SetUV3(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
}
}
}

void BakePivot()
{
Expand All @@ -32,7 +79,7 @@ void BakePivot()
InitBakeChannel(BakeChannel.UV0);
foreach (PaintJob job in jobs)
{
Vector3 lp = job.meshFilter.transform.localPosition;
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localPosition : job.meshFilter.transform.position;
job.stream.SetUV0(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
Expand All @@ -42,7 +89,7 @@ void BakePivot()
InitBakeChannel(BakeChannel.UV1);
foreach (PaintJob job in jobs)
{
Vector3 lp = job.meshFilter.transform.localPosition;
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localPosition : job.meshFilter.transform.position;
job.stream.SetUV1(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
Expand All @@ -52,7 +99,7 @@ void BakePivot()
InitBakeChannel(BakeChannel.UV2);
foreach (PaintJob job in jobs)
{
Vector3 lp = job.meshFilter.transform.localPosition;
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localPosition : job.meshFilter.transform.position;
job.stream.SetUV2(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
Expand All @@ -62,7 +109,7 @@ void BakePivot()
InitBakeChannel(BakeChannel.UV3);
foreach (PaintJob job in jobs)
{
Vector3 lp = job.meshFilter.transform.localPosition;
Vector3 lp = bakePivotUseLocal ? job.meshFilter.transform.localPosition : job.meshFilter.transform.position;
job.stream.SetUV3(new Vector4(lp.x, lp.y, lp.z, UnityEngine.Random.Range(0.0f, 1.0f)), job.verts.Length);
}
break;
Expand Down
7 changes: 6 additions & 1 deletion Editor/VertexPainterWindow_GUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,18 @@ void DrawBakeGUI()
GUILayout.Space(10);
GUILayout.Box("Bake Pivot", new GUILayoutOption[]{GUILayout.ExpandWidth(true), GUILayout.Height(20)});
pivotTarget = (PivotTarget)EditorGUILayout.EnumPopup("Store in", pivotTarget);
bakePivotUseLocal = EditorGUILayout.Toggle("Use Local Space", bakePivotUseLocal);

EditorGUILayout.BeginHorizontal();
EditorGUILayout.Space();
if (GUILayout.Button("Bake"))
if (GUILayout.Button("Bake Pivot"))
{
BakePivot();
}
if (GUILayout.Button("Bake Rotation"))
{
BakeRotation();
}
EditorGUILayout.Space();
EditorGUILayout.EndHorizontal();

Expand Down
9 changes: 9 additions & 0 deletions Examples/PivotBaking.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Examples/PivotBaking/pivot.mat
Binary file not shown.
8 changes: 8 additions & 0 deletions Examples/PivotBaking/pivot.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions Examples/PivotBaking/pivot.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Shader "Unlit/pivot"
{
Properties
{

}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float4 pivot : TEXCOORD2;
};

struct v2f
{
float4 vertex : SV_POSITION;
float4 color : TEXCOORD0;
};


v2f vert (appdata v)
{
v2f o;

// total rotation to apply in radians
float3 rotate = float3(0, _Time.y, 0);
float3 c = cos(rotate);
float3 s = sin(rotate);

// make a matrix for each axis
float4x4 rotateXMtx = float4x4( 1, 0, 0, 0,
0, c.x, -s.x, 0,
0, s.x, c.x, 0,
0, 0, 0, 1);


float4x4 rotateYMtx = float4x4( c.y, 0, s.y, 0,
0, 1, 0, 0,
-s.y, 0, c.y, 0,
0, 0, 0, 1);


float4x4 rotateZMtx = float4x4( c.z, -s.z, 0, 0,
s.z, c.z, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);

// compute the vertex location as an offset from the pivot point
float4 pivot = v.pivot;
float4 offset = v.vertex - pivot;
// rotate around each axis
float4 rx = mul(offset, rotateXMtx);
float4 rxy = mul(rx, rotateYMtx);
float4 rxyz = mul(rxy, rotateZMtx);

// take the final rotation, and add the pivot back in to get our final position, then transform into MVP space
o.vertex = mul(UNITY_MATRIX_MVP, rxyz + pivot);
// the baker puts a random number into the W channel. This is VERY useful, for instance, if you want
// each object to rotate at a random speed or at a random angle, or for use in a random function as
// a seed.
o.color = v.pivot.wwww;
return o;
}

fixed4 frag (v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
}
9 changes: 9 additions & 0 deletions Examples/PivotBaking/pivot.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Examples/PivotBaking/pivot_combined.asset
Binary file not shown.
8 changes: 8 additions & 0 deletions Examples/PivotBaking/pivot_combined.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Examples/PivotBaking/pivot_complete.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Examples/PivotBaking/pivot_complete.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Examples/PivotBaking/pivot_start.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Examples/PivotBaking/pivot_start.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c995861

Please sign in to comment.