Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prefab saving when overwriting existing prefab #236

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions Assets/Editor/DragDropHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,44 @@ private static DragAndDropVisualMode OnProjectBrowserDrop(int dragInstanceId, st
{
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;
if (srcVolRendObj.dataset != prefabVolRendObj.dataset)
{
// Dataset changed => remove old one form asset to avoid wasting space
if (prefabVolRendObj.dataset != null)
AssetDatabase.RemoveObjectFromAsset(prefabVolRendObj.dataset);
VolumeDataset dataset = ScriptableObject.Instantiate(srcVolRendObj.dataset);
AssetDatabase.AddObjectToAsset(dataset, prefab);
prefabVolRendObj.dataset = dataset;
}
if (srcVolRendObj.transferFunction != prefabVolRendObj.transferFunction)
{
if (prefabVolRendObj.transferFunction != null)
AssetDatabase.RemoveObjectFromAsset(prefabVolRendObj.transferFunction);
TransferFunction transferFunction = ScriptableObject.Instantiate(srcVolRendObj.transferFunction);
AssetDatabase.AddObjectToAsset(transferFunction, prefab);
prefabVolRendObj.transferFunction = transferFunction;
}
if (srcVolRendObj.meshRenderer.sharedMaterial != prefabVolRendObj.meshRenderer.sharedMaterial)
{
if (prefabVolRendObj.meshRenderer.sharedMaterial != null)
AssetDatabase.RemoveObjectFromAsset(prefabVolRendObj.meshRenderer.sharedMaterial);
Material material = Material.Instantiate(srcVolRendObj.meshRenderer.sharedMaterial);
AssetDatabase.AddObjectToAsset(material, prefab);
prefabVolRendObj.meshRenderer.material = material;
}
}
PrefabUtility.SavePrefabAsset(prefab);
for (int i = 0; i < volRendObjects.Length; i++)
{
VolumeRenderedObject srcVolRendObj = volRendObjects[i];
VolumeRenderedObject prefabVolRendObj = prefabVolRendObjects[i];
if (!AssetDatabase.Contains(srcVolRendObj.dataset))
ScriptableObject.DestroyImmediate(srcVolRendObj.dataset);
srcVolRendObj.dataset = prefabVolRendObj.dataset;
srcVolRendObj.transferFunction = prefabVolRendObj.transferFunction;
srcVolRendObj.meshRenderer.sharedMaterial = prefabVolRendObj.meshRenderer.sharedMaterial;
srcVolRendObj.UpdateMaterialProperties();
}
}

return DragAndDropVisualMode.Copy;
Expand Down
10 changes: 5 additions & 5 deletions Assets/Scripts/VolumeData/VolumeDataset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ await Task.Run(() => {
texture = new Texture3D(dimX, dimY, dimZ, texformat, false);
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixelData(pixelBytes, 0);
texture.Apply();
texture.Apply(false, true);
dataTexture = texture;
pixelBytes.Dispose();
}
Expand All @@ -299,7 +299,7 @@ await Task.Run(() => {
texture = new Texture3D(dimX, dimY, dimZ, texformat, false);
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixelData(pixelBytes, 0);
texture.Apply();
texture.Apply(false, true);
pixelBytes.Dispose();
}
}
Expand All @@ -315,7 +315,7 @@ await Task.Run(() => {
for (int z = 0; z < dimZ; z++)
texture.SetPixel(x, y, z, new Color((float)(data[x + y * dimX + z * (dimX * dimY)] - minValue) / maxRange, 0.0f, 0.0f, 0.0f));

texture.Apply();
texture.Apply(false, true);
}
progressHandler.EndStage();
Debug.Log("Texture generation done.");
Expand Down Expand Up @@ -370,7 +370,7 @@ await Task.Run(() => {
}
progressHandler.EndStage();
progressHandler.StartStage(0.2f, "Uploading gradient texture");
textureTmp.Apply();
textureTmp.Apply(false, true);

progressHandler.EndStage();
Debug.Log("Gradient gereneration done.");
Expand Down Expand Up @@ -401,7 +401,7 @@ await Task.Run(() => {
Texture3D texture = new Texture3D(dimX, dimY, dimZ, texformat, false);
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixels(cols);
texture.Apply();
texture.Apply(false, true);
progressHandler.EndStage();

Debug.Log("Gradient gereneration done.");
Expand Down
8 changes: 5 additions & 3 deletions Assets/Scripts/VolumeObject/VolumeRenderedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public async Task SetTransferFunctionAsync(TransferFunction tf, IProgressHandler
await UpdateMaterialPropertiesAsync(progressHandler);
}

private void UpdateMaterialProperties(IProgressHandler progressHandler = null)
public void UpdateMaterialProperties(IProgressHandler progressHandler = null)
{
Task task = UpdateMaterialPropertiesAsync(progressHandler);
}
Expand All @@ -293,8 +293,10 @@ private async Task UpdateMaterialPropertiesAsync(IProgressHandler progressHandle
try
{
bool useGradientTexture = tfRenderMode == TFRenderMode.TF2D || renderMode == RenderMode.IsosurfaceRendering || lightingEnabled;
Texture3D texture = useGradientTexture ? await dataset.GetGradientTextureAsync(progressHandler) : null;
meshRenderer.sharedMaterial.SetTexture("_GradientTex", texture);
Texture3D gradientTexture = useGradientTexture ? await dataset.GetGradientTextureAsync(progressHandler) : null;
Texture3D dataTexture = await dataset.GetDataTextureAsync(progressHandler);
meshRenderer.sharedMaterial.SetTexture("_DataTex", dataTexture);
meshRenderer.sharedMaterial.SetTexture("_GradientTex", gradientTexture);
UpdateMatInternal();
}
finally
Expand Down
Loading