From e2aba5e38e1986dc2551715e4b36b84801769d4e Mon Sep 17 00:00:00 2001 From: Rob Cupisz Date: Thu, 1 Sep 2022 13:49:40 +0200 Subject: [PATCH] Fix SDFTexture scene view gizmo and URP and Built-In RP --- Editor/SDFTexture.shader | 8 ++-- Editor/SDFTextureEditor.cs | 47 +++++++++++-------- Editor/Unity.DemoTeam.MeshToSDF.Editor.asmdef | 14 +++++- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/Editor/SDFTexture.shader b/Editor/SDFTexture.shader index 07d2ce5..11eed90 100644 --- a/Editor/SDFTexture.shader +++ b/Editor/SDFTexture.shader @@ -31,7 +31,7 @@ #define AXIS_Y 1 #define COLOR_POS 1 - #define COLOR_NEG float4(0.72, 0, 1, 1) + #define COLOR_NEG float3(0.72, 0, 1) v2f vert(appdata v) { @@ -41,18 +41,18 @@ return o; } - fixed4 frag(v2f i) : SV_Target + float4 frag(v2f i) : SV_Target { float3 uvw = float3(i.texcoord, _Z); - // Axis if (_Axis == AXIS_X) uvw = float3(_Z, i.texcoord.yx); else if (_Axis == AXIS_Y) uvw = float3(i.texcoord.x, _Z, i.texcoord.y); float dist = tex3D(_SDF, uvw).r * _DistanceScale; - return dist > 0.0 ? dist * COLOR_POS : -dist * COLOR_NEG; + float3 color = dist > 0.0 ? dist * COLOR_POS : -dist * COLOR_NEG; + return float4(color, 1); } ENDHLSL diff --git a/Editor/SDFTextureEditor.cs b/Editor/SDFTextureEditor.cs index 71a677e..0cec97d 100644 --- a/Editor/SDFTextureEditor.cs +++ b/Editor/SDFTextureEditor.cs @@ -80,16 +80,6 @@ public class SDFTextureEditor : Editor if (s_Material == null) s_Material = new Material(Shader.Find("Hidden/SDFTexture")); - if (s_Props == null) - s_Props = new MaterialPropertyBlock(); - - s_Props.Clear(); - s_Props.SetFloat(Uniforms._Z, s_Slice); - s_Props.SetInt(Uniforms._Axis, (int)s_Axis); - s_Props.SetVector("_VoxelResolution", new Vector4(voxelResolution.x, voxelResolution.y, voxelResolution.z)); - s_Props.SetFloat(Uniforms._DistanceScale, distanceScale); - s_Props.SetTexture("_SDF", sdf); - Vector3 dir = Vector3.forward * voxelBounds.size.z; Quaternion rot = Quaternion.identity; switch (s_Axis) @@ -101,7 +91,29 @@ public class SDFTextureEditor : Editor matrix *= Matrix4x4.Translate(dir * (s_Slice - 0.5f)); matrix *= Matrix4x4.Scale(voxelBounds.size); matrix *= Matrix4x4.Rotate(rot); + +#if USING_HDRP + if (s_Props == null) + s_Props = new MaterialPropertyBlock(); + + s_Props.Clear(); + s_Props.SetFloat(Uniforms._Z, s_Slice); + s_Props.SetInt(Uniforms._Axis, (int)s_Axis); + s_Props.SetVector("_VoxelResolution", new Vector4(voxelResolution.x, voxelResolution.y, voxelResolution.z)); + s_Props.SetFloat(Uniforms._DistanceScale, distanceScale); + s_Props.SetTexture("_SDF", sdf); + Graphics.DrawMesh(s_Quad, matrix, s_Material, layer:0, camera, submeshIndex:0, properties:s_Props); +#else // URP and Built-In RP + s_Material.SetFloat(Uniforms._Z, s_Slice); + s_Material.SetInt(Uniforms._Axis, (int)s_Axis); + s_Material.SetVector("_VoxelResolution", new Vector4(voxelResolution.x, voxelResolution.y, voxelResolution.z)); + s_Material.SetFloat(Uniforms._DistanceScale, distanceScale); + s_Material.SetTexture("_SDF", sdf); + + s_Material.SetPass(0); + Graphics.DrawMeshNow(s_Quad, matrix); +#endif } static void OnSceneGUI(UnityEditor.SceneView sceneview) @@ -116,16 +128,13 @@ public class SDFTextureEditor : Editor Handles.matrix = matrix; Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual; + if (s_SDFTexture.sdf != null && voxelBounds.extents != Vector3.zero) + { + DoSDFSlice(matrix, sceneview.camera, s_SDFTexture.voxelResolution, voxelBounds, distanceScale:1, s_SDFTexture.sdf); + DoHandles(voxelBounds); + } + DoBounds(s_SDFTexture); - - if (s_SDFTexture.sdf == null) - return; - - if (voxelBounds.extents == Vector3.zero) - return; - - DoSDFSlice(matrix, sceneview.camera, s_SDFTexture.voxelResolution, voxelBounds, distanceScale:1, s_SDFTexture.sdf); - DoHandles(voxelBounds); } public override void OnInspectorGUI() diff --git a/Editor/Unity.DemoTeam.MeshToSDF.Editor.asmdef b/Editor/Unity.DemoTeam.MeshToSDF.Editor.asmdef index f954582..f8655e3 100644 --- a/Editor/Unity.DemoTeam.MeshToSDF.Editor.asmdef +++ b/Editor/Unity.DemoTeam.MeshToSDF.Editor.asmdef @@ -13,5 +13,17 @@ "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [] + "defineConstraints": [], + "versionDefines": [ + { + "name": "com.unity.render-pipelines.universal", + "expression": "0.0", + "define": "USING_URP" + }, + { + "name": "com.unity.render-pipelines.high-definition", + "expression": "0.0", + "define": "USING_HDRP" + } + ] } \ No newline at end of file