From b9df4d5f55f232c93a453358cca0f1d582cf1cd7 Mon Sep 17 00:00:00 2001 From: doJester13 Date: Sun, 7 Mar 2021 00:53:17 +0100 Subject: [PATCH] UV islands handling improved --- Assets/Scripts/PaintManager.cs | 15 ++++++++++++++- Assets/Scripts/Paintable.cs | 9 +++++++++ Assets/Shaders/ExtendIslands.shader | 25 +++++++++++++++---------- Assets/Shaders/TexturePainter.shader | 7 ++++++- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/Assets/Scripts/PaintManager.cs b/Assets/Scripts/PaintManager.cs index ad58f32..305205e 100644 --- a/Assets/Scripts/PaintManager.cs +++ b/Assets/Scripts/PaintManager.cs @@ -6,6 +6,7 @@ public class PaintManager : Singleton{ public Shader texturePaint; public Shader extendIslands; + int prepareUVID = Shader.PropertyToID("_PrepareUV"); int positionID = Shader.PropertyToID("_PainterPosition"); int hardnessID = Shader.PropertyToID("_Hardness"); int strengthID = Shader.PropertyToID("_Strength"); @@ -13,6 +14,8 @@ public class PaintManager : Singleton{ int blendOpID = Shader.PropertyToID("_BlendOp"); int colorID = Shader.PropertyToID("_PainterColor"); int textureID = Shader.PropertyToID("_MainTex"); + int uvOffsetID = Shader.PropertyToID("_OffsetUV"); + int uvIslandsID = Shader.PropertyToID("_UVIslands"); Material paintMaterial; Material extendMaterial; @@ -30,30 +33,40 @@ public class PaintManager : Singleton{ public void initTextures(Paintable paintable){ RenderTexture mask = paintable.getMask(); + RenderTexture uvIslands = paintable.getUVIslands(); RenderTexture extend = paintable.getExtend(); RenderTexture support = paintable.getSupport(); + Renderer rend = paintable.getRenderer(); command.SetRenderTarget(mask); command.SetRenderTarget(extend); command.SetRenderTarget(support); + paintMaterial.SetFloat(prepareUVID, 1); + command.SetRenderTarget(uvIslands); + command.DrawRenderer(rend, paintMaterial, 0); + Graphics.ExecuteCommandBuffer(command); command.Clear(); } + public void paint(Paintable paintable, Vector3 pos, float radius = 1f, float hardness = .5f, float strength = .5f, Color? color = null){ - RenderTexture mask = paintable.getMask(); + RenderTexture uvIslands = paintable.getUVIslands(); RenderTexture extend = paintable.getExtend(); RenderTexture support = paintable.getSupport(); Renderer rend = paintable.getRenderer(); + paintMaterial.SetFloat(prepareUVID, 0); paintMaterial.SetVector(positionID, pos); paintMaterial.SetFloat(hardnessID, hardness); paintMaterial.SetFloat(strengthID, strength); paintMaterial.SetFloat(radiusID, radius); paintMaterial.SetTexture(textureID, support); paintMaterial.SetColor(colorID, color ?? Color.red); + extendMaterial.SetFloat(uvOffsetID, paintable.extendsIslandOffset); + extendMaterial.SetTexture(uvIslandsID, uvIslands); command.SetRenderTarget(mask); command.DrawRenderer(rend, paintMaterial, 0); diff --git a/Assets/Scripts/Paintable.cs b/Assets/Scripts/Paintable.cs index 5a9fe0f..e8a0755 100644 --- a/Assets/Scripts/Paintable.cs +++ b/Assets/Scripts/Paintable.cs @@ -3,7 +3,10 @@ using UnityEngine; public class Paintable : MonoBehaviour { const int TEXTURE_SIZE = 1024; + public float extendsIslandOffset = 1; + RenderTexture extendIslandsRenderTexture; + RenderTexture uvIslandsRenderTexture; RenderTexture maskRenderTexture; RenderTexture supportTexture; @@ -12,16 +15,21 @@ public class Paintable : MonoBehaviour { int maskTextureID = Shader.PropertyToID("_MaskTexture"); public RenderTexture getMask() => maskRenderTexture; + public RenderTexture getUVIslands() => uvIslandsRenderTexture; public RenderTexture getExtend() => extendIslandsRenderTexture; public RenderTexture getSupport() => supportTexture; public Renderer getRenderer() => rend; void Start() { maskRenderTexture = new RenderTexture(TEXTURE_SIZE, TEXTURE_SIZE, 0); + maskRenderTexture.filterMode = FilterMode.Bilinear; extendIslandsRenderTexture = new RenderTexture(TEXTURE_SIZE, TEXTURE_SIZE, 0); extendIslandsRenderTexture.filterMode = FilterMode.Bilinear; + uvIslandsRenderTexture = new RenderTexture(TEXTURE_SIZE, TEXTURE_SIZE, 0); + uvIslandsRenderTexture.filterMode = FilterMode.Bilinear; + supportTexture = new RenderTexture(TEXTURE_SIZE, TEXTURE_SIZE, 0); supportTexture.filterMode = FilterMode.Bilinear; @@ -33,6 +41,7 @@ public class Paintable : MonoBehaviour { void OnDisable(){ maskRenderTexture.Release(); + uvIslandsRenderTexture.Release(); extendIslandsRenderTexture.Release(); supportTexture.Release(); } diff --git a/Assets/Shaders/ExtendIslands.shader b/Assets/Shaders/ExtendIslands.shader index 2f0decf..aa3db24 100644 --- a/Assets/Shaders/ExtendIslands.shader +++ b/Assets/Shaders/ExtendIslands.shader @@ -1,6 +1,8 @@ Shader "TNTC/ExtendIslands"{ Properties{ _MainTex ("Texture", 2D) = "white" {} + _UVIslands ("Texture UVIsalnds", 2D) = "white" {} + _OffsetUV ("UVOffset", float) = 1 } SubShader{ @@ -28,6 +30,8 @@ sampler2D _MainTex; float4 _MainTex_ST; float4 _MainTex_TexelSize; + float _OffsetUV; + sampler2D _UVIslands; v2f vert (appdata v){ v2f o; @@ -37,19 +41,20 @@ } fixed4 frag (v2f i) : SV_Target{ - float2 offsets[8] = {float2(-1,0), float2(1,0), float2(0,1), float2(0,-1), float2(-1,1), float2(1,1), float2(1,-1), float2(-1,-1)}; + float2 offsets[8] = {float2(-_OffsetUV, 0), float2(_OffsetUV, 0), float2(0, _OffsetUV), float2(0, -_OffsetUV), float2(-_OffsetUV, _OffsetUV), float2(_OffsetUV, _OffsetUV), float2(_OffsetUV, -_OffsetUV), float2(-_OffsetUV, -_OffsetUV)}; float2 uv = i.uv; float4 color = tex2D(_MainTex, uv); + float4 island = tex2D(_UVIslands, uv); - float4 extendedColor = color; - for (int i = 0; i < offsets.Length; i++){ - float2 currentUV = uv + offsets[i] * _MainTex_TexelSize.xy; - float4 offsettedColor = tex2D(_MainTex, currentUV); - extendedColor = max(offsettedColor, extendedColor); - } - - color = extendedColor; - + if(island.z < 1){ + float4 extendedColor = color; + for (int i = 0; i < offsets.Length; i++){ + float2 currentUV = uv + offsets[i] * _MainTex_TexelSize.xy; + float4 offsettedColor = tex2D(_MainTex, currentUV); + extendedColor = max(offsettedColor, extendedColor); + } + color = extendedColor; + } return color; } ENDCG diff --git a/Assets/Shaders/TexturePainter.shader b/Assets/Shaders/TexturePainter.shader index aa71871..8da86d6 100644 --- a/Assets/Shaders/TexturePainter.shader +++ b/Assets/Shaders/TexturePainter.shader @@ -22,6 +22,7 @@ float _Hardness; float _Strength; float4 _PainterColor; + float _PrepareUV; struct appdata{ float4 vertex : POSITION; @@ -49,7 +50,11 @@ return o; } - fixed4 frag (v2f i) : SV_Target{ + fixed4 frag (v2f i) : SV_Target{ + if(_PrepareUV > 0 ){ + return float4(0, 0, 1, 1); + } + float4 col = tex2D(_MainTex, i.uv); float f = mask(i.worldPos, _PainterPosition, _Radius, _Hardness); float edge = f * _Strength;