Guest User

Untitled

a guest
Dec 14th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. Shader "Unlit/SimpleInkPaint"
  2. {
  3. Properties{
  4. _MainTex("MainTex", 2D) = "white" // メインテクスチャ
  5. _Brush("Brush", 2D) = "white" // ブラシテクスチャ
  6. _BrushScale("BrushScale", FLOAT) = 0.1 // ブラシサイズ
  7. _ControlColor("ControlColor", VECTOR) = (0, 0, 0, 0) // ブラシの色
  8. _PaintUV("Hit UV Position", VECTOR) = (0, 0, 0, 0) // ブラシで塗りたい位置
  9. }
  10.  
  11. SubShader{
  12. CGINCLUDE
  13. struct app_data {
  14. float4 vertex:POSITION;
  15. float4 uv:TEXCOORD0;
  16. };
  17.  
  18. struct v2f {
  19. float4 screen:SV_POSITION;
  20. float4 uv:TEXCOORD0;
  21. };
  22.  
  23. sampler2D _MainTex;
  24. sampler2D _Brush;
  25. float4 _PaintUV;
  26. float _BrushScale;
  27. float4 _ControlColor;
  28. ENDCG
  29.  
  30. Pass{
  31. CGPROGRAM
  32. #pragma vertex vert
  33. #pragma fragment frag
  34. #include "UnityCG.cginc"
  35. bool IsPaintRange(float2 mainUV, float2 paintUV, float brushScale) {
  36. return
  37. paintUV.x - brushScale < mainUV.x &&
  38. mainUV.x < paintUV.x + brushScale &&
  39. paintUV.y - brushScale < mainUV.y &&
  40. mainUV.y < paintUV.y + brushScale;
  41. }
  42.  
  43. v2f vert(app_data i) {
  44. v2f o;
  45. o.screen = UnityObjectToClipPos(i.vertex);
  46. o.uv = i.uv;
  47. return o;
  48. }
  49.  
  50. float4 frag(v2f i) : SV_TARGET{
  51. float h = _BrushScale;
  52. float4 mainColor = tex2D(_MainTex, i.uv.xy);
  53. float4 brushColor = float4(1, 1, 1, 1);
  54.  
  55. // ブラシ範囲に入っているかチェック
  56. if (IsPaintRange(i.uv, _PaintUV, h)) {
  57. // 入力されたuv値をtextureに合わせて変換
  58. float2 uv = (i.uv - _PaintUV) / h * 0.5 + 0.5;
  59. brushColor = tex2D(_Brush, uv.xy);
  60. // ブラシの形に沿って、ブラシ色にする
  61. return mainColor * (1 - brushColor.a * _ControlColor.a) + _ControlColor * _ControlColor.a * brushColor.a;
  62. }
  63. return mainColor;
  64. }
  65. ENDCG
  66. }
  67. }
  68. }
Add Comment
Please, Sign In to add comment