Skip to content

Commit

Permalink
Clone dataset, tf and material when saving volume as prefab (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlavik1 authored Feb 22, 2024
1 parent 33f2357 commit 186d6cd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Assets/Editor/DragDropHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if UNITY_2021_2_OR_NEWER
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

Expand All @@ -16,6 +18,8 @@ static void OnLoad()
DragAndDrop.AddDropHandler(OnSceneDrop);
// Scene hierarchy
DragAndDrop.AddDropHandler(OnHierarchyDrop);
// Project browser
DragAndDrop.AddDropHandler(OnProjectBrowserDrop);
}

private static DragAndDropVisualMode OnSceneDrop(Object dropUpon, Vector3 worldPosition, Vector2 viewportPosition, Transform parentForDraggedObjects, bool perform)
Expand Down Expand Up @@ -53,6 +57,48 @@ private static DragAndDropVisualMode OnHierarchyDrop(int dropTargetInstanceID, H

return DragAndDropVisualMode.Move;
}

private static DragAndDropVisualMode OnProjectBrowserDrop(int dragInstanceId, string dropUponPath, bool perform)
{
bool shouldHandle = DragAndDrop.objectReferences.Any(obj =>
obj is GameObject && (obj as GameObject).GetComponentsInChildren<VolumeRenderedObject>().Length > 0);
if (!shouldHandle)
return DragAndDropVisualMode.None;
else if (!perform)
return DragAndDropVisualMode.Copy;

foreach (Object objRef in DragAndDrop.objectReferences)
{
GameObject gameObject = objRef as GameObject;
if (gameObject == null)
continue;
VolumeRenderedObject[] volRendObjects = gameObject.GetComponentsInChildren<VolumeRenderedObject>();
if (volRendObjects.Length == 0)
continue;

string prefabPath = Path.Combine(dropUponPath, gameObject.name + ".prefab");
GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, prefabPath, InteractionMode.AutomatedAction);
VolumeRenderedObject[] prefabVolRendObjects = prefab.GetComponentsInChildren<VolumeRenderedObject>();
Debug.Assert(volRendObjects.Length == prefabVolRendObjects.Length);
for (int i = 0; i < volRendObjects.Length; i++)
{
VolumeRenderedObject srcVolRendObj = volRendObjects[i];
VolumeRenderedObject prefabVolRendObj = prefabVolRendObjects[i];
VolumeDataset dataset = ScriptableObject.Instantiate(srcVolRendObj.dataset);
TransferFunction transferFunction = ScriptableObject.Instantiate(srcVolRendObj.transferFunction);
Material material = Material.Instantiate(srcVolRendObj.meshRenderer.sharedMaterial);
AssetDatabase.AddObjectToAsset(dataset, prefab);
AssetDatabase.AddObjectToAsset(transferFunction, prefab);
AssetDatabase.AddObjectToAsset(material, prefab);
prefabVolRendObj.dataset = dataset;
prefabVolRendObj.transferFunction = transferFunction;
prefabVolRendObj.meshRenderer.material = material;
}
PrefabUtility.SavePrefabAsset(prefab);
}

return DragAndDropVisualMode.Copy;
}
}
}
#endif
13 changes: 13 additions & 0 deletions Assets/Scripts/VolumeObject/VolumeRenderedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ private async Task UpdateMaterialPropertiesAsync(IProgressHandler progressHandle

private void UpdateMatInternal()
{
if (meshRenderer.sharedMaterial.GetTexture("_DataTex") == null)
{
meshRenderer.sharedMaterial.SetTexture("_DataTex", dataset.GetDataTexture());
}

if (meshRenderer.sharedMaterial.GetTexture("_NoiseTex") == null)
{
const int noiseDimX = 512;
const int noiseDimY = 512;
Texture2D noiseTexture = NoiseTextureGenerator.GenerateNoiseTexture(noiseDimX, noiseDimY);
meshRenderer.sharedMaterial.SetTexture("_NoiseTex", noiseTexture);
}

if (tfRenderMode == TFRenderMode.TF2D)
{
meshRenderer.sharedMaterial.SetTexture("_TFTex", transferFunction2D.GetTexture());
Expand Down

0 comments on commit 186d6cd

Please sign in to comment.