浏览代码

Add Distortion Texture functionality

/LW-DistortionPass
Matt Dean 6 年前
当前提交
35596864
共有 7 个文件被更改,包括 202 次插入0 次删除
  1. 25
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs
  2. 1
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.asset
  3. 1
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.cs
  4. 16
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs
  5. 37
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  6. 112
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightSampling.shader
  7. 10
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightSampling.shader.meta

25
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs


_8x = 8
}
public enum TextureScale
{
Full,
Half,
Quarter
}
public enum DefaultMaterialType
{
Standard = 0,

[SerializeField] private bool m_SupportsVertexLight = false;
[SerializeField] private bool m_RequireDepthTexture = false;
[SerializeField] private bool m_RequireSoftParticles = false;
[SerializeField] private bool m_RequiresDistortionTexture = false;
[SerializeField] private TextureScale m_DistortionTextureScale = TextureScale.Half;
[SerializeField] private bool m_SupportsHDR = false;
[SerializeField] private MSAAQuality m_MSAA = MSAAQuality._4x;
[SerializeField] private float m_RenderScale = 1.0f;

get { return m_RequireSoftParticles; }
}
public bool RequireDistortionTexture
{
get { return m_RequiresDistortionTexture; }
}
public TextureScale DistortionTextureScale
{
get { return m_DistortionTextureScale; }
set { m_DistortionTextureScale = value; }
}
public bool SupportsHDR
{
get { return m_SupportsHDR; }

public Shader ScreenSpaceShadowShader
{
get { return resources != null ? resources.ScreenSpaceShadowShader : null; }
}
public Shader SamplingShader
{
get { return resources != null ? resources.SamplingShader : null; }
}
}
}

1
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.asset


CopyDepthShader: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3}
ScreenSpaceShadowShader: {fileID: 4800000, guid: 0f854b35a0cf61a429bd5dcfea30eddd,
type: 3}
SamplingShader: {fileID: 4800000, guid: 04c410c9937594faa893a11dceb85f7e, type: 3}

1
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.cs


public Shader BlitShader;
public Shader CopyDepthShader;
public Shader ScreenSpaceShadowShader;
public Shader SamplingShader;
}

16
ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/LightweightAssetEditor.cs


public static GUIContent requireSoftParticles = new GUIContent("Soft Particles", "If enabled the pipeline will enable SOFT_PARTICLES keyword.");
public static GUIContent requireDistortionTexture = new GUIContent("Distortion Texture", "If enabled the pipeline will copy the screen to texture after opaque objects are drawn. For transparent objects this can be bound in shaders as _CameraDistortionTexture.");
public static GUIContent distortionTextureScale = new GUIContent("Distortion Scale", "The scale of the original screen size that is used for the distortion texture");
public static GUIContent shadowType = new GUIContent("Type",
"Global shadow settings. Options are NO_SHADOW, HARD_SHADOWS and SOFT_SHADOWS.");

}
AnimBool m_ShowSoftParticles = new AnimBool();
AnimBool m_ShowDistortionTextureScale = new AnimBool();
private int kMaxSupportedPixelLights = 8;
private float kMinRenderScale = 0.1f;

private SerializedProperty m_SupportsVertexLightProp;
private SerializedProperty m_RequireDepthTextureProp;
private SerializedProperty m_RequireSoftParticlesProp;
private SerializedProperty m_RequireDistortionTextureProp;
private SerializedProperty m_DistortionTextureScaleProp;
private SerializedProperty m_ShadowTypeProp;
private SerializedProperty m_ShadowNearPlaneOffsetProp;
private SerializedProperty m_ShadowDistanceProp;

m_SupportsVertexLightProp = serializedObject.FindProperty("m_SupportsVertexLight");
m_RequireDepthTextureProp = serializedObject.FindProperty("m_RequireDepthTexture");
m_RequireSoftParticlesProp = serializedObject.FindProperty("m_RequireSoftParticles");
m_RequireDistortionTextureProp = serializedObject.FindProperty("m_RequiresDistortionTexture");
m_DistortionTextureScaleProp = serializedObject.FindProperty("m_DistortionTextureScale");
m_ShadowTypeProp = serializedObject.FindProperty("m_ShadowType");
m_ShadowNearPlaneOffsetProp = serializedObject.FindProperty("m_ShadowNearPlaneOffset");
m_ShadowDistanceProp = serializedObject.FindProperty("m_ShadowDistance");

m_ShowSoftParticles.valueChanged.AddListener(Repaint);
m_ShowSoftParticles.value = m_RequireSoftParticlesProp.boolValue;
m_ShowDistortionTextureScale.valueChanged.AddListener(Repaint);
m_ShowDistortionTextureScale.value = m_DistortionTextureScaleProp.boolValue;
m_ShowDistortionTextureScale.valueChanged.RemoveListener(Repaint);
m_ShowDistortionTextureScale.target = m_RequireDistortionTextureProp.boolValue;
}
void DrawAnimatedProperty(SerializedProperty prop, GUIContent content, AnimBool animation)

EditorGUILayout.PropertyField(m_SupportsVertexLightProp, Styles.enableVertexLightLabel);
EditorGUILayout.PropertyField(m_RequireDepthTextureProp, Styles.requireDepthTexture);
DrawAnimatedProperty(m_RequireSoftParticlesProp, Styles.requireSoftParticles, m_ShowSoftParticles);
EditorGUILayout.PropertyField(m_RequireDistortionTextureProp, Styles.requireDistortionTexture);
DrawAnimatedProperty(m_DistortionTextureScaleProp, Styles.distortionTextureScale, m_ShowDistortionTextureScale);
EditorGUILayout.PropertyField(m_HDR, Styles.hdrContent);
EditorGUILayout.PropertyField(m_MSAA, Styles.msaaContent);

37
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


// If soft particles are enabled and no depth prepass is performed we need to copy depth.
public static int depthCopy;
// Camera distortion target. Used for accessing screen contents during transparent pass.
public static int distortion;
}
public class LightweightPipeline : RenderPipeline

private RenderTargetIdentifier m_DepthRT;
private RenderTargetIdentifier m_CopyDepth;
private RenderTargetIdentifier m_Color;
private RenderTargetIdentifier m_DistortionRT;
private bool m_IntermediateTextureArray;
private bool m_RequireDepthTexture;

private Material m_CopyDepthMaterial;
private Material m_ErrorMaterial;
private Material m_ScreenSpaceShadowsMaterial;
private Material m_SamplingMaterial;
private int m_BlitTexID = Shader.PropertyToID("_BlitTex");
private CopyTextureSupport m_CopyTextureSupport;

CameraRenderTargetID.copyColor = Shader.PropertyToID("_CameraCopyColorRT");
CameraRenderTargetID.depth = Shader.PropertyToID("_CameraDepthTexture");
CameraRenderTargetID.depthCopy = Shader.PropertyToID("_CameraCopyDepthTexture");
CameraRenderTargetID.distortion = Shader.PropertyToID("_CameraDistortionTexture");
m_ShadowMapRT = new RenderTargetIdentifier(m_ShadowMapRTID);
m_ScreenSpaceShadowMapRT = new RenderTargetIdentifier(m_ScreenSpaceShadowMapRTID);

m_DepthRT = new RenderTargetIdentifier(CameraRenderTargetID.depth);
m_CopyDepth = new RenderTargetIdentifier(CameraRenderTargetID.depthCopy);
m_DistortionRT = new RenderTargetIdentifier(CameraRenderTargetID.distortion);
m_PostProcessRenderContext = new PostProcessRenderContext();
m_CopyTextureSupport = SystemInfo.copyTextureSupport;

m_BlitMaterial = CoreUtils.CreateEngineMaterial(m_Asset.BlitShader);
m_CopyDepthMaterial = CoreUtils.CreateEngineMaterial(m_Asset.CopyDepthShader);
m_ScreenSpaceShadowsMaterial = CoreUtils.CreateEngineMaterial(m_Asset.ScreenSpaceShadowShader);
m_SamplingMaterial = CoreUtils.CreateEngineMaterial(m_Asset.SamplingShader);
m_ErrorMaterial = CoreUtils.CreateEngineMaterial("Hidden/InternalErrorShader");
}

CoreUtils.Destroy(m_ErrorMaterial);
CoreUtils.Destroy(m_CopyDepthMaterial);
CoreUtils.Destroy(m_BlitMaterial);
CoreUtils.Destroy(m_SamplingMaterial);
#if UNITY_EDITOR
SceneViewDrawMode.ResetDrawMode();

StopStereoRendering(ref context, frameRenderingConfiguration);
}
private void DistortionPass(ref ScriptableRenderContext context)
{
CommandBuffer cmd = CommandBufferPool.Get("Distortion");
switch(m_Asset.DistortionTextureScale)
{
case TextureScale.Half:
cmd.GetTemporaryRT(CameraRenderTargetID.distortion, m_CurrCamera.pixelWidth / 2, m_CurrCamera.pixelHeight / 2, 0, FilterMode.Bilinear, m_ColorFormat);
cmd.Blit(m_CurrCameraColorRT, CameraRenderTargetID.distortion, m_SamplingMaterial, 0);
break;
case TextureScale.Quarter:
cmd.GetTemporaryRT(CameraRenderTargetID.distortion, m_CurrCamera.pixelWidth / 4, m_CurrCamera.pixelHeight / 4, 0, FilterMode.Bilinear, m_ColorFormat);
cmd.Blit(m_CurrCameraColorRT, CameraRenderTargetID.distortion, m_SamplingMaterial, 1);
break;
default:
cmd.GetTemporaryRT(CameraRenderTargetID.distortion, m_CurrCamera.pixelWidth, m_CurrCamera.pixelHeight, 0, FilterMode.Bilinear, m_ColorFormat);
cmd.Blit(m_CurrCameraColorRT, CameraRenderTargetID.distortion);
break;
}
SetRenderTarget(cmd, m_CurrCameraColorRT, m_DepthRT);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
private void ForwardPass(List<VisibleLight> visibleLights, FrameRenderingConfiguration frameRenderingConfiguration, ref ScriptableRenderContext context, ref LightData lightData, bool stereoEnabled)
{
SetupShaderConstants(visibleLights, ref context, ref lightData);

SetRenderTarget(cmd, m_CurrCameraColorRT, depthRT);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
if(m_Asset.RequireDistortionTexture)
{
DistortionPass(ref context);
}
}
private void RenderTransparents(ref ScriptableRenderContext context, RendererConfiguration config)

112
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightSampling.shader


Shader "Hidden/LightweightPipeline/Sampling"
{
Properties
{
_MainTex("Albedo", 2D) = "white" {}
}
HLSLINCLUDE
#include "../ShaderLibrary/Core.hlsl"
struct VertexInput
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct Interpolators
{
half4 pos : SV_POSITION;
half4 texcoord : TEXCOORD0;
};
Interpolators Vertex(VertexInput i)
{
Interpolators o;
UNITY_SETUP_INSTANCE_ID(i);
UNITY_TRANSFER_INSTANCE_ID(i, o);
o.pos = TransformObjectToHClip(i.vertex.xyz);
float4 projPos = o.pos * 0.5;
projPos.xy = projPos.xy + projPos.w;
o.texcoord.xy = i.texcoord;
o.texcoord.zw = projPos.xy;
return o;
}
half4 DownsampleBox4Tap(Texture2D tex, SamplerState samplerTex, float2 uv, float2 texelSize, float amount)
{
float4 d = texelSize.xyxy * float4(-amount, -amount, amount, amount);
half4 s;
s = (SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.xy));
s += (SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.zy));
s += (SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.xw));
s += (SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.zw));
return s * (1.0 / 4.0);
}
ENDHLSL
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "LightweightPipeline"}
LOD 100
// 0 - Downsample - Box filtering
Pass
{
Tags { "LightMode" = "LightweightForward"}
ZTest Always
ZWrite Off
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma vertex Vertex
#pragma fragment FragBoxDownsample
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
float4 _MainTex_TexelSize;
half4 FragBoxDownsample(Interpolators i) : SV_Target
{
half4 col = DownsampleBox4Tap(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, _MainTex_TexelSize.xy, 1.0);
return half4(col.rgb, 1);
}
ENDHLSL
}
// 1 - 2x Downsample - Box filtering
Pass
{
Tags { "LightMode" = "LightweightForward"}
ZTest Always
ZWrite Off
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma vertex Vertex
#pragma fragment FragBoxDownsample
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
float4 _MainTex_TexelSize;
half4 FragBoxDownsample(Interpolators i) : SV_Target
{
half4 col = DownsampleBox4Tap(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, _MainTex_TexelSize.xy, 2.0);
return half4(col.rgb, 1);
}
ENDHLSL
}
}
}

10
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightSampling.shader.meta


fileFormatVersion: 2
guid: 04c410c9937594faa893a11dceb85f7e
timeCreated: 1505729520
licenseType: Pro
ShaderImporter:
externalObjects: {}
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存