Skip to content

Commit

Permalink
Changes ported over from MegaSplat dev
Browse files Browse the repository at this point in the history
- Added Mesh Cache baking, which allows you to bake out all of the
painted meshes into a single asset file and mark them static
- Some stuff made public for materials
  • Loading branch information
slipster216 committed Feb 9, 2017
1 parent 9ddb222 commit 3d95f4c
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 53 deletions.
12 changes: 12 additions & 0 deletions Editor/CustomBrushes/BlendNormalsBrush.cs.meta

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

8 changes: 8 additions & 0 deletions Editor/CustomBrushes/NormalBlendBrush.asset.meta

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

108 changes: 108 additions & 0 deletions Editor/CustomUtilities/SaveMeshCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

namespace JBooth.VertexPainterPro
{
[System.Serializable]
public class SaveMeshCache : IVertexPainterUtility
{
public string GetName()
{
return "Bake Scene to Mesh Cache";
}

bool enabledStaticBatching = false;
bool keepMeshReadWrite = true;
bool useCurrentJobs = true;

public void OnGUI(PaintJob[] jobs)
{
EditorGUILayout.HelpBox("Bakes all VertexInstanceStreams down to new meshes, and replaces the current meshes with the baked ones", MessageType.Info);

enabledStaticBatching = EditorGUILayout.Toggle("Enable Static Batching", enabledStaticBatching);
keepMeshReadWrite = EditorGUILayout.Toggle("Keep Mesh Editable", keepMeshReadWrite);
useCurrentJobs = EditorGUILayout.Toggle("Process only current selection", useCurrentJobs);

EditorGUILayout.BeginHorizontal();
EditorGUILayout.Space();
if (GUILayout.Button("Save and Replace"))
{
Bake(enabledStaticBatching, keepMeshReadWrite, useCurrentJobs ? jobs : null);
}

EditorGUILayout.Space();
EditorGUILayout.EndHorizontal();
}


public static void Bake(bool enableStaticBatching, bool keepMeshesReadWrite, PaintJob[] jobs = null)
{
List<VertexInstanceStream> streams = new List<VertexInstanceStream>();
if (jobs == null)
{
streams = new List<VertexInstanceStream>(GameObject.FindObjectsOfType<VertexInstanceStream>());
}
else
{
for (int i = 0; i < jobs.Length; ++i)
{
streams.Add(jobs[i].stream);
}
}

if (streams.Count == 0)
{
Debug.Log("No streams to save");
return;
}

var scene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();
string path = scene.path;
path = path.Replace("\\", "/");
if (path.Contains(".unity"))
{
path = path.Replace(".unity", "");
}
path += "_meshcache.asset";

AssetDatabase.DeleteAsset(path);
for (int i = 0; i < streams.Count; ++i)
{
var stream = streams[i];
var mf = stream.GetComponent<MeshFilter>();
if (mf == null || mf.sharedMesh == null)
continue;

var msci = mf.GetComponent<MegaSplatCollisionInfo>();
if (msci)
{
msci.BakeForSerialize();
EditorUtility.SetDirty(msci);
}

Mesh m = VertexPainterUtilities.BakeDownMesh(mf.sharedMesh, stream);
m.name = mf.gameObject.name;
if (i == 0)
{
AssetDatabase.CreateAsset(m, path);
}
else
{
AssetDatabase.AddObjectToAsset(m, path);
}

mf.sharedMesh = m;
mf.gameObject.isStatic = enableStaticBatching;
m.UploadMeshData(!keepMeshesReadWrite);
GameObject.DestroyImmediate(mf.gameObject.GetComponent<VertexInstanceStream>());

EditorUtility.SetDirty(mf.gameObject);
}
AssetDatabase.SaveAssets();

}

}
}
12 changes: 12 additions & 0 deletions Editor/CustomUtilities/SaveMeshCache.cs.meta

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

25 changes: 21 additions & 4 deletions Editor/VertexPainterWindow_GUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static bool DrawRollup(string text, bool defaultState = true, bool inset
rolloutStyle = GUI.skin.box;
rolloutStyle.normal.textColor = EditorGUIUtility.isProSkin ? Color.white : Color.black;
}
GUI.contentColor = EditorGUIUtility.isProSkin ? Color.white : Color.black;
if (inset == true)
{
EditorGUILayout.BeginHorizontal();
Expand Down Expand Up @@ -192,8 +193,6 @@ void DrawChannelGUI()
var oldShow = showVertexShader;
EditorGUILayout.BeginHorizontal();
showVertexShader = GUILayout.Toggle(showVertexShader, "Show Vertex Data (ctrl-V)");
showNormals = GUILayout.Toggle(showNormals, "Normals");
showTangents = GUILayout.Toggle(showTangents, "Tangents");
if (oldShow != showVertexShader)
{
UpdateDisplayMode();
Expand Down Expand Up @@ -221,8 +220,10 @@ void DrawChannelGUI()
brushVisualization = (BrushVisualization)EditorGUILayout.EnumPopup("Brush Visualization", brushVisualization);
EditorGUILayout.BeginHorizontal();
showVertexPoints = GUILayout.Toggle(showVertexPoints, "Show Brush Influence");
showVertexSize = EditorGUILayout.Slider(" Size", showVertexSize, 0.2f, 10);
showVertexSize = EditorGUILayout.Slider(showVertexSize, 0.2f, 10);
showVertexColor = EditorGUILayout.ColorField(showVertexColor, GUILayout.Width(40));
showNormals = GUILayout.Toggle(showNormals, "N");
showTangents = GUILayout.Toggle(showTangents, "T");
EditorGUILayout.EndHorizontal();
bool oldHideMeshWireframe = hideMeshWireframe;
hideMeshWireframe = !GUILayout.Toggle(!hideMeshWireframe, "Show Wireframe (ctrl-W)");
Expand Down Expand Up @@ -373,7 +374,7 @@ void DrawBrushSettingsGUI()
{
brushSize = EditorGUILayout.Slider("Brush Size", brushSize, 0.01f, 30.0f);
brushFlow = EditorGUILayout.Slider("Brush Flow", brushFlow, 0.1f, 128.0f);
brushFalloff = EditorGUILayout.Slider("Brush Falloff", brushFalloff, 0.1f, 4.0f);
brushFalloff = EditorGUILayout.Slider("Brush Falloff", brushFalloff, 0.1f, 3.5f);

if (tab == Tab.Paint && flowTarget != FlowTarget.ColorBA && flowTarget != FlowTarget.ColorRG)
{
Expand All @@ -398,12 +399,20 @@ void DrawCustomGUI()
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Fill"))
{
if (OnBeginStroke != null)
{
OnBeginStroke(jobs);
}
for (int i = 0; i < jobs.Length; ++i)
{
Undo.RecordObject(jobs[i].stream, "Vertex Painter Fill");
FillMesh(jobs[i]);
}
Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
if (OnEndStroke != null)
{
OnEndStroke();
}
}

EditorGUILayout.EndHorizontal();
Expand Down Expand Up @@ -489,11 +498,19 @@ void DrawPaintGUI()
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Fill"))
{
if (OnBeginStroke != null)
{
OnBeginStroke(jobs);
}
for (int i = 0; i < jobs.Length; ++i)
{
Undo.RecordObject(jobs[i].stream, "Vertex Painter Fill");
FillMesh(jobs[i]);
}
if (OnEndStroke != null)
{
OnEndStroke();
}
Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
}
if (GUILayout.Button("Random"))
Expand Down
Loading

0 comments on commit 3d95f4c

Please sign in to comment.