Skip to content

Commit

Permalink
Added triplanar projection and a depth prefab.
Browse files Browse the repository at this point in the history
  • Loading branch information
AkaiMage authored Sep 13, 2019
1 parent 06586e1 commit 37c0a64
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Cancerspace.shader
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
_StencilReadMask ("Read Mask", Int) = 255
_StencilWriteMask ("Write Mask", Int) = 255

[Enum(Flat, 0, Sphere, 1, Mesh, 2, Walls, 3)] _ProjectionType ("Projection Type", Int) = 0
[Enum(Flat, 0, Sphere, 1, Mesh, 2, Walls, 3, Triplanar, 4)] _ProjectionType ("Projection Type", Int) = 0
_ProjectionRotX ("Rotation X", Range(-360, 360)) = 0
_ProjectionRotY ("Rotation Y", Range(-360, 360)) = 0
_ProjectionRotZ ("Rotation Z", Range(-360, 360)) = 0
Expand Down Expand Up @@ -180,6 +180,7 @@
#define PROJECTION_SPHERE 1
#define PROJECTION_MESH 2
#define PROJECTION_WALLS 3
#define PROJECTION_TRIPLANAR 4

#define BLENDMODE_MULTIPLY 0
#define BLENDMODE_SCREEN 1
Expand Down Expand Up @@ -624,10 +625,10 @@
return posWorld;
}

float2 calculateScreenUVs(float3 posWorld, float2 meshUV) {
float2 calculateScreenUVs(int projectionType, float3 posWorld, float2 meshUV, float3 triplanarWorld, float3 triplanarNormal) {
float2 screenSpaceOverlayUV = 0;

UNITY_BRANCH switch (_ProjectionType) {
UNITY_BRANCH switch (projectionType) {
case PROJECTION_FLAT:
screenSpaceOverlayUV = computeScreenSpaceOverlayUV(rotateProjectionWorld(posWorld));
break;
Expand All @@ -644,6 +645,15 @@
screenSpaceOverlayUV = (rd / bot).yz;
break;
}
case PROJECTION_TRIPLANAR: {
triplanarNormal = abs(triplanarNormal);

screenSpaceOverlayUV =
step(triplanarNormal.y, triplanarNormal.x) * step(triplanarNormal.z, triplanarNormal.x) * triplanarWorld.zy +
step(triplanarNormal.x, triplanarNormal.y) * step(triplanarNormal.z, triplanarNormal.y) * triplanarWorld.xz +
step(triplanarNormal.x, triplanarNormal.z) * step(triplanarNormal.y, triplanarNormal.z) * triplanarWorld.xy;
break;
}
}

return screenSpaceOverlayUV;
Expand Down Expand Up @@ -707,7 +717,9 @@
float4 viewPos = float4(UnityWorldToViewPos(float4(o.posWorld, 1)), 1);

UNITY_BRANCH if (!_ScreenReprojection) {
float2 screenUV = calculateScreenUVs(o.posWorld, v.uv);
int projectionType = _ProjectionType;
if (projectionType == PROJECTION_TRIPLANAR) projectionType = PROJECTION_FLAT;
float2 screenUV = calculateScreenUVs(_ProjectionType, o.posWorld, v.uv, 0, 0);
fixed allAmp = calculateFalloffAmplitude(screenUV, -1234);
float rotation = allAmp * _ScreenRotationAngle;
viewPos.xy = rotate(viewPos.xy, rotation);
Expand Down Expand Up @@ -736,7 +748,15 @@
VRFix = .5;
#endif

float2 screenSpaceOverlayUV = calculateScreenUVs(i.posWorld, i.uv);
float3 triplanarWorld = depth * normalize(i.posWorld - _WorldSpaceCameraPos) + _WorldSpaceCameraPos;

float3 triplanarNormal;
if (isInMirror()) triplanarNormal = cross(ddx(triplanarWorld), ddy(triplanarWorld));
else triplanarNormal = cross(-ddx(triplanarWorld), ddy(triplanarWorld));
triplanarNormal = normalize(triplanarNormal);


float2 screenSpaceOverlayUV = calculateScreenUVs(_ProjectionType, i.posWorld, i.uv, triplanarWorld, triplanarNormal);
fixed allAmp = calculateFalloffAmplitude(screenSpaceOverlayUV, depth);

float2 distortion = 0;
Expand Down
78 changes: 78 additions & 0 deletions DepthEnabler.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1001 &100100000
Prefab:
m_ObjectHideFlags: 1
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications: []
m_RemovedComponents: []
m_ParentPrefab: {fileID: 0}
m_RootGameObject: {fileID: 1817648053211934}
m_IsPrefabParent: 1
--- !u!1 &1817648053211934
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 5
m_Component:
- component: {fileID: 4279043899762536}
- component: {fileID: 108660156176868420}
m_Layer: 0
m_Name: DepthEnabler
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4279043899762536
Transform:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1817648053211934}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!108 &108660156176868420
Light:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1817648053211934}
m_Enabled: 1
serializedVersion: 8
m_Type: 1
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_Intensity: 0.001
m_Range: 10
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 1
m_Resolution: 0
m_CustomResolution: -1
m_Strength: 0
m_Bias: 0
m_NormalBias: 0
m_NearPlane: 0.1
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 32768
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 0
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0
m_ShadowAngle: 0

0 comments on commit 37c0a64

Please sign in to comment.