diff --git a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders.meta b/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders.meta deleted file mode 100644 index a69dc50..0000000 --- a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders.meta +++ /dev/null @@ -1,9 +0,0 @@ -fileFormatVersion: 2 -guid: 4fad0d0b5018b8649ac41af2fef8dbf2 -folderAsset: yes -timeCreated: 1478011152 -licenseType: Pro -DefaultImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders/uDD_Cursor.shader b/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders/uDD_Cursor.shader deleted file mode 100644 index 5bab139..0000000 --- a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders/uDD_Cursor.shader +++ /dev/null @@ -1,81 +0,0 @@ -Shader "uDesktopDuplication/Cursor" -{ - -Properties -{ - _MainTex ("Texture", 2D) = "white" {} - [KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2 -} - -SubShader -{ - -Tags { "RenderType"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "Queue" = "Transparent+1" } - -Cull [_Cull] -ZWrite Off -Offset -0.01, -0.01 - -CGINCLUDE - -#include "UnityCG.cginc" -#include "../../../Shaders/uDD_Common.cginc" - -half4 _PositionScale; -#define _PointerX _PositionScale.x -#define _PointerY _PositionScale.y -#define _PointerWidth _PositionScale.z -#define _PointerHeight _PositionScale.w - -struct appdata -{ - float4 vertex : POSITION; - float2 uv : TEXCOORD0; -}; - -struct v2f -{ - float4 vertex : SV_POSITION; - float2 uv : TEXCOORD0; -}; - -sampler2D _MainTex; - -v2f vert(appdata v) -{ - v2f o; - o.vertex = UnityObjectToClipPos(v.vertex); - o.uv = v.uv; - return o; -} - -fixed4 frag(v2f i) : SV_Target -{ - uddInvertUV(i.uv); - i.uv.x = (i.uv.x - _PointerX) / _PointerWidth; - i.uv.y = (i.uv.y - _PointerY) / _PointerHeight; - fixed4 color = tex2D(_MainTex, i.uv); - color.a *= step(0, i.uv.x) * step(0, i.uv.y) * step(i.uv.x, 1) * step(i.uv.y, 1); - clip(color.a - 0.01); - uddToLinearIfNeeded(color.rgb); - return color; -} - -ENDCG - -Pass -{ - CGPROGRAM - #pragma vertex vert - #pragma fragment frag - #pragma multi_compile ___ INVERT_X - #pragma multi_compile ___ INVERT_Y - #pragma multi_compile ___ VERTICAL - ENDCG -} - -} - -Fallback "Unlit/Texture" - -} diff --git a/Assets/uDesktopDuplication/Scripts/Cursor.cs b/Assets/uDesktopDuplication/Scripts/Cursor.cs index b7d1dc7..820c32f 100644 --- a/Assets/uDesktopDuplication/Scripts/Cursor.cs +++ b/Assets/uDesktopDuplication/Scripts/Cursor.cs @@ -15,34 +15,12 @@ public class Cursor : MonoBehaviour private Texture uddTexture_; private Monitor monitor { get { return uddTexture_.monitor; } } - private Texture2D pointerTexture_; - private Material cursorMaterial_; - - private Dictionary pointerTextures_ = new Dictionary(); - - void OnEnable() - { - uddTexture_ = GetComponent(); - } + private Texture2D currentTexture_; + private Dictionary textures_ = new Dictionary(); void Start() { - var clone = new GameObject(name + " Cursor"); - clone.transform.SetParent(transform); - clone.transform.localPosition = Vector3.zero; - clone.transform.localRotation = Quaternion.identity; - clone.transform.localScale = Vector3.one; - - var filter = clone.AddComponent(); - filter.mesh = GetComponent().sharedMesh; - - var renderer = clone.AddComponent(); - var shader = Shader.Find("uDesktopDuplication/Cursor"); - cursorMaterial_ = new Material(shader); - renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; - renderer.receiveShadows = false; - renderer.motionVectors = false; - renderer.material = cursorMaterial_; + uddTexture_ = GetComponent(); } void Update() @@ -70,41 +48,24 @@ public class Cursor : MonoBehaviour if (w == 0 || h == 0) return; var scale = new Vector2(w, h); - if (!pointerTextures_.ContainsKey(scale)) { + if (!textures_.ContainsKey(scale)) { var texture = new Texture2D(w, h, TextureFormat.BGRA32, false); texture.wrapMode = TextureWrapMode.Clamp; - pointerTextures_.Add(scale, texture); + textures_.Add(scale, texture); } - var pointerTexture = pointerTextures_[scale]; + + var pointerTexture = textures_[scale]; monitor.UpdateCursorTexture(pointerTexture.GetNativeTexturePtr()); - cursorMaterial_.SetTexture("_MainTex", pointerTexture); + uddTexture_.material.SetTexture("_CursorTex", pointerTexture); } void UpdateMaterial() { - if (uddTexture_.invertX) { - cursorMaterial_.EnableKeyword("INVERT_X"); - } else { - cursorMaterial_.DisableKeyword("INVERT_X"); - } - - if (uddTexture_.invertY) { - cursorMaterial_.EnableKeyword("INVERT_Y"); - } else { - cursorMaterial_.DisableKeyword("INVERT_Y"); - } - - if (monitor.isVertical) { - cursorMaterial_.EnableKeyword("VERTICAL"); - } else { - cursorMaterial_.DisableKeyword("VERTICAL"); - } - var x = (float)monitor.pointerX / monitor.width; var y = (float)monitor.pointerY / monitor.height; var w = (float)monitor.pointerShapeWidth / monitor.width; var h = (float)monitor.pointerShapeHeight / monitor.height; - cursorMaterial_.SetVector("_PositionScale", new Vector4(x, y, w, h)); + uddTexture_.material.SetVector("_CursorPositionScale", new Vector4(x, y, w, h)); } } diff --git a/Assets/uDesktopDuplication/Scripts/Texture.cs b/Assets/uDesktopDuplication/Scripts/Texture.cs index b3a5379..d914d13 100644 --- a/Assets/uDesktopDuplication/Scripts/Texture.cs +++ b/Assets/uDesktopDuplication/Scripts/Texture.cs @@ -13,8 +13,8 @@ public class Texture : MonoBehaviour set { monitor_ = value; - material_ = GetComponent().material; - material_.mainTexture = monitor_.texture; + material = GetComponent().material; + material.mainTexture = monitor_.texture; } } @@ -27,7 +27,11 @@ public class Texture : MonoBehaviour public bool invertX = false; public bool invertY = false; - private Material material_; + public Material material + { + get; + private set; + } void OnEnable() { @@ -45,21 +49,21 @@ public class Texture : MonoBehaviour void UpdateMaterial() { if (invertX) { - material_.EnableKeyword("INVERT_X"); + material.EnableKeyword("INVERT_X"); } else { - material_.DisableKeyword("INVERT_X"); + material.DisableKeyword("INVERT_X"); } if (invertY) { - material_.EnableKeyword("INVERT_Y"); + material.EnableKeyword("INVERT_Y"); } else { - material_.DisableKeyword("INVERT_Y"); + material.DisableKeyword("INVERT_Y"); } if (monitor.isVertical) { - material_.EnableKeyword("VERTICAL"); + material.EnableKeyword("VERTICAL"); } else { - material_.DisableKeyword("VERTICAL"); + material.DisableKeyword("VERTICAL"); } } } diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Common.cginc b/Assets/uDesktopDuplication/Shaders/uDD_Common.cginc index c91470e..4fe696d 100644 --- a/Assets/uDesktopDuplication/Shaders/uDD_Common.cginc +++ b/Assets/uDesktopDuplication/Shaders/uDD_Common.cginc @@ -3,6 +3,23 @@ #include "UnityCG.cginc" +struct appdata +{ + float4 vertex : POSITION; + float2 uv : TEXCOORD0; +}; + +struct v2f +{ + float4 vertex : SV_POSITION; + float2 uv : TEXCOORD0; +}; + +struct Input +{ + float2 uv_MainTex; +}; + inline void uddInvertUV(inout float2 uv) { #ifdef INVERT_X @@ -25,12 +42,30 @@ inline void uddToLinearIfNeeded(inout fixed3 rgb) } } -inline fixed4 uddGetTexture(sampler2D tex, float2 uv) +inline fixed4 uddGetScreenTexture(sampler2D tex, float2 uv) { - uddInvertUV(uv); fixed4 c = tex2D(tex, uv); - uddToLinearIfNeeded(c.rgb); return c; } +inline fixed4 uddGetCursorTexture(sampler2D tex, float2 uv, fixed4 cursorPosScale) +{ + uv.x = (uv.x - cursorPosScale.x) / cursorPosScale.z; + uv.y = (uv.y - cursorPosScale.y) / cursorPosScale.w; + fixed4 c = tex2D(tex, uv); + fixed a = c.a * step(0, uv.x) * step(0, uv.y) * step(uv.x, 1) * step(uv.y, 1); + c *= step(0.01, a); + return c; +} + +inline fixed4 uddGetScreenTextureWithCursor(sampler2D screenTex, sampler2D cursorTex, float2 uv, fixed4 cursorPosScale) +{ + uddInvertUV(uv); + fixed4 screen = uddGetScreenTexture(screenTex, uv); + fixed4 cursor = uddGetCursorTexture(cursorTex, uv, cursorPosScale); + fixed4 color = lerp(screen, cursor, cursor.a); + uddToLinearIfNeeded(color.rgb); + return color; +} + #endif \ No newline at end of file diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Params.cginc b/Assets/uDesktopDuplication/Shaders/uDD_Params.cginc new file mode 100644 index 0000000..8da5e26 --- /dev/null +++ b/Assets/uDesktopDuplication/Shaders/uDD_Params.cginc @@ -0,0 +1,13 @@ +#ifndef UDD_PARAMS_CGINC +#define UDD_PARAMS_CGINC + +sampler2D _MainTex; +fixed4 _Color; +sampler2D _CursorTex; +half4 _CursorPositionScale; + +#ifndef SURFACE_SHADER +float4 _MainTex_ST; +#endif + +#endif \ No newline at end of file diff --git a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders/uDD_Cursor.shader.meta b/Assets/uDesktopDuplication/Shaders/uDD_Params.cginc.meta similarity index 67% rename from Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders/uDD_Cursor.shader.meta rename to Assets/uDesktopDuplication/Shaders/uDD_Params.cginc.meta index 95a4abc..acfba57 100644 --- a/Assets/uDesktopDuplication/Resources/uDesktopDuplication/Shaders/uDD_Cursor.shader.meta +++ b/Assets/uDesktopDuplication/Shaders/uDD_Params.cginc.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: d224cadaa1bfa2d46a6224ce2a1443c7 -timeCreated: 1477847636 +guid: 54044418ab4785c4a84dd4be507db6c2 +timeCreated: 1478266678 licenseType: Pro ShaderImporter: defaultTextures: [] diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Standard.shader b/Assets/uDesktopDuplication/Shaders/uDD_Standard.shader index 8c9f185..d71e3ae 100644 --- a/Assets/uDesktopDuplication/Shaders/uDD_Standard.shader +++ b/Assets/uDesktopDuplication/Shaders/uDD_Standard.shader @@ -24,22 +24,16 @@ SubShader #pragma multi_compile ___ INVERT_Y #pragma multi_compile ___ VERTICAL + #define SURFACE_SHADER #include "./uDD_Common.cginc" + #include "./uDD_Params.cginc" - sampler2D _MainTex; - - struct Input - { - float2 uv_MainTex; - }; - - half _Glossiness; - half _Metallic; - fixed4 _Color; + half _Glossiness; + half _Metallic; void surf(Input IN, inout SurfaceOutputStandard o) { - fixed4 c = uddGetTexture(_MainTex, IN.uv_MainTex) * _Color; + fixed4 c = uddGetScreenTextureWithCursor(_MainTex, _CursorTex, IN.uv_MainTex, _CursorPositionScale) * _Color; o.Albedo = c.rgb; o.Metallic = _Metallic; o.Smoothness = _Glossiness; diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader b/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader index ad98c4f..c27d1a7 100644 --- a/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader +++ b/Assets/uDesktopDuplication/Shaders/uDD_Unlit.shader @@ -17,24 +17,8 @@ Cull [_Cull] CGINCLUDE -#include "UnityCG.cginc" #include "./uDD_Common.cginc" - -struct appdata -{ - float4 vertex : POSITION; - float2 uv : TEXCOORD0; -}; - -struct v2f -{ - float4 vertex : SV_POSITION; - float2 uv : TEXCOORD0; -}; - -sampler2D _MainTex; -float4 _MainTex_ST; -fixed4 _Color; +#include "./uDD_Params.cginc" v2f vert(appdata v) { @@ -46,7 +30,7 @@ v2f vert(appdata v) fixed4 frag(v2f i) : SV_Target { - return uddGetTexture(_MainTex, i.uv) * _Color; + return uddGetScreenTextureWithCursor(_MainTex, _CursorTex, i.uv, _CursorPositionScale); } ENDCG @@ -66,4 +50,4 @@ Pass Fallback "Unlit/Texture" -} +} \ No newline at end of file diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Unlit_BlackMask.shader b/Assets/uDesktopDuplication/Shaders/uDD_Unlit_BlackMask.shader index 7c517f3..e80bf60 100644 --- a/Assets/uDesktopDuplication/Shaders/uDD_Unlit_BlackMask.shader +++ b/Assets/uDesktopDuplication/Shaders/uDD_Unlit_BlackMask.shader @@ -20,24 +20,9 @@ Blend SrcAlpha OneMinusSrcAlpha CGINCLUDE -#include "UnityCG.cginc" #include "./uDD_Common.cginc" +#include "./uDD_Params.cginc" -struct appdata -{ - float4 vertex : POSITION; - float2 uv : TEXCOORD0; -}; - -struct v2f -{ - float4 vertex : SV_POSITION; - float2 uv : TEXCOORD0; -}; - -sampler2D _MainTex; -float4 _MainTex_ST; -fixed4 _Color; fixed _Mask; v2f vert(appdata v) @@ -50,22 +35,22 @@ v2f vert(appdata v) fixed4 frag(v2f i) : SV_Target { - fixed4 tex = uddGetTexture(_MainTex, i.uv); - fixed alpha = pow((tex.r + tex.g + tex.b) / 3.0, _Mask); - return fixed4(tex.rgb * _Color.rgb, alpha); + fixed4 tex = uddGetScreenTextureWithCursor(_MainTex, _CursorTex, i.uv, _CursorPositionScale); + fixed alpha = pow((tex.r + tex.g + tex.b) / 3.0, _Mask); + return fixed4(tex.rgb * _Color.rgb, alpha * _Color.a); } ENDCG Pass { - CGPROGRAM - #pragma vertex vert - #pragma fragment frag - #pragma multi_compile ___ INVERT_X - #pragma multi_compile ___ INVERT_Y - #pragma multi_compile ___ VERTICAL - ENDCG + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #pragma multi_compile ___ INVERT_X + #pragma multi_compile ___ INVERT_Y + #pragma multi_compile ___ VERTICAL + ENDCG } } diff --git a/Assets/uDesktopDuplication/Shaders/uDD_Unlit_Transparent.shader b/Assets/uDesktopDuplication/Shaders/uDD_Unlit_Transparent.shader index 6fe5b05..fbf62ba 100644 --- a/Assets/uDesktopDuplication/Shaders/uDD_Unlit_Transparent.shader +++ b/Assets/uDesktopDuplication/Shaders/uDD_Unlit_Transparent.shader @@ -19,24 +19,8 @@ Blend SrcAlpha OneMinusSrcAlpha CGINCLUDE -#include "UnityCG.cginc" #include "./uDD_Common.cginc" - -struct appdata -{ - float4 vertex : POSITION; - float2 uv : TEXCOORD0; -}; - -struct v2f -{ - float4 vertex : SV_POSITION; - float2 uv : TEXCOORD0; -}; - -sampler2D _MainTex; -float4 _MainTex_ST; -fixed4 _Color; +#include "./uDD_Params.cginc" v2f vert(appdata v) { @@ -48,7 +32,7 @@ v2f vert(appdata v) fixed4 frag(v2f i) : SV_Target { - return fixed4(uddGetTexture(_MainTex, i.uv).rgb, 1.0) * _Color; + return fixed4(uddGetScreenTextureWithCursor(_MainTex, _CursorTex, i.uv, _CursorPositionScale).rgb, 1.0) * _Color; } ENDCG