浏览代码

Add a depth gradient shader.

Modify script to update layout of the RawImage to align with the screen orientation.
/4.1
Todd Stinson 4 年前
当前提交
4236858f
共有 6 个文件被更改,包括 282 次插入19 次删除
  1. 2
      Assets/Scenes/Depth/DepthImages.unity
  2. 83
      Assets/Scripts/TestDepthImage.cs
  3. 80
      Assets/Materials/EnvironmentDepthMaterial.mat
  4. 8
      Assets/Materials/EnvironmentDepthMaterial.mat.meta
  5. 119
      Assets/Shaders/EnvironmentDepthGradient.shader
  6. 9
      Assets/Shaders/EnvironmentDepthGradient.shader.meta

2
Assets/Scenes/Depth/DepthImages.unity


m_Script: {fileID: 11500000, guid: 1344c3c82d62a2a41a3576d8abb8e3ea, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Material: {fileID: 2100000, guid: 1822b099f6c09434b97c31e70184ff72, type: 2}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1

83
Assets/Scripts/TestDepthImage.cs


/// </summary>
public class TestDepthImage : MonoBehaviour
{
/// <summary>
/// Name of the texture rotation property in the shader.
/// </summary>
const string k_TextureRotationName = "_TextureRotation";
/// <summary>
/// ID of the texture rotation property in the shader.
/// </summary>
static readonly int k_TextureRotationId = Shader.PropertyToID(k_TextureRotationName);
[SerializeField]
[Tooltip("The AROcclusionManager which will produce frame events.")]
AROcclusionManager m_OcclusionManager;

set { m_ImageInfo = value; }
}
void LogTextureInfo(StringBuilder stringBuilder, string textureName, Texture2D texture)
/// <summary>
/// The current screen orientation remembered so that we are only updating the raw image layout when it changes.
/// </summary>
ScreenOrientation m_CurrentScreenOrientation;
void OnEnable()
stringBuilder.AppendFormat("texture : {0}\n", textureName);
if (texture == null)
{
stringBuilder.AppendFormat(" <null>\n");
}
else
{
stringBuilder.AppendFormat(" format : {0}\n", texture.format.ToString());
stringBuilder.AppendFormat(" width : {0}\n", texture.width);
stringBuilder.AppendFormat(" height : {0}\n", texture.height);
stringBuilder.AppendFormat(" mipmap : {0}\n", texture.mipmapCount);
}
m_CurrentScreenOrientation = Screen.orientation;
LayoutRawImage();
}
void Update()

Debug.Log(sb.ToString());
}
// To use the stencil, be sure the HumanSegmentationStencilMode property on the AROcclusionManager is set to a
// non-disabled value.
Debug.Assert(m_RawImage != null, "no raw image");
// To use the depth, be sure the HumanSegmentationDepthMode property on the AROcclusionManager is set to a
/// non-disabled value.
// m_RawImage.texture = eventArgs.humanDepth;
if (m_CurrentScreenOrientation != Screen.orientation)
{
m_CurrentScreenOrientation = Screen.orientation;
LayoutRawImage();
}
}
void LogTextureInfo(StringBuilder stringBuilder, string textureName, Texture2D texture)
{
stringBuilder.AppendFormat("texture : {0}\n", textureName);
if (texture == null)
{
stringBuilder.AppendFormat(" <null>\n");
}
else
{
stringBuilder.AppendFormat(" format : {0}\n", texture.format.ToString());
stringBuilder.AppendFormat(" width : {0}\n", texture.width);
stringBuilder.AppendFormat(" height : {0}\n", texture.height);
stringBuilder.AppendFormat(" mipmap : {0}\n", texture.mipmapCount);
}
}
void LayoutRawImage()
{
Debug.Assert(m_RawImage != null, "no raw image");
switch (m_CurrentScreenOrientation)
{
case ScreenOrientation.LandscapeRight:
m_RawImage.rectTransform.sizeDelta = new Vector2(640.0f, 480.0f);
m_RawImage.material.SetFloat(k_TextureRotationId, 0.0f);
break;
case ScreenOrientation.LandscapeLeft:
m_RawImage.rectTransform.sizeDelta = new Vector2(640.0f, 480.0f);
m_RawImage.material.SetFloat(k_TextureRotationId, 180.0f);
break;
case ScreenOrientation.PortraitUpsideDown:
m_RawImage.rectTransform.sizeDelta = new Vector2(480.0f, 640.0f);
m_RawImage.material.SetFloat(k_TextureRotationId, 270.0f);
break;
case ScreenOrientation.Portrait:
default:
m_RawImage.rectTransform.sizeDelta = new Vector2(480.0f, 640.0f);
m_RawImage.material.SetFloat(k_TextureRotationId, 90.0f);
break;
}
}
}
}

