UV islands handling improved

This commit is contained in:
doJester13 2021-03-07 00:53:17 +01:00
parent da5d9c79d4
commit b9df4d5f55
4 changed files with 44 additions and 12 deletions

View File

@ -6,6 +6,7 @@ public class PaintManager : Singleton<PaintManager>{
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<PaintManager>{
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<PaintManager>{
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);

View File

@ -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();
}

View File

@ -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

View File

@ -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;