add displacement mapping example.
This commit is contained in:
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,58 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Assertions;
|
||||||
|
|
||||||
|
[RequireComponent(typeof(uDesktopDuplication.Texture))]
|
||||||
|
public class DisplacementMapping : MonoBehaviour
|
||||||
|
{
|
||||||
|
public enum TargetMonitor
|
||||||
|
{
|
||||||
|
Self,
|
||||||
|
Next,
|
||||||
|
Prev,
|
||||||
|
}
|
||||||
|
public TargetMonitor target = TargetMonitor.Self;
|
||||||
|
|
||||||
|
uDesktopDuplication.Texture uddTexture_;
|
||||||
|
|
||||||
|
int dispTexId_;
|
||||||
|
int dispFactorId_;
|
||||||
|
int tessMinDistId_;
|
||||||
|
int tessMaxDistId_;
|
||||||
|
int tessFactorId_;
|
||||||
|
|
||||||
|
[Range(0.0f, 10f)] public float displacementFactor = 0.1f;
|
||||||
|
[Range(0.1f, 10f)] public float tessellationMinDist = 0.5f;
|
||||||
|
[Range(1.0f, 50f)] public float tessellationMaxDist = 25f;
|
||||||
|
[Range(1.0f, 50f)] public float tessellationFactor = 25f;
|
||||||
|
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
uddTexture_ = GetComponent<uDesktopDuplication.Texture>();
|
||||||
|
Assert.IsNotNull(uddTexture_);
|
||||||
|
|
||||||
|
dispTexId_ = Shader.PropertyToID("_DispTex");
|
||||||
|
dispFactorId_ = Shader.PropertyToID("_DispFactor");
|
||||||
|
tessMinDistId_ = Shader.PropertyToID("_TessMinDist");
|
||||||
|
tessMaxDistId_ = Shader.PropertyToID("_TessMaxDist");
|
||||||
|
tessFactorId_ = Shader.PropertyToID("_TessFactor");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
var id = uddTexture_.monitor.id;
|
||||||
|
switch (target) {
|
||||||
|
case TargetMonitor.Self: break;
|
||||||
|
case TargetMonitor.Next: ++id; break;
|
||||||
|
case TargetMonitor.Prev: --id; break;
|
||||||
|
}
|
||||||
|
id = Mathf.Clamp(id, 0, uDesktopDuplication.Manager.monitorCount - 1);
|
||||||
|
var monitor = uDesktopDuplication.Manager.GetMonitor(id);
|
||||||
|
monitor.shouldBeUpdated = true;
|
||||||
|
|
||||||
|
uddTexture_.material.SetTexture(dispTexId_, monitor.texture);
|
||||||
|
uddTexture_.material.SetFloat(dispFactorId_, displacementFactor);
|
||||||
|
uddTexture_.material.SetFloat(tessMinDistId_, tessellationMinDist);
|
||||||
|
uddTexture_.material.SetFloat(tessMaxDistId_, tessellationMaxDist);
|
||||||
|
uddTexture_.material.SetFloat(tessFactorId_, tessellationFactor);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 859a2446b86e55c43932212872c82748
|
||||||
|
timeCreated: 1480146517
|
||||||
|
licenseType: Pro
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -43,32 +43,7 @@ public class GazePointAnalyzer : MonoBehaviour
|
|||||||
|
|
||||||
public Vector3 GetWorldPositionFromCoord(int u, int v)
|
public Vector3 GetWorldPositionFromCoord(int u, int v)
|
||||||
{
|
{
|
||||||
var monitor = uddTexture_.monitor;
|
return uddTexture_.GetWorldPositionFromCoord(u, v);
|
||||||
|
|
||||||
// Mesh & Scale information
|
|
||||||
var mesh = GetComponent<MeshFilter>().sharedMesh;
|
|
||||||
var width = transform.localScale.x * (mesh.bounds.extents.x * 2f);
|
|
||||||
var height = transform.localScale.y * (mesh.bounds.extents.y * 2f);
|
|
||||||
|
|
||||||
// Local position (scale included).
|
|
||||||
var x = (float)(u - monitor.width / 2) / monitor.width;
|
|
||||||
var y = -(float)(v - monitor.height / 2) / monitor.height;
|
|
||||||
var localPos = new Vector3(width * x, height * y, 0f);
|
|
||||||
|
|
||||||
// Bending
|
|
||||||
if (uddTexture_.bend) {
|
|
||||||
var radius = uddTexture_.radius;
|
|
||||||
var angle = localPos.x / radius;
|
|
||||||
if (uddTexture_.meshForwardDirection == MeshForwardDirection.Y) {
|
|
||||||
localPos.y -= radius * (1f - Mathf.Cos(angle));
|
|
||||||
} else {
|
|
||||||
localPos.z -= radius * (1f - Mathf.Cos(angle));
|
|
||||||
}
|
|
||||||
localPos.x = radius * Mathf.Sin(angle);
|
|
||||||
}
|
|
||||||
|
|
||||||
// To world position
|
|
||||||
return transform.position + (transform.rotation * localPos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CalcAveragePos()
|
void CalcAveragePos()
|
||||||
|
|||||||
Binary file not shown.
@@ -16,7 +16,6 @@ public class Texture : MonoBehaviour
|
|||||||
if (monitor_ != null) {
|
if (monitor_ != null) {
|
||||||
material = GetComponent<Renderer>().material; // clone
|
material = GetComponent<Renderer>().material; // clone
|
||||||
material.mainTexture = monitor_.texture;
|
material.mainTexture = monitor_.texture;
|
||||||
material.SetTexture("_DispTex", monitor_.texture);
|
|
||||||
material.SetFloat("_Width", transform.localScale.x);
|
material.SetFloat("_Width", transform.localScale.x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,6 +203,33 @@ public class Texture : MonoBehaviour
|
|||||||
material.DisableKeyword("USE_CLIP");
|
material.DisableKeyword("USE_CLIP");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Vector3 GetWorldPositionFromCoord(int u, int v)
|
||||||
|
{
|
||||||
|
// Mesh & Scale information
|
||||||
|
var mesh = GetComponent<MeshFilter>().sharedMesh;
|
||||||
|
var width = transform.localScale.x * (mesh.bounds.extents.x * 2f);
|
||||||
|
var height = transform.localScale.y * (mesh.bounds.extents.y * 2f);
|
||||||
|
|
||||||
|
// Local position (scale included).
|
||||||
|
var x = (float)(u - monitor.width / 2) / monitor.width;
|
||||||
|
var y = -(float)(v - monitor.height / 2) / monitor.height;
|
||||||
|
var localPos = new Vector3(width * x, height * y, 0f);
|
||||||
|
|
||||||
|
// Bending
|
||||||
|
if (bend) {
|
||||||
|
var angle = localPos.x / radius;
|
||||||
|
if (meshForwardDirection == MeshForwardDirection.Y) {
|
||||||
|
localPos.y -= radius * (1f - Mathf.Cos(angle));
|
||||||
|
} else {
|
||||||
|
localPos.z -= radius * (1f - Mathf.Cos(angle));
|
||||||
|
}
|
||||||
|
localPos.x = radius * Mathf.Sin(angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// To world position
|
||||||
|
return transform.position + (transform.rotation * localPos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ inline fixed4 uddGetScreenTexture(float2 uv)
|
|||||||
return uddGetTexture(_MainTex, uv);
|
return uddGetTexture(_MainTex, uv);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void uddBendVertex(inout float4 v, half radius, half width, half thickness)
|
inline void uddBendVertex(inout float3 v, half radius, half width, half thickness)
|
||||||
{
|
{
|
||||||
#ifdef BEND_ON
|
#ifdef BEND_ON
|
||||||
half angle = width * v.x / radius;
|
half angle = width * v.x / radius;
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ half _Thickness;
|
|||||||
v2f vert(appdata v)
|
v2f vert(appdata v)
|
||||||
{
|
{
|
||||||
v2f o;
|
v2f o;
|
||||||
uddBendVertex(v.vertex, _Radius, _Width, _Thickness);
|
uddBendVertex(v.vertex.xyz, _Radius, _Width, _Thickness);
|
||||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||||
return o;
|
return o;
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ half _Thickness;
|
|||||||
v2f vert(appdata v)
|
v2f vert(appdata v)
|
||||||
{
|
{
|
||||||
v2f o;
|
v2f o;
|
||||||
uddBendVertex(v.vertex, _Radius, _Width, _Thickness);
|
uddBendVertex(v.vertex.xyz, _Radius, _Width, _Thickness);
|
||||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||||
return o;
|
return o;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ Properties
|
|||||||
[PowerSlider(10.0)] _Thickness("Thickness", Range(0.01, 10)) = 1
|
[PowerSlider(10.0)] _Thickness("Thickness", Range(0.01, 10)) = 1
|
||||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||||
_DispTex ("Displacement Map", 2D) = "black" {}
|
_DispTex ("Displacement Map", 2D) = "black" {}
|
||||||
_Displacement("Displacement Factor", Range(0, 5.0)) = 1
|
_DispFactor("Displacement Factor", Range(0, 5.0)) = 1
|
||||||
_TessMinDist("Tessellation Min Distance", Range(0.1, 100.0)) = 1.0
|
_TessMinDist("Tessellation Min Distance", Range(0.1, 100.0)) = 1.0
|
||||||
_TessMaxDist("Tessellation Max Distance", Range(0.1, 100.0)) = 5.0
|
_TessMaxDist("Tessellation Max Distance", Range(0.1, 100.0)) = 5.0
|
||||||
_TessFactor("Tessellation Factor", Range(0.1, 50.0)) = 10.0
|
_TessFactor("Tessellation Factor", Range(0.1, 50.0)) = 10.0
|
||||||
@@ -35,7 +35,7 @@ half _Width;
|
|||||||
half _Thickness;
|
half _Thickness;
|
||||||
Texture2D _DispTex;
|
Texture2D _DispTex;
|
||||||
SamplerState sampler_DispTex;
|
SamplerState sampler_DispTex;
|
||||||
half _Displacement;
|
half _DispFactor;
|
||||||
half _TessMinDist;
|
half _TessMinDist;
|
||||||
half _TessMaxDist;
|
half _TessMaxDist;
|
||||||
half _TessFactor;
|
half _TessFactor;
|
||||||
@@ -76,10 +76,7 @@ struct DsOutput
|
|||||||
HsInput vert(VsInput i)
|
HsInput vert(VsInput i)
|
||||||
{
|
{
|
||||||
HsInput o;
|
HsInput o;
|
||||||
float4 v = float4(i.vertex, 1.0);
|
o.f4Position = float4(i.vertex, 1.0);
|
||||||
uddBendNormal(v.x, i.normal, _Radius, _Width);
|
|
||||||
uddBendVertex(v, _Radius, _Width, _Thickness);
|
|
||||||
o.f4Position = v;
|
|
||||||
o.f3Normal = i.normal;
|
o.f3Normal = i.normal;
|
||||||
o.f2TexCoord = i.texcoord;
|
o.f2TexCoord = i.texcoord;
|
||||||
return o;
|
return o;
|
||||||
@@ -139,7 +136,10 @@ DsOutput domain(
|
|||||||
bary.y * i[1].f2TexCoord +
|
bary.y * i[1].f2TexCoord +
|
||||||
bary.z * i[2].f2TexCoord;
|
bary.z * i[2].f2TexCoord;
|
||||||
|
|
||||||
float disp = length(_DispTex.SampleLevel(sampler_DispTex, o.f2TexCoord, 0)) * _Displacement;
|
uddBendNormal(f3Position.x, f3Normal, _Radius, _Width);
|
||||||
|
uddBendVertex(f3Position, _Radius, _Width, _Thickness);
|
||||||
|
|
||||||
|
float disp = length(_DispTex.SampleLevel(sampler_DispTex, o.f2TexCoord, 0)) * _DispFactor;
|
||||||
f3Position.xyz += f3Normal * disp;
|
f3Position.xyz += f3Normal * disp;
|
||||||
|
|
||||||
o.f4Position = mul(UNITY_MATRIX_MVP, float4(f3Position.xyz, 1.0));
|
o.f4Position = mul(UNITY_MATRIX_MVP, float4(f3Position.xyz, 1.0));
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ half _Thickness;
|
|||||||
v2f vert(appdata v)
|
v2f vert(appdata v)
|
||||||
{
|
{
|
||||||
v2f o;
|
v2f o;
|
||||||
uddBendVertex(v.vertex, _Radius, _Width, _Thickness);
|
uddBendVertex(v.vertex.xyz, _Radius, _Width, _Thickness);
|
||||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||||
return o;
|
return o;
|
||||||
|
|||||||
Reference in New Issue
Block a user