80
Assets/Materials/EnvironmentDepthMaterial.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: EnvironmentDepthMaterial
m_Shader: {fileID: 4800000, guid: 1aab677bdaaa5470588b1194d3f271be, 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
- _MaxDistance: 8
- _Metallic: 0
- _MinDistance: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _TextureRotation: 0
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

8
Assets/Materials/EnvironmentDepthMaterial.mat.meta


fileFormatVersion: 2
guid: 1822b099f6c09434b97c31e70184ff72
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

119
Assets/Shaders/EnvironmentDepthGradient.shader


Shader "Unlit/EnvironmentDepthGradient"
{
Properties
{
_MainTex ("Main Texture", 2D) = "black" {}
_MinDistance ("Min Distance", Float) = 0.0
_MaxDistance ("Max Distance", Float) = 8.0
_TextureRotation ("Texture Rotation (Degrees)", Float) = 0.0
}
SubShader
{
Tags
{
"Queue" = "Geometry"
"RenderType" = "Opaque"
"ForceNoShadowCasting" = "True"
}
Pass
{
Cull Off
ZTest Always
ZWrite Off
Lighting Off
LOD 100
Tags
{
"LightMode" = "Always"
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define real half
#define real3 half3
#define real4 half4
#define TransformObjectToHClip UnityObjectToClipPos
#define DECLARE_TEXTURE2D_FLOAT(texture) UNITY_DECLARE_TEX2D_FLOAT(texture)
#define DECLARE_SAMPLER_FLOAT(sampler)
#define 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;
};
real _TextureRotation;
v2f vert (appdata v)
{
float angle = radians(_TextureRotation);
float cosrot = cos(angle);
float sinrot = sin(angle);
float2 tmp = float2(0.5f - v.texcoord.x, v.texcoord.y - 0.5f);
v2f o;
o.position = TransformObjectToHClip(v.position);
o.texcoord = float2(tmp.x * cosrot - tmp.y * sinrot + 0.5f, tmp.x * sinrot + tmp.y * cosrot + 0.5f);
return o;
}
real3 HSVtoRGB(real3 arg1)
{
real4 K = half4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
real3 P = abs(frac(arg1.xxx + K.xyz) * 6.0 - K.www);
return arg1.z * lerp(K.xxx, saturate(P - K.xxx), arg1.y);
}
DECLARE_TEXTURE2D_FLOAT(_MainTex);
DECLARE_SAMPLER_FLOAT(sampler_MainTex);
real _MinDistance;
real _MaxDistance;
fragment_output frag (v2f i)
{
// Sample the environment depth (in meters).
float envDistance = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).r;
real lerpFactor = (envDistance - _MinDistance) / (_MaxDistance - _MinDistance);
real hue = lerp(0.70f, -0.15f, saturate(lerpFactor));
if (hue < 0.0f)
{
hue += 1.0f;
}
real3 color = real3(hue, 0.9, 0.6);
fragment_output o;
o.color = real4(HSVtoRGB(color), 1.0);
return o;
}
ENDHLSL
}
}
}

9
Assets/Shaders/EnvironmentDepthGradient.shader.meta


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