public class BuiltinSkyParameters
{
public Matrix4x4 viewProjMatrix ;
public Matrix4x4 invViewProjMatrix ;
public Vector3 cameraPosWS ;
public Vector4 screenSize ;
public RenderTargetIdentifier colorBuffer ;
public RenderTargetIdentifier depthBuffer ;
public static RenderTargetIdentifier invalidRTI = - 1 ;
public static RenderTargetIdentifier nullRT = - 1 ;
}
public class SkyManager
if ( m_SkyboxCubemapRT = = null )
{
m_SkyboxCubemapRT = new RenderTexture ( resolution , resolution , 1 , RenderTextureFormat . ARGBHalf ) ;
m_SkyboxCubemapRT = new RenderTexture ( resolution , resolution , 0 , RenderTextureFormat . ARGBHalf , RenderTextureReadWrite . Linear ) ;
m_SkyboxCubemapRT . dimension = TextureDimension . Cube ;
m_SkyboxCubemapRT . useMipMap = true ;
m_SkyboxCubemapRT . autoGenerateMips = true ; // Generate regular mipmap for filtered importance sampling
m_SkyboxGGXCubemapRT = new RenderTexture ( resolution , resolution , 1 , RenderTextureFormat . ARGBHalf ) ;
m_SkyboxGGXCubemapRT = new RenderTexture ( resolution , resolution , 0 , RenderTextureFormat . ARGBHalf , RenderTextureReadWrite . Linear ) ;
m_SkyboxGGXCubemapRT . dimension = TextureDimension . Cube ;
m_SkyboxGGXCubemapRT . useMipMap = true ;
m_SkyboxGGXCubemapRT . autoGenerateMips = false ;
int height = ( int ) LightSamplingParameters . TextureHeight ;
// + 1 because we store the value of the integral of the cubemap at the end of the texture.
m_SkyboxMarginalRowCdfRT = new RenderTexture ( height + 1 , 1 , 1 , RenderTextureFormat . RFloat ) ;
m_SkyboxMarginalRowCdfRT . dimension = TextureDimension . Tex2D ;
m_SkyboxMarginalRowCdfRT = new RenderTexture ( height + 1 , 1 , 0 , RenderTextureFormat . RFloat , RenderTextureReadWrite . Linear ) ;
m_SkyboxMarginalRowCdfRT . useMipMap = false ;
m_SkyboxMarginalRowCdfRT . autoGenerateMips = false ;
m_SkyboxMarginalRowCdfRT . enableRandomWrite = true ;
// TODO: switch the format to R16 (once it's available) to save some bandwidth.
m_SkyboxConditionalCdfRT = new RenderTexture ( width , height , 1 , RenderTextureFormat . RFloat ) ;
m_SkyboxConditionalCdfRT . dimension = TextureDimension . Tex2D ;
m_SkyboxConditionalCdfRT = new RenderTexture ( width , height , 0 , RenderTextureFormat . RFloat , RenderTextureReadWrite . Linear ) ;
m_SkyboxConditionalCdfRT . useMipMap = false ;
m_SkyboxConditionalCdfRT . autoGenerateMips = false ;
m_SkyboxConditionalCdfRT . enableRandomWrite = true ;
m_CubemapScreenSize = new Vector4 ( ( float ) resolution , ( float ) resolution , 1.0f / ( float ) resolution , 1.0f / ( float ) resolution ) ;
}
void RebuildSkyMeshes ( )
void RebuildSkyMeshes ( float nearPlane , float farPlane )
Matrix4x4 cubeProj = Matrix4x4 . Perspective ( 9 0.0f , 1.0f , 0.1f , 1.0f ) ;
Matrix4x4 cubeProj = Matrix4x4 . Perspective ( 9 0.0f , 1.0f , nearPlane , farPlane ) ;
new Vector3 ( 0.0f , 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , - 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , - 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , - 1.0f , 0.0f ) ,
new Vector3 ( 0.0f , - 1.0f , 0.0f ) ,
} ;
for ( int i = 0 ; i < 6 ; + + i )
m_faceCameraInvViewProjectionMatrix [ i ] = m_faceCameraViewProjectionMatrix [ i ] . inverse ;
// When rendering into a texture the render will be flip (due to legacy unity openGL behavior), so we need to flip UV here...
m_CubemapFaceMesh [ i ] = BuildSkyMesh ( Vector3 . zero , m_faceCameraInvViewProjectionMatrix [ i ] , true ) ;
m_CubemapFaceMesh [ i ] = BuildSkyMesh ( Vector3 . zero , m_faceCameraInvViewProjectionMatrix [ i ] , false ) ;
}
}
}
Shader . SetGlobalTexture ( "_SkyTexture" , m_SkyboxGGXCubemapRT ) ;
}
public void Resize ( )
public void Resize ( float nearPlane , float farPlane )
RebuildSkyMeshes ( ) ;
RebuildSkyMeshes ( nearPlane , farPlane ) ;
}
public void Build ( )
{
for ( int i = 0 ; i < 6 ; + + i )
{
Utilities . SetRenderTarget ( builtinParams . renderContext , target , ClearFlag . ClearNone , 0 , ( CubemapFace ) i ) ;
builtinParams . viewProjMatrix = m_faceCameraViewProjectionMatrix [ i ] ;
builtinParams . depthBuffer = BuiltinSkyParameters . invalidRTI ;
builtinParams . depthBuffer = BuiltinSkyParameters . nullRT ;
Utilities . SetRenderTarget ( builtinParams . renderContext , target , ClearFlag . ClearNone , 0 , ( CubemapFace ) i ) ;
m_Renderer . RenderSky ( builtinParams , skyParameters , true ) ;
}
}
if ( m_useMIS )
{
m_iblFilterGgx . FilterCubemapGgxMis ( renderContext , mipCount , input , targe t, m_SkyboxConditionalCdfRT , m_SkyboxMarginalRowCdfRT , m_CubemapFaceMesh ) ;
m_iblFilterGgx . FilterCubemapMIS ( renderContext , input , target , mipCoun t, m_SkyboxConditionalCdfRT , m_SkyboxMarginalRowCdfRT , m_CubemapFaceMesh ) ;
m_iblFilterGgx . FilterCubemapGgx ( renderContext , mipCount , input , targe t, m_CubemapFaceMesh ) ;
m_iblFilterGgx . FilterCubemap ( renderContext , input , target , mipCoun t, m_CubemapFaceMesh ) ;
}
}
}
m_BuiltinParameters . renderContext = renderContext ;
m_BuiltinParameters . sunLight = sunLight ;
m_BuiltinParameters . invViewProjMatrix = camera . invViewProjectionMatrix ;
m_BuiltinParameters . viewProjMatrix = camera . viewProjectionMatrix ;
m_BuiltinParameters . cameraPosWS = camera . camera . transform . position ;
m_BuiltinParameters . screenSize = camera . screenSize ;
m_BuiltinParameters . skyMesh = BuildSkyMesh ( camera . camera . GetComponent < Transform > ( ) . position , m_BuiltinParameters . invViewProjMatrix , false ) ;
Utilities . SetRenderTarget ( renderContext , colorBuffer , depthBuffer ) ;
m_Renderer . SetRenderTargets ( m_BuiltinParameters ) ;
m_Renderer . RenderSky ( m_BuiltinParameters , skyParameters , false ) ;
}
}