Skip to content

Commit

Permalink
added brush and implemented opacity size and hardness
Browse files Browse the repository at this point in the history
  • Loading branch information
IRCSS committed Oct 20, 2019
1 parent 37bc9b9 commit 3646bfd
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 7 deletions.
7 changes: 4 additions & 3 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ MonoBehaviour:
m_MinValue: 0
m_MaxValue: 1
m_WholeNumbers: 0
m_Value: 0
m_Value: 1
m_OnValueChanged:
m_PersistentCalls:
m_Calls: []
Expand Down Expand Up @@ -1405,7 +1405,7 @@ MonoBehaviour:
m_MinValue: 0
m_MaxValue: 1
m_WholeNumbers: 0
m_Value: 0
m_Value: 0.5
m_OnValueChanged:
m_PersistentCalls:
m_Calls: []
Expand Down Expand Up @@ -2059,6 +2059,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ColorPickerShader: {fileID: 4800000, guid: b27fa81f06a9f23438866297617dfde2, type: 3}
brushShader: {fileID: 4800000, guid: 04eb8c138cbd7fc43a8aa07ac9d9760e, type: 3}
--- !u!1 &1902939436
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2319,7 +2320,7 @@ MonoBehaviour:
m_MinValue: 0
m_MaxValue: 1
m_WholeNumbers: 0
m_Value: 0
m_Value: 0.5
m_OnValueChanged:
m_PersistentCalls:
m_Calls: []
Expand Down
70 changes: 70 additions & 0 deletions Assets/Scripts/BrushShader.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Shader "Unlit/BrushShader"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}

}
SubShader
{
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
ZWrite Off
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 proj : TEXCOORD2;
};

sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D_float _CameraDepthTexture;
float4 _Color;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.proj = ComputeScreenPos(o.vertex);
COMPUTE_EYEDEPTH(o.proj.z);
//Use this for linear one
//o.proj.z = COMPUTE_DEPTH_01;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);

return o;
}

fixed4 frag(v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
float depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.proj.xy / i.proj.w));

float a = saturate(depth - i.proj.z);
a = smoothstep(0., 0.2, a);
a = 1. - a;
a = pow(a, 10.);
col.a *= a;
return col* _Color;
}
ENDCG
}
}
}
9 changes: 9 additions & 0 deletions Assets/Scripts/BrushShader.shader.meta

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

64 changes: 63 additions & 1 deletion Assets/Scripts/InputUIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class InputUIManager : MonoBehaviour, IPointerClickHandler, IDragHandler
{

public Shader ColorPickerShader;
public Shader brushShader;

private RawImage valueSaturationPicker;
private RawImage huePicker;
Expand All @@ -16,13 +17,24 @@ public class InputUIManager : MonoBehaviour, IPointerClickHandler, IDragHandler

private Button switchButton;

private Slider opacitySlider;
private Slider sizeSlider;
private Slider hardnessSlider;


private GameObject[] allUIElements;
private RenderTexture valueSaturationImage;
private RenderTexture huePickerImage;
private Material mColorPicker;

private Vector3 ColorPickerCurrentHSV = new Vector3(1f, 1f, 0.5f);
private GameObject mouseRepresentation;
private GameObject mouseHardnessRepresentation;
private Material mouseMaterial;
private Material mouseSoftnessMaterial;

private Vector3 ColorPickerCurrentHSV = new Vector3(1f, 1f, 0.5f);
private float brushSize;
private float brushHardness;


void OnDisable()
Expand Down Expand Up @@ -57,6 +69,22 @@ void Start ()
switchButton = InitializeUIElement("SwitchBackForeGround").GetComponent<Button>();
switchButton.onClick.AddListener(() => buttonCallBack(switchButton));

opacitySlider = InitializeUIElement("Opacity").GetComponent<Slider>();
sizeSlider = InitializeUIElement("Size").GetComponent<Slider>();
hardnessSlider = InitializeUIElement("Hardness").GetComponent<Slider>();


mouseRepresentation = GameObject.CreatePrimitive(PrimitiveType.Sphere);
mouseRepresentation.GetComponent<SphereCollider>().enabled = false;
mouseMaterial = new Material(brushShader);

mouseRepresentation.GetComponent<Renderer>().material = mouseMaterial;

mouseSoftnessMaterial = new Material(brushShader);
mouseHardnessRepresentation = GameObject.CreatePrimitive(PrimitiveType.Sphere);
mouseHardnessRepresentation.GetComponent<SphereCollider>().enabled = false;
mouseHardnessRepresentation.GetComponent<Renderer>().material = mouseSoftnessMaterial;

}

