upload cursor texture in native plugin.
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,4 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace uDesktopDuplication
|
||||
{
|
||||
@@ -16,18 +14,6 @@ public class Cursor : MonoBehaviour
|
||||
private Texture uddTexture_;
|
||||
private Monitor monitor { get { return uddTexture_.monitor; } }
|
||||
|
||||
struct TextureInfo
|
||||
{
|
||||
public Texture2D texture;
|
||||
public System.IntPtr ptr;
|
||||
public TextureInfo(Texture2D texture)
|
||||
{
|
||||
this.texture = texture;
|
||||
this.ptr = texture.GetNativeTexturePtr();
|
||||
}
|
||||
}
|
||||
private Dictionary<int, TextureInfo> textures_ = new Dictionary<int, TextureInfo>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
uddTexture_ = GetComponent<Texture>();
|
||||
@@ -37,9 +23,8 @@ public class Cursor : MonoBehaviour
|
||||
{
|
||||
if (monitor.isCursorVisible) {
|
||||
UpdatePosition();
|
||||
UpdateTexture();
|
||||
}
|
||||
UpdateMaterial();
|
||||
UpdateCoords();
|
||||
}
|
||||
|
||||
void UpdatePosition()
|
||||
@@ -51,33 +36,10 @@ public class Cursor : MonoBehaviour
|
||||
worldPosition = transform.TransformPoint(localPos);
|
||||
}
|
||||
|
||||
void UpdateTexture()
|
||||
{
|
||||
var w = monitor.cursorShapeWidth;
|
||||
var h = monitor.cursorShapeHeight;
|
||||
if (w == 0 || h == 0) return;
|
||||
|
||||
var key = w + h * 100;
|
||||
if (!textures_.ContainsKey(key)) {
|
||||
var texture = new Texture2D(w, h, TextureFormat.BGRA32, false);
|
||||
texture.wrapMode = TextureWrapMode.Clamp;
|
||||
textures_.Add(key, new TextureInfo(texture));
|
||||
}
|
||||
|
||||
var cursorTexture = textures_[key];
|
||||
Assert.IsNotNull(cursorTexture.texture);
|
||||
monitor.GetCursorTexture(cursorTexture.ptr);
|
||||
uddTexture_.material.SetTexture("_CursorTex", cursorTexture.texture);
|
||||
}
|
||||
|
||||
void UpdateMaterial()
|
||||
void UpdateCoords()
|
||||
{
|
||||
var x = monitor.isCursorVisible ? (float)monitor.cursorX / monitor.width : -9999f;
|
||||
var y = monitor.isCursorVisible ? (float)monitor.cursorY / monitor.height : -9999f;
|
||||
var w = (float)monitor.cursorShapeWidth / monitor.width;
|
||||
var h = (float)monitor.cursorShapeHeight / monitor.height;
|
||||
uddTexture_.material.SetVector("_CursorPositionScale", new Vector4(x, y, w, h));
|
||||
|
||||
coord = new Vector2(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,7 +253,7 @@ public class Monitor
|
||||
|
||||
public void Render()
|
||||
{
|
||||
if (texture_ && available) {
|
||||
if (texture_ && available && texturePtr_ != System.IntPtr.Zero) {
|
||||
Lib.SetTexturePtr(id, texturePtr_);
|
||||
GL.IssuePluginEvent(Lib.GetRenderEventFunc(), id);
|
||||
}
|
||||
|
||||
@@ -64,32 +64,14 @@ inline void uddConvertToLinearIfNeeded(inout fixed3 rgb)
|
||||
}
|
||||
|
||||
inline fixed4 uddGetScreenTexture(float2 uv)
|
||||
{
|
||||
fixed4 c = tex2D(_MainTex, uv);
|
||||
return c;
|
||||
}
|
||||
|
||||
inline fixed4 uddGetCursorTexture(float2 uv)
|
||||
{
|
||||
uv.x = (uv.x - _CursorX) / _CursorWidth;
|
||||
uv.y = (uv.y - _CursorY) / _CursorHeight;
|
||||
fixed4 c = tex2D(_CursorTex, uv);
|
||||
fixed a = step(0, uv.x) * step(0, uv.y) * step(uv.x, 1) * step(uv.y, 1);
|
||||
c.a *= step(0.01, a);
|
||||
return c;
|
||||
}
|
||||
|
||||
inline fixed4 uddGetScreenTextureWithCursor(float2 uv)
|
||||
{
|
||||
uv = uddInvertUV(uv);
|
||||
#ifdef USE_CLIP
|
||||
uv = uddClipUV(uv);
|
||||
#endif
|
||||
fixed4 screen = uddGetScreenTexture(uddRotateUV(uv));
|
||||
fixed4 cursor = uddGetCursorTexture(uv);
|
||||
fixed4 color = lerp(screen, cursor, cursor.a);
|
||||
uddConvertToLinearIfNeeded(color.rgb);
|
||||
return color;
|
||||
fixed4 c = tex2D(_MainTex, uddRotateUV(uv));
|
||||
uddConvertToLinearIfNeeded(c.rgb);
|
||||
return c;
|
||||
}
|
||||
|
||||
inline void uddBendVertex(inout float4 v, half radius, half width, half thickness)
|
||||
|
||||
@@ -34,7 +34,7 @@ SubShader
|
||||
|
||||
void surf(Input IN, inout SurfaceOutputStandard o)
|
||||
{
|
||||
fixed4 c = uddGetScreenTextureWithCursor(IN.uv_MainTex) * _Color;
|
||||
fixed4 c = uddGetScreenTexture(IN.uv_MainTex) * _Color;
|
||||
o.Albedo = c.rgb;
|
||||
o.Metallic = _Metallic;
|
||||
o.Smoothness = _Glossiness;
|
||||
|
||||
@@ -39,7 +39,7 @@ v2f vert(appdata v)
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
return uddGetScreenTextureWithCursor(i.uv);
|
||||
return uddGetScreenTexture(i.uv);
|
||||
}
|
||||
|
||||
ENDCG
|
||||
|
||||
@@ -43,7 +43,7 @@ v2f vert(appdata v)
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
fixed4 tex = uddGetScreenTextureWithCursor(i.uv);
|
||||
fixed4 tex = uddGetScreenTexture(i.uv);
|
||||
fixed alpha = pow((tex.r + tex.g + tex.b) / 3.0, _Mask);
|
||||
return fixed4(tex.rgb * _Color.rgb, alpha * _Color.a);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ v2f vert(appdata v)
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
return fixed4(uddGetScreenTextureWithCursor(i.uv).rgb, 1.0) * _Color;
|
||||
return fixed4(uddGetScreenTexture(i.uv).rgb, 1.0) * _Color;
|
||||
}
|
||||
|
||||
ENDCG
|
||||
|
||||
Reference in New Issue
Block a user