浏览代码

added fog shader, hooked up to UI

/ARCoreShaderFog
Dan 4 年前
当前提交
dcd36516
共有 15 个文件被更改,包括 1339 次插入7 次删除
  1. 7
      Assets/Common/Materials/PlacedSphereMat.mat
  2. 5
      ProjectSettings/EditorBuildSettings.asset
  3. 3
      ProjectSettings/GraphicsSettings.asset
  4. 101
      Assets/Shaders/Materials/ARKitFogMat.mat
  5. 8
      Assets/Shaders/Materials/ARKitFogMat.mat.meta
  6. 1001
      Assets/Shaders/Scenes/Fog.unity
  7. 7
      Assets/Shaders/Scenes/Fog.unity.meta
  8. 8
      Assets/Shaders/Scripts.meta
  9. 8
      Assets/Shaders/Shaders.meta
  10. 18
      Assets/Shaders/Scripts/ApplyFog.cs
  11. 11
      Assets/Shaders/Scripts/ApplyFog.cs.meta
  12. 160
      Assets/Shaders/Shaders/ARKitFogBackground.shader
  13. 9
      Assets/Shaders/Shaders/ARKitFogBackground.shader.meta

7
Assets/Common/Materials/PlacedSphereMat.mat


m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: PlacedSphereMat
m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3}
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_CustomRenderQueue: 2050
stringTagMap:
RenderType: Opaque
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3

5
ProjectSettings/EditorBuildSettings.asset


- enabled: 0
path: Assets/ImageTracking/Scenes/ImageTracking.unity
guid: 90c9c5e58ea6f4125979d49ff7533aee
- enabled: 1
- enabled: 0
- enabled: 1
path: Assets/Shaders/Scenes/Fog.unity
guid: 0e21c82866efe438fa962dbed0b66a7f
m_configObjects:
UnityEditor.XR.ARCore.ARCoreSettings: {fileID: 11400000, guid: 6390cd816397648a5b1cc5fc4723e0ba,
type: 2}

3
ProjectSettings/GraphicsSettings.asset


m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
m_CustomRenderPipeline: {fileID: 11400000, guid: efc9f20e6a4b0423ebd19efb551d21a0,
type: 2}
m_CustomRenderPipeline: {fileID: 0}
m_TransparencySortMode: 0
m_TransparencySortAxis: {x: 0, y: 0, z: 1}
m_DefaultRenderingPath: 1

101
Assets/Shaders/Materials/ARKitFogMat.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: ARKitFogMat
m_Shader: {fileID: 4800000, guid: f10bd4466a3844ae6b63ecfa080b9a57, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EnvironmentDepth:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _textureCbCr:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _textureY:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _AlphaClip: 0
- _Blend: 0
- _BumpScale: 1
- _Cull: 2
- _Cutoff: 0.5
- _DstBlend: 0
- _EnvironmentReflections: 1
- _GlossMapScale: 0
- _Glossiness: 0
- _GlossyReflections: 0
- _Metallic: 0
- _OcclusionStrength: 1
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0.5
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _Surface: 0
- _WorkflowMode: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
--- !u!114 &2593910778208443215
MonoBehaviour:
m_ObjectHideFlags: 11
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: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 1

8
Assets/Shaders/Materials/ARKitFogMat.mat.meta


fileFormatVersion: 2
guid: 07cf155ddd7aa4bd6b10047c7dde5a19
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

1001
Assets/Shaders/Scenes/Fog.unity
文件差异内容过多而无法显示
查看文件

7
Assets/Shaders/Scenes/Fog.unity.meta


fileFormatVersion: 2
guid: 0e21c82866efe438fa962dbed0b66a7f
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Assets/Shaders/Scripts.meta


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

8
Assets/Shaders/Shaders.meta


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

18
Assets/Shaders/Scripts/ApplyFog.cs


using System;
using System.Text;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARFoundation.Samples
{
public class ApplyFog : MonoBehaviour
{
public void OnSliderValueChanged(Slider slider)
{
RenderSettings.fogEndDistance = slider.value;
}
}
}

11
Assets/Shaders/Scripts/ApplyFog.cs.meta


fileFormatVersion: 2
guid: ca8044bf0ec4943a59c25d210733b43c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

160
Assets/Shaders/Shaders/ARKitFogBackground.shader


Shader "Unlit/ARKitFogBackground"
{
Properties
{
_textureY ("TextureY", 2D) = "white" {}
_textureCbCr ("TextureCbCr", 2D) = "black" {}
_EnvironmentDepth ("EnvironmentDepth", 2D) = "black" {}
}
SubShader
{
Tags
{
"Queue" = "Background"
"RenderType" = "Background"
"ForceNoShadowCasting" = "True"
}
Pass
{
Cull Off
ZTest Always
ZWrite On
Lighting Off
LOD 100
Tags
{
"LightMode" = "Always"
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_local __ ARKIT_ENVIRONMENT_DEPTH_ENABLED
#pragma multi_compile_fog
#include "UnityCG.cginc"
#define real4 half4
#define real4x4 half4x4
#define TransformObjectToHClip UnityObjectToClipPos
#define FastSRGBToLinear GammaToLinearSpace
#define ARKIT_TEXTURE2D_HALF(texture) UNITY_DECLARE_TEX2D_HALF(texture)
#define ARKIT_SAMPLER_HALF(sampler)
#define ARKIT_TEXTURE2D_FLOAT(texture) UNITY_DECLARE_TEX2D_FLOAT(texture)
#define ARKIT_SAMPLER_FLOAT(sampler)
#define ARKIT_SAMPLE_TEXTURE2D(texture,sampler,texcoord) UNITY_SAMPLE_TEX2D(texture,texcoord)
struct appdata
{
float3 position : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
struct fragment_output
{
real4 color : SV_Target;
float depth : SV_Depth;
};
CBUFFER_START(UnityARFoundationPerFrame)
// Device display transform is provided by the AR Foundation camera background renderer.
float4x4 _UnityDisplayTransform;
CBUFFER_END
v2f vert (appdata v)
{
// Transform the position from object space to clip space.
float4 position = TransformObjectToHClip(v.position);
// Remap the texture coordinates based on the device rotation.
float2 texcoord = mul(float3(v.texcoord, 1.0f), _UnityDisplayTransform).xy;
v2f o;
o.position = position;
o.texcoord = texcoord;
return o;
}
CBUFFER_START(ARKitColorTransformations)
static const real4x4 s_YCbCrToSRGB = real4x4(
real4(1.0h, 0.0000h, 1.4020h, -0.7010h),
real4(1.0h, -0.3441h, -0.7141h, 0.5291h),
real4(1.0h, 1.7720h, 0.0000h, -0.8860h),
real4(0.0h, 0.0000h, 0.0000h, 1.0000h)
);
CBUFFER_END
inline float ConvertDistanceToDepth(float d)
{
// Clip any distances smaller than the near clip plane, and compute the depth value from the distance.
return (d < _ProjectionParams.y) ? 0.0f : ((0.5f / _ZBufferParams.z) * ((1.0f / d) - _ZBufferParams.w));
}
ARKIT_TEXTURE2D_HALF(_textureY);
ARKIT_SAMPLER_HALF(sampler_textureY);
ARKIT_TEXTURE2D_HALF(_textureCbCr);
ARKIT_SAMPLER_HALF(sampler_textureCbCr);
ARKIT_TEXTURE2D_FLOAT(_EnvironmentDepth);
ARKIT_SAMPLER_FLOAT(sampler_EnvironmentDepth);
fragment_output frag (v2f i)
{
// Sample the video textures (in YCbCr).
real4 ycbcr = real4(ARKIT_SAMPLE_TEXTURE2D(_textureY, sampler_textureY, i.texcoord).r,
ARKIT_SAMPLE_TEXTURE2D(_textureCbCr, sampler_textureCbCr, i.texcoord).rg,
1.0h);
// Convert from YCbCr to sRGB.
real4 videoColor = mul(s_YCbCrToSRGB, ycbcr);
#if !UNITY_COLORSPACE_GAMMA
// If rendering in linear color space, convert from sRGB to RGB.
videoColor.xyz = FastSRGBToLinear(videoColor.xyz);
#endif // !UNITY_COLORSPACE_GAMMA
// Assume the background depth is the back of the depth clipping volume.
float depthValue = 0.0f;
#if ARKIT_ENVIRONMENT_DEPTH_ENABLED
// Sample the environment depth (in meters).
float envDistance = ARKIT_SAMPLE_TEXTURE2D(_EnvironmentDepth, sampler_EnvironmentDepth, i.texcoord).r;
// Convert the distance to depth.
depthValue = ConvertDistanceToDepth(envDistance);
#if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
UNITY_CALC_FOG_FACTOR(depthValue);
UNITY_FOG_LERP_COLOR(videoColor, unity_FogColor, unityFogFactor);
#endif // defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
#endif // ARKIT_ENVIRONMENT_DEPTH_ENABLED
fragment_output o;
o.color = videoColor;
o.depth = depthValue;
return o;
}
ENDHLSL
}
}
}

9
Assets/Shaders/Shaders/ARKitFogBackground.shader.meta


fileFormatVersion: 2
guid: f10bd4466a3844ae6b63ecfa080b9a57
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存