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
+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");
}
}
}