- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
That day of internet outage is probably one of the happiest days for me using the unity3d engine. On that fateful day Camera.RenderWithShader was born which lived on to become the backbone of almost all of our post processing effects. The best way to explain how this makes a world of difference in our projects is to just give examples of how we handled post processing effects before and after replacement shaders.
Jetpack Brontosa***s
What We Did
We haven’t posted a graphics postmortem for Jetpack Brontosa***s yet. This post will simplify the techniques we used in order to demonstrate how we accomplished the “multiple dimension” effect. Each object in Jetpack Brontosa***s has at least three renderers associated with it. One for the nightmare/death dimension, one for the dream/living dimension, and one for the mask that determines which dimension gets rendered to the screen. The renderers are divided into each dimension by using different layers. We have a different camera for each dimension. One camera is set to render all the renderers in the nightmare dimension, one camera is set for the dream dimension, and one for the mask. These cameras output their renders to separate render textures. Then we combine the two dimensions based on the color in the mask.
The implementation required each of the three renderers to have its own game object, material, and layer. As a result, the complexity of the scenes increased, producing redundant information and human errors.
What We Could Have Done
If we had the ability to use replacement shaders during Jetpack Brontosa***s production, everyone’s life would have been easier. Most of the renderers in Jetpack Brontosa***s used a vertex color shader. Because the renderers shared the same shader, using a replacement shader is an easy change.
An important thing to note here is that all of the tags have the same Key/Value pair.
The dream replacement shader:
Shader “Bronto/Dream Replace” {
SubShader {
Tags {“RenderEffect”=“Multidimensional”}
Pass {
ColorMaterial AmbientAndDiffuse
Lighting Off
SetTexture [_DreamTex] {
Combine texture * primary, primary
}
}
}
}
The death replacement shader:
Shader “Bronto/Death Replace” {
SubShader {
Tags {“RenderEffect”=“Multidimensional”}
Pass {
ColorMaterial AmbientAndDiffuse
Lighting Off
SetTexture [_DeathTex] {
Combine texture * primary, primary
}
}
}
}
The mask replacement shader:
Shader “Bronto/Mask Replace” {
SubShader {
Tags {“RenderEffect”=“Multidimensional”}
Pass {
Lighting Off
Color [_MaskColor]
}
}
}
Now we have one shader that has texture and color information for all of dimensions in a single material. No need for more than one renderer per object anymore!
The shader used by the material for the renderers (this shader is also used for rendering the object to the scene view):
Shader “Bronto/Multidimensional Object” {
Properties {
_DreamTex (“Dream Dimension Texture”, 2D) = “white” {}
_DeathTex (“Death Dimension Texture”, 2D) = “white” {}
_MaskColor (“Mask Color”, Color) = (1,0,0,1) // Alpha used for interpolating between the two textures in the scene view
}
SubShader {
Tags {“RenderEffect”=“Multidimensional”}
Pass {
ColorMaterial AmbientAndDiffuse
Lighting Off
SetTexture [_DreamTex] {
Combine texture
}
SetTexture [_DeathTex] {
constantColor [_MaskColor]
combine previous lerp(constant) texture
}
SetTexture [_DeathTex] {
combine previous * primary
}
}
}
}
For ingame rendering the material shader is never used. The only shaders used are the replacement shaders.
Now we have a script on the scene’s camera that renders the scene with each replacement shader and then composites them. The scene’s camera should be set to render nothing in its culling mask.
#pragma strict
@script ExecuteInEditMode
@script RequireComponent (Camera)
// The culling mask that should be used for rendering
var cullingMask : LayerMask;
// The replacement shaders
var dreamReplacementShader : Shader;
var deathReplacementShader : Shader;
var maskReplacmentShader : Shader;
// The magic composite material
var dimensionCompositeMaterial : Material;
// The render textures for each dimension
private var dreamRT : RenderTexture;
private var deathRT : RenderTexture;
private var maskRT : RenderTexture;
// The camera that renders the replacement shaders (Don’t access this directly, use GetPPCamera())
private var ppCamera:Camera;
/**
* Handle any needed pre processing
*/
function OnPreCull() {
// Start from nothing
CleanRenderTextures();
// Reference to ppCamera’s camera
var cam:Camera = GetPPCamera();
// Set up camera
cam.CopyFrom(this.camera);
cam.cullingMask = this.cullingMask;
cam.clearFlags = CameraClearFlags.Skybox;
cam.backgroundColor = Color(0.0,0.0,0.0,0.0);
// Render Dream Dimension
dreamRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 16);
cam.targetTexture = dreamRT;
cam.RenderWithShader(this.dreamReplacementShader, “RenderEffect”);
// Render Death Dimension
deathRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 16);
cam.targetTexture = deathRT;
cam.RenderWithShader(this.deathReplacementShader, “RenderEffect”);
// Render Death Dimension
maskRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 16);
cam.targetTexture = maskRT;
cam.RenderWithShader(this.maskReplacementShader, “RenderEffect”);
}
/**
* Post Processing magic
* @param source
* @param destination
*/
function OnRenderImage(source:RenderTexture, destination:RenderTexture)
{
// We do nothing with the source render texture, the camera didn’t do anything to it anyway!
// Magically composite the render textures together into the final render
// The shader used in the dimensionCompositeMaterial for compositing these textures is outside the scope of this post
// Will have a post about CG full screen post processing effects sometime in the future
RenderTexture.active = destination;
dimensionCompositeMaterial.SetTexture(“_DreamRender”, dreamRT);
dimensionCompositeMaterial.SetTexture(“_DeathRender”, deathRT);
dimensionCompositeMaterial.SetTexture(“_MaskRender”, maskRT);
GL.PushMatrix ();
GL.LoadOrtho ();
for (var i:int = 0; i < dimensionCompositeMaterial.passCount; i++) {
dimensionCompositeMaterial.SetPass (i);
DrawQuad();
}
GL.PopMatrix ();
// Clean up
CleanRenderTextures();
}
/**
* Cleanup if we get disabled
*/
function OnDisable()
{
CleanResources();
}
/**
* Camera that renders the replacement shaders
* ppCamera getter
* @return
*/
private function GetPPCamera():Camera
{
// Create the shader camera if it doesn’t exist yet
if(!ppCamera) {
ppCamera = new GameObject(“PPCamera”, Camera);
ppCamera.camera.enabled = false;
ppCamera.hideFlags = HideFlags.HideAndDontSave;
}
return ppCamera.camera;
}
/**
* Cleanup all resources used for Post Processing
*/
private function CleanResources()
{
if(ppCamera)
{
DestroyImmediate(ppCamera);
}
CleanRenderTextures();
}
/**
* Cleanup Temporary RenderTexture resources
*/
private function CleanRenderTextures()
{
if(deathRT != null) {
RenderTexture.ReleaseTemporary(deathRT);
deathRT = null;
}
if(dreamRT != null) {
RenderTexture.ReleaseTemporary(dreamRT);
dreamRT = null;
}
if(maskRT != null) {
RenderTexture.ReleaseTemporary(maskRT);
maskRT = null;
}
} |
|