Skip to content

Commit

Permalink
Add Cube Blitting
Browse files Browse the repository at this point in the history
Yeouch signed cube blitting sucks!

Almost wish I stayed with the numerical derivatives...  hell, a numerical derivative here would probably be cheaper...
  • Loading branch information
zalo committed Oct 2, 2017
1 parent 7feca75 commit 3134ca2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Assets/Volume/Resources/DistanceFieldTexture.shader
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
alpha += 0.2; //This is like the near clipping plane
}

for(int i=1; i<20; i++) {
for(int i=0; i<20; i++) {
float3 pos = startingPos - (viewDirection*alpha);
if(valid && (abs(pos.x)>0.501 || abs(pos.y)>0.501 || abs(pos.z)>0.501)){
valid = false;
Expand Down
63 changes: 63 additions & 0 deletions Assets/Volume/Resources/blitCube.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Shader "Unlit/blitCube"
{
Properties
{
_Center ("Center", Vector) = (0.5,0.5,0.5,0.0)
_Extent ("Extent", Vector) = (0.15,0.15,0.15,0.0)
}

SubShader
{
Lighting Off
Blend One Zero

Pass
{
CGPROGRAM
#include "UnityCustomRenderTexture.cginc"

#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag

float3 _Center;
float3 _Extent;

//WHY IS NEAREST POINT ON THE SURFACE OF A CUBE SO HARD
float4 frag(v2f_customrendertexture IN) : COLOR
{
float4 col = tex3D(_SelfTexture3D, IN.globalTexcoord + float3(0, 0, 0.5/_CustomRenderTextureDepth));
float3 boxSpacePos = (IN.globalTexcoord - _Center.xyz);
float distMultiplier = 1.0;

if(abs(boxSpacePos.x) < _Extent.x &&
abs(boxSpacePos.y) < _Extent.y &&
abs(boxSpacePos.z) < _Extent.z){
float maxCoord = max(max(abs(boxSpacePos.x),
abs(boxSpacePos.y)),
abs(boxSpacePos.z));
if(maxCoord == abs(boxSpacePos.x)){
boxSpacePos.x = sign(boxSpacePos.x) * _Extent.x;
}else if(maxCoord == abs(boxSpacePos.y)){
boxSpacePos.y = sign(boxSpacePos.y) * _Extent.y;
}else if(maxCoord == abs(boxSpacePos.z)){
boxSpacePos.z = sign(boxSpacePos.z) * _Extent.z;
}
distMultiplier *= -1.0;
} else {
boxSpacePos = float3(sign(boxSpacePos.x) * min(abs(boxSpacePos.x), _Extent.x),
sign(boxSpacePos.y) * min(abs(boxSpacePos.y), _Extent.y),
sign(boxSpacePos.z) * min(abs(boxSpacePos.z), _Extent.z));
}

float3 posOnSurface = (boxSpacePos+_Center.xyz);
float newDistance = length(IN.globalTexcoord-posOnSurface) * distMultiplier;
if(newDistance<col.a){
col.rgb = (IN.globalTexcoord-posOnSurface)/newDistance;
col.a = newDistance;
}
return col;
}
ENDCG
}
}
}
9 changes: 9 additions & 0 deletions Assets/Volume/Resources/blitCube.shader.meta

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

0 comments on commit 3134ca2

Please sign in to comment.