Skip to content

Commit

Permalink
fixed rendering issue on Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
i-saint committed Jan 15, 2018
1 parent 31d2a1f commit 6eed49d
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 179 deletions.
37 changes: 19 additions & 18 deletions NormalPainter/Assets/UTJ/NormalPainter/Scripts/NormalPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -213,6 +214,8 @@ void BeginEdit()

if (m_matVisualize == null)
m_matVisualize = new Material(AssetDatabase.LoadAssetAtPath<Shader>(AssetDatabase.GUIDToAssetPath("03871fa9be0375f4c91cb4842f15b890")));
if (m_matOverlay == null)
m_matOverlay = new Material(AssetDatabase.LoadAssetAtPath<Shader>(AssetDatabase.GUIDToAssetPath("b531c1011d0464740aa59c2809bbcbb2")));
if (m_matBake == null)
m_matBake = new Material(AssetDatabase.LoadAssetAtPath<Shader>(AssetDatabase.GUIDToAssetPath("4ddd0053dc720414b8afc76bf0a93f8e")));
if (m_csBakeFromMap == null)
Expand Down Expand Up @@ -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; }
}

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ public enum VisualizeType
Normals,
Tangents,
Binormals,
LocalSpaceNormalsOverlay,
TangentSpaceNormalsOverlay,
TangentsOverlay,
BinormalsOverlay,
UVOverlay,
VertexColorOverlay,
Lasso,
BrushRange,
RayPosition,
Expand Down
184 changes: 184 additions & 0 deletions NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
Shader "Hidden/NormalPainter/Overlay" {

CGINCLUDE
#include "UnityCG.cginc"

StructuredBuffer<float3> _BaseNormals;
StructuredBuffer<float4> _BaseTangents;
StructuredBuffer<float3> _Points;
StructuredBuffer<float3> _Normals;
StructuredBuffer<float4> _Tangents;
StructuredBuffer<float> _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
}
}
}
10 changes: 10 additions & 0 deletions NormalPainter/Assets/UTJ/NormalPainter/Shaders/Overlay.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6eed49d

Please sign in to comment.