André McGrail
6 年前
当前提交
5b3f36b7
共有 6 个文件被更改,包括 983 次插入 和 0 次删除
-
29Assets/Scripts/BasicAO.cs
-
11Assets/Scripts/BasicAO.cs.meta
-
113Assets/Shaders/BasicAO.shader
-
9Assets/Shaders/BasicAO.shader.meta
-
698Assets/Textures/LDR_LLL1_0.png
-
123Assets/Textures/LDR_LLL1_0.png.meta
|
|||
using System; |
|||
using UnityEngine; |
|||
using UnityEngine.Rendering.PostProcessing; |
|||
|
|||
[Serializable] |
|||
[PostProcess(typeof(BasicAORenderer), PostProcessEvent.BeforeStack, "Custom/BasicAO")] |
|||
public sealed class BasicAO : PostProcessEffectSettings |
|||
{ |
|||
[Range(0f, 1f), Tooltip("Effect intensity.")] |
|||
public FloatParameter intensity = new FloatParameter { value = 1.0f }; |
|||
|
|||
public TextureParameter noiseTexture = new TextureParameter{ value = null}; |
|||
} |
|||
|
|||
public sealed class BasicAORenderer : PostProcessEffectRenderer<BasicAO> |
|||
{ |
|||
public override void Render(PostProcessRenderContext context) |
|||
{ |
|||
var sheet = context.propertySheets.Get(Shader.Find("Hidden/Post/BasicAO")); |
|||
|
|||
var noiseTexture = settings.noiseTexture.value == null |
|||
? RuntimeUtilities.whiteTexture |
|||
: settings.noiseTexture.value; |
|||
|
|||
sheet.properties.SetTexture("_NoiseTex", noiseTexture); |
|||
sheet.properties.SetFloat("_Intensity", settings.intensity); |
|||
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 96e0849e4b2f8b549aa4dcdd04b0fe56 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
Shader "Hidden/Post/BasicAO" |
|||
{ |
|||
HLSLINCLUDE |
|||
//#include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl" |
|||
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl" |
|||
|
|||
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex); |
|||
TEXTURE2D_SAMPLER2D(_NoiseTex, sampler_NoiseTex); |
|||
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture); |
|||
|
|||
float _Blend; |
|||
float _Intensity; |
|||
|
|||
float3 normal_from_depth(float depth, float2 texcoords) |
|||
{ |
|||
const float2 offset1 = float2(0.0,0.001); |
|||
const float2 offset2 = float2(0.001,0.0); |
|||
|
|||
float depth1 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, texcoords + offset1).r); |
|||
float depth2 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, texcoords + offset2).r); |
|||
|
|||
float3 p1 = float3(offset1, depth1 - depth); |
|||
float3 p2 = float3(offset2, depth2 - depth); |
|||
|
|||
float3 normal = cross(p1, p2); |
|||
normal.z = -normal.z; |
|||
|
|||
return normalize(normal); |
|||
} |
|||
|
|||
float4 Frag(VaryingsDefault i) : SV_Target |
|||
{ |
|||
float side = round(1-i.texcoord.x); |
|||
// |
|||
// float3 d1 = Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord).rrr) * (1 - side); |
|||
// float3 d2 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord).rrr) * side; |
|||
// d2 /= 10000; |
|||
// |
|||
// d1.x = frac(d1.x); |
|||
// d1.z = frac(d1.z * 10); |
|||
// d2.x = frac(d2.x); |
|||
// d2.z = frac(d2.z * 10); |
|||
// |
|||
// return float4(d1 + d2, 1); |
|||
|
|||
/*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;*/ |
|||
float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord) * (1 - side); |
|||
|
|||
const float base = 0.1; |
|||
|
|||
const float area = 50; |
|||
const float falloff = 0.5; |
|||
|
|||
const float radius = 2; |
|||
|
|||
const int samples = 16; |
|||
float3 sample_sphere[samples] = { |
|||
float3( 0.5381, 0.1856,-0.4319), float3( 0.1379, 0.2486, 0.4430), |
|||
float3( 0.3371, 0.5679,-0.0057), float3(-0.6999,-0.0451,-0.0019), |
|||
float3( 0.0689,-0.1598,-0.8547), float3( 0.0560, 0.0069,-0.1843), |
|||
float3(-0.0146, 0.1402, 0.0762), float3( 0.0100,-0.1924,-0.0344), |
|||
float3(-0.3577,-0.5301,-0.4358), float3(-0.3169, 0.1063, 0.0158), |
|||
float3( 0.0103,-0.5869, 0.0046), float3(-0.0897,-0.4940, 0.3287), |
|||
float3( 0.7119,-0.0154,-0.0918), float3(-0.0533, 0.0596,-0.5411), |
|||
float3( 0.0352,-0.0631, 0.5460), float3(-0.4776, 0.2847,-0.0271) |
|||
}; |
|||
|
|||
float3 random = SAMPLE_TEXTURE2D(_NoiseTex, sampler_NoiseTex, i.texcoord * 6.0).rgb; |
|||
|
|||
float depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord).r); |
|||
|
|||
float3 position = float3(i.texcoord, depth); |
|||
float3 normal = normal_from_depth(depth, i.texcoord); |
|||
|
|||
float radius_depth = radius/depth; |
|||
float occlusion = 0.0; |
|||
|
|||
for(int i=0; i < samples; i++) { |
|||
|
|||
float3 ray = radius_depth * reflect(sample_sphere[i], random); |
|||
float3 hemi_ray = position + sign(dot(ray,normal)) * ray; |
|||
|
|||
float occ_depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, saturate(hemi_ray.xy)).r); |
|||
float difference = depth - occ_depth; |
|||
|
|||
occlusion += step(falloff, difference) * (1.0-smoothstep(falloff, area, difference)); |
|||
} |
|||
|
|||
float ao = 1.0 - _Intensity * occlusion * (1.0 / samples); |
|||
|
|||
return saturate(ao + base) * (color + side); |
|||
} |
|||
|
|||
ENDHLSL |
|||
|
|||
SubShader |
|||
{ |
|||
Cull Off ZWrite Off ZTest Always |
|||
|
|||
Pass |
|||
{ |
|||
HLSLPROGRAM |
|||
|
|||
#pragma vertex VertDefault |
|||
#pragma fragment Frag |
|||
|
|||
ENDHLSL |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 95be7c43c392a2940a10526dd489b7c2 |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 7247454f970266b4c8d99e1f01e15296 |
|||
TextureImporter: |
|||
internalIDToNameTable: [] |
|||
externalObjects: {} |
|||
serializedVersion: 9 |
|||
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 |
|||
grayScaleToAlpha: 0 |
|||
generateCubemap: 6 |
|||
cubemapConvolution: 0 |
|||
seamlessCubemap: 0 |
|||
textureFormat: 1 |
|||
maxTextureSize: 2048 |
|||
textureSettings: |
|||
serializedVersion: 2 |
|||
filterMode: -1 |
|||
aniso: -1 |
|||
mipBias: -100 |
|||
wrapU: 0 |
|||
wrapV: 0 |
|||
wrapW: 0 |
|||
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: 0 |
|||
alphaIsTransparency: 0 |
|||
spriteTessellationDetail: -1 |
|||
textureType: 0 |
|||
textureShape: 1 |
|||
singleChannelComponent: 1 |
|||
maxTextureSizeSet: 0 |
|||
compressionQualitySet: 0 |
|||
textureFormatSet: 0 |
|||
platformSettings: |
|||
- serializedVersion: 2 |
|||
buildTarget: DefaultTexturePlatform |
|||
maxTextureSize: 256 |
|||
resizeAlgorithm: 0 |
|||
textureFormat: -1 |
|||
textureCompression: 0 |
|||
compressionQuality: 50 |
|||
crunchedCompression: 0 |
|||
allowsAlphaSplitting: 0 |
|||
overridden: 0 |
|||
androidETC2FallbackOverride: 0 |
|||
- serializedVersion: 2 |
|||
buildTarget: Standalone |
|||
maxTextureSize: 256 |
|||
resizeAlgorithm: 0 |
|||
textureFormat: -1 |
|||
textureCompression: 0 |
|||
compressionQuality: 50 |
|||
crunchedCompression: 0 |
|||
allowsAlphaSplitting: 0 |
|||
overridden: 0 |
|||
androidETC2FallbackOverride: 0 |
|||
- serializedVersion: 2 |
|||
buildTarget: Android |
|||
maxTextureSize: 256 |
|||
resizeAlgorithm: 0 |
|||
textureFormat: -1 |
|||
textureCompression: 0 |
|||
compressionQuality: 50 |
|||
crunchedCompression: 0 |
|||
allowsAlphaSplitting: 0 |
|||
overridden: 0 |
|||
androidETC2FallbackOverride: 0 |
|||
- serializedVersion: 2 |
|||
buildTarget: WebGL |
|||
maxTextureSize: 256 |
|||
resizeAlgorithm: 0 |
|||
textureFormat: -1 |
|||
textureCompression: 0 |
|||
compressionQuality: 50 |
|||
crunchedCompression: 0 |
|||
allowsAlphaSplitting: 0 |
|||
overridden: 0 |
|||
androidETC2FallbackOverride: 0 |
|||
spriteSheet: |
|||
serializedVersion: 2 |
|||
sprites: [] |
|||
outline: [] |
|||
physicsShape: [] |
|||
bones: [] |
|||
spriteID: |
|||
internalID: 0 |
|||
vertices: [] |
|||
indices: |
|||
edges: [] |
|||
weights: [] |
|||
secondaryTextures: [] |
|||
spritePackingTag: |
|||
pSDRemoveMatte: 0 |
|||
pSDShowRemoveMatteOption: 0 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue