AR初版demo
This commit is contained in:
1 parent
c1c93c2a60
commit
e85d5f8613
82 files changed
+4010
-62
No files matched your search
@@ -0,0 +1,56 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using System.Collections;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(Light))]
|
||||
public class SetShadowMapAsGlobalTexture : MonoBehaviour
|
||||
{
|
||||
public string textureSemanticName = "_SunCascadedShadowMap";
|
||||
#if UNITY_EDITOR
|
||||
public bool reset;
|
||||
#endif
|
||||
|
||||
private RenderTexture shadowMapRenderTexture;
|
||||
private CommandBuffer commandBuffer;
|
||||
private Light lightComponent;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
lightComponent = GetComponent<Light>();
|
||||
SetupCommandBuffer();
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
lightComponent.RemoveCommandBuffer(LightEvent.AfterShadowMap, commandBuffer);
|
||||
ReleaseCommandBuffer();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void Update()
|
||||
{
|
||||
if (reset)
|
||||
{
|
||||
OnDisable();
|
||||
OnEnable();
|
||||
reset = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void SetupCommandBuffer()
|
||||
{
|
||||
commandBuffer = new CommandBuffer();
|
||||
|
||||
RenderTargetIdentifier shadowMapRenderTextureIdentifier = BuiltinRenderTextureType.CurrentActive;
|
||||
commandBuffer.SetGlobalTexture(textureSemanticName, shadowMapRenderTextureIdentifier);
|
||||
|
||||
lightComponent.AddCommandBuffer(LightEvent.AfterShadowMap, commandBuffer);
|
||||
}
|
||||
|
||||
void ReleaseCommandBuffer()
|
||||
{
|
||||
commandBuffer.Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 35515eb99bdd1a24ba62e325b5705ecd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,174 @@
|
||||
UNITY_DECLARE_SHADOWMAP(_SunCascadedShadowMap);
|
||||
float4 _SunCascadedShadowMap_TexelSize;
|
||||
|
||||
#define GET_CASCADE_WEIGHTS(wpos, z) getCascadeWeights_splitSpheres(wpos)
|
||||
#define GET_SHADOW_FADE(wpos, z) getShadowFade_SplitSpheres(wpos)
|
||||
|
||||
#define GET_SHADOW_COORDINATES(wpos,cascadeWeights) getShadowCoord(wpos,cascadeWeights)
|
||||
|
||||
/**
|
||||
* Gets the cascade weights based on the world position of the fragment and the poisitions of the split spheres for each cascade.
|
||||
* Returns a float4 with only one component set that corresponds to the appropriate cascade.
|
||||
*/
|
||||
inline fixed4 getCascadeWeights_splitSpheres(float3 wpos)
|
||||
{
|
||||
float3 fromCenter0 = wpos.xyz - unity_ShadowSplitSpheres[0].xyz;
|
||||
float3 fromCenter1 = wpos.xyz - unity_ShadowSplitSpheres[1].xyz;
|
||||
float3 fromCenter2 = wpos.xyz - unity_ShadowSplitSpheres[2].xyz;
|
||||
float3 fromCenter3 = wpos.xyz - unity_ShadowSplitSpheres[3].xyz;
|
||||
float4 distances2 = float4(dot(fromCenter0,fromCenter0), dot(fromCenter1,fromCenter1), dot(fromCenter2,fromCenter2), dot(fromCenter3,fromCenter3));
|
||||
fixed4 weights = float4(distances2 < unity_ShadowSplitSqRadii);
|
||||
weights.yzw = saturate(weights.yzw - weights.xyz);
|
||||
return weights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the shadow fade based on the world position of the fragment, and the distance from the shadow fade center
|
||||
*/
|
||||
inline float getShadowFade_SplitSpheres( float3 wpos )
|
||||
{
|
||||
float sphereDist = distance(wpos.xyz, unity_ShadowFadeCenterAndType.xyz);
|
||||
half shadowFade = saturate(sphereDist * _LightShadowData.z + _LightShadowData.w);
|
||||
return shadowFade;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the shadowmap coordinates for the given fragment based on the world position and z-depth.
|
||||
* These coordinates belong to the shadowmap atlas that contains the maps for all cascades.
|
||||
*/
|
||||
inline float4 getShadowCoord( float4 wpos, fixed4 cascadeWeights )
|
||||
{
|
||||
float3 sc0 = mul (unity_WorldToShadow[0], wpos).xyz;
|
||||
float3 sc1 = mul (unity_WorldToShadow[1], wpos).xyz;
|
||||
float3 sc2 = mul (unity_WorldToShadow[2], wpos).xyz;
|
||||
float3 sc3 = mul (unity_WorldToShadow[3], wpos).xyz;
|
||||
return float4(sc0 * cascadeWeights[0] + sc1 * cascadeWeights[1] + sc2 * cascadeWeights[2] + sc3 * cascadeWeights[3], 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines the different components of a shadow coordinate and returns the final coordinate.
|
||||
*/
|
||||
inline float3 combineShadowcoordComponents (float2 baseUV, float2 deltaUV, float depth, float2 receiverPlaneDepthBias)
|
||||
{
|
||||
float3 uv = float3( baseUV + deltaUV, depth );
|
||||
uv.z += dot (deltaUV, receiverPlaneDepthBias); // apply the depth bias
|
||||
return uv;
|
||||
}
|
||||
|
||||
/**
|
||||
* PCF shadowmap filtering based on a 3x3 kernel (optimized with 4 taps)
|
||||
*
|
||||
* Algorithm: http://the-witness.net/news/2013/09/shadow-mapping-summary-part-1/
|
||||
* Implementation example: http://mynameismjp.wordpress.com/2013/09/10/shadow-maps/
|
||||
*/
|
||||
half sampleShadowmap_PCF3x3 (float4 coord, float2 receiverPlaneDepthBias)
|
||||
{
|
||||
const float2 offset = float2(0.5,0.5);
|
||||
float2 uv = (coord.xy * _SunCascadedShadowMap_TexelSize.zw) + offset;
|
||||
float2 base_uv = (floor(uv) - offset) * _SunCascadedShadowMap_TexelSize.xy;
|
||||
float2 st = frac(uv);
|
||||
|
||||
float2 uw = float2( 3-2*st.x, 1+2*st.x );
|
||||
float2 u = float2( (2-st.x) / uw.x - 1, (st.x)/uw.y + 1 );
|
||||
u *= _SunCascadedShadowMap_TexelSize.x;
|
||||
|
||||
float2 vw = float2( 3-2*st.y, 1+2*st.y );
|
||||
float2 v = float2( (2-st.y) / vw.x - 1, (st.y)/vw.y + 1);
|
||||
v *= _SunCascadedShadowMap_TexelSize.y;
|
||||
|
||||
half shadow;
|
||||
half sum = 0;
|
||||
|
||||
sum += uw[0] * vw[0] * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u[0], v[0]), coord.z, receiverPlaneDepthBias) );
|
||||
sum += uw[1] * vw[0] * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u[1], v[0]), coord.z, receiverPlaneDepthBias) );
|
||||
sum += uw[0] * vw[1] * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u[0], v[1]), coord.z, receiverPlaneDepthBias) );
|
||||
sum += uw[1] * vw[1] * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u[1], v[1]), coord.z, receiverPlaneDepthBias) );
|
||||
|
||||
shadow = sum / 16.0f;
|
||||
shadow = lerp (_LightShadowData.r, 1.0f, shadow);
|
||||
|
||||
return shadow;
|
||||
}
|
||||
|
||||
/**
|
||||
* PCF shadowmap filtering based on a 5x5 kernel (optimized with 9 taps)
|
||||
*
|
||||
* Algorithm: http://the-witness.net/news/2013/09/shadow-mapping-summary-part-1/
|
||||
* Implementation example: http://mynameismjp.wordpress.com/2013/09/10/shadow-maps/
|
||||
*/
|
||||
half sampleShadowmap_PCF5x5 (float4 coord, float2 receiverPlaneDepthBias)
|
||||
{
|
||||
|
||||
const float2 offset = float2(0.5,0.5);
|
||||
float2 uv = (coord.xy * _SunCascadedShadowMap_TexelSize.zw) + offset;
|
||||
float2 base_uv = (floor(uv) - offset) * _SunCascadedShadowMap_TexelSize.xy;
|
||||
float2 st = frac(uv);
|
||||
|
||||
float3 uw = float3( 4-3*st.x, 7, 1+3*st.x );
|
||||
float3 u = float3( (3-2*st.x) / uw.x - 2, (3+st.x)/uw.y, st.x/uw.z + 2 );
|
||||
u *= _SunCascadedShadowMap_TexelSize.x;
|
||||
|
||||
float3 vw = float3( 4-3*st.y, 7, 1+3*st.y );
|
||||
float3 v = float3( (3-2*st.y) / vw.x - 2, (3+st.y)/vw.y, st.y/vw.z + 2 );
|
||||
v *= _SunCascadedShadowMap_TexelSize.y;
|
||||
|
||||
half shadow;
|
||||
half sum = 0.0f;
|
||||
|
||||
half3 accum = uw * vw.x;
|
||||
sum += accum.x * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.x,v.x), coord.z, receiverPlaneDepthBias) );
|
||||
sum += accum.y * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.y,v.x), coord.z, receiverPlaneDepthBias) );
|
||||
sum += accum.z * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.z,v.x), coord.z, receiverPlaneDepthBias) );
|
||||
|
||||
accum = uw * vw.y;
|
||||
sum += accum.x * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.x,v.y), coord.z, receiverPlaneDepthBias) );
|
||||
sum += accum.y * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.y,v.y), coord.z, receiverPlaneDepthBias) );
|
||||
sum += accum.z * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.z,v.y), coord.z, receiverPlaneDepthBias) );
|
||||
|
||||
accum = uw * vw.z;
|
||||
sum += accum.x * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.x,v.z), coord.z, receiverPlaneDepthBias) );
|
||||
sum += accum.y * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.y,v.z), coord.z, receiverPlaneDepthBias) );
|
||||
sum += accum.z * UNITY_SAMPLE_SHADOW( _SunCascadedShadowMap, combineShadowcoordComponents( base_uv, float2(u.z,v.z), coord.z, receiverPlaneDepthBias) );
|
||||
|
||||
shadow = sum / 144.0f;
|
||||
|
||||
shadow = lerp (_LightShadowData.r, 1.0f, shadow);
|
||||
|
||||
|
||||
return shadow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Samples the shadowmap at the given coordinates.
|
||||
*/
|
||||
half unity_sampleShadowmap( float4 coord )
|
||||
{
|
||||
half shadow = UNITY_SAMPLE_SHADOW(_SunCascadedShadowMap,coord);
|
||||
shadow = lerp(_LightShadowData.r, 1.0, shadow);
|
||||
return shadow;
|
||||
}
|
||||
|
||||
|
||||
|
||||
///////////////////
|
||||
/**
|
||||
* Gets the shadows attenuations at world positions.
|
||||
*/
|
||||
|
||||
half GetSunShadowsAttenuation(float3 worldPositions, float screenDepth)
|
||||
{
|
||||
fixed4 cascadeWeights = GET_CASCADE_WEIGHTS(worldPositions.xyz, screenDepth);
|
||||
return unity_sampleShadowmap(GET_SHADOW_COORDINATES(float4(worldPositions, 1), cascadeWeights));
|
||||
}
|
||||
|
||||
half GetSunShadowsAttenuation_PCF3x3(float3 worldPositions, float screenDepth, float receiverPlaneDepthBias)
|
||||
{
|
||||
fixed4 cascadeWeights = GET_CASCADE_WEIGHTS(worldPositions.xyz, screenDepth);
|
||||
return sampleShadowmap_PCF3x3(GET_SHADOW_COORDINATES(float4(worldPositions, 1), cascadeWeights), receiverPlaneDepthBias);
|
||||
}
|
||||
|
||||
half GetSunShadowsAttenuation_PCF5x5(float3 worldPositions, float screenDepth, float receiverPlaneDepthBias)
|
||||
{
|
||||
fixed4 cascadeWeights = GET_CASCADE_WEIGHTS(worldPositions.xyz, screenDepth);
|
||||
return sampleShadowmap_PCF5x5(GET_SHADOW_COORDINATES(float4(worldPositions, 1), cascadeWeights), receiverPlaneDepthBias);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c43ee60bb2d53eb4bbc448c8c29f40d7
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,78 @@
|
||||
%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: TransparentPlane
|
||||
m_Shader: {fileID: 4800000, guid: b7bad1d63b69ded4f963a9fd7a590267, type: 3}
|
||||
m_ShaderKeywords:
|
||||
m_LightmapFlags: 4
|
||||
m_EnableInstancingVariants: 0
|
||||
m_DoubleSidedGI: 0
|
||||
m_CustomRenderQueue: -1
|
||||
stringTagMap: {}
|
||||
disabledShaderPasses: []
|
||||
m_SavedProperties:
|
||||
serializedVersion: 3
|
||||
m_TexEnvs:
|
||||
- _BumpMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailAlbedoMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailMask:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailNormalMap:
|
||||
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}
|
||||
- _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}
|
||||
- _ParallaxMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
m_Floats:
|
||||
- _BumpScale: 1
|
||||
- _Cutoff: 0.5
|
||||
- _DetailNormalMapScale: 1
|
||||
- _DstBlend: 0
|
||||
- _GlossMapScale: 1
|
||||
- _Glossiness: 0.5
|
||||
- _GlossyReflections: 1
|
||||
- _Metallic: 0
|
||||
- _Mode: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.02
|
||||
- _ShadowIntensity: 0.6
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
- _SrcBlend: 1
|
||||
- _UVSec: 0
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 304fbd5441391b74cbc49963e461c7e0
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 2100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
|
||||
|
||||
Shader "Custom/TransparentShadowCollector"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_Color("Main Color", Color) = (1,1,1,.5)
|
||||
_ShadowIntensity("Shadow Intensity", Range(0, 1)) = 0.6
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
Tags {"Queue" = "AlphaTest" }
|
||||
Pass
|
||||
{
|
||||
Tags {"LightMode" = "ForwardBase" }
|
||||
Cull Back
|
||||
Blend SrcAlpha OneMinusSrcAlpha
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#pragma multi_compile_fwdbase
|
||||
#include "UnityCG.cginc"
|
||||
#include "AutoLight.cginc"
|
||||
uniform fixed4 _Color;
|
||||
uniform float _ShadowIntensity;
|
||||
struct v2f
|
||||
{
|
||||
float4 pos : SV_POSITION;
|
||||
LIGHTING_COORDS(0,1)
|
||||
};
|
||||
v2f vert(appdata_base v)
|
||||
{
|
||||
v2f o;
|
||||
o.pos = UnityObjectToClipPos(v.vertex);
|
||||
TRANSFER_VERTEX_TO_FRAGMENT(o);
|
||||
|
||||
return o;
|
||||
}
|
||||
fixed4 frag(v2f i) : COLOR
|
||||
{
|
||||
float attenuation = LIGHT_ATTENUATION(i);
|
||||
return fixed4(0,0,0,(1 - attenuation) * _ShadowIntensity) * _Color;
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
Fallback "VertexLit"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7bad1d63b69ded4f963a9fd7a590267
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user