Last active
August 29, 2015 14:02
-
-
Save sugi-cho/98bcb0eea51b451b50da to your computer and use it in GitHub Desktop.
Unityでお絵かき http://sugi.cc/post/87411110741/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[RequireComponent(typeof(Camera))] | |
public class Draw : MonoBehaviour | |
{ | |
public Color brushColor; | |
public float | |
brushSize = 10f, | |
brushIntencity = 1f; | |
RenderTexture canvas; | |
Material | |
init, | |
draw; | |
Vector2 prePos; | |
// Use this for initialization | |
void Start () | |
{ | |
init = new Material (Shader.Find ("Custom/WhitePaper")); | |
draw = new Material (Shader.Find ("Custom/SimpleBrush")); | |
canvas = new RenderTexture (Screen.width, Screen.height, 0); | |
canvas.Create (); | |
Graphics.Blit (null, canvas, init); | |
//キャンバスをinitマテリアルで初期化する。 | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
if (Input.GetMouseButtonDown (0)) | |
DrawToCanvas (Input.mousePosition, true); | |
else if (Input.GetMouseButton (0)) | |
DrawToCanvas (Input.mousePosition); | |
} | |
void DrawToCanvas (Vector2 pos, bool first = false) | |
{ | |
if (first) | |
prePos = pos; | |
//マウスダウンの時は、前回の位置=現在の位置にする。そうしないと、前描いた線から繋がっちゃう | |
float | |
dist = Vector2.Distance (pos, prePos), | |
size = brushSize * Mathf.Max (1 - dist / 100f, 0.5f), | |
intencity = brushIntencity * Mathf.Clamp01 (dist / 30f); | |
//ちょっと、ブラシのサイズと濃さが描くスピードで変わるよう調整。 | |
//前回のフレームから位置が大きくはなれていたら、sizeの4分の1の距離ずつ、前回の位置から現在の位置まで、ブラシで描く | |
for (float f = 0; f < dist; f += size*0.25f) { | |
Vector2 p = Vector2.Lerp (pos, prePos, f / dist); | |
draw.SetTexture ("_MainTex", canvas); | |
draw.SetColor ("_Color", brushColor); | |
draw.SetVector ("_Draw", new Vector4 (p.x / Screen.width, p.y / Screen.height, size, intencity)); | |
//_Draw(x,y,z,w)のVectorプロパティに、位置、ブラシのサイズ、濃さを入れます。 | |
RenderTexture rt = RenderTexture.GetTemporary (canvas.width, canvas.height, 0); | |
Graphics.Blit (canvas, rt); | |
Graphics.Blit (rt, canvas, draw); | |
//ここで、ブラシを適用してる | |
RenderTexture.ReleaseTemporary (rt); | |
} | |
prePos = pos; | |
//現在の位置をprePosに入れておく | |
} | |
void OnRenderImage (RenderTexture s, RenderTexture d) | |
{ | |
Graphics.Blit (canvas, d); | |
//カメラ出力にcanvasを表示。 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/SimpleBrush" { | |
Properties { | |
_MainTex ("Base (RGB)", 2D) = "white" {} | |
_Color ("color", Color) = (0,0,0,1) | |
_Draw ("draw prop(pos.xy,size,alpha)", Vector) = (0,0,0,0) | |
} | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
sampler2D _MainTex; | |
half4 _MainTex_TexelSize; | |
half4 _Color,_Draw; | |
//_Draw.xyの位置(0-1座標)に半径_Draw.z(pixel)の丸いブラシを描く | |
fixed4 frag(v2f_img i) : COLOR{ | |
fixed4 canvas = tex2D(_MainTex, i.uv); | |
half2 pix = _MainTex_TexelSize.xy; | |
half d = distance(i.uv/pix, _Draw.xy/pix)/_Draw.z; | |
return lerp(canvas, _Color, pow(saturate(1-d),2)*_Draw.w); | |
} | |
ENDCG | |
SubShader { | |
ZTest Always Cull Off ZWrite Off | |
Fog { Mode off } | |
ColorMask RGB | |
pass{ | |
CGPROGRAM | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#pragma vertex vert_img | |
#pragma fragment frag | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/WhitePaper" { | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
fixed4 frag(v2f_img i) : COLOR{ | |
return 1; | |
} | |
ENDCG | |
SubShader { | |
ZTest Always Cull Off ZWrite Off | |
Fog { Mode off } | |
ColorMask RGB | |
pass{ | |
CGPROGRAM | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#pragma vertex vert_img | |
#pragma fragment frag | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment