Skip to content

Commit

Permalink
A direct sampler shader for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
arigo committed Jun 26, 2020
1 parent 72009db commit a57c182
Show file tree
Hide file tree
Showing 54 changed files with 3,733 additions and 5 deletions.
30 changes: 29 additions & 1 deletion Assets/CVCT_Voxelize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CVCT_Voxelize : MonoBehaviour
public int drawCascade;

Camera _projectCam;
Matrix4x4 _world_to_light_local_matrix;


void UpdateVoxelization()
Expand Down Expand Up @@ -82,6 +83,13 @@ void UpdateVoxelization()

RenderTexture.ReleaseTemporary(dummy_target);
cb_gv.Release();

/* the scale in _world_to_light_local_matrix is such that the 0th cascade
* corresponds to the box from (-1,-1,-1) to (+1,+1,+1).
*/
float sz = 0.5f * gridResolution * gridPixelSize;
var mat = Matrix4x4.Scale(Vector3.one / sz) * cam.transform.worldToLocalMatrix;
_world_to_light_local_matrix = mat;
}

Camera FetchProjectionCamera()
Expand Down Expand Up @@ -125,6 +133,18 @@ void UpdateTracing()
}


void SetGlobalUniforms()
{
var v = Vector4.zero;
v.x = gridResolution;
v.y = 1f / gridCascades;

Shader.SetGlobalVector("_CVCT_GridResolution", v);
Shader.SetGlobalMatrix("_CVCT_WorldToLightLocalMatrix", _world_to_light_local_matrix);
Shader.SetGlobalTexture("_CVCT_LightTex3d", _tex3d_light);
}


/*********************************************************************/

RenderTexture _tex3d_gv;
Expand All @@ -133,6 +153,7 @@ void UpdateTracing()
private void Start()
{
DestroyTargets();
UpdateLights();
}

void DestroyTarget(ref RenderTexture tex)
Expand Down Expand Up @@ -178,7 +199,7 @@ RenderTexture CreateTex3dLight()
return tg;
}

void Update()
public void UpdateLights()
{
if (_tex3d_gv != null && _tex3d_gv.width == gridResolution && _tex3d_gv.height == gridResolution * gridCascades &&
_tex3d_light != null && _tex3d_light.width == gridResolution && _tex3d_light.height == gridResolution * gridCascades)
Expand All @@ -201,9 +222,16 @@ void Update()

_tex3d_light.Create();
UpdateTracing();

SetGlobalUniforms();
}

#if UNITY_EDITOR
void Update()
{
UpdateLights();
}

void OnDrawGizmos()
{
if (drawGizmosGV)
Expand Down
8 changes: 8 additions & 0 deletions Assets/Sampler.meta

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

77 changes: 77 additions & 0 deletions Assets/Sampler/SamplerMaterial.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
%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: SamplerMaterial
m_Shader: {fileID: 4800000, guid: 48b99b800ea72b54da0df56e5c073d00, 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
- _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}
8 changes: 8 additions & 0 deletions Assets/Sampler/SamplerMaterial.mat.meta

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

91 changes: 91 additions & 0 deletions Assets/Sampler/SamplerShader.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
Shader "Unlit/SamplerShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 4.5
/*#include "Assets/ShaderDebugger/debugger.cginc"*/

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};

struct v2f
{
float4 vertex : SV_POSITION;
float4 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
};

float4 _Color;
float4x4 _CVCT_WorldToLightLocalMatrix;
float4 _CVCT_GridResolution;
sampler3D _CVCT_LightTex3d;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.worldNormal = mul((float3x3)unity_ObjectToWorld, v.normal);
return o;
}

float light_at_point(v2f i)
{
float3 norm = mul((float3x3)_CVCT_WorldToLightLocalMatrix, i.worldNormal);
if (norm.z >= 0)
return 0;

/* the scale in this matrix is such that the 0th cascade corresponds to
the box from (-1,-1,-1) to (+1,+1,+1). */
float3 pos = mul(_CVCT_WorldToLightLocalMatrix, i.worldPos).xyz;

float3 central_pos = abs(pos);
float central_distance = max(central_pos.x, max(central_pos.y, central_pos.z));
/* central_distance => cascade:
<= 1 0
<= 2 1
<= 4 2
<= 8 3
*/
float cascade = ceil(log2(max(1.0, central_distance)));

pos *= pow(0.5, cascade); /* (-1,-1,-1) to (+1,+1,+1) in the right cascade */
pos = pos * 0.5 + 0.5; /* (0,0,0) to (1,1,1) */
pos.y += cascade; /* (0,c,0) to (1,1+c,1) */
pos.y *= _CVCT_GridResolution.y; /* rescale along y */

float light = pos.y < 1 ? tex3D(_CVCT_LightTex3d, pos).r : 1;

/*uint root = DebugFragment(i.vertex);
DbgValue4(root, float4(pos, light));*/

return light * (-normalize(norm).z);
}

fixed4 frag (v2f i) : SV_Target
{
float4 col = _Color;
col *= light_at_point(i);
return col;
}
ENDCG
}
}
}
9 changes: 9 additions & 0 deletions Assets/Sampler/SamplerShader.shader.meta

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

83 changes: 80 additions & 3 deletions Assets/Scenes/Scene 1.unity
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,12 @@ MonoBehaviour:
gridCascades: 5
cullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Bits: 55
gvShader: {fileID: 4800000, guid: 9495c431e1a9dea4eb1379d3f4c23bb3, type: 3}
gvCompute: {fileID: 7200000, guid: 66402ca1a7c909a469122dcc655beddd, type: 3}
drawGizmosGV: 0
drawGizmosLight: 1
drawCascade: 3
drawGizmosLight: 0
drawCascade: 0
--- !u!4 &1209605626
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -736,6 +736,83 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 899312650}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1919510770
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1919510773}
- component: {fileID: 1919510772}
- component: {fileID: 1919510771}
m_Layer: 8
m_Name: Sphere
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!23 &1919510771
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1919510770}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: a39843438085f0042aa29674c4233722, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &1919510772
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1919510770}
m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1919510773
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1919510770}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -2.225, y: 0.786, z: -2.197}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1610333014733714316
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
8 changes: 8 additions & 0 deletions Assets/ShaderDebugger.meta

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

8 changes: 8 additions & 0 deletions Assets/ShaderDebugger/Demo GeomShader.meta

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

Loading

0 comments on commit a57c182

Please sign in to comment.