Raymarcher cleanup

This commit is contained in:
Crispy 2023-07-22 23:48:05 +02:00
parent 08bb09a5da
commit 57d07d2258
6 changed files with 7 additions and 207 deletions

View file

@ -64,7 +64,7 @@ Material:
- _Glossiness: 0.5
- _GlossyReflections: 1
- _MaxDist: 100
- _MaxSteps: 256
- _MaxSteps: 128
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
@ -72,7 +72,7 @@ Material:
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _SurfDist: 0.001
- _SurfDist: 0.01
- _UVSec: 0
- _ZWrite: 1
m_Colors:

View file

@ -64,7 +64,7 @@ Material:
- _Glossiness: 0.5
- _GlossyReflections: 1
- _MaxDist: 100
- _MaxSteps: 256
- _MaxSteps: 128
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
@ -72,7 +72,7 @@ Material:
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _SurfDist: 0.001
- _SurfDist: 0.01
- _UVSec: 0
- _ZWrite: 1
m_Colors:

View file

@ -1,192 +0,0 @@
Shader "CrispyPin/Raymarch1"
{
Properties
{
[Header(Raymarcher Properties)]
_MaxSteps ("Max steps", Int) = 256
_MaxDist ("Max distance", Float) = 100
_SurfDist ("Surface distance threshold", Range(0.00001, 0.05)) = 0.001
}
SubShader
{
Tags { "RenderType"="Opaque" }
Cull Off
LOD 100
/* Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 ro : TEXCOORD1;
float3 hitPos : TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//object space
o.ro = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1));
o.hitPos = v.vertex;
//world space
//o.ro = _WorldSpaceCameraPos;
//o.hitPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
discard;
return fixed4(0,0,0,0);
}
ENDCG
} */
Pass
{
Name "AddPass"
Tags { "LightMode" = "ForwardAdd"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 ro : TEXCOORD1;
float3 hitPos : TEXCOORD2;
};
int _MaxSteps;
float _MaxDist;
float _SurfDist;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//object space
o.ro = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1));
o.hitPos = v.vertex;
//world space
//o.ro = _WorldSpaceCameraPos;
//o.hitPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
float smoothMin(float a, float b, float k)
{
return min(a, b) - pow(max(k - abs(a-b), 0), 3)/(6*k*k);
}
float sdSphere(float3 p, float3 o, float r) {
return length(p - o) - r;
}
float sdBox(float3 p, float3 dim)
{
return length(float3(
max(abs(p.x) - dim.x/2.0, 0),
max(abs(p.y) - dim.y/2.0, 0),
max(abs(p.z) - dim.z/2.0, 0)));
}
float3 repDomain(float3 p, float3 r)
{
return fmod(abs(p + r/2.0), r) - r/2.0;
}
float GetDist(float3 p)
{
/* float d = 0;
float s = sin(100*_Time + p.x*10) * 0.02 + 0.028;
float s2 = cos(105*_Time + p.x*10) * 0.02 + 0.028;
//s += sin(p.x/10)*0.03;
float3 rep = repDomain(p, float3(0.026, 0.15, 0.2));// fmod(abs(p + 0.05), 0.1)-0.05;
d = sdBox(rep, float3(0.02, s2, s)) - 0.004;
d = smoothMin(d, sdBox(rep, float3(0.01, s*1.0, s2*1.5)) - 0.004, 0.01);
*/
float d = sdSphere(p, 0, 0.1f);
// float3 second = ObjSpaceLightDir(0);
// float3 second = _WorldSpaceLightPos0;
float3 second = mul(unity_WorldToObject, _WorldSpaceLightPos0);
d = smoothMin(d, sdBox(p-second, 0.3f), 0.2);
//d = smoothMin(d, sdSphere(p, float3(0,0,0), 5), .3);
return d;
}
//marches a ray through the scene
float Raymarch(float3 ro, float3 rd)
{
float rayLen = 0;// total distance marched / distance from origin
float dist; // distance from the raymarched scene
for ( int i = 0; i < _MaxSteps; i++)
{
//position = origin + distance * direction
float3 p = ro + rayLen * rd;
dist = GetDist(p);
rayLen += dist;// move forward
if (dist < _SurfDist || rayLen > _MaxDist) {
break;
}
}
return rayLen;
}
float3 GetNormal(float3 p)
{
float2 e = float2(0.001, 0);
float3 n = GetDist(p) - float3(
GetDist(p-e.xyy),
GetDist(p-e.yxy),
GetDist(p-e.yyx));
return normalize(n);
}
fixed4 frag (v2f i) : SV_Target
{
float3 ro = i.ro;
float3 rd = normalize(i.hitPos - ro);
float d = Raymarch(ro, rd);
fixed4 col = 1;
if (d >= _MaxDist)
{
discard;
}
float3 p = ro + rd * d;
float3 n = GetNormal(p);
col.rgb = dot(n, normalize(float3(1,0.5,1)));
col *= float4(d, 1, 1, 1);
return col;
}
ENDCG
}
}
}

View file

@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: f9118b166a8a02f3a8984624e2611e6c
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,4 +1,4 @@
Shader "CrispyPin/Raymarch1"
Shader "CrispyPin/RaymarchOrigin"
{
Properties
{

View file

@ -170,7 +170,8 @@ fragOut frag (v2f i) {
float3 n = GetNormal(p);
// col.rgb = max(dot(n, normalize(float3(1,0.5,1))), 0.05f);
col.r = dot(-rd, n);
col.rgb = lerp(0, float3(0,1,1), pow(1 - dot(-rd, n), 2));
// col *= float4(d, 1, 1, 1);
fragOut o;