using System ;
using System ;
using System.Collections.Generic ;
using UnityEngine ;
using UnityEngine.Rendering ;
public class OnTileDeferredRenderPipeline : RenderPipelineAsset {
#if UNITY_EDITOR
[UnityEditor.MenuItem("RenderPipeline/OnTileDeferredPipeline/Create Pipeline Asset", false, 17 )]
[UnityEditor.MenuItem("Assets/Create/Render Pipeline/On Tile Deferred/Render Pipeline", priority = CoreUtils.assetCreateMenuPriority )]
static void CreateDeferredRenderPipeline ( )
{
var instance = ScriptableObject . CreateInstance < OnTileDeferredRenderPipeline > ( ) ;
[UnityEditor.MenuItem("RenderPipeline/OnTileDeferredPipeline /Material Upgraders /Upgrade Standard Shader Materials")]
[UnityEditor.MenuItem("Edit/ Render Pipeline/On Tile Deferred/Upgrade/Upgrade Standard Shader Materials", priority = CoreUtils.editMenuPriority )]
static void SetupDeferredRenderPipelineMaterials ( )
{
Renderer [ ] _renderers = Component . FindObjectsOfType < Renderer > ( ) ;
if ( _material = = null )
continue ;
if ( _material . shader . name . Contains ( "Standard (Specular setup)" ) ) {
_material . shader = Shader . Find ( "Standard-SRP (Specular setup)" ) ;
} else if ( _material . shader . name . Contains ( "Standard" ) ) {
{
return new OnTileDeferredRenderPipelineInstance ( this ) ;
}
[SerializeField] ShadowSettings m_ShadowSettings = new ShadowSettings ( ) ;
ShadowSetup m_ShadowSetup ;
// TODO: When graphics/renderpass lands, replace code that uses boolean below with SystemInfo.supportsReadOnlyDepth
#if UNITY_EDITOR || UNITY_STANDALONE
static bool s_SupportsReadOnlyDepth = true ;
#else
#else
static bool s_SupportsReadOnlyDepth = false ;
#endif
s_GBufferEmission = new RenderPassAttachment ( RenderTextureFormat . ARGBHalf ) { hideFlags = HideFlags . HideAndDontSave } ;
s_Depth = new RenderPassAttachment ( RenderTextureFormat . Depth ) { hideFlags = HideFlags . HideAndDontSave } ;
s_CameraTarget = s_GBufferAlbedo ;
s_GBufferEmission . Clear ( new Color ( 0.0f , 0.0f , 0.0f , 0.0f ) , 1.0f , 0 ) ;
s_Depth . Clear ( new Color ( ) , 1.0f , 0 ) ;
m_ReflectionNearAndFarClipMaterial . SetInt ( "_DstABlend" , ( int ) BlendMode . Zero ) ;
m_ReflectionNearAndFarClipMaterial . SetInt ( "_CullMode" , ( int ) CullMode . Off ) ;
m_ReflectionNearAndFarClipMaterial . SetInt ( "_CompareFunc" , ( int ) CompareFunction . Always ) ;
m_CookieTexArray = new TextureCache2D ( ) ;
m_CubeCookieTexArray = new TextureCacheCubemap ( ) ;
m_CubeReflTexArray = new TextureCacheCubemap ( ) ;
void ExecuteRenderLoop ( Camera camera , CullResults cullResults , ScriptableRenderContext loop )
{
using ( RenderPass rp = new RenderPass ( loop , camera . pixelWidth , camera . pixelHeight , 1 , s_SupportsReadOnlyDepth ?
using ( RenderPass rp = new RenderPass ( loop , camera . pixelWidth , camera . pixelHeight , 1 , s_SupportsReadOnlyDepth ?
using ( new RenderPass . SubPass ( rp , s_SupportsReadOnlyDepth ?
using ( new RenderPass . SubPass ( rp , s_SupportsReadOnlyDepth ?
new [ ] { s_GBufferAlbedo , s_GBufferSpecRough , s_GBufferNormal , s_GBufferEmission } :
new [ ] { s_GBufferAlbedo , s_GBufferSpecRough , s_GBufferNormal , s_GBufferEmission , s_GBufferRedF32 } , null ) ) {
using ( var cmd = new CommandBuffer { name = "Create G-Buffer" } ) {
loop . ExecuteCommandBuffer ( cmd ) ;
// render opaque objects using Deferred pass
var drawSettings = new DrawRendererSettings ( camera , new ShaderPassName ( "Deferred" ) ) {
sorting = { flags = SortFlags . CommonOpaque } ,
}
//Lighting Pass
using ( new RenderPass . SubPass ( rp , new [ ] { s_GBufferEmission } ,
using ( new RenderPass . SubPass ( rp , new [ ] { s_GBufferEmission } ,
new [ ] { s_GBufferAlbedo , s_GBufferSpecRough , s_GBufferNormal , s_SupportsReadOnlyDepth ? s_Depth : s_GBufferRedF32 } , true ) )
{
using ( var cmd = new CommandBuffer { name = "Deferred Lighting and Reflections Pass" } )
}
}
}
// Utilites
static Matrix4x4 GetFlipMatrix ( )
{
{
return camera . projectionMatrix * GetFlipMatrix ( ) ;
}
Matrix4x4 PerspectiveCotanMatrix ( float cotangent , float zNear , float zFar )
{
float deltaZ = zNear - zFar ;
Matrix4x4 scaled = Matrix4x4 . Scale ( combinedExtent * 2.0f ) ;
mat = mat * Matrix4x4 . Translate ( boxOffset ) * scaled ;
var probeRadius = combinedExtent . magnitude ;
var viewDistance = eyePlane . GetDistanceToPoint ( boxOffset ) ;
bool intersectsNear = viewDistance - probeRadius < = nearDistanceFudged ;
// draw the base probe
// TODO: (cleanup) dont use builtins like unity_SpecCube0
{
{
var props = new MaterialPropertyBlock ( ) ;
props . SetFloat ( "_LightAsQuad" , 1.0f ) ;
Matrix4x4 temp3 = PerspectiveCotanMatrix ( chsa , 0.0f , range ) ;
return temp2 * temp1 * temp3 * worldToLight ;
}
void RenderSpotlight ( VisibleLight light , CommandBuffer cmd , MaterialPropertyBlock properties , bool renderAsQuad , bool intersectsNear , bool deferred )
{
float range = light . range ;
// Setup Spot Rendering mesh matrix
float sideLength = range / chsa ;
// scalingFactor corrosoponds to the scale factor setting (and wether file scale is used) of mesh in Unity mesh inspector.
// A scale factor setting in Unity of 0.01 would require this to be set to 100. A scale factor setting of 1, is just 1 here.
// scalingFactor corrosoponds to the scale factor setting (and wether file scale is used) of mesh in Unity mesh inspector.
// A scale factor setting in Unity of 0.01 would require this to be set to 100. A scale factor setting of 1, is just 1 here.
lightToWorld = lightToWorld * Matrix4x4 . Scale ( new Vector3 ( sideLength * SpotLightMeshScaleFactor , sideLength * SpotLightMeshScaleFactor , range * SpotLightMeshScaleFactor ) ) ;
//set default cookie for spot light if there wasnt one added to the light manually
Vector3 lightPos = light . localToWorld . GetColumn ( 3 ) ; //position
float range = light . range ;
// scalingFactor corrosoponds to the scale factor setting (and wether file scale is used) of mesh in Unity mesh inspector.
// A scale factor setting in Unity of 0.01 would require this to be set to 100. A scale factor setting of 1, is just 1 here.
// scalingFactor corrosoponds to the scale factor setting (and wether file scale is used) of mesh in Unity mesh inspector.
// A scale factor setting in Unity of 0.01 would require this to be set to 100. A scale factor setting of 1, is just 1 here.
if ( cookie ! = null )
if ( cookie ! = null )
else
else
if ( renderAsQuad )
if ( renderAsQuad )
else if ( intersectsNear )
else if ( intersectsNear )
else
else
cmd . DrawMesh ( m_PointLightMesh , matrix , m_FiniteDeferredLightingMaterial , 0 , 0 , properties ) ;
}
void RenderLightsDeferred ( Camera camera , CullResults inputs , CommandBuffer cmd , ScriptableRenderContext loop )
{
int lightCount = inputs . visibleLights . Count ;
for ( int lightNum = 0 ; lightNum < lightCount ; lightNum + + )
for ( int lightNum = 0 ; lightNum < lightCount ; lightNum + + )
{
VisibleLight light = inputs . visibleLights [ lightNum ] ;
Vector3 lightPos = light . localToWorld . GetColumn ( 3 ) ; //position
Vector3 lightDir = light . localToWorld . GetColumn ( 2 ) ; //z axis
float range = light . range ;
float chsa = GetCotanHalfSpotAngle ( light . spotAngle ) ;
// Setup Light Matrix
m_LightMatrix [ i ] = SpotlightMatrix ( light , worldToLight , range , chsa ) ;
m_LightMatrix [ i ] = SpotlightMatrix ( light , worldToLight , range , chsa ) ;
} else if ( light . lightType = = LightType . Directional ) {
m_LightData [ i ] . x = LightDefinitions . DIRECTIONAL_LIGHT ;
var decodeVals = rl . hdr ;
// C is reflection volume center in world space (NOT same as cube map capture point)
var e = bnds . extents ;
var C = mat . MultiplyPoint ( boxOffset ) ;
var e = bnds . extents ;
var C = mat . MultiplyPoint ( boxOffset ) ;
var combinedExtent = e + new Vector3 ( blendDistance , blendDistance , blendDistance ) ;
Vector3 vx = mat . GetColumn ( 0 ) ;
cmd . SetGlobalFloat ( "_useLegacyCookies" , UseLegacyCookies ? 1.0f : 0.0f ) ;
cmd . SetGlobalFloat ( "_transparencyShadows" , TransparencyShadows ? 1.0f : 0.0f ) ;
}
}
}