Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Content/Addressables/Linux.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Content/Resources/Vision.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Assets/Content/Resources/Vision/SpacePlane.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Shader "Custom/SpacePlane"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "black" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
ZWrite On
Tags {"Queue"="Transparent" "RenderType"="Transparent" }
LOD 200
CGPROGRAM
#include "VisionCG.cginc"
#pragma surface surf Standard fullforwardshadows alpha

// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0

sampler2D _MainTex;

struct Input
{
float2 uv_MainTex;
float3 worldPos;
};

half _Glossiness;
half _Metallic;
fixed4 _Color;

void surf (Input IN, inout SurfaceOutputStandard o)
{
if(IsVisible(IN.worldPos))
{
discard;
}

float4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgba;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;

}
ENDCG
}
Fallback "Diffuse"
}
10 changes: 10 additions & 0 deletions Assets/Content/Resources/Vision/SpacePlane.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions Assets/Content/Resources/Vision/Spaceplane.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Spaceplane
m_Shader: {fileID: 4800000, guid: d7a3002ca6faa3b588f87113808c11bd, type: 3}
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 2000
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_Ints: []
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _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}
m_BuildTextureStacks: []
8 changes: 8 additions & 0 deletions Assets/Content/Resources/Vision/Spaceplane.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 129 additions & 0 deletions Assets/Content/Resources/Vision/VisionCG.cginc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#ifndef VISION_CG_INCLUDED
#define VISION_CG_INCLUDED


UNITY_DECLARE_TEX2D(_CameraDepthTexture);
UNITY_DECLARE_TEX2D(_VisionMap);
float4x4 _PlayerCameraInvViewProj;
float4 _PlayerPos;
float _PlayerAngle;
float _ViewConeWidth;
float _ViewRange;
//TODO setup for multiplatform

inline bool IsVisible(float2 screenUV);
inline bool IsVisible(float3 posWorld);

inline float2 WorldToFov(float3 posWorld);
inline float4 FragToClip(float4 frag);
inline float3 ClipToWorld(float2 posClip);
inline bool depthIsSky(float depth);
inline float modulo(float x, float y);


//Returns true if the point should be visible to the player
//Prefer posWorld version where possible

//Use with fragment position in any normal fragment shader
inline bool IsVisible(float4 posFrag)
{
float4 posClip = FragToClip(posFrag);
return IsVisible(posClip.xy);
}
//Use with UVs in postprocessing shaders
inline bool IsVisible(float2 screenUV)
{
float3 posWorld = ClipToWorld(screenUV);
return IsVisible(posWorld);
}
//Checks world coordinates against polar depth texture
//Use with world positions in shaders that supply them
inline bool IsVisible(float3 posWorld)
{
float2 polarCoords = WorldToFov(posWorld);

float centerAngle = _ViewConeWidth / 2;
float centerAngleWorld = _PlayerAngle;
float rawOffset = polarCoords.y - centerAngleWorld;
float clampedOffset = modulo(rawOffset, UNITY_TWO_PI) - UNITY_PI;

//return early if angle is outside the view cone
if ((abs(clampedOffset) > centerAngle) && (polarCoords.x > 1)) return false;

float currentAngle = centerAngle + clampedOffset;
float2 viewUV = float2(currentAngle / _ViewConeWidth, 0);

float currentDepth = polarCoords.x;
float wallDepth = UNITY_SAMPLE_TEX2D(_VisionMap, viewUV).r * _ViewRange;

return currentDepth < wallDepth;
}

// Inputs fragment position (pixelX, pixelY, depth, perspective)
// Outputs clip position([-1,1],[-1,1], depth, perspective)
inline float4 FragToClip(float4 frag)
{
frag.xy /= _ScreenParams.xy;
frag.xy = frag.xy * 2 - 1;
return frag;
}

//Converts clip position ([-1,1][-1,1]) to world position
inline float3 ClipToWorld(float2 posClip)
{
float2 screenUV = posClip * 0.5 + 0.5;
float rawDepth = UNITY_SAMPLE_TEX2D(_CameraDepthTexture, screenUV).r;

#if !defined(UNITY_REVERSED_Z)
rawDepth = lerp(UNITY_NEAR_CLIP_VALUE, 1, rawDepth);
#endif

float4 adjClip = float4(posClip, rawDepth, 1);

float4 worldspace = mul(_PlayerCameraInvViewProj, adjClip);

if(depthIsSky(rawDepth))
{
//Ensures skybox is always visible
//Skybox should have dedicated handling in the future
return _PlayerPos;
}
return worldspace.xyz / worldspace.w;

}

//Converts world position to polar coordinates (distance, radians) relative to player
inline float2 WorldToFov(float3 posWorld)
{
float2 posFov = (posWorld - _PlayerPos).xz;
float rawAngle;

if (posFov.x == 0) rawAngle = 0;
else rawAngle = atan2(posFov.x, -posFov.y);

float adjAngle = -rawAngle;
float clampedAngle = modulo(adjAngle, UNITY_TWO_PI);

float worldDepth = length(posFov);
float2 polarCoords = float2(worldDepth, clampedAngle);

return polarCoords;
}

//Detect if skybox, requires special handling
inline bool depthIsSky(float depth)
{
#if defined(UNITY_REVERSED_Z)
return (depth <= 0.0);
#else
return (depth >= 1.0);
#endif
}

inline float modulo(float x, float y)
{
return x - y * floor(x/y);
}


#endif
7 changes: 7 additions & 0 deletions Assets/Content/Resources/Vision/VisionCG.cginc.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions Assets/Content/Resources/Vision/VisionMask.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Shader "Vision/VisionMask"
{
//TODO Clean this up for multiplatform and add comments
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"
#include "VisionCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};

UNITY_DECLARE_TEX2D(_MainTex);
v2f vert(appdata v)
{
v2f o;
o.vertex = float4(v.vertex.xy, 0, 1);
o.uv = v.vertex.xy;

if (_ProjectionParams.x < 0) o.uv.y = (1 - o.uv.y) - 1;

return o;
}
float4 frag(v2f i) : SV_Target
{
if(IsVisible(i.uv)) return float4(1,1,1,1);
else return float4(0,0,0,0);
}

ENDCG
}
}
}
10 changes: 10 additions & 0 deletions Assets/Content/Resources/Vision/VisionMask.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading