Skip to content

Commit

Permalink
Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
k3min committed Mar 13, 2015
1 parent 3e90342 commit da79280
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 13 deletions.
83 changes: 74 additions & 9 deletions UnLighted/Hand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ public class Hand : MonoBehaviour
{
private Joint joint;
private RaycastHit hit;
private float size;
private Material material;

public float MaxDistance = 2;
public float Break = 1000;
public Pointer Pointer = new Pointer
{
Min = 2,
Max = 8,
Color = new Color(0, 0, 0, 0.5f),
Segments = 16,
Thickness = 3,
Speed = 10
};

public bool Valid
{
Expand Down Expand Up @@ -40,11 +51,16 @@ public Rigidbody Holding

private void Awake()
{

this.material = new Material(Shader.Find("Hidden/UnLighted/Pointer"));
}

private void Update()
{
var origin = Camera.main.transform.position;
var direction = Camera.main.transform.forward;

Physics.Raycast(origin, direction, out this.hit, this.MaxDistance);

if (!Input.GetButtonDown("Hand"))
{
return;
Expand All @@ -58,14 +74,6 @@ private void Update()
return;
}

var origin = Camera.main.transform.position;
var direction = Camera.main.transform.forward;

if (!Physics.Raycast(origin, direction, out this.hit, this.MaxDistance))
{
return;
}

if (!this.Valid)
{
return;
Expand All @@ -86,5 +94,62 @@ private void Update()
this.Holding = this.hit.rigidbody;
this.Holding.solverIterationCount = PlayerController.SolverCount;
}

private Vector3 Vertex(float i, float o)
{
var v = Vector3.zero;
var f = ((float)i / this.Pointer.Segments) * Mathf.PI * 2f;
var s = this.size + (o * this.Pointer.Thickness);

v.x = ((Mathf.Cos(f) / Screen.width) * s) + 0.5f;
v.y = ((Mathf.Sin(f) / Screen.height) * s) + 0.5f;

return v;
}

private void OnGUI()
{
var s = (this.Holding != null || this.Valid) ? this.Pointer.Max : this.Pointer.Min;

this.size = Mathf.Lerp(this.size, s, this.Pointer.Speed * Time.deltaTime);

GL.PushMatrix();
GL.LoadOrtho();

this.material.SetPass(0);
this.material.SetColor("_Color", this.Pointer.Color);

GL.Begin(GL.QUADS);

for (var i = 0; i < this.Pointer.Segments; i++)
{
GL.TexCoord2(0, 0);
GL.Vertex(this.Vertex(i, 0));

GL.TexCoord2(0, 0);
GL.Vertex(this.Vertex(i + 1, 0));

GL.TexCoord2(0, 1);
GL.Vertex(this.Vertex(i + 1, 1));

GL.TexCoord2(0, 1);
GL.Vertex(this.Vertex(i, 1));
}

GL.End();

GL.PopMatrix();
}
}

[System.Serializable]
public struct Pointer
{
public float Min;
public float Max;
public Color Color;
public int Segments;
public float Thickness;
public float Speed;
}
}
2 changes: 0 additions & 2 deletions UnLighted/ImageEffects/MotionBlur.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class MotionBlur : ImageEffectBase
private Matrix4x4 VP;

public float TargetFPS = 60;
public int MaxSamples = 32;
public int Downsample = 1;
public bool Debug;

Expand Down Expand Up @@ -93,7 +92,6 @@ public override void OnRenderImage(RenderTexture a, RenderTexture b)

this.Material.SetTexture("_MotionTex", this.motion);
this.Material.SetFloat("_MotionScale", (1f / this.TargetFPS) / Time.deltaTime);
this.Material.SetInt("_MaxSamples", this.MaxSamples);

Graphics.Blit(a, b, this.Material, 1);
}
Expand Down
5 changes: 3 additions & 2 deletions UnLighted/Resources/Shaders/ImageEffects/MotionBlur.shader
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#pragma fragmentoption ARB_precision_hint_fastest
#pragma only_renderers d3d11 opengl

#define MAX_SAMPLES 30

float4x4 _Proj;

float2 EncodeMotion(float4 cur, float4 prv)
Expand Down Expand Up @@ -65,7 +67,6 @@

sampler2D _MotionTex;
float _MotionScale;
int _MaxSamples;

float4 frag(v2f_img i) : COLOR
{
Expand All @@ -74,7 +75,7 @@
vel *= _MotionScale;

int magnitude = round(length(vel * _MainTex_TexelSize.zw));
int samples = clamp(magnitude, 1, _MaxSamples);
int samples = clamp(magnitude, 1, MAX_SAMPLES);

float4 res = tex2D(_MainTex, i.uv);

Expand Down
41 changes: 41 additions & 0 deletions UnLighted/Resources/Shaders/ImageEffects/Pointer.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Shader "Hidden/UnLighted/Pointer"
{
Properties
{
_MainTex("", 2D) = "" {}
}

SubShader
{
Pass
{
ZWrite Off

Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM

#include "UnityCG.cginc"
#include "./../Includes/Vars.cginc"

#pragma vertex vert_img
#pragma fragment frag

#pragma fragmentoption ARB_precision_hint_fastest
#pragma only_renderers d3d11 opengl

float4 _Color;

float4 frag(v2f_img i) : COLOR
{
float4 res = _Color;

res.a *= pow(sin(i.uv.y * PI), 2.0);

return res;
}

ENDCG
}
}
}
3 changes: 3 additions & 0 deletions UnLighted/UnLighted.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,8 @@
<None Include="Resources\Shaders\ImageEffects\MotionBlur.shader">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Resources\Shaders\ImageEffects\Pointer.shader">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

0 comments on commit da79280

Please sign in to comment.