// Update is called once per frame
Expand All @@ -71,7 +99,23 @@ void Update () {
Graphics.Blit(Texture2D.whiteTexture, huePickerImage, mColorPicker, 1);

Shader.SetGlobalColor("_BrushColor", foreground.color);

Shader.SetGlobalFloat("_BrushOpacity", opacitySlider.value);
brushSize = sizeSlider.value * .6f;
Shader.SetGlobalFloat("_BrushSize", brushSize);
brushHardness = hardnessSlider.value;
Shader.SetGlobalFloat("_BrushHardness", brushHardness);

if (TexturePaint.mouseWorldPosition.x == Mathf.Infinity) mouseRepresentation.transform.position = new Vector3(1000f, 1000f, 1000f);
else mouseRepresentation.transform.position = TexturePaint.mouseWorldPosition;
mouseRepresentation.transform.localScale = new Vector3(brushSize*2.0f, brushSize * 2.0f, brushSize * 2.0f);

mouseHardnessRepresentation.transform.position = mouseRepresentation.transform.position;
mouseHardnessRepresentation.transform.localScale = mouseRepresentation.transform.localScale * brushHardness;


mouseMaterial.SetColor("_Color", foreground.color);
mouseSoftnessMaterial.SetColor("_Color", new Color(1f-foreground.color.r, 1f - foreground.color.g, 1f - foreground.color.b));
}

private GameObject InitializeUIElement(string name)
Expand Down Expand Up @@ -124,6 +168,7 @@ public void OnPointerClick(PointerEventData eventData)

public void OnDrag(PointerEventData eventData)
{
if (eventData.pointerCurrentRaycast.gameObject == null) return;
string objectName = eventData.pointerCurrentRaycast.gameObject.name;

switch (objectName)
Expand Down Expand Up @@ -154,6 +199,23 @@ private void buttonCallBack(Button buttonPressed)
}
}

private void sliderCallBack(float value, Slider s)
{
string nameOfSlider = s.gameObject.name;
switch (nameOfSlider)
{
case "Opacity":

break;
case "Size":

break;
case "Hardness":

break;
}
}



}
4 changes: 3 additions & 1 deletion Assets/Scripts/TexturePaint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class TexturePaint : MonoBehaviour {
public RenderTexture runTimeTexture; // the actual shader the stuff are going to be drawn to
public Mesh meshToDraw;

public static Vector3 mouseWorldPosition;

// --------------------------------
private Material m; // material which the UV shader is binded to
private CommandBuffer cb; // commandbuffer used to run the UV shader on camera event
Expand Down Expand Up @@ -84,7 +86,7 @@ private void Update()

mwp.w = Input.GetMouseButton(0)? 1 : 0;


mouseWorldPosition = mwp;
Shader.SetGlobalVector("_Mouse", mwp);
m.SetMatrix("mesh_Object2World", meshGameobject.transform.localToWorldMatrix);

Expand Down
9 changes: 7 additions & 2 deletions Assets/Scripts/TexturePaint.shader
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
float4x4 mesh_Object2World;
sampler2D _MainTex;
float4 _BrushColor;
float _BrushOpacity;
float _BrushHardness;
float _BrushSize;

// =====================================================================================================================
// VERTEX FRAGMENT ----------------------------------
Expand All @@ -64,10 +67,12 @@
fixed4 frag (v2f i) : SV_Target
{
float4 col = tex2D(_MainTex, i.uv);
float size = _BrushSize;
float soft = _BrushHardness;
float f = distance(_Mouse.xyz, i.worldPos);
f = 1.-smoothstep(0., 0.02, f);
f = 1.-smoothstep(size*soft, size, f);

col = lerp(col, _BrushColor, f * _Mouse.w);
col = lerp(col, _BrushColor, f * _Mouse.w * _BrushOpacity);
col = saturate(col);

return col;
Expand Down

0 comments on commit 3646bfd

Please sign in to comment.