浏览代码

Pseudo Flares working

/feature-custom-postprocessing
André McGrail 4 年前
当前提交
ff8ee98f
共有 12 个文件被更改,包括 1463 次插入46 次删除
  1. 42
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoFlareVolume.cs
  2. 94
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoLensflareFeature.cs
  3. 150
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PsudoLenflare.shader
  4. 4
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PsudoLenflare.shader.meta
  5. 8
      Assets/Scripts/_Test.meta
  6. 8
      Assets/Scripts/_Test/CustomPP.meta
  7. 1001
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/10.jpg
  8. 106
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/10.jpg.meta
  9. 32
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/Hidden_PostFX_PseudoLensFlare.mat
  10. 8
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/Hidden_PostFX_PseudoLensFlare.mat.meta
  11. 48
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoFlare.asset
  12. 8
      Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoFlare.asset.meta

42
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoFlareVolume.cs


using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
[Range(0f, 1f), Tooltip("Grayscale effect intensity")]
public FloatParameter blend = new FloatParameter(0.5f);
public BoolParameter enabled = new BoolParameter(false);
[Range(-10f, 10f), Tooltip("Grayscale effect intensity")]
public FloatParameter offset = new FloatParameter(0.0f);
[Range(0f, 10f), Tooltip("Grayscale effect intensity")]
public FloatParameter power = new FloatParameter(1.0f);
public FloatParameter ghostSpacing = new FloatParameter(0.25f);
public IntParameter ghostCount = new IntParameter(3);
public FloatParameter haloWidth = new FloatParameter(1.0f);
protected override void OnEnable()
{
base.OnEnable();
//RenderPipelineManager.beginCameraRendering += Inject;
}
protected override void OnDisable()
{
base.OnDisable();
//RenderPipelineManager.beginCameraRendering -= Inject;
}
private void Inject(ScriptableRenderContext context, Camera camera)
{
PseudoFlareVolume component = VolumeManager.instance.stack.GetComponent(typeof(PseudoFlareVolume)) as PseudoFlareVolume;
if (component == null || !component.enabled.value)
{
Debug.Log($"no volume component in camera {camera.name}");
return;
}
var renderer = camera.GetUniversalAdditionalCameraData().scriptableRenderer;
Debug.Log($"effect volume {renderer}");
PseudoLensflareFeature.InjectPass(renderer, new PseudoLensflareFeature.PseudoLensflarePass(new Material(Shader.Find("Hidden/PostFX/PseudoLensFlare"))));
}
}

94
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoLensflareFeature.cs


public class PseudoLensflareFeature : ScriptableRendererFeature
{
class PseudoLensflarePass : ScriptableRenderPass
public class PseudoLensflarePass : ScriptableRenderPass
private Material samplingMaterial;
RenderTargetHandle m_TmpRT1;
RenderTargetHandle m_TmpRT2;
renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
samplingMaterial = CoreUtils.CreateEngineMaterial("Hidden/Universal Render Pipeline/Sampling");
renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
var width = cameraTextureDescriptor.width / 4;
var height = cameraTextureDescriptor.height / 4;
m_TmpRT1.Init("tempRT1");
m_TmpRT2.Init("tempRT2");
cmd.GetTemporaryRT(m_TmpRT1.id, width, height, 0, FilterMode.Bilinear, cameraTextureDescriptor.graphicsFormat);
cmd.GetTemporaryRT(m_TmpRT2.id, width, height, 0, FilterMode.Bilinear, cameraTextureDescriptor.graphicsFormat);
}
public void Setup(RenderTargetIdentifier source, RenderTargetHandle destination, PseudoFlareVolume component)
{
this.component = component;

blitMaterial.SetFloat(Blend, component.blend.value);
blitMaterial.SetFloat(Offset, component.offset.value);
blitMaterial.SetFloat(Power, component.power.value);
blitMaterial.SetFloat(GhostSpacing, component.ghostSpacing.value);
blitMaterial.SetFloat(GhostCount, component.ghostCount.value);
blitMaterial.SetFloat(HaloWidth, component.haloWidth.value);
RenderTextureDescriptor opaqueDesc = renderingData.cameraData.cameraTargetDescriptor;
opaqueDesc.depthBufferBits = 0;
// first pass
cmd.SetGlobalFloat(BlurOffset, 1.5f);
cmd.Blit(source, m_TmpRT1.Identifier(), blitMaterial, 1);
Blit(cmd, source, destination.Identifier(), blitMaterial);
const int passes = 5;
for (var i=1; i<passes-1; i++) {
cmd.SetGlobalFloat(BlurOffset, 0.5f + i);
cmd.Blit(m_TmpRT1.Identifier(), m_TmpRT2.Identifier(), blitMaterial, 1);
// pingpong
var rttmp = m_TmpRT1;
m_TmpRT1 = m_TmpRT2;
m_TmpRT2 = rttmp;
}
// final pass
cmd.SetGlobalFloat(BlurOffset, 0.5f + passes - 1f);
Blit(cmd, m_TmpRT1.Identifier(), source, blitMaterial);
public override void OnCameraCleanup(CommandBuffer cmd)
{
base.OnCameraCleanup(cmd);
CoreUtils.Destroy(samplingMaterial);
}
private static readonly int Blend = Shader.PropertyToID("_Blend");
private static readonly int Offset = Shader.PropertyToID("_Offset");
private static readonly int Power = Shader.PropertyToID("_Power");
private static readonly int GhostSpacing = Shader.PropertyToID("_GhostSpacing");
private static readonly int GhostCount = Shader.PropertyToID("_GhostCount");
private static readonly int HaloWidth = Shader.PropertyToID("_HaloWidth");
private static readonly int BlurOffset = Shader.PropertyToID("_BlurOffset");
public override void Create()
{

public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
InjectPass(renderer, _blitPass);
}
public static void InjectPass(ScriptableRenderer renderer, PseudoLensflarePass pass)
{
if (component == null || !component.active) return;
if (blitMaterial == null)
if (component == null || !component.enabled.value) return;
if (pass.blitMaterial == null)
Debug.LogWarningFormat("Missing Blit Material. {0} blit pass will not execute. Check for missing reference in the assigned renderer.", GetType().Name);
Debug.LogWarningFormat("Missing Blit Material. blit pass will not execute. Check for missing reference in the assigned renderer.");
_blitPass.Setup(src, dest, component);
renderer.EnqueuePass(_blitPass);
pass.Setup(src, dest, component);
renderer.EnqueuePass(pass);
}
}

150
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PsudoLenflare.shader


Properties
{
_MainTex ("Texture", 2D) = "white" {}
_StarTexture ("StarTex", 2D) = "gray" {}
float _Blend;
TEXTURE2D(_StarTexture); SAMPLER(sampler_StarTexture);
#define SampleScene(uv) SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv)
#define Half2Half half2(0.5, 0.5)
float4 _MainTex_TexelSize;
float _Power;
float _Offset;
float _GhostSpacing;
const uint _GhostCount;
float _HaloWidth;
float4 Frag(Varyings i) : SV_Target
Varyings VertDefault(Attributes v)
float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
float luminance = dot(color.rgb, float3(0.2126729, 0.7151522, 0.0721750));
color.rgb = lerp(color.rgb, luminance.xxx, _Blend.xxx);
return color;
Varyings o;
o.vertex = float4(v.vertex.xy * 2.0 - 1.0, 0.0, 1.0);
o.vertex.y = -o.vertex.y;
o.texcoord = v.vertex.xy;
return o;
Cull Off ZWrite Off ZTest Always
Cull Off
ZWrite Off
ZTest Always
Name "PseudoFlare"
Blend One One
Varyings VertDefault(Attributes v)
half3 Threshold(float3 color, float offset, float power)
{
//return color;
return max(half3(0.0, 0.0, 0.0), color + offset) * power;
}
half3 Threshold(float3 color)
{
//return color;
return Threshold(color, _Offset, _Power);
}
half3 ChromaticSample(float2 uv, float2 direction, half3 distortion)
Varyings o;
o.vertex = float4(v.vertex.xy * 2.0 - 1.0, 0.0, 1.0);
o.texcoord = v.vertex.xy;
return o;
return half3(
SampleScene(uv + direction * distortion.r).r,
SampleScene(uv + direction * distortion.g).g,
SampleScene(uv + direction * distortion.b).b
);
half3 GenerateFeatures(float2 uv)
{
uv = 1.0 - uv;
half2 texelSize = _MainTex_TexelSize.xy;
half2 ghostVec = (Half2Half - uv) * _GhostSpacing;
half3 result = 0.0;
for (int i = 0; i < _GhostCount; ++i)
{
half2 offset = frac(uv + ghostVec * float(i));
float weight = length(Half2Half - offset) / length(Half2Half);
weight = pow(1.0 - weight, 5.0);
result += Threshold(SampleScene(offset).xyz) * weight;
}
half star = SAMPLE_TEXTURE2D(_StarTexture, sampler_StarTexture, uv);
half uDistortion = 3.0;
half3 distortion = half3(-texelSize.x * uDistortion, 0.0, texelSize.x * uDistortion);
half2 direction = normalize(ghostVec);
half v = _ScreenParams.y / _ScreenParams.x;
uv.y = (uv.y - 0.5) * v + 0.5;
ghostVec.y = ghostVec.y * v;
float2 haloVec = normalize(ghostVec) * _HaloWidth;
float weight = length(Half2Half - frac(uv + haloVec)) * 2;
weight = pow(1.0 - weight, 5.0) * star;
result += Threshold(ChromaticSample(uv + haloVec, direction, distortion), _Offset, 2 * _Power) * weight;
return result;
}
float4 Frag(Varyings i) : SV_Target
{
half3 color = GenerateFeatures(i.texcoord);
return half4(color, 1.0);
}
ENDHLSL
}
Pass
{
Name "DownscaleBlur"
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment Frag
float _BlurOffset;
half3 Blur(TEXTURE2D_PARAM(tex, samplerTex), float2 uv, float2 texelSize, float offset)
{
float i = offset;
half3 col;
col.rgb = SAMPLE_TEXTURE2D( tex, samplerTex, uv ).rgb;
col.rgb += SAMPLE_TEXTURE2D( tex, samplerTex, uv + float2( i, i ) * texelSize ).rgb;
col.rgb += SAMPLE_TEXTURE2D( tex, samplerTex, uv + float2( i, -i ) * texelSize ).rgb;
col.rgb += SAMPLE_TEXTURE2D( tex, samplerTex, uv + float2( -i, i ) * texelSize ).rgb;
col.rgb += SAMPLE_TEXTURE2D( tex, samplerTex, uv + float2( -i, -i ) * texelSize ).rgb;
col.rgb /= 5.0f;
return col;
}
float4 Frag(Varyings i) : SV_Target
{
half3 color = Blur(_MainTex, sampler_MainTex, i.texcoord, _MainTex_TexelSize.xy, _BlurOffset);
return half4(color, 1.0);
}
ENDHLSL
}
}

4
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PsudoLenflare.shader.meta


guid: 933b5941d4ae5084a92ee525b3fe5d28
ShaderImporter:
externalObjects: {}
defaultTextures: []
defaultTextures:
- _MainTex: {instanceID: 0}
- _StarTexture: {fileID: 2800000, guid: c84063caa820d4d3d841494090dd8052, type: 3}
nonModifiableTextures: []
userData:
assetBundleName:

8
Assets/Scripts/_Test.meta


fileFormatVersion: 2
guid: 54f6c326c0c534bc6a69775d6981fcc9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Assets/Scripts/_Test/CustomPP.meta


fileFormatVersion: 2
guid: debc907df84854b5180ea4d11942e90f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1001
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/10.jpg
文件差异内容过多而无法显示
查看文件

106
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/10.jpg.meta


fileFormatVersion: 2
guid: c84063caa820d4d3d841494090dd8052
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 0
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 2
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 10
textureShape: 1
singleChannelComponent: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 2
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 2
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

32
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/Hidden_PostFX_PseudoLensFlare.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Hidden_PostFX_PseudoLensFlare
m_Shader: {fileID: 4800000, guid: 933b5941d4ae5084a92ee525b3fe5d28, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _StarTexture:
m_Texture: {fileID: 2800000, guid: c84063caa820d4d3d841494090dd8052, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
m_Colors: []
m_BuildTextureStacks: []

8
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/Hidden_PostFX_PseudoLensFlare.mat.meta


fileFormatVersion: 2
guid: 34a190576a5724c6cbdbfe682e2f3671
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

48
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoFlare.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d7fd9488000d3734a9e00ee676215985, type: 3}
m_Name: PseudoFlare
m_EditorClassIdentifier:
components:
- {fileID: 8989136020346695368}
--- !u!114 &8989136020346695368
MonoBehaviour:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 89201a0f12f0ed4479dc17dd1184d946, type: 3}
m_Name: PseudoFlareVolume
m_EditorClassIdentifier:
active: 1
m_AdvancedMode: 0
enabled:
m_OverrideState: 1
m_Value: 1
offset:
m_OverrideState: 1
m_Value: -1.51
power:
m_OverrideState: 1
m_Value: 0.76
ghostSpacing:
m_OverrideState: 1
m_Value: 0.57
ghostCount:
m_OverrideState: 1
m_Value: 4
haloWidth:
m_OverrideState: 1
m_Value: 0.45

8
Assets/Scripts/_Test/CustomPP/PsudoLensFlare/PseudoFlare.asset.meta


fileFormatVersion: 2
guid: f979669a5f9bf4a4db7e28cc61423f02
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存