make shader one pass.

This commit is contained in:
hecomi
2016-11-04 22:45:57 +09:00
parent 908988dcaa
commit 690dba23cc
11 changed files with 96 additions and 226 deletions
@@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: 4fad0d0b5018b8649ac41af2fef8dbf2
folderAsset: yes
timeCreated: 1478011152
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -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"
}
+9 -48
View File
@@ -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<Vector2, Texture2D> pointerTextures_ = new Dictionary<Vector2, Texture2D>();
void OnEnable()
{
uddTexture_ = GetComponent<Texture>();
}
private Texture2D currentTexture_;
private Dictionary<Vector2, Texture2D> textures_ = new Dictionary<Vector2, Texture2D>();
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<MeshFilter>();
filter.mesh = GetComponent<MeshFilter>().sharedMesh;
var renderer = clone.AddComponent<MeshRenderer>();
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<Texture>();
}
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));
}
}
+13 -9
View File
@@ -13,8 +13,8 @@ public class Texture : MonoBehaviour
set
{
monitor_ = value;
material_ = GetComponent<Renderer>().material;
material_.mainTexture = monitor_.texture;
material = GetComponent<Renderer>().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");
}
}
}
@@ -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
@@ -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
@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: d224cadaa1bfa2d46a6224ce2a1443c7
timeCreated: 1477847636
guid: 54044418ab4785c4a84dd4be507db6c2
timeCreated: 1478266678
licenseType: Pro
ShaderImporter:
defaultTextures: []
@@ -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;
@@ -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"
}
}
@@ -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
}
}
@@ -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