Skip to content

Commit

Permalink
BAM
Browse files Browse the repository at this point in the history
  • Loading branch information
k3min committed Apr 3, 2015
1 parent d4b7a4a commit 556fe8a
Show file tree
Hide file tree
Showing 26 changed files with 266 additions and 173 deletions.
4 changes: 2 additions & 2 deletions Editor/UnLightedEd/ImageEffects/ThicknessEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static void Hide(Object target)
{
var camera = (target as Thickness).camera;

camera.enabled = false;
camera.hideFlags = HideFlags.NotEditable | HideFlags.HideInHierarchy;
camera.depth = -2;
camera.hideFlags = HideFlags.HideInHierarchy;
}
}
}
40 changes: 40 additions & 0 deletions Editor/UnLightedEd/ShaderIncludePostprocessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using UnityEditor;
using System.IO;
using System.Linq;

namespace UnLightedEd
{
// Analysis disable once ConvertToStaticType
internal class ShaderIncludePostprocessor : AssetPostprocessor
{
private static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[] moved, string[] movedFrom)
{
foreach (var path in imported)
{
if (Path.GetExtension(path) != ".cginc")
{
continue;
}

ShaderIncludePostprocessor.ReimportDependents(path);
}
}

private static void ReimportDependents(string path)
{
var guids = AssetDatabase.FindAssets("t:shader");
var fileName = Path.GetFileName(path);

foreach (var asset in guids.Select(AssetDatabase.GUIDToAssetPath))
{
using (var stream = new StreamReader(asset))
{
if (stream.ReadToEnd().Contains(fileName))
{
AssetDatabase.ImportAsset(asset);
}
}
}
}
}
}
1 change: 1 addition & 0 deletions Editor/UnLightedEd/UnLightedEd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Controllers\CameraControllerEditor.cs" />
<Compile Include="ImageEffects\ThicknessEditor.cs" />
<Compile Include="UberTranslucencyMaterialEditor.cs" />
<Compile Include="ShaderIncludePostprocessor.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Box\Bake.bat">
Expand Down
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
UnLighted
=========
# UnLighted

![image](http://i.imgur.com/n7hSTKi.gif)

Expand All @@ -9,30 +8,33 @@ For now, UnLighted expects:
- Unity 4.6.1
- Linear color space
- Deferred rendering path
- The ShaderManager component

Features
--------
## Features

What's already there:

- Per-object motion blur
- (Hejl) Tonemapping
- Lens distortion
- Bloom
- LUT based color grading
- Physically based über shader (with glass variant)
- Box projected cubemaps (with tools to render them)
- Optimized lighting pass & better soft shadows (at this point, point support only)
- Rigid player controller with optional head bob
- Separate camera controller with smooth free look
- Awesome trigger system
- Moving platform component
- Manager class
- *Hand* component
- Game manager with scriptable states

Demo Time!
----------
- Image effects
- Extenable base component
- Per-object motion blur
- (Hejl) Tonemapping
- Lens distortion
- Bloom & Flares
- LUT based color grading
- Shading
- Physically based *Über* shader (with glass & transulent variant)
- Box projected cubemaps (with tools to render them)
- Optimized lighting pass & better soft shadows (at this point, point support only)
- Other components
- Rigid player controller (with optional head bob)
- Separate camera controller (with smooth free look)
- Awesome trigger system
- Moving platform component
- Extenable manager component
- *Hand* component
- Game manager with scriptable states

### Demo Time!

![image](http://i.imgur.com/yI7ZXVS.png)

Expand Down
2 changes: 1 addition & 1 deletion UnLighted/ImageEffects/Bloom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void OnRenderImage(RenderTexture a, RenderTexture b)
var w = a.width >> i;
var h = a.height >> i;

var rt = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGBHalf);
var rt = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);

this.Common.Threshold(a, rt, this.Threshold);
this.Common.Blur(rt, this.BlurIterations, this.BlurSize / (float)(1 << i));
Expand Down
5 changes: 3 additions & 2 deletions UnLighted/ImageEffects/BoxProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ private void Awake()
this.camera.nearClipPlane = 0.1f;
this.camera.farClipPlane = 100;
this.camera.hdr = true;
this.camera.clearFlags = CameraClearFlags.Depth;
this.camera.clearFlags = CameraClearFlags.SolidColor;
this.camera.backgroundColor = Color.black;
this.camera.cullingMask = 1;

this.tex = new Texture2D(BoxProbe.size, BoxProbe.size, TextureFormat.ARGB32, false, true);
Expand Down Expand Up @@ -63,7 +64,7 @@ public override void OnRenderImage(RenderTexture a, RenderTexture b)
return;
}

var rt = RenderTexture.GetTemporary(BoxProbe.size, BoxProbe.size, 0, RenderTextureFormat.ARGB32);
var rt = RenderTexture.GetTemporary(BoxProbe.size, BoxProbe.size, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);

base.OnRenderImage(a, rt);

Expand Down
2 changes: 1 addition & 1 deletion UnLighted/ImageEffects/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Overlay(RenderTexture a, RenderTexture b, Overlay o, Texture t, floa

public void Blur(RenderTexture rt, int iterations, Vector2 size)
{
var rt2 = RenderTexture.GetTemporary(rt.width, rt.height, 0, RenderTextureFormat.ARGBHalf);
var rt2 = RenderTexture.GetTemporary(rt.width, rt.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);

for (var i = 0; i < iterations; i++)
{
Expand Down
6 changes: 3 additions & 3 deletions UnLighted/ImageEffects/Flares.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Flares : ImageEffectBase
public float Halo = 0.5f;
public float Distortion = 1.5f;
public int BlurIterations = 2;
public Vector2 BlurSize = new Vector2(1, 1);
public Vector2 BlurSize = Vector2.one;
public int Downsample;

[HideInInspector]
Expand All @@ -26,15 +26,15 @@ public override void OnRenderImage(RenderTexture a, RenderTexture b)
var w = a.width >> i;
var h = a.height >> i;

var rt = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGBHalf);
var rt = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);

this.Common.Threshold(a, rt, this.Threshold);
this.Common.Blur(rt, this.BlurIterations, this.BlurSize / (float)(1 << i));

this.Material.SetTexture("_LensColor", this.LensColor);
this.Material.SetVector("_Params", new Vector3(this.Ghost, this.Halo, this.Distortion));

var rt2 = this.Debug ? b : RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGBHalf);
var rt2 = this.Debug ? b : RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);

base.OnRenderImage(rt, rt2);

Expand Down
17 changes: 10 additions & 7 deletions UnLighted/ImageEffects/MotionBlur.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MotionBlur : ImageEffectBase
private Matrix4x4 invVP;
private Matrix4x4 VP;

public float TargetFPS = 60;
public int TargetFPS = 60;
public int Downsample = 1;

[HideInInspector]
Expand All @@ -26,7 +26,7 @@ private void Awake()
var w = Screen.width >> ImageEffectBase.Level(this.Downsample);
var h = Screen.height >> ImageEffectBase.Level(this.Downsample);

this.motion = new RenderTexture(w, h, 0, RenderTextureFormat.RGHalf);
this.motion = new RenderTexture(w, h, 0, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear);

foreach (var mr in Object.FindObjectsOfType<MeshRenderer>())
{
Expand All @@ -39,7 +39,7 @@ private void Awake()
}
}

private void OnPreRender()
private void UpdateTransform()
{
var view = this.camera.worldToCameraMatrix;
var proj = GL.GetGPUProjectionMatrix(this.camera.projectionMatrix, false);
Expand All @@ -51,23 +51,26 @@ private void OnPreRender()
this.objects.ForEach(o => o.UpdateTransform(this.VP));
}

private void OnPostRender()
private void RenderVector()
{
this.Material.SetMatrix("_Proj", this.prvVP * this.invVP);

Graphics.Blit(null, this.motion, this.Material, 0);

var rt = RenderTexture.active;

Graphics.SetRenderTarget(this.motion);
RenderTexture.active = this.motion;

this.objects.ForEach(o => o.RenderVectors(this.Material));
this.objects.ForEach(o => o.RenderVector(this.Material));

Graphics.SetRenderTarget(rt);
RenderTexture.active = rt;
}

public override void OnRenderImage(RenderTexture a, RenderTexture b)
{
this.UpdateTransform();
this.RenderVector();

if (this.Debug)
{
Graphics.Blit(this.motion, b);
Expand Down
8 changes: 2 additions & 6 deletions UnLighted/ImageEffects/Thickness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ private void Awake()
var w = Screen.width >> ImageEffectBase.Level(this.Downsample);
var h = Screen.height >> ImageEffectBase.Level(this.Downsample);

this.thickness = new RenderTexture(w, h, 0, RenderTextureFormat.ARGB32);
this.thickness = new RenderTexture(w, h, 0, RenderTextureFormat.RHalf, RenderTextureReadWrite.Linear);

Shader.SetGlobalTexture("_Thickness", this.thickness);
}

private void Start()
{
this.camera.enabled = true;
}

private void OnPreCull()
{
this.camera.CopyFrom(Camera.main);
Expand All @@ -32,6 +27,7 @@ private void OnPreCull()
this.camera.backgroundColor = new Color(0, 0, 0, 0);
this.camera.depthTextureMode |= DepthTextureMode.DepthNormals;
this.camera.cullingMask = -1;
this.camera.hdr = true;
this.camera.renderingPath = RenderingPath.Forward;
this.camera.targetTexture = this.thickness;

Expand Down
6 changes: 4 additions & 2 deletions UnLighted/ImageEffects/Tonemap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class Tonemap : ImageEffectBase

private void Awake()
{
this.lum = new RenderTexture(1 << this.MipLevel, 1 << this.MipLevel, 0, RenderTextureFormat.ARGBHalf)
var x = 1 << this.MipLevel;

this.lum = new RenderTexture(x, x, 0, RenderTextureFormat.RHalf, RenderTextureReadWrite.Linear)
{
useMipMap = true
};
Expand All @@ -37,7 +39,7 @@ public override void OnRenderImage(RenderTexture a, RenderTexture b)
return;
}

var tmp = RenderTexture.GetTemporary(4, 4, 0, RenderTextureFormat.ARGBHalf);
var tmp = RenderTexture.GetTemporary(4, 4, 0, RenderTextureFormat.RHalf, RenderTextureReadWrite.Linear);

Graphics.Blit(this.lum, tmp, this.Material, this.first ? 1 : 0);

Expand Down
8 changes: 5 additions & 3 deletions UnLighted/ImageEffects/Visualize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public enum Buffer
GeomertyRoughness,
GeomertySpecular,
Depth,
LightColor,
LightSpecular,
Thickness
LightBufferColor,
LightBufferSpecular,
Thickness,
CombinedNormal,
CombinedDepth
}
}
}
6 changes: 2 additions & 4 deletions UnLighted/MotionBlurObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class MotionBlurObject : MonoBehaviour
private bool moved;
private Material[] materials;

public bool Fixed { get { return this.rigidbody != null; } }

private void Awake()
{
this.mesh = this.GetComponent<MeshFilter>().mesh;
Expand Down Expand Up @@ -42,7 +40,7 @@ public void UpdateTransform(Matrix4x4 vp)
this.MVP = vp * this.transform.localToWorldMatrix;
}

public void RenderVectors(Material material)
public void RenderVector(Material material)
{
if (!this.renderer.isVisible || !this.moved)
{
Expand All @@ -57,7 +55,7 @@ public void RenderVectors(Material material)
{
var type = this.materials[i].GetTag("RenderType", true);

if (type == "Opaque" && material.SetPass(2))
if ((type == "Opaque" || type == "Translucent") && material.SetPass(2))
{
Graphics.DrawMeshNow(this.mesh, matrix, i);
}
Expand Down
14 changes: 6 additions & 8 deletions UnLighted/Resources/Shaders/ImageEffects/MotionBlur.shader
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
float2 a = cur.xy / cur.w;
float2 b = prv.xy / prv.w;

return a - b;
return ((a - b) * 0.5) + 0.5;
}

inline float2 DecodeMotion(float2 motion)
{
return motion;
return (motion * 2.0) - 1.0;
}

ENDCG
Expand Down Expand Up @@ -71,9 +71,7 @@
float4 frag(v2f_img i) : COLOR
{
float2 vel = DecodeMotion(tex2D(_MotionTex, i.uv).xy) * _MotionScale;

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

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

Expand Down Expand Up @@ -128,10 +126,10 @@

float4 frag(v2f i) : COLOR
{
float z = ((i.screen.z / i.screen.w) * 0.5) + 0.5;
float d = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screen));
float a = Linear01Depth(((i.screen.z / i.screen.w) * 0.5) + 0.5);
float b = DecodeFloatRG(tex2Dproj(_CameraDepthNormalsTexture, UNITY_PROJ_COORD(i.screen)).zw);

if ((z - d) > EPSILON)
if ((a - b) > EPSILON)
{
discard;
}
Expand Down
4 changes: 2 additions & 2 deletions UnLighted/Resources/Shaders/ImageEffects/Thickness.shader
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

float4 frag(v2f_img i) : COLOR
{
float d1 = DecodeFloatRGBA(tex2D(_MainTex, i.uv));
float d1 = tex2D(_MainTex, i.uv);
float d2 = DecodeFloatRG(tex2D(_CameraDepthNormalsTexture, i.uv).zw);

return 1.0 - saturate((d1 - d2) * _ProjectionParams.z);
Expand Down Expand Up @@ -70,7 +70,7 @@

float4 frag(v2f i) : COLOR
{
return EncodeFloatRGBA(min(i.depth, 1.0 - EPSILON));
return i.depth;
}

ENDCG
Expand Down
Loading

0 comments on commit 556fe8a

Please sign in to comment.