-
Notifications
You must be signed in to change notification settings - Fork 14
/
RawImageWithMouse.cs
67 lines (57 loc) · 1.52 KB
/
RawImageWithMouse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RawImageWithMouse : MonoBehaviour
{
public Material Material;
public RawImage Image;
Vector3[] _Corners = new Vector3[4];
Vector4 _Mouse = Vector4.zero;
Vector4 _Resolution = Vector4.zero;
Rect _Rect = Rect.zero;
const string iMouse = "iMouse";
const string iResolution = "iResolution";
void Update()
{
Image.rectTransform.GetWorldCorners(_Corners);
_Rect.position = _Corners[0];
_Rect.size = _Corners[2] - _Corners[0];
_Mouse.x = Input.mousePosition.x - _Rect.x;
_Mouse.y = Input.mousePosition.y - _Rect.y;
Material.SetVector(iMouse, _Mouse);
_Resolution.x = Image.rectTransform.rect.width;
_Resolution.y = Image.rectTransform.rect.height;
Material.SetVector(iResolution, _Resolution);
}
}
/* Script can be tested with following shader:
Shader "WhiteCircle"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex VSMain
#pragma fragment PSMain
float4 iMouse;
float4 iResolution;
float Sphere (float2 p, float2 c, float r)
{
return step(length(p-c)-r, 0.0);
}
void VSMain (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0)
{
vertex = UnityObjectToClipPos(vertex);
}
void PSMain (float4 vertex:POSITION, float2 uv:TEXCOORD0, out float4 fragColor:SV_TARGET)
{
float s = Sphere(uv, iMouse.xy/iResolution.xy, 0.1);
fragColor = float4(s.xxx, 1.0);
}
ENDCG
}
}
}
*/