Skip to content
This repository has been archived by the owner on Aug 24, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/3d-render-texture' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
vertxxyz committed Aug 26, 2019
2 parents 203a13c + 6976066 commit c8abfa0
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 95 deletions.
159 changes: 81 additions & 78 deletions Editor/N3DTexturePreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
using System.Reflection;
using UnityEditor;
using UnityEngine;

#if !UNITY_2018_1_OR_NEWER
using System.Linq;
#endif
using Object = UnityEngine.Object;

namespace Vertx
{
Expand Down Expand Up @@ -41,29 +38,29 @@ void OnEnable()
defaultEditor = CreateEditor(targets, Type.GetType("UnityEditor.Texture3DInspector, UnityEditor"));

//Find all types of I3DMaterialOverride, and query whether there's a valid material for the current target.

#if UNITY_2018_1_OR_NEWER
IEnumerable<Type> i3DMaterialOverrideTypes = (IEnumerable<Type>) Type.GetType("UnityEditor.EditorAssemblies, UnityEditor").GetMethod(
"GetAllTypesWithInterface", BindingFlags.NonPublic | BindingFlags.Static, null, new[] {typeof(Type)}, null
).Invoke(null, new object[] {typeof(I3DMaterialOverride)});
#else
IEnumerable<Type> i3DMaterialOverrideTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => p != typeof(I3DMaterialOverride) && typeof(I3DMaterialOverride).IsAssignableFrom(p));
#endif

foreach (Type i3DMaterialOverrideType in i3DMaterialOverrideTypes)
//Safe cast to allow Render Texture 3D to fall-back to this class.
Texture3D texture3D = target as Texture3D;
if (texture3D != null)
{
I3DMaterialOverride i3DMaterialOverride = (I3DMaterialOverride) Activator.CreateInstance(i3DMaterialOverrideType);
m_Material = i3DMaterialOverride.GetMaterial((Texture3D) target);
if (m_Material != null)
foreach (Type i3DMaterialOverrideType in i3DMaterialOverrideTypes)
{
materialOverride = i3DMaterialOverride;
break;
I3DMaterialOverride i3DMaterialOverride = (I3DMaterialOverride) Activator.CreateInstance(i3DMaterialOverrideType);
m_Material3D = i3DMaterialOverride.GetMaterial(texture3D);
if (m_Material3D != null)
{
materialOverride = i3DMaterialOverride;
break;
}
}
}

rCallback = r => { material.SetFloat(R, r ? 1 : 0); };
gCallback = g => { material.SetFloat(G, g ? 1 : 0); };
bCallback = b => { material.SetFloat(B, b ? 1 : 0); };
rCallback = r => { material3D.SetFloat(R, r ? 1 : 0); };
gCallback = g => { material3D.SetFloat(G, g ? 1 : 0); };
bCallback = b => { material3D.SetFloat(B, b ? 1 : 0); };
x = 1;
y = 1;
z = 1;
Expand All @@ -89,6 +86,8 @@ protected override void OnDisable()

public override void OnInspectorGUI() => defaultEditor.OnInspectorGUI();

public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height) => defaultEditor.RenderStaticPreview(assetPath, subAssets, width, height);

private float zoom = 3f;

protected float x = 1, y = 1, z = 1;
Expand All @@ -105,47 +104,77 @@ enum Axis
public override void OnPreviewSettings()
{
defaultEditor.OnPreviewSettings();
PreviewSettings();
}

public void PreviewSettings()
{
bool hasR = false, hasG = false, hasB = false;
// ReSharper disable once PossibleInvalidCastExceptionInForeachLoop
foreach (Texture3D texture3D in targets)
foreach (Object texture in targets)
{
if (texture3D == null) // texture might have disappeared while we're showing this in a preview popup
continue;
NTexturePreview.CheckRGBFormats(texture3D.format, out bool _hasR, out bool _hasG, out bool _hasB);
bool _hasR, _hasG, _hasB;
switch (texture)
{
case Texture3D texture3D:
NTexturePreview.CheckRGBFormats(texture3D.format, out _hasR, out _hasG, out _hasB);
break;
case RenderTexture renderTexture:
NTexturePreview.CheckRGBFormats(renderTexture.format, out _hasR, out _hasG, out _hasB);
break;
default:
continue;
}


hasR = hasR || _hasR;
hasB = hasB || _hasB;
hasG = hasG || _hasG;
}

if (ImplementAxisSliders() && (materialOverride == null || materialOverride.ImplementAxisSliders()))
{
Texture3D defaultTex3D = target as Texture3D;
if (defaultTex3D != null)
int width, height, depth;

switch (target)
{
using (EditorGUI.ChangeCheckScope changeCheckScope = new EditorGUI.ChangeCheckScope())
case Texture3D texture3D:
width = texture3D.width;
height = texture3D.height;
depth = texture3D.depth;
break;
case RenderTexture renderTexture:
width = renderTexture.width;
height = renderTexture.height;
depth = renderTexture.volumeDepth;
break;
default:
return;
}

using (EditorGUI.ChangeCheckScope changeCheckScope = new EditorGUI.ChangeCheckScope())
{
Vector3 size = new Vector3(width, height, depth);
Vector3 sizeCurrent = new Vector3(x * (size.x - 1) + 1, y * (size.y - 1) + 1, z * (size.z - 1) + 1);
axis = (Axis) EditorGUILayout.EnumPopup(axis, s_Styles.previewDropDown, GUILayout.Width(25));
switch (axis)
{
Vector3 size = new Vector3(defaultTex3D.width, defaultTex3D.height, defaultTex3D.depth);
Vector3 sizeCurrent = new Vector3(x * (size.x - 1) + 1, y * (size.y - 1) + 1, z * (size.z - 1) + 1);
axis = (Axis) EditorGUILayout.EnumPopup(axis, s_Styles.previewDropDown, GUILayout.Width(25));
switch (axis)
{
case Axis.X:
x = Mathf.RoundToInt(GUILayout.HorizontalSlider((int) sizeCurrent.x, 1, (int) size.x, s_Styles.previewSlider, s_Styles.previewSliderThumb, GUILayout.Width(200)) - 1) / (size.x - 1);
EditorGUILayout.LabelField(sizeCurrent.x.ToString(), s_Styles.previewLabel, GUILayout.Width(25));
break;
case Axis.Y:
y = Mathf.RoundToInt(GUILayout.HorizontalSlider((int) sizeCurrent.y, 1, (int) size.y, s_Styles.previewSlider, s_Styles.previewSliderThumb, GUILayout.Width(200)) - 1) / (size.y - 1);
EditorGUILayout.LabelField(sizeCurrent.y.ToString(), s_Styles.previewLabel, GUILayout.Width(25));
break;
case Axis.Z:
z = Mathf.RoundToInt(GUILayout.HorizontalSlider((int) sizeCurrent.z, 1, (int) size.z, s_Styles.previewSlider, s_Styles.previewSliderThumb, GUILayout.Width(200)) - 1) / (size.z - 1);
EditorGUILayout.LabelField(sizeCurrent.z.ToString(), s_Styles.previewLabel, GUILayout.Width(25));
break;
}

if (changeCheckScope.changed)
SetXYZFloats();
case Axis.X:
x = Mathf.RoundToInt(GUILayout.HorizontalSlider((int) sizeCurrent.x, 1, (int) size.x, s_Styles.previewSlider, s_Styles.previewSliderThumb, GUILayout.Width(200)) - 1) / (size.x - 1);
EditorGUILayout.LabelField(sizeCurrent.x.ToString(), s_Styles.previewLabel, GUILayout.Width(25));
break;
case Axis.Y:
y = Mathf.RoundToInt(GUILayout.HorizontalSlider((int) sizeCurrent.y, 1, (int) size.y, s_Styles.previewSlider, s_Styles.previewSliderThumb, GUILayout.Width(200)) - 1) / (size.y - 1);
EditorGUILayout.LabelField(sizeCurrent.y.ToString(), s_Styles.previewLabel, GUILayout.Width(25));
break;
case Axis.Z:
z = Mathf.RoundToInt(GUILayout.HorizontalSlider((int) sizeCurrent.z, 1, (int) size.z, s_Styles.previewSlider, s_Styles.previewSliderThumb, GUILayout.Width(200)) - 1) / (size.z - 1);
EditorGUILayout.LabelField(sizeCurrent.z.ToString(), s_Styles.previewLabel, GUILayout.Width(25));
break;
}

if (changeCheckScope.changed)
SetXYZFloats();
}
}

Expand All @@ -162,9 +191,9 @@ public override void OnPreviewSettings()

void SetXYZFloats()
{
material.SetFloat(X, x);
material.SetFloat(Y, y);
material.SetFloat(Z, z);
material3D.SetFloat(X, x);
material3D.SetFloat(Y, y);
material3D.SetFloat(Z, z);
Repaint();
}

Expand Down Expand Up @@ -196,7 +225,7 @@ public override void OnPreviewGUI(Rect r, GUIStyle background)
return;

InitPreview();
material.mainTexture = target as Texture;
material3D.mainTexture = target as Texture;

m_PreviewUtility.BeginPreview(r, background);
bool oldFog = RenderSettings.fog;
Expand All @@ -207,7 +236,7 @@ public override void OnPreviewGUI(Rect r, GUIStyle background)

cameraTransform.rotation = Quaternion.identity;
Quaternion rot = Quaternion.Euler(m_PreviewDir.y, 0, 0) * Quaternion.Euler(0, m_PreviewDir.x, 0);
m_PreviewUtility.DrawMesh(Mesh, Vector3.zero, rot, material, 0);
m_PreviewUtility.DrawMesh(Mesh, Vector3.zero, rot, material3D, 0);
m_PreviewUtility.Render();

Unsupported.SetRenderSettingsUseFogNoDirty(oldFog);
Expand All @@ -225,18 +254,6 @@ void InitPreview()

private I3DMaterialOverride materialOverride;

private Material material
{
get
{
if (m_Material == null)
m_Material = new Material(Resources.Load<Shader>("RGB3DShader"));
return m_Material;
}
}

private Material m_Material;

#region PreviewGUI

//This region contains code taken from the internal PreviewGUI class.
Expand Down Expand Up @@ -279,20 +296,6 @@ public static Vector2 Drag2D(Vector2 scrollPosition, Rect position)

#endregion

private Mesh mesh;
protected Mesh Mesh
{
get
{
if (mesh == null)
mesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx");
return mesh;
}
}

public override bool HasPreviewGUI()
{
return defaultEditor.HasPreviewGUI();
}
public override bool HasPreviewGUI() => defaultEditor.HasPreviewGUI();
}
}
13 changes: 12 additions & 1 deletion Editor/NRenderTexturePreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Vertx
{
Expand All @@ -26,6 +27,16 @@ protected override void OnDisable()
DestroyImmediate(defaultEditor);
}

public override void OnInspectorGUI() => defaultEditor.OnInspectorGUI();
public override void OnInspectorGUI()
{
defaultEditor.OnInspectorGUI();
RenderTexture rt = target as RenderTexture;
if (rt != null && IsVolume() && rt.depth != 0)
{
EditorGUILayout.HelpBox(noSupportFor3DWithDepth, MessageType.Error);
}
}

public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height) => defaultEditor.RenderStaticPreview(assetPath, subAssets, width, height);
}
}
Loading

0 comments on commit c8abfa0

Please sign in to comment.