Skip to content

Commit

Permalink
integrate image effect
Browse files Browse the repository at this point in the history
chengkehan authored Nov 7, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1038a67 commit d83576e
Showing 10 changed files with 154 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Assets/ShadowVolume/Doc/Readme.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ You can bake static-shadow using Shadow Volume.

### Advantage

Generally, using Lightmap to present shadow in a scene. If you need sharp shadow edge, a huge size Lightmap texture must be baked. It will cost mush performance during rendering these Lightmap textures. Sometimes, even more a size of 4096x4096 texture is not enough to present sharp shadow edge. And so many Lightmap textures will break the Static-Batching.
Normally, using Lightmap to present shadow in a scene. If you need sharp shadow edge, a huge size Lightmap texture must be baked. It will cost mush performance during rendering these Lightmap textures. Sometimes, even more a size of 4096x4096 texture is not enough to present sharp shadow edge. And so many Lightmap textures will break the Static-Batching.

In this case, Shadow Volume is a solution. It is particularly suitable for Toon-Rendering-Shadow and Polygon-Style-Shadow.

9 changes: 9 additions & 0 deletions Assets/ShadowVolume/Example/ImageEffect.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/ShadowVolume/Example/ImageEffect/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Shader "Hidden/ShadowVolume/TestImageEffect"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
float4 _MainTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb = saturate((col.rgb - 0.5) * 1.35 + 0.5);
return col;
}
ENDCG
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Assets/ShadowVolume/Example/ImageEffect/TestImageEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
[ImageEffectAllowedInSceneView]
public class TestImageEffect : ShadowVolumeImageEffect
{
private Material _mtrl = null;
private Material Mtrl
{
get
{
if(_mtrl == null)
{
_mtrl = new Material(Shader.Find("Hidden/ShadowVolume/TestImageEffect"));
}
return _mtrl;
}
}

protected virtual void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, Mtrl);
}

private void Update()
{
// Do nothing
}

private void OnDestroy()
{
if (_mtrl != null)
{
DestroyImmediate(_mtrl);
_mtrl = null;
}
}

public override void DrawImageEffect(RenderTexture source, RenderTexture destination)
{
OnRenderImage(source, destination);
}
}
12 changes: 12 additions & 0 deletions Assets/ShadowVolume/Example/ImageEffect/TestImageEffect.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Assets/ShadowVolume/Script/ShadowVolumeCamera.cs
Original file line number Diff line number Diff line change
@@ -699,7 +699,8 @@ private RenderTexture DrawImageEffects(RenderTexture source)
continue;
}

if (mono.enabled)
ShadowVolumeImageEffect imageEffect = imageEffects[i].effect;
if (imageEffect.available)
{
if(src == null)
{
@@ -710,7 +711,6 @@ private RenderTexture DrawImageEffects(RenderTexture source)
dest = RenderTexture.GetTemporary(source.width, source.height, 0, RenderTextureFormat.ARGB32);
}

ShadowVolumeImageEffect imageEffect = imageEffects[i].effect;
imageEffect.DrawImageEffect(src, dest);

RenderTexture temp = src;
@@ -767,6 +767,15 @@ private void CollectImageEffects()
iei.effect = mono as ShadowVolumeImageEffect;
iei.mono = mono;
imageEffects.Add(iei);

if(isRenderTextureComposite)
{
mono.enabled = false;
}
else
{
mono.enabled = iei.effect.available;
}
}
}
}
6 changes: 4 additions & 2 deletions Assets/ShadowVolume/Script/ShadowVolumeImageEffect.cs
Original file line number Diff line number Diff line change
@@ -2,7 +2,9 @@
using System.Collections.Generic;
using UnityEngine;

public interface ShadowVolumeImageEffect
public abstract class ShadowVolumeImageEffect : MonoBehaviour
{
void DrawImageEffect(RenderTexture source, RenderTexture destination);
public bool available = true;

abstract public void DrawImageEffect(RenderTexture source, RenderTexture destination);
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ Shadow Volume for Static-Scene-Object of Unity

### Advantage

Generally, using Lightmap to present shadow in a scene. If you need sharp shadow edge, a huge size Lightmap texture must be baked. It will cost mush performance during rendering these Lightmap textures. Sometimes, even a size of 4096x4096 texture is not enough to present sharp shadow edge. And so many Lightmap textures will break the Static-Batching.
Normally, using Lightmap to present shadow in a scene. If you need sharp shadow edge, a huge size Lightmap texture must be baked. It will cost mush performance during rendering these Lightmap textures. Sometimes, even a size of 4096x4096 texture is not enough to present sharp shadow edge. And so many Lightmap textures will break the Static-Batching.

In this case, Shadow Volume is a solution. It is particularly suitable for Toon-Rendering-Shadow and Polygon-Style-Shadow.

0 comments on commit d83576e

Please sign in to comment.