fix raymarching depth; add old rm shaders
This commit is contained in:
parent
9034d9e2a2
commit
4695be0423
29 changed files with 2242 additions and 7 deletions
81
Assets/raymarched/Interactive/Raymarch_multi.mat
Normal file
81
Assets/raymarched/Interactive/Raymarch_multi.mat
Normal file
|
@ -0,0 +1,81 @@
|
|||
%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: Raymarch_multi
|
||||
m_Shader: {fileID: 4800000, guid: d9b470f9858f75d04aa12fe1b15b6ccb, 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
|
||||
- _MaxDist: 100
|
||||
- _MaxSteps: 256
|
||||
- _Metallic: 0
|
||||
- _Mode: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.02
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
- _SrcBlend: 1
|
||||
- _SurfDist: 0.001
|
||||
- _UVSec: 0
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
- _ObjectA: {r: -0.07, g: 0, b: 0, a: 1}
|
8
Assets/raymarched/Interactive/Raymarch_multi.mat.meta
Normal file
8
Assets/raymarched/Interactive/Raymarch_multi.mat.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 035a99d20f5635cd7a7cbba57b7068bc
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 2100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
80
Assets/raymarched/Interactive/Raymarch_origin.mat
Normal file
80
Assets/raymarched/Interactive/Raymarch_origin.mat
Normal file
|
@ -0,0 +1,80 @@
|
|||
%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: Raymarch_origin
|
||||
m_Shader: {fileID: 4800000, guid: 201705fcbc20f869eb5efb98f515535d, 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
|
||||
- _MaxDist: 100
|
||||
- _MaxSteps: 128
|
||||
- _Metallic: 0
|
||||
- _Mode: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.02
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
- _SrcBlend: 1
|
||||
- _SurfDist: 0.01
|
||||
- _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
Assets/raymarched/Interactive/Raymarch_origin.mat.meta
Normal file
8
Assets/raymarched/Interactive/Raymarch_origin.mat.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 84c53e5d5a6cb7acfaefff643285aa1b
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 2100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
80
Assets/raymarched/Interactive/Raymarch_secondary.mat
Normal file
80
Assets/raymarched/Interactive/Raymarch_secondary.mat
Normal file
|
@ -0,0 +1,80 @@
|
|||
%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: Raymarch_secondary
|
||||
m_Shader: {fileID: 4800000, guid: a0417c3af2dc9eeb08d9bc5c14ede4e0, type: 3}
|
||||
m_ShaderKeywords:
|
||||
m_LightmapFlags: 4
|
||||
m_EnableInstancingVariants: 0
|
||||
m_DoubleSidedGI: 0
|
||||
m_CustomRenderQueue: 3000
|
||||
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
|
||||
- _MaxDist: 100
|
||||
- _MaxSteps: 128
|
||||
- _Metallic: 0
|
||||
- _Mode: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.02
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
- _SrcBlend: 1
|
||||
- _SurfDist: 0.01
|
||||
- _UVSec: 0
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 42ff39292c8dca070a3e11f533bd9c53
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 2100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
56
Assets/raymarched/Interactive/raymarch_origin.shader
Normal file
56
Assets/raymarched/Interactive/raymarch_origin.shader
Normal file
|
@ -0,0 +1,56 @@
|
|||
Shader "CrispyPin/RaymarchOrigin"
|
||||
{
|
||||
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
|
||||
{
|
||||
// TODO: does this even do anything? its supposed to be on by default
|
||||
ZWrite On
|
||||
CGPROGRAM
|
||||
#pragma vertex vertex
|
||||
#pragma fragment fragment
|
||||
|
||||
#define BASE_PASS
|
||||
#include "raymarcher.cginc"
|
||||
|
||||
v2f vertex (appdata v){
|
||||
return vert(v);
|
||||
}
|
||||
|
||||
fragOut fragment (v2f i) {
|
||||
return frag(i);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
|
||||
Pass
|
||||
{
|
||||
Tags { "LightMode" = "ForwardAdd"}
|
||||
ZWrite On
|
||||
CGPROGRAM
|
||||
#pragma vertex vertex
|
||||
#pragma fragment fragment
|
||||
|
||||
#include "raymarcher.cginc"
|
||||
|
||||
v2f vertex (appdata v){
|
||||
return vert(v);
|
||||
}
|
||||
|
||||
fragOut fragment (v2f i) {
|
||||
return frag(i);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 201705fcbc20f869eb5efb98f515535d
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
37
Assets/raymarched/Interactive/raymarch_secondary.shader
Normal file
37
Assets/raymarched/Interactive/raymarch_secondary.shader
Normal file
|
@ -0,0 +1,37 @@
|
|||
Shader "CrispyPin/Raymarch2"
|
||||
{
|
||||
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 vertex
|
||||
#pragma fragment fragment
|
||||
|
||||
#define IS_SECONDARY_SHAPE
|
||||
#include "raymarcher.cginc"
|
||||
|
||||
v2f vertex (appdata v){
|
||||
return vert(v);
|
||||
}
|
||||
|
||||
fragOut fragment (v2f i) {
|
||||
return frag(i);
|
||||
// fragOut o = frag(i);
|
||||
// o.col = 0.4f;
|
||||
// return o;
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a0417c3af2dc9eeb08d9bc5c14ede4e0
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
189
Assets/raymarched/Interactive/raymarcher.cginc
Normal file
189
Assets/raymarched/Interactive/raymarcher.cginc
Normal file
|
@ -0,0 +1,189 @@
|
|||
#ifndef RAY_MARCH_LIB_INCLUDED
|
||||
#define RAY_MARCH_LIB_INCLUDED
|
||||
/*
|
||||
define IS_SECONDARY_SHAPE to only render the second shape
|
||||
*/
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
#include "UnityLightingCommon.cginc"
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float4 vertex : SV_POSITION;
|
||||
float3 ro : TEXCOORD1;
|
||||
float3 hitPos : TEXCOORD2;
|
||||
};
|
||||
|
||||
struct fragOut
|
||||
{
|
||||
fixed4 col : SV_Target;
|
||||
float depth : SV_Depth;
|
||||
};
|
||||
|
||||
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 smin(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)));
|
||||
}
|
||||
|
||||
float sdLine(float3 p, float3 a, float3 b, float r) {
|
||||
float3 pa = p - a;
|
||||
float3 ba = b - a;
|
||||
float h = clamp(dot(pa, ba) / dot(ba, ba), 0, 1);
|
||||
return length(pa - ba * h) - r;
|
||||
}
|
||||
|
||||
float sdCappedTorus( float3 p, float2 sc, float ra, float rb) {
|
||||
p.x = abs(p.x);
|
||||
float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
|
||||
return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
|
||||
}
|
||||
|
||||
float3 RotateY(float3 p, float a) {
|
||||
return mul(float3x3(cos(a), 0, sin(a), 0, 1, 0, -sin(a), 0, cos(a)), p);
|
||||
}
|
||||
|
||||
float Kijetesantakalu(float3 p, float r) {
|
||||
float d = sdSphere(p, float3(0, 0, 0.2f), r);// left eye
|
||||
d = min(d, sdSphere(p, float3(0.15f, 0, 0.2f), r)); // right eye
|
||||
d = min(d, sdLine(p, float3(0, 0, -0.4f), float3(-0.35f, 0, -0.4f), r)); // bottom line
|
||||
d = smin(d, sdLine(p, float3(0, 0, -0.4f), float3(-0.1f, 0, -0.15f), r), 0.003f); // leg hook
|
||||
d = min(d, sdLine(p, float3(0.15f, 0, -0.4f), float3(0.15f, 0, -0.1f), r)); // middle leg
|
||||
d = min(d, sdLine(p, float3(0.3f, 0, -0.4f), float3(0.3f, 0, 0), r)); // front leg
|
||||
d = smin(d, sdLine(p, float3(0.45f, 0, 0.1f), float3(0.3f, 0, 0), r), 0.003f); // lower snout
|
||||
d = smin(d, sdLine(p, float3(0.45f, 0, 0.1f), float3(0.2f, 0, 0.35f), r), 0.003f); // upper snout
|
||||
d = smin(d, sdLine(p, float3(0.2f, 0, 0.45f), float3(0.2f, 0, 0.35f), r), 0.003f); // ear
|
||||
float an = 0.9;
|
||||
d = smin(d, sdCappedTorus((RotateY(p, 0.8f) - float3(0.15f, 0, -0.05f)).xzy, float2(sin(an),cos(an)), 0.4, r), 0.003f); // ear
|
||||
d = smin(d, sdLine(p, float3(-0.25f, 0, 0), float3(-0.25f, 0, -0.4f), r), 0.003f); // back
|
||||
d = smin(d, sdLine(p, float3(-0.25f, 0, -0.1f), float3(-0.35f, 0, -0.1f), r), 0.003f); // tail top
|
||||
d = smin(d, sdLine(p, float3(-0.35f, 0, -0.1f), float3(-0.35f, 0, -0.4f), r), 0.003f); // stripe
|
||||
an = 3.1415f*0.5;
|
||||
d = smin(d, sdCappedTorus((RotateY(p, an) - float3(-0.25f, 0, 0.35f)).xzy, float2(sin(an),cos(an)), 0.15f, r), 0.003f); // tail
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
float FirstShape(float3 p) {
|
||||
return Kijetesantakalu(p, 0.05f);
|
||||
}
|
||||
|
||||
float SecondShape(float3 p) {
|
||||
// return sdBox(p, 0.3f) - 0.01f;
|
||||
// return Kijetesantakalu(p, 0.04f) - 0.01f;
|
||||
return sdSphere(p, 0, 0.3f);
|
||||
}
|
||||
|
||||
|
||||
float GetDist(float3 p) {
|
||||
#ifdef IS_SECONDARY_SHAPE
|
||||
return SecondShape(p);
|
||||
#else
|
||||
#ifdef BASE_PASS
|
||||
return FirstShape(p);
|
||||
#else
|
||||
float first = FirstShape(p);
|
||||
float3 second_pos = mul(unity_WorldToObject, _WorldSpaceLightPos0);
|
||||
float second = SecondShape(p - second_pos);
|
||||
return smin(first, second, 0.15f);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
fragOut frag (v2f i) {
|
||||
#ifndef IS_SECONDARY_SHAPE
|
||||
#ifndef BASE_PASS
|
||||
if (length(_LightColor0 - float4(0,0,1,0)) > 0.01f ){
|
||||
discard;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
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 = max(dot(n, normalize(float3(1,0.5,1))), 0.05f);
|
||||
|
||||
col.rgb = lerp(0, float3(0,1,1), pow(1 - dot(-rd, n), 3));
|
||||
|
||||
// col *= float4(d, 1, 1, 1);
|
||||
fragOut o;
|
||||
o.col = col;
|
||||
// o.col = _LightColor0;
|
||||
float4 vClipPos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(p, 1)));
|
||||
float zDepth = vClipPos.z / vClipPos.w;
|
||||
#if !defined(UNITY_REVERSED_Z) // basically only OpenGL (unity editor on linux)
|
||||
zDepth = zDepth * 0.5 + 0.5; // remap -1 to 1 range to 0.0 to 1.0
|
||||
#endif
|
||||
o.depth = zDepth;
|
||||
return o;
|
||||
}
|
||||
|
||||
#endif
|
9
Assets/raymarched/Interactive/raymarcher.cginc.meta
Normal file
9
Assets/raymarched/Interactive/raymarcher.cginc.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5ee593b026d03ab61b5bbf93bce72808
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
192
Assets/raymarched/Interactive/rm_combined.shader
Normal file
192
Assets/raymarched/Interactive/rm_combined.shader
Normal file
|
@ -0,0 +1,192 @@
|
|||
Shader "CrispyPin/RaymarchMultiObject"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
[Header(Object positions)]
|
||||
_ObjectA ("Object A", Vector) = (0, 0, 0)
|
||||
[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"
|
||||
#include "UnityLightingCommon.cginc"
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION;
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float4 vertex : SV_POSITION;
|
||||
float3 ro : TEXCOORD1;
|
||||
float3 hitPos : TEXCOORD2;
|
||||
};
|
||||
|
||||
struct fragOut
|
||||
{
|
||||
fixed4 col : SV_Target;
|
||||
float depth : SV_Depth;
|
||||
};
|
||||
|
||||
int _MaxSteps;
|
||||
float _MaxDist;
|
||||
float _SurfDist;
|
||||
float3 _ObjectA;
|
||||
|
||||
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 smin(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)));
|
||||
}
|
||||
|
||||
float sdLine(float3 p, float3 a, float3 b, float r) {
|
||||
float3 pa = p - a;
|
||||
float3 ba = b - a;
|
||||
float h = clamp(dot(pa, ba) / dot(ba, ba), 0, 1);
|
||||
return length(pa - ba * h) - r;
|
||||
}
|
||||
|
||||
float sdCappedTorus( float3 p, float2 sc, float ra, float rb) {
|
||||
p.x = abs(p.x);
|
||||
float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
|
||||
return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
|
||||
}
|
||||
|
||||
float3 RotateY(float3 p, float a) {
|
||||
return mul(float3x3(cos(a), 0, sin(a), 0, 1, 0, -sin(a), 0, cos(a)), p);
|
||||
}
|
||||
|
||||
float Kijetesantakalu(float3 p, float r) {
|
||||
float d = sdSphere(p, float3(0, 0, 0.2f), r);// left eye
|
||||
d = min(d, sdSphere(p, float3(0.15f, 0, 0.2f), r)); // right eye
|
||||
d = min(d, sdLine(p, float3(0, 0, -0.4f), float3(-0.35f, 0, -0.4f), r)); // bottom line
|
||||
d = smin(d, sdLine(p, float3(0, 0, -0.4f), float3(-0.1f, 0, -0.15f), r), 0.003f); // leg hook
|
||||
d = min(d, sdLine(p, float3(0.15f, 0, -0.4f), float3(0.15f, 0, -0.1f), r)); // middle leg
|
||||
d = min(d, sdLine(p, float3(0.3f, 0, -0.4f), float3(0.3f, 0, 0), r)); // front leg
|
||||
d = smin(d, sdLine(p, float3(0.45f, 0, 0.1f), float3(0.3f, 0, 0), r), 0.003f); // lower snout
|
||||
d = smin(d, sdLine(p, float3(0.45f, 0, 0.1f), float3(0.2f, 0, 0.35f), r), 0.003f); // upper snout
|
||||
d = smin(d, sdLine(p, float3(0.2f, 0, 0.45f), float3(0.2f, 0, 0.35f), r), 0.003f); // ear
|
||||
float an = 0.9;
|
||||
d = smin(d, sdCappedTorus((RotateY(p, 0.8f) - float3(0.15f, 0, -0.05f)).xzy, float2(sin(an),cos(an)), 0.4, r), 0.003f); // ear
|
||||
d = smin(d, sdLine(p, float3(-0.25f, 0, 0), float3(-0.25f, 0, -0.4f), r), 0.003f); // back
|
||||
d = smin(d, sdLine(p, float3(-0.25f, 0, -0.1f), float3(-0.35f, 0, -0.1f), r), 0.003f); // tail top
|
||||
d = smin(d, sdLine(p, float3(-0.35f, 0, -0.1f), float3(-0.35f, 0, -0.4f), r), 0.003f); // stripe
|
||||
an = 3.1415f*0.5;
|
||||
d = smin(d, sdCappedTorus((RotateY(p, an) - float3(-0.25f, 0, 0.35f)).xzy, float2(sin(an),cos(an)), 0.15f, r), 0.003f); // tail
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
float FirstShape(float3 p) {
|
||||
return Kijetesantakalu(p, 0.05f);
|
||||
}
|
||||
|
||||
float SecondShape(float3 p) {
|
||||
// return sdBox(p, 0.3f) - 0.01f;
|
||||
// return Kijetesantakalu(p, 0.04f) - 0.01f;
|
||||
return sdSphere(p, 0, 0.3f);
|
||||
}
|
||||
|
||||
|
||||
float GetDist(float3 p) {
|
||||
|
||||
float first = FirstShape(p);
|
||||
// float3 second_pos = mul(unity_WorldToObject, _WorldSpaceLightPos0);
|
||||
float second = SecondShape(p - _ObjectA);
|
||||
return smin(first, second, 0.15f);
|
||||
|
||||
}
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
fragOut frag (v2f i) {
|
||||
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 = max(dot(n, normalize(float3(1,0.5,1))), 0.05f);
|
||||
|
||||
col.rgb = lerp(0, float3(0,1,1), pow(1 - dot(-rd, n), 2));
|
||||
|
||||
// col *= float4(d, 1, 1, 1);
|
||||
fragOut o;
|
||||
o.col = col;
|
||||
// o.col = _LightColor0;
|
||||
float4 vClipPos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(p, 1)));
|
||||
o.depth = (vClipPos.z / vClipPos.w + 1.0) * 0.5;
|
||||
return o;
|
||||
}
|
||||
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
9
Assets/raymarched/Interactive/rm_combined.shader.meta
Normal file
9
Assets/raymarched/Interactive/rm_combined.shader.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d9b470f9858f75d04aa12fe1b15b6ccb
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue