Added Unity project files
This commit is contained in:
@ -0,0 +1,168 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
[RequireComponent(typeof(Camera))]
|
||||
public class HighlightsFX : PostEffectsBase
|
||||
{
|
||||
public static HighlightsFX Instance { get; protected set; }
|
||||
|
||||
protected readonly HashSet<Renderer> objectsToRender = new HashSet<Renderer>();
|
||||
protected Shader highlightShader;
|
||||
protected Material highlightMaterial;
|
||||
|
||||
#region HighlightsFX Singleton Methods
|
||||
|
||||
public static void EnableObjectHighlight(Renderer outlineRenderer, bool enable)
|
||||
{
|
||||
if(Instance != null)
|
||||
{
|
||||
Instance.EnableOutline(outlineRenderer, enable);
|
||||
}
|
||||
}
|
||||
|
||||
public void EnableOutline(Renderer outlineRenderer, bool enable)
|
||||
{
|
||||
if(outlineRenderer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(enable)
|
||||
{
|
||||
objectsToRender.Add(outlineRenderer);
|
||||
}
|
||||
else
|
||||
{
|
||||
objectsToRender.Remove(outlineRenderer);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constants
|
||||
|
||||
private const int DRAW_DEPTH_PREPASS = 0;
|
||||
private const int DRAW_HIGHLIGHT_PASS = 1;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Serialized Fields
|
||||
|
||||
[SerializeField]
|
||||
private Color highlightColor = new Color(0.0f, 0.573f, 1.0f, 1.0f);
|
||||
|
||||
[SerializeField]
|
||||
[Range(0.0f, 10.0f)]
|
||||
private float mobilePulseSpeed = 2.0f;
|
||||
|
||||
[SerializeField]
|
||||
[Range(0.0f, 1.0f)]
|
||||
private float mobileMinimumOpacity = 0.2f;
|
||||
|
||||
[SerializeField]
|
||||
[Range(0.0f, 1.0f)]
|
||||
private float mobileMaximumOpacity = 0.4f;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private static readonly int highlightColorId = Shader.PropertyToID("_HighlightColor");
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if(Instance != null)
|
||||
{
|
||||
Debug.LogError("HighlightsFX - More than one instance detected.");
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
|
||||
highlightShader = Shader.Find("Hidden/VRChat/MobileHighlight");
|
||||
}
|
||||
|
||||
private void OnPostRender()
|
||||
{
|
||||
DrawMobileHighlight();
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mobile Highlight
|
||||
|
||||
private void DrawMobileHighlight()
|
||||
{
|
||||
objectsToRender.RemoveWhere(o => o == null);
|
||||
|
||||
float highlightAlpha = Mathf.Lerp(mobileMinimumOpacity, mobileMaximumOpacity, 0.5f * Mathf.Sin(mobilePulseSpeed * Time.timeSinceLevelLoad) + 0.5f);
|
||||
highlightMaterial.SetColor(highlightColorId, new Color(highlightColor.r, highlightColor.g, highlightColor.b, highlightColor.a * highlightAlpha));
|
||||
|
||||
foreach(Renderer highlightedRenderer in objectsToRender)
|
||||
{
|
||||
MeshFilter meshFilter = highlightedRenderer.GetComponent<MeshFilter>();
|
||||
if(meshFilter == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(meshFilter.sharedMesh == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Mesh sharedMesh = meshFilter.sharedMesh;
|
||||
Matrix4x4 localToWorldMatrix = highlightedRenderer.transform.localToWorldMatrix;
|
||||
highlightMaterial.SetPass(DRAW_DEPTH_PREPASS);
|
||||
Graphics.DrawMeshNow(sharedMesh, localToWorldMatrix);
|
||||
}
|
||||
|
||||
foreach(Renderer highlightedRenderer in objectsToRender)
|
||||
{
|
||||
MeshFilter meshFilter = highlightedRenderer.GetComponent<MeshFilter>();
|
||||
if(meshFilter == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(meshFilter.sharedMesh == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Mesh sharedMesh = meshFilter.sharedMesh;
|
||||
Matrix4x4 localToWorldMatrix = highlightedRenderer.transform.localToWorldMatrix;
|
||||
highlightMaterial.SetPass(DRAW_HIGHLIGHT_PASS);
|
||||
Graphics.DrawMeshNow(sharedMesh, localToWorldMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region PostEffectBase Methods
|
||||
|
||||
public override bool CheckResources()
|
||||
{
|
||||
CheckSupport(false);
|
||||
highlightMaterial = CheckShaderAndCreateMaterial(highlightShader, highlightMaterial);
|
||||
|
||||
if(!isSupported)
|
||||
{
|
||||
ReportAutoDisable();
|
||||
}
|
||||
|
||||
return isSupported;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 296a1de81b60e451a9b96e4407e7c413
|
||||
timeCreated: 1435855266
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,260 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(Camera))]
|
||||
public class PostEffectsBase : MonoBehaviour
|
||||
{
|
||||
protected bool supportHDRTextures = true;
|
||||
protected bool supportDX11 = false;
|
||||
protected bool isSupported = true;
|
||||
|
||||
private List<Material> createdMaterials = new List<Material>();
|
||||
|
||||
protected Material CheckShaderAndCreateMaterial(Shader s, Material m2Create)
|
||||
{
|
||||
if (!s)
|
||||
{
|
||||
Debug.Log("Missing shader in " + ToString());
|
||||
enabled = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (s.isSupported && m2Create && m2Create.shader == s)
|
||||
return m2Create;
|
||||
|
||||
if (!s.isSupported)
|
||||
{
|
||||
NotSupported();
|
||||
Debug.Log("The shader " + s.ToString() + " on effect " + ToString() + " is not supported on this platform!");
|
||||
return null;
|
||||
}
|
||||
|
||||
m2Create = new Material(s);
|
||||
createdMaterials.Add(m2Create);
|
||||
m2Create.hideFlags = HideFlags.DontSave;
|
||||
|
||||
return m2Create;
|
||||
}
|
||||
|
||||
|
||||
protected Material CreateMaterial(Shader s, Material m2Create)
|
||||
{
|
||||
if (!s)
|
||||
{
|
||||
Debug.Log("Missing shader in " + ToString());
|
||||
return null;
|
||||
}
|
||||
|
||||
if (m2Create && (m2Create.shader == s) && (s.isSupported))
|
||||
return m2Create;
|
||||
|
||||
if (!s.isSupported)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
m2Create = new Material(s);
|
||||
createdMaterials.Add(m2Create);
|
||||
m2Create.hideFlags = HideFlags.DontSave;
|
||||
|
||||
return m2Create;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
isSupported = true;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
RemoveCreatedMaterials();
|
||||
}
|
||||
|
||||
private void RemoveCreatedMaterials()
|
||||
{
|
||||
while (createdMaterials.Count > 0)
|
||||
{
|
||||
Material mat = createdMaterials[0];
|
||||
createdMaterials.RemoveAt(0);
|
||||
#if UNITY_EDITOR
|
||||
DestroyImmediate(mat);
|
||||
#else
|
||||
Destroy(mat);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
protected bool CheckSupport()
|
||||
{
|
||||
return CheckSupport(false);
|
||||
}
|
||||
|
||||
|
||||
public virtual bool CheckResources()
|
||||
{
|
||||
Debug.LogWarning("CheckResources () for " + ToString() + " should be overwritten.");
|
||||
return isSupported;
|
||||
}
|
||||
|
||||
|
||||
protected void Start()
|
||||
{
|
||||
CheckResources();
|
||||
}
|
||||
|
||||
protected bool CheckSupport(bool needDepth)
|
||||
{
|
||||
isSupported = true;
|
||||
supportHDRTextures = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf);
|
||||
supportDX11 = SystemInfo.graphicsShaderLevel >= 50 && SystemInfo.supportsComputeShaders;
|
||||
|
||||
#if !UNITY_2019_1_OR_NEWER
|
||||
if (!SystemInfo.supportsImageEffects)
|
||||
{
|
||||
NotSupported();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (needDepth && !SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth))
|
||||
{
|
||||
NotSupported();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (needDepth)
|
||||
GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected bool CheckSupport(bool needDepth, bool needHdr)
|
||||
{
|
||||
if (!CheckSupport(needDepth))
|
||||
return false;
|
||||
|
||||
if (needHdr && !supportHDRTextures)
|
||||
{
|
||||
NotSupported();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public bool Dx11Support()
|
||||
{
|
||||
return supportDX11;
|
||||
}
|
||||
|
||||
|
||||
protected void ReportAutoDisable()
|
||||
{
|
||||
Debug.LogWarning("The image effect " + ToString() + " has been disabled as it's not supported on the current platform.");
|
||||
}
|
||||
|
||||
// deprecated but needed for old effects to survive upgrading
|
||||
bool CheckShader(Shader s)
|
||||
{
|
||||
Debug.Log("The shader " + s.ToString() + " on effect " + ToString() + " is not part of the Unity 3.2+ effects suite anymore. For best performance and quality, please ensure you are using the latest Standard Assets Image Effects (Pro only) package.");
|
||||
if (!s.isSupported)
|
||||
{
|
||||
NotSupported();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void NotSupported()
|
||||
{
|
||||
enabled = false;
|
||||
isSupported = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
protected void DrawBorder(RenderTexture dest, Material material)
|
||||
{
|
||||
float x1;
|
||||
float x2;
|
||||
float y1;
|
||||
float y2;
|
||||
|
||||
RenderTexture.active = dest;
|
||||
bool invertY = true; // source.texelSize.y < 0.0ff;
|
||||
// Set up the simple Matrix
|
||||
GL.PushMatrix();
|
||||
GL.LoadOrtho();
|
||||
|
||||
for (int i = 0; i < material.passCount; i++)
|
||||
{
|
||||
material.SetPass(i);
|
||||
|
||||
float y1_; float y2_;
|
||||
if (invertY)
|
||||
{
|
||||
y1_ = 1.0f; y2_ = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
y1_ = 0.0f; y2_ = 1.0f;
|
||||
}
|
||||
|
||||
// left
|
||||
x1 = 0.0f;
|
||||
x2 = 0.0f + 1.0f / (dest.width * 1.0f);
|
||||
y1 = 0.0f;
|
||||
y2 = 1.0f;
|
||||
GL.Begin(GL.QUADS);
|
||||
|
||||
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
|
||||
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
|
||||
|
||||
// right
|
||||
x1 = 1.0f - 1.0f / (dest.width * 1.0f);
|
||||
x2 = 1.0f;
|
||||
y1 = 0.0f;
|
||||
y2 = 1.0f;
|
||||
|
||||
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
|
||||
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
|
||||
|
||||
// top
|
||||
x1 = 0.0f;
|
||||
x2 = 1.0f;
|
||||
y1 = 0.0f;
|
||||
y2 = 0.0f + 1.0f / (dest.height * 1.0f);
|
||||
|
||||
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
|
||||
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
|
||||
|
||||
// bottom
|
||||
x1 = 0.0f;
|
||||
x2 = 1.0f;
|
||||
y1 = 1.0f - 1.0f / (dest.height * 1.0f);
|
||||
y2 = 1.0f;
|
||||
|
||||
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
|
||||
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
|
||||
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
|
||||
|
||||
GL.End();
|
||||
}
|
||||
|
||||
GL.PopMatrix();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2f217b24afbcbe409a887140474afa6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6afd4fdf21e53e14597d6776423c0e36
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,87 @@
|
||||
Shader "Hidden/VRChat/MobileHighlight"
|
||||
{
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
CGINCLUDE
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
float4 _HighlightColor;
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
UNITY_VERTEX_INPUT_INSTANCE_ID
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
struct v2f
|
||||
{
|
||||
float4 pos : SV_POSITION;
|
||||
UNITY_VERTEX_OUTPUT_STEREO
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
v2f vert( appdata i )
|
||||
{
|
||||
v2f o;
|
||||
UNITY_SETUP_INSTANCE_ID(i);
|
||||
UNITY_INITIALIZE_OUTPUT(v2f, o);
|
||||
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
|
||||
|
||||
o.pos = UnityObjectToClipPos(i.vertex);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
fixed4 frag( v2f i ) : SV_Target
|
||||
{
|
||||
return _HighlightColor;
|
||||
}
|
||||
|
||||
ENDCG
|
||||
|
||||
SubShader
|
||||
{
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Depth Pre-pass
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Pass
|
||||
{
|
||||
Name "DepthPrepass"
|
||||
Tags { "LightMode" = "Always" }
|
||||
ColorMask 0
|
||||
Cull Off
|
||||
ZTest Always
|
||||
ZWrite On
|
||||
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
ENDCG
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Render the object with a transparent solid coloured highlight.
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Pass
|
||||
{
|
||||
Name "HighlightPass"
|
||||
Tags { "LightMode" = "Always" }
|
||||
Cull Off
|
||||
ZWrite Off
|
||||
ZTest Equal
|
||||
Blend SrcAlpha OneMinusSrcAlpha
|
||||
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c3382d604d882e46b871743fd208392
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user