From e72eff0ee7ef275f02c7e1e3ce0f29ed408303a4 Mon Sep 17 00:00:00 2001 From: hecomi Date: Thu, 27 Oct 2016 18:17:37 +0900 Subject: [PATCH] add mouse cursor. --- .../Materials/uDD_Cursor.mat | Bin 0 -> 5260 bytes .../Materials/uDD_Cursor.mat.meta | 8 +++ ...uDesktopDuplication.mat => uDD_Screen.mat} | Bin 5064 -> 5100 bytes ...plication.mat.meta => uDD_Screen.mat.meta} | 0 Assets/uDesktopDuplication/Resources.meta | 9 +++ .../Resources/uDesktopDuplication.meta | 9 +++ .../Resources/uDesktopDuplication/Images.meta | 9 +++ .../uDesktopDuplication/Images/cursor.png | Bin 0 -> 967 bytes .../Images/cursor.png.meta | 59 ++++++++++++++++ .../uDesktopDuplication/Scenes/Example.unity | Bin 17096 -> 18860 bytes .../Scripts/MouseDrawer.cs | 44 ++++++++++++ .../Scripts/MouseDrawer.cs.meta | 12 ++++ .../Scripts/uDesktopDuplication.cs | 61 ++++++++++++---- .../Scripts/uDesktopDuplication.cs.meta | 4 +- Assets/uDesktopDuplication/Shaders.meta | 9 +++ .../Shaders/uDD_Unlit.shader | 66 ++++++++++++++++++ .../Shaders/uDD_Unlit.shader.meta | 9 +++ .../uDesktopDuplication/main.cpp | 2 +- 18 files changed, 285 insertions(+), 16 deletions(-) create mode 100644 Assets/uDesktopDuplication/Materials/uDD_Cursor.mat create mode 100644 Assets/uDesktopDuplication/Materials/uDD_Cursor.mat.meta rename Assets/uDesktopDuplication/Materials/{uDesktopDuplication.mat => uDD_Screen.mat} (82%) rename Assets/uDesktopDuplication/Materials/{uDesktopDuplication.mat.meta => uDD_Screen.mat.meta} (100%) create mode 100644 Assets/uDesktopDuplication/Resources.meta create mode 100644 Assets/uDesktopDuplication/Resources/uDesktopDuplication.meta create mode 100644 Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images.meta create mode 100644 Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images/cursor.png create mode 100644 Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images/cursor.png.meta create mode 100644 Assets/uDesktopDuplication/Scripts/MouseDrawer.cs create mode 100644 Assets/uDesktopDuplication/Scripts/MouseDrawer.cs.meta create mode 100644 Assets/uDesktopDuplication/Shaders.meta create mode 100644 Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader create mode 100644 Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader.meta diff --git a/Assets/uDesktopDuplication/Materials/uDD_Cursor.mat b/Assets/uDesktopDuplication/Materials/uDD_Cursor.mat new file mode 100644 index 0000000000000000000000000000000000000000..76143ccd6eec57927bb9a8cfe9bef6b56a67b3d1 GIT binary patch literal 5260 zcmeI0%WvF79LImVc{ELVmA)x$!y5{u6k498+jNsOq1`6UHU)7p?(QUO@UA1 zeL@2DT7g7`R3UKzA;bZJ1UD{7<%CcV<#D6E_KK>8?|1ASc9Nj{13S@Ap80tE_3ts> zj4d*7Rb=x85xHCB9vnmt4j&laKeu0QFD)(QasXRk7q4C4b?f@}){VEOZeHGTX_ zkCXj4vb`XZVE&RGBqBNdecmIzLoEL;up*dFj+yXLwE~p#KGQ=s)j~OL$E_w;%q{$mw(L z2zih8b0@B$pM&JudgK)5z76AegW)-Wh8%`5UK-o-c<aczRJF?Y;o4Sn*>glOb9!QVrl{&0<~=Fe8c?YwOx zM`J$sppMKNb+!{G}U`r}~4aBk#KlJiV{2 zznk-qP|McmI$ceFDC2pY{Q&(-FzP>mW0oJCx34n(py54MTn`!EYsK|2IU4iRnYTv> z6Y@U!5WRNZ9yPpQ=kq&^>Ca<^ue0vU=PZqPz`E}f8Pkuz7`DBitgt0Mi zU!%T1qtCVf6d_tp)@l9r^ZvAEDeqkW!-n_E@6i4gV?Lw%U?M|W=Q85GhEXr<8R8E9 z2S((422-Bu{Dl#9o+a*>-`06<8E@+xFg&m8cmreR^B{Rb|Lr`_tz9zkQ&Ne+i_8N|A#i1+m@z%dD?!NLv@!Qg;7iT_N|7DL6>2|sq z=w_gsfo=x68R%wUjSLLpd~tW7VX|i0NoH~g<|Cw_ejDvn9 z=GNLd+N>{di($0{J4agDxaD$2RqT5Hh<{d9gEi*!wK|($a-(-LmG{WRM8b5zH?Od42NkOjz&Ao<- zB2F#vQ${*>aYoJgsuH6%`c$Uf&Mjx7@sp8L3aUzvMD+D0m*qHAjrq97liBs{ynzf} z{p=9)^L~vF0`!%I9Thn_y0PK~CH14{c*+|V!j`#~Tx9EH64Rvkb)QXsq6u7SSwR#O;0M==`j_v2g L%;CItOA7x2uZeqa literal 0 HcmV?d00001 diff --git a/Assets/uDesktopDuplication/Materials/uDD_Cursor.mat.meta b/Assets/uDesktopDuplication/Materials/uDD_Cursor.mat.meta new file mode 100644 index 0000000..1833d2c --- /dev/null +++ b/Assets/uDesktopDuplication/Materials/uDD_Cursor.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 033e0db89e0ca1d46a399c953629f67d +timeCreated: 1477555367 +licenseType: Pro +NativeFormatImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Materials/uDesktopDuplication.mat b/Assets/uDesktopDuplication/Materials/uDD_Screen.mat similarity index 82% rename from Assets/uDesktopDuplication/Materials/uDesktopDuplication.mat rename to Assets/uDesktopDuplication/Materials/uDD_Screen.mat index 1570aa2e4b478e9bfe69416db5fc862acbb15b34..77806c988f2f3f4cb99fdd4a15a193db896af1a7 100644 GIT binary patch delta 166 zcmX@1{zjdHfq`QQ1B39JjT|i;>Tj4C7(^LB`2T+(#lXM_A`VqqpD5dQ*LQN&a<^qg zD_xm4Kjg4xpZtMC&xi{sUFzZzADmp2nwkeyZ|4cp#UKR4o_=AjK_T%G3h}PKp25MM s{(hUi1XeLJicJ10cw7N!IYYc>URi2UNdyB!gFO;I5@f>WJwiN;0E@>fc>n+a delta 157 zcmaE(enOptfq~;71B391jT|i;?kAWT7(^LB`2T+(#lXM_CZPmB5~nD&IKQ+gIki~7 zG%vHHGCrv^Gp8goFFv)Rq$qLoK@LxLJzk)4zr>QvveXcuXlYR@kPkLb3t|o@kcxNp d^$ZU7^!H=f>?g2_kx^*!Pr>7x9|-X<0sxoqB$xmI diff --git a/Assets/uDesktopDuplication/Materials/uDesktopDuplication.mat.meta b/Assets/uDesktopDuplication/Materials/uDD_Screen.mat.meta similarity index 100% rename from Assets/uDesktopDuplication/Materials/uDesktopDuplication.mat.meta rename to Assets/uDesktopDuplication/Materials/uDD_Screen.mat.meta diff --git a/Assets/uDesktopDuplication/Resources.meta b/Assets/uDesktopDuplication/Resources.meta new file mode 100644 index 0000000..4644b85 --- /dev/null +++ b/Assets/uDesktopDuplication/Resources.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 6e15bc51aada81042a909c808e25c176 +folderAsset: yes +timeCreated: 1477555207 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Resources/uDesktopDuplication.meta b/Assets/uDesktopDuplication/Resources/uDesktopDuplication.meta new file mode 100644 index 0000000..f18d145 --- /dev/null +++ b/Assets/uDesktopDuplication/Resources/uDesktopDuplication.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 48b92326b146fa848beec6f35365b1f4 +folderAsset: yes +timeCreated: 1477555207 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images.meta b/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images.meta new file mode 100644 index 0000000..fc24967 --- /dev/null +++ b/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: a615a0cde495b914d95b9d2d53182305 +folderAsset: yes +timeCreated: 1477555207 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images/cursor.png b/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Images/cursor.png new file mode 100644 index 0000000000000000000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FL2@^^I$;e_Zec$uV{hP;lKAZJK{_=$l+A6P=#IDAa+4A>n}t@q$N+67cv0( zCFpf%$*)E+b#hSnC+rnpQzd_dB~_^znIM@IV<@hc=8!3%T`*x|So6>-M^n)xkfRm8B5ye^Ke@p&&+@7W?(uS2m((-n@3V_keL=VoL4uulGq00xU_ z)B>2<+j0Zrt?|rjNyH6)%dnvQNl+$YJ>4hN1+7_}nMJJ0EXk@#TKnU4g>a=VY{}I~ K&lR>NbMPPVLmdeK delta 468 zcmZ28nejv`qaOo<)m{b$rxOee4E#X$10c;{s%N5Sm}bcE@Bjb*j6hKaApQUBHVF}p z)(weI<@Y)@=Pp?`V~(QnpDWVK4gE8Z%QN|FNdwh+Z}f|2(z?gM&|nGVu>i3o5HoNv zFfalkNZbi3E(I2!yq`&1u{a<Lx1xSYk z8v{cCkOq0M2qXXmsZhBLw#oZ|ru<-=e9KnK(Se`X>Jg8g`rdcKz+ciz*wqMG~GlPn{8ZYFiyVXz%%)Qo5N%S z_l`-!_&Udw#$C9 LqP+|Y6Hqk(Z5Ddf diff --git a/Assets/uDesktopDuplication/Scripts/MouseDrawer.cs b/Assets/uDesktopDuplication/Scripts/MouseDrawer.cs new file mode 100644 index 0000000..259d559 --- /dev/null +++ b/Assets/uDesktopDuplication/Scripts/MouseDrawer.cs @@ -0,0 +1,44 @@ +using UnityEngine; + +namespace uDesktopDuplication +{ + +[RequireComponent(typeof(uDesktopDuplication))] +public class MouseDrawer : MonoBehaviour +{ + [SerializeField] + Transform cursor; + [SerializeField] + Vector2 modelScale = Vector2.one; + [SerializeField] + Vector2 offset = new Vector2(0.5f, 0.5f); + + uDesktopDuplication udd_; + + void OnEnable() + { + udd_ = GetComponent(); + udd_.onMouseMove += OnMouseMove; + } + + void OnDisable() + { + udd_.onMouseMove -= OnMouseMove; + } + + void OnMouseMove(Vector2 pos) + { + var x = pos.x * modelScale.x * 0.5f; + var y = pos.y * modelScale.y * 0.5f; + var iy = udd_.invertY ? +1 : -1; + var localPos = transform.right * x + iy * transform.up * y; + + var worldPos = transform.TransformPoint(localPos); + worldPos += cursor.right * offset.x * cursor.localScale.x; + worldPos += -cursor.up * offset.y * cursor.localScale.y; + + cursor.position = worldPos; + } +} + +} \ No newline at end of file diff --git a/Assets/uDesktopDuplication/Scripts/MouseDrawer.cs.meta b/Assets/uDesktopDuplication/Scripts/MouseDrawer.cs.meta new file mode 100644 index 0000000..081062b --- /dev/null +++ b/Assets/uDesktopDuplication/Scripts/MouseDrawer.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: d2d9e4ca459ecb44da8815bafe9655c5 +timeCreated: 1477559806 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {fileID: 2800000, guid: 89616e59e1cccb346bd4e959257990ca, type: 3} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs b/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs index 9479d8d..e3e8f3d 100644 --- a/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs +++ b/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs @@ -3,6 +3,9 @@ using System; using System.Collections; using System.Runtime.InteropServices; +namespace uDesktopDuplication +{ + public class uDesktopDuplication : MonoBehaviour { [DllImport("uDesktopDuplication")] @@ -22,34 +25,64 @@ public class uDesktopDuplication : MonoBehaviour [DllImport("uDesktopDuplication")] private static extern IntPtr GetRenderEventFunc(); - public bool isPointerVisible = false; - public int pointerX = 0; - public int pointerY = 0; + private Material material_; + public bool invertX = false; + public bool invertY = false; - Coroutine renderCoroutine_ = null; + public delegate void MouseMoveEventHandler(Vector2 pos); + public MouseMoveEventHandler onMouseMove { get; set; } + + private Coroutine renderCoroutine_ = null; void OnEnable() { var tex = new Texture2D(GetWidth(), GetHeight(), TextureFormat.BGRA32, false); - GetComponent().sharedMaterial.mainTexture = tex; + material_ = GetComponent().material; + material_.mainTexture = tex; SetTexturePtr(tex.GetNativeTexturePtr()); renderCoroutine_ = StartCoroutine(OnRender()); } void OnDisable() - { - if (renderCoroutine_ != null) { - StopCoroutine(renderCoroutine_); - renderCoroutine_ = null; - } + { + if (renderCoroutine_ != null) { + StopCoroutine(renderCoroutine_); + renderCoroutine_ = null; + } } void Update() { - isPointerVisible = IsPointerVisible(); - pointerX = GetPointerX(); - pointerY = GetPointerY(); + UpdateMouseEvent(); + UpdateMaterial(); + } + + void UpdateMouseEvent() + { + var isVisible = IsPointerVisible(); + if (isVisible && onMouseMove != null) { + var x = GetPointerX(); + var y = GetPointerY(); + var w = GetWidth(); + var h = GetHeight(); + onMouseMove(new Vector2(2f * x / w - 1f, 1f - 2f * y / h)); + } + } + + void UpdateMaterial() + { + if (invertX) { + material_.EnableKeyword("INVERT_X"); + } else { + material_.DisableKeyword("INVERT_X"); + } + + if (invertY) { + material_.EnableKeyword("INVERT_Y"); + } else { + material_.DisableKeyword("INVERT_Y"); + } } IEnumerator OnRender() @@ -59,4 +92,6 @@ public class uDesktopDuplication : MonoBehaviour GL.IssuePluginEvent(GetRenderEventFunc(), 0); } } +} + } \ No newline at end of file diff --git a/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs.meta b/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs.meta index 4b7ab66..70283ee 100644 --- a/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs.meta +++ b/Assets/uDesktopDuplication/Scripts/uDesktopDuplication.cs.meta @@ -1,12 +1,12 @@ fileFormatVersion: 2 guid: bb54a34570e4747429b1c5b66c69d356 -timeCreated: 1452445649 +timeCreated: 1477559813 licenseType: Pro MonoImporter: serializedVersion: 2 defaultReferences: [] executionOrder: 0 - icon: {instanceID: 0} + icon: {fileID: 2800000, guid: 89616e59e1cccb346bd4e959257990ca, type: 3} userData: assetBundleName: assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Shaders.meta b/Assets/uDesktopDuplication/Shaders.meta new file mode 100644 index 0000000..ffa7474 --- /dev/null +++ b/Assets/uDesktopDuplication/Shaders.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 9844af21eb203794b852f0bcaa5f2be2 +folderAsset: yes +timeCreated: 1477556975 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader b/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader new file mode 100644 index 0000000..c54965d --- /dev/null +++ b/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader @@ -0,0 +1,66 @@ +Shader "uDesktopDuplication/Unlit" +{ + +Properties +{ + _MainTex ("Texture", 2D) = "white" {} +} + +SubShader +{ + +Tags { "RenderType"="Opaque" } + +CGINCLUDE + +#include "UnityCG.cginc" + +struct appdata +{ + float4 vertex : POSITION; + float2 uv : TEXCOORD0; +}; + +struct v2f +{ + float4 vertex : SV_POSITION; + float2 uv : TEXCOORD0; +}; + +sampler2D _MainTex; +float4 _MainTex_ST; + +v2f vert (appdata v) +{ + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.uv = TRANSFORM_TEX(v.uv, _MainTex); + return o; +} + +fixed4 frag (v2f i) : SV_Target +{ +#ifdef INVERT_X + i.uv.x *= -1.0; +#endif +#ifdef INVERT_Y + i.uv.y *= -1.0; +#endif + return tex2D(_MainTex, i.uv); +} + +ENDCG + +Pass +{ + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #pragma multi_compile ___ INVERT_X + #pragma multi_compile ___ INVERT_Y + ENDCG +} + +} + +} diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader.meta b/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader.meta new file mode 100644 index 0000000..f907452 --- /dev/null +++ b/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 2ca7b38c676bddd439a77a646a279a54 +timeCreated: 1477556963 +licenseType: Pro +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp index 166829a..9a3c0c7 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp @@ -34,7 +34,7 @@ extern "C" { // Search the main monitor from all outputs. IDXGIOutput* output; - for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); j++) + for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j) { DXGI_OUTPUT_DESC outputDesc; output->GetDesc(&outputDesc);