Hi, all!
I am interested in converting mouse input to the UV texture space, something like this:
My initial strategy was to:
- Obtain the Vector3 mouse point from screen space to world space, after casting a ray cast onto the object where we want to manipulate
- Set the value of the Vector3 mouse point (in world space) and send it to the Material’s shader property, titled “MousePoint”
- Inside the shader, convert the mouse point from World Space to Clip Space. Next, convert the mouse point from clip space to screen space.
Here’s my code:
Shader "Unlit/MouePosShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_mousePoint("mousePoint", Vector) = (0,0,0)
_dist("dist", float) = 0.02
_centerPt("CenterPoint", float) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal: NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float2 mousePos: TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _mousePoint;
float _dist;
float _centerPt;
float2 _mousePointModified;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float4 mouseVertex = UnityWorldToClipPos(_mousePoint);
o.mousePos = ComputeScreenPos(mouseVertex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
// sample the texture
float d = length(i.uv + i.mousePos - float2(0.5,0.5));
float p = 1. - smoothstep(_dist - (_dist * 0.01),
_dist + (_dist * 0.01),
dot(d, d) * 4.0);
fixed4 col = fixed4(p, p, p, 0);
return col;
}
ENDCG
}
}
}
The problem I had bumped into was that the mouse point either is not moving, or the proper matrix conversion from mouse point world space to mouse point UV space isn’t taking place. I wanted to get some outside feedback on how to make this matrix transformation work out.
Thanks!