From 6eed49d309286a88cedf5e61b19f5f64472be67f Mon Sep 17 00:00:00 2001 From: i-saint Date: Tue, 16 Jan 2018 04:47:52 +0900 Subject: [PATCH] fixed rendering issue on Mac --- .../NormalPainter/Scripts/NormalPainter.cs | 37 ++-- .../Scripts/NormalPainter_impl.cs | 6 - .../UTJ/NormalPainter/Shaders/Overlay.shader | 184 ++++++++++++++++++ .../NormalPainter/Shaders/Overlay.shader.meta | 10 + .../NormalPainter/Shaders/Visualizer.shader | 163 +--------------- 5 files changed, 221 insertions(+), 179 deletions(-) create mode 100644 NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader create mode 100644 NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader.meta diff --git a/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter.cs b/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter.cs index d783f71..bb30f49 100644 --- a/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter.cs +++ b/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter.cs @@ -41,6 +41,7 @@ public class Record [SerializeField] Mesh m_meshVector; [SerializeField] Mesh m_meshLasso; [SerializeField] Material m_matVisualize; + [SerializeField] Material m_matOverlay; [SerializeField] Material m_matBake; [SerializeField] ComputeShader m_csBakeFromMap; @@ -52,7 +53,7 @@ public class Record ComputeBuffer m_cbSelection; ComputeBuffer m_cbBaseNormals; ComputeBuffer m_cbBaseTangents; - ComputeBuffer m_cbBrushSamples; + Texture2D m_texBrushSamples; CommandBuffer m_cmdDraw; bool m_skinned; @@ -213,6 +214,8 @@ void BeginEdit() if (m_matVisualize == null) m_matVisualize = new Material(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath("03871fa9be0375f4c91cb4842f15b890"))); + if (m_matOverlay == null) + m_matOverlay = new Material(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath("b531c1011d0464740aa59c2809bbcbb2"))); if (m_matBake == null) m_matBake = new Material(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath("4ddd0053dc720414b8afc76bf0a93f8e"))); if (m_csBakeFromMap == null) @@ -367,7 +370,7 @@ void ReleaseComputeBuffers() if (m_cbSelection != null) { m_cbSelection.Release(); m_cbSelection = null; } if (m_cbBaseNormals != null) { m_cbBaseNormals.Release(); m_cbBaseNormals = null; } if (m_cbBaseTangents != null) { m_cbBaseTangents.Release(); m_cbBaseTangents = null; } - if (m_cbBrushSamples!= null) { m_cbBrushSamples.Release(); m_cbBrushSamples = null; } + if (m_texBrushSamples != null) { DestroyImmediate(m_texBrushSamples); m_texBrushSamples = null; } if (m_cmdDraw != null) { m_cmdDraw.Release(); m_cmdDraw = null; } } @@ -824,14 +827,14 @@ void OnDrawGizmosSelected() if (brushMode) { var bd = m_settings.activeBrush; - if (m_cbBrushSamples == null) + if (m_texBrushSamples == null) { - m_cbBrushSamples = new ComputeBuffer(bd.samples.Length, 4); + m_texBrushSamples = new Texture2D(bd.samples.Length, 1, TextureFormat.RFloat, false); } - m_cbBrushSamples.SetData(bd.samples); + m_texBrushSamples.LoadRawTextureData(bd.samples, bd.samples.Length * 4); + m_texBrushSamples.Apply(); m_matVisualize.SetVector("_BrushPos", new Vector4(m_rayPos.x, m_rayPos.y, m_rayPos.z, bd.radius)); - m_matVisualize.SetInt("_NumBrushSamples", bd.samples.Length); - m_matVisualize.SetBuffer("_BrushSamples", m_cbBrushSamples); + m_matVisualize.SetTexture("_BrushSamples", m_texBrushSamples); } else { @@ -867,18 +870,16 @@ void OnDrawGizmosSelected() // overlay if(m_settings.modelOverlay != ModelOverlay.None) { - int pass = 0; - switch (m_settings.modelOverlay) - { - case ModelOverlay.LocalSpaceNormals: pass = (int)VisualizeType.LocalSpaceNormalsOverlay; break; - case ModelOverlay.TangentSpaceNormals: pass = (int)VisualizeType.TangentSpaceNormalsOverlay; break; - case ModelOverlay.Tangents: pass = (int)VisualizeType.TangentsOverlay; break; - case ModelOverlay.Binormals: pass = (int)VisualizeType.BinormalsOverlay; break; - case ModelOverlay.UV: pass = (int)VisualizeType.UVOverlay; break; - case ModelOverlay.VertexColor: pass = (int)VisualizeType.VertexColorOverlay; break; - } + if (m_cbPoints != null) m_matOverlay.SetBuffer("_Points", m_cbPoints); + if (m_cbNormals != null) m_matOverlay.SetBuffer("_Normals", m_cbNormals); + if (m_cbTangents != null) m_matOverlay.SetBuffer("_Tangents", m_cbTangents); + if (m_cbSelection != null) m_matOverlay.SetBuffer("_Selection", m_cbSelection); + if (m_cbBaseNormals != null) m_matOverlay.SetBuffer("_BaseNormals", m_cbBaseNormals); + if (m_cbBaseTangents != null) m_matOverlay.SetBuffer("_BaseTangents", m_cbBaseTangents); + + int pass = (int)m_settings.modelOverlay - 1; for (int si = 0; si < m_meshTarget.subMeshCount; ++si) - m_cmdDraw.DrawRenderer(renderer, m_matVisualize, si, pass); + m_cmdDraw.DrawRenderer(renderer, m_matOverlay, si, pass); } // visualize brush range diff --git a/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter_impl.cs b/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter_impl.cs index 373807b..a006aed 100644 --- a/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter_impl.cs +++ b/NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter_impl.cs @@ -102,12 +102,6 @@ public enum VisualizeType Normals, Tangents, Binormals, - LocalSpaceNormalsOverlay, - TangentSpaceNormalsOverlay, - TangentsOverlay, - BinormalsOverlay, - UVOverlay, - VertexColorOverlay, Lasso, BrushRange, RayPosition, diff --git a/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader b/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader new file mode 100644 index 0000000..b95acd4 --- /dev/null +++ b/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader @@ -0,0 +1,184 @@ +Shader "Hidden/NormalPainter/Overlay" { + +CGINCLUDE +#include "UnityCG.cginc" + +StructuredBuffer _BaseNormals; +StructuredBuffer _BaseTangents; +StructuredBuffer _Points; +StructuredBuffer _Normals; +StructuredBuffer _Tangents; +StructuredBuffer _Selection; + + + +struct ia_out +{ + float4 vertex : POSITION; + float4 normal : NORMAL; + float4 uv : TEXCOORD0; + float4 color : COLOR; + uint vertexID : SV_VertexID; + uint instanceID : SV_InstanceID; +}; + +struct vs_out +{ + float4 vertex : SV_POSITION; + float4 color : TEXCOORD0; +}; + + +vs_out vert_local_space_normals_overlay(ia_out v) +{ + vs_out o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.color.rgb = v.normal.xyz * 0.5 + 0.5; + o.color.a = 1.0; + return o; +} + +float3 ToBaseTangentSpace(uint vid, float3 n) +{ + float3 base_normal = _BaseNormals[vid]; + float4 base_tangent = _BaseTangents[vid]; + float3 base_binormal = normalize(cross(base_normal, base_tangent.xyz) * base_tangent.w); + float3x3 tbn = float3x3(base_tangent.xyz, base_binormal, base_normal); + return normalize(mul(n, transpose(tbn))); +} + +vs_out vert_tangent_space_normals_overlay(ia_out v) +{ + vs_out o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.color.rgb = ToBaseTangentSpace(v.vertexID, v.normal.xyz) * 0.5 + 0.5; + o.color.a = 1.0; + return o; +} + +vs_out vert_tangents_overlay(ia_out v) +{ + vs_out o; + o.vertex = UnityObjectToClipPos(v.vertex); + + float4 tangent = _Tangents[v.vertexID]; + o.color.rgb = (tangent.xyz * tangent.w) * 0.5 + 0.5; + o.color.a = 1.0; + return o; +} + +vs_out vert_binormals_overlay(ia_out v) +{ + vs_out o; + o.vertex = UnityObjectToClipPos(v.vertex); + + float4 tangent = _Tangents[v.vertexID]; + float3 binormal = normalize(cross(v.normal.xyz, tangent.xyz * tangent.w)); + o.color.rgb = binormal * 0.5 + 0.5; + o.color.a = 1.0; + return o; +} + +vs_out vert_uv_overlay(ia_out v) +{ + vs_out o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.color = float4(v.uv.xy, 0.0, 1.0); + return o; +} + +vs_out vert_color_overlay(ia_out v) +{ + vs_out o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.color = v.color; + return o; +} + + +float4 frag(vs_out v) : SV_Target +{ + return v.color; +} + +ENDCG + + SubShader + { + Tags{ "RenderType" = "Transparent" "Queue" = "Transparent+99" } + Blend SrcAlpha OneMinusSrcAlpha + ZWrite Off + + // pass 0: local space normals overlay + Pass + { + ZTest LEqual + + CGPROGRAM + #pragma vertex vert_local_space_normals_overlay + #pragma fragment frag + #pragma target 4.5 + ENDCG + } + + // pass 1: tangent space normals overlay + Pass + { + ZTest LEqual + + CGPROGRAM + #pragma vertex vert_tangent_space_normals_overlay + #pragma fragment frag + #pragma target 4.5 + ENDCG + } + + // pass 2: tangents overlay + Pass + { + ZTest LEqual + + CGPROGRAM + #pragma vertex vert_tangents_overlay + #pragma fragment frag + #pragma target 4.5 + ENDCG + } + + // pass 3: binormals overlay + Pass + { + ZTest LEqual + + CGPROGRAM + #pragma vertex vert_binormals_overlay + #pragma fragment frag + #pragma target 4.5 + ENDCG + } + + // pass 4: uv overlay + Pass + { + ZTest LEqual + + CGPROGRAM + #pragma vertex vert_uv_overlay + #pragma fragment frag + #pragma target 4.5 + ENDCG + } + + // pass 5: vertex color overlay + Pass + { + ZTest LEqual + + CGPROGRAM + #pragma vertex vert_color_overlay + #pragma fragment frag + #pragma target 4.5 + ENDCG + } + } +} diff --git a/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader.meta b/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader.meta new file mode 100644 index 0000000..8d85ee3 --- /dev/null +++ b/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: b531c1011d0464740aa59c2809bbcbb2 +timeCreated: 1516045056 +licenseType: Pro +ShaderImporter: + externalObjects: {} + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Visualizer.shader b/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Visualizer.shader index 39bf548..d3ba958 100644 --- a/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Visualizer.shader +++ b/NormalPainter/Assets/UTJ/NormalPainter/Shaders/Visualizer.shader @@ -19,16 +19,11 @@ float4 _Direction; int _OnlySelected = 0; float4x4 _Transform; -StructuredBuffer _BaseNormals; -StructuredBuffer _BaseTangents; StructuredBuffer _Points; StructuredBuffer _Normals; StructuredBuffer _Tangents; StructuredBuffer _Selection; - -int _NumBrushSamples; -StructuredBuffer _BrushSamples; - +sampler2D _BrushSamples; struct ia_out @@ -62,12 +57,9 @@ vs_out vert_vertices(ia_out v) vs_out o; o.vertex = vertex; o.color = lerp(_VertexColor, _VertexColor2, s); - - - float d = length(pos - _BrushPos.xyz); - if (d < _BrushPos.w) { - int bsi = clamp(1.0f - d / _BrushPos.w, 0, 1) * (_NumBrushSamples - 1); - o.color.rgb += _VertexColor3.rgb * _BrushSamples[bsi]; + float d = length(pos - _BrushPos.xyz) / _BrushPos.w; + if (d < 1) { + o.color.rgb += _VertexColor3.rgb * tex2Dlod(_BrushSamples, float4(1 - d, 0, 0, 0)).r; } return o; } @@ -129,73 +121,6 @@ vs_out vert_binormals(ia_out v) return o; } - -vs_out vert_local_space_normals_overlay(ia_out v) -{ - vs_out o; - o.vertex = UnityObjectToClipPos(v.vertex); - o.color.rgb = v.normal.xyz * 0.5 + 0.5; - o.color.a = 1.0; - return o; -} - -float3 ToBaseTangentSpace(uint vid, float3 n) -{ - float3 base_normal = _BaseNormals[vid]; - float4 base_tangent = _BaseTangents[vid]; - float3 base_binormal = normalize(cross(base_normal, base_tangent.xyz) * base_tangent.w); - float3x3 tbn = float3x3(base_tangent.xyz, base_binormal, base_normal); - return normalize(mul(n, transpose(tbn))); -} - -vs_out vert_tangent_space_normals_overlay(ia_out v) -{ - vs_out o; - o.vertex = UnityObjectToClipPos(v.vertex); - o.color.rgb = ToBaseTangentSpace(v.vertexID, v.normal.xyz) * 0.5 + 0.5; - o.color.a = 1.0; - return o; -} - -vs_out vert_tangents_overlay(ia_out v) -{ - vs_out o; - o.vertex = UnityObjectToClipPos(v.vertex); - - float4 tangent = _Tangents[v.vertexID]; - o.color.rgb = (tangent.xyz * tangent.w) * 0.5 + 0.5; - o.color.a = 1.0; - return o; -} - -vs_out vert_binormals_overlay(ia_out v) -{ - vs_out o; - o.vertex = UnityObjectToClipPos(v.vertex); - - float4 tangent = _Tangents[v.vertexID]; - float3 binormal = normalize(cross(v.normal.xyz, tangent.xyz * tangent.w)); - o.color.rgb = binormal * 0.5 + 0.5; - o.color.a = 1.0; - return o; -} - -vs_out vert_uv_overlay(ia_out v) -{ - vs_out o; - o.vertex = UnityObjectToClipPos(v.vertex); - o.color = float4(v.uv.xy, 0.0, 1.0); - return o; -} - -vs_out vert_color_overlay(ia_out v) -{ - vs_out o; - o.vertex = UnityObjectToClipPos(v.vertex); - o.color = v.color; - return o; -} - vs_out vert_lasso(ia_out v) { vs_out o; @@ -316,80 +241,8 @@ ENDCG #pragma target 4.5 ENDCG } - - // pass 4: local space normals overlay - Pass - { - ZTest LEqual - - CGPROGRAM - #pragma vertex vert_local_space_normals_overlay - #pragma fragment frag - #pragma target 4.5 - ENDCG - } - - // pass 5: tangent space normals overlay - Pass - { - ZTest LEqual - - CGPROGRAM - #pragma vertex vert_tangent_space_normals_overlay - #pragma fragment frag - #pragma target 4.5 - ENDCG - } - - // pass 6: tangents overlay - Pass - { - ZTest LEqual - - CGPROGRAM - #pragma vertex vert_tangents_overlay - #pragma fragment frag - #pragma target 4.5 - ENDCG - } - - // pass 7: binormals overlay - Pass - { - ZTest LEqual - - CGPROGRAM - #pragma vertex vert_binormals_overlay - #pragma fragment frag - #pragma target 4.5 - ENDCG - } - - // pass 8: uv overlay - Pass - { - ZTest LEqual - - CGPROGRAM - #pragma vertex vert_uv_overlay - #pragma fragment frag - #pragma target 4.5 - ENDCG - } - - // pass 9: vertex color overlay - Pass - { - ZTest LEqual - - CGPROGRAM - #pragma vertex vert_color_overlay - #pragma fragment frag - #pragma target 4.5 - ENDCG - } - // pass 10: lasso + // pass 4: lasso Pass { ZTest Always @@ -401,7 +254,7 @@ ENDCG ENDCG } - // pass 11: brush range + // pass 5: brush range Pass { ZTest LEqual @@ -413,7 +266,7 @@ ENDCG ENDCG } - // pass 12: ray position + // pass 6: ray position Pass { ZTest LEqual @@ -425,7 +278,7 @@ ENDCG ENDCG } - // pass 13: direction + // pass 7: direction Pass { ZTest LEqual