From a38f2ec889ab3111a27475200b92ab7fffc0c73c Mon Sep 17 00:00:00 2001 From: spatialfree Date: Mon, 6 Dec 2021 15:20:15 -0500 Subject: [PATCH] oriels --- Assets/floor.png | Bin 0 -> 7061 bytes Assets/oriel.hlsl | 93 ++++++++++++++++++++++++++++++++++++---------- Program.cs | 32 ++++++++-------- 3 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 Assets/floor.png diff --git a/Assets/floor.png b/Assets/floor.png new file mode 100644 index 0000000000000000000000000000000000000000..e1452d0e95e3345eac38540298eabdfe174ed4a3 GIT binary patch literal 7061 zcmeHLeQXnD7{39sIUpGhlnA=>reMVOu7!5=>@m7_V--5bsAEM1ukBrXti7vu*R>sS zjAam6_>e$UVDc?7ItkkX@q-VAkO`3yg@j;q;X_1F*#ejahtcP*wFAf0|G7W<_TKmR zywCf*zvtt9?^cwT%^a2eXf}c%qnxFV3IrJjkHe6xA@J9;WdCXC+~(X>a+PZ~=@Y_w z+Anw+eLNfi96_vw@d)j!W@OaM1UR0;x<5OFp`4$>=9yfCD`IDYTxqSy%&jeR`)aFw z7C%-vJ=+>5K|z?2X*3=V@e&!QFjX%J&&n{4p{j^nO<`58a?~z}3~JJw^aM6N8?}ml zmaK4;q$Wd;!h*6KA#pqwi|J!i^nw_`4Hk<9Cycn!r~?U|l;CALuH&VAMMYAFgOPk9 z7m+!EM-`p4SBT0Kh5?SIl*=63ubr1t+Xo_W1;Gt^0>4unww1Q!F6lTO86eEWxj}R* z$N=$3P>=;FC=9Yf8XhnajB>gD-K=o@Kkr|74sdK6)o z;OE$cgO(Xsm@pc22BVHJyA39iC?E-gj(~^cs;V>uub-Cb|0AzBnJnkxOsLAig&96c zR+^N4*fS_>7eazK7cvlxGpHt~qX1k07+4QbayR{u({3*p1(pi|CRNNVLY>8Sv%z9E z>x_DXnjWZC4H~kPlYps&k};{AnajME>?Knu#HkR9sv$$tKE+KG77+!1)W`T$n_y}Z zl>}Cf(IR6DK(tVpjb%A-XaX%)A{#a6Eh+z{C&YpbC>xx1JY^WJWbfaWS@8k3TUCKO z(jDd{$DoWrJ;Kuk2|DSL#_&+bL?~FZJZ&uqlChKn-Ujf6AWyH2u01)~F=tFiAxn52 zt$X&oGqvT-JX34yIK!BSed`x)+E8$VJ-vU!nZ^0XD$kb9@m@UBaH#&Hw#uf`V`ZM) z@i&{Rca<1>YdUXz*L~o!-2KV9rk$oEyMM{ZU)O$k+g725@4B2gJj-;#yZL=~)4EeT zMjv0@`_eP+{nsk?9^cXzJAE{F-|(@f!}g8brN)lNNtNQR3%c{HZtSWNTe4#FMceuD z?{#F~Zkgse_rlT2C#@rQm(UBZKhjax`c~UWI$^2HKhQF^d&hzcEBw0)bhik3$Du6D$Ln5;oa@}S-&+pUb)%$)zl9^ z_;X3$qM!eGec9D6)J>6{)z8Jv=O=eh%gbDSBbs$BbK1%E*W3Guo{6;wci!sVNx66u7HuX*T-sX25y!ud0%{F^y!LxNk zhu8xjw=X?$6HcB7Bc&b*K}O^%zYJt!^Ed<<)8=&8-0|jL&l*M#wdD-`qWA417uQ@t zbVz&8@KdX1Tvog_DTiKT7BpF-p~=CZK^@S{cmtaH zoD686&;&Jlw4l&ZL8}y6ebi0`?aB|_quK?L|uDPAcsDI#bAVQ0V`!^c2 zmjv*=_6`LkXg4DuL3>};J|}3YpnW>hKA8RYwXp8$_8jxe(Le&~CgLnEbL=j9wc#&p C{Pwp1 literal 0 HcmV?d00001 diff --git a/Assets/oriel.hlsl b/Assets/oriel.hlsl index eb99009..b74cb10 100644 --- a/Assets/oriel.hlsl +++ b/Assets/oriel.hlsl @@ -10,7 +10,7 @@ Texture2D tex; // : register(t0); SamplerState tex_s; // : register(s0); cbuffer BufferData : register(b3) { - float3 windDirection; + float3 position; float windStrength; }; @@ -85,38 +85,55 @@ float sdSphere(float3 p, float s) { return length(p) - s; } -float sdPlane(float3 p, float3 n, float h) -{ +float sdPlane(float3 p, float3 n, float h) { // n must be normalized return dot(p,n) + h; } -float sdBox(float3 p, float3 b) -{ +float sdBox(float3 p, float3 b) { float3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); } -float sdOctahedron(float3 p, float s) -{ +float sdOctahedron(float3 p, float s) { p = abs(p); return (p.x + p.y + p.z - s) * 0.57735027; } +float sdBoxFrame(float3 p, float3 b, float e) { + p = abs(p) - b; + float3 q = abs(p + e) - e; + return min( + min( + length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), + length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0) + ), + length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0) + ); +} + +float opRep(float3 p, float3 c) +{ + float3 q = modf(p + 0.5 * c, c) - 0.5 * c; + return sdSphere(q, 0.1); +} + float map(float3 pos) { // pos.x = _center.x + pos.x; // pos.y = _center.y + pos.y; // pos.z = _center.z - pos.z; - float sphere = sdSphere(pos + float3(0, 0.5, 0) - _center, 0.1); + // float sphere = sdSphere(pos + float3(0, 0.5, 0) - _center, 0.1); // return sdLink(pos, 0.1, 0.1, 0.1); - float octo = sdOctahedron(pos - _center, 0.1); + float octo = sdOctahedron(pos - _center - position, 0.2); + float frame = sdBoxFrame(pos - _center - position, float3(0.06, 0.06, 0.06), 0.004); // return lerp(sphere, octo, windStrength); - float plane = sdPlane(pos - _center, float3(0, 1, 0), 0); + float plane = sdPlane(pos - _center + float3(0, 1.5, 0), float3(0, 1, 0), 0); - float phere = lerp(plane, sphere, windStrength); - return min(phere, octo); - // return octo; + float blendd = lerp(octo, frame, windStrength); + return min(plane, blendd); + + // return opRep(pos - _center, float3(0, 0, 0)); } float3 calcNormal(float3 pos) @@ -145,6 +162,26 @@ float calcAO(float3 pos, float3 nor) return clamp(1.0 - 3.0 * occ, 0.0, 1.0) * (0.5 + 0.5 * nor.y); } +float calcShadow(float3 pos, float3 light) { + float3 rd = normalize(light - pos); + float3 ro = pos + rd * 0.1; + + float tmax = 100; + float t = 0.0; + for (int i = 0; i < 256; i++) { + float3 pos = ro + t * rd; + float h = map(pos); + if (h < 0.0001 || t > tmax) break; + t += h; + } + if (t < tmax) { + t = 0; + } else { + t = 1; + } + + return t; +} // float RayMarch(vec3 ro, vec3 rd) { // float dO=0.; @@ -176,7 +213,7 @@ float4 ps(psIn input) : SV_TARGET { // input.color = float4(float3(1,1,1) * max(tri_raycast(input.world, ray), 0.0), 1); // raymarch - float tmax = 3.0; + float tmax = 100; float t = 0.0; for (int i = 0; i < 256; i++) { float3 pos = ro + t * rd; @@ -186,16 +223,34 @@ float4 ps(psIn input) : SV_TARGET { } // shading/lighting - float3 col = float3(0.0, 0.0, 0.0); + float3 col = float3(0.5, 0.75, 0.9); if (t < tmax) { float3 pos = ro + t * rd; + float3 light = float3(0.0, 1.0, 0.0); + float3 lightDir = normalize(light - pos); float3 nor = calcNormal(pos); - float dif = clamp(dot(nor, float3(0.7, 0.6, 0.4)), 0.0, 1.0); - float amb = 0.5 + 0.5 * dot(nor, float3(0.0, 0.8, 0.6)); + float dif = clamp(dot(nor, lightDir), 0.0, 1.0); + float amb = 0.5 + 0.5 * dot(nor, lightDir); float ao = calcAO(pos, nor); - dif *= ao; - col = float3(0.2, 0.3, 0.4) * amb + float3(0.8, 0.7, 0.5) * dif; + float sh = calcShadow(pos, light); + dif *= ao * sh; + col = float3(0.1, 0.5, 0.3) * amb + float3(0.6, 0.8, 0.3) * dif; + + // float3 lightPos = float3(0, 3, 0); + // float3 rayo = pos; + // float3 rayd = normalize(lightPos - pos); + // float ttmax = 6.0; + // float tt = 0.0; + // for (int i = 0; i < 256; i++) { + // float3 pp = rayo + tt * rayd; + // float hh = map(pp); + // if (hh < 0.0001 || tt > tmax) break; + // tt += hh; + // } + // if (tt < length(lightPos - rayo)) { + // col *= 0.5; + // } } // input.color = float4(float3(1,1,1) * max(t, 0.0), 1); diff --git a/Program.cs b/Program.cs index b88b809..af4d76a 100644 --- a/Program.cs +++ b/Program.cs @@ -45,7 +45,10 @@ public class Mono { floor.AddBox(new Vec3(0.1f, scale / 2, scale), 1, new Vec3(scale / 2, scale / 4, 0)); // and ceiling floor.AddBox(new Vec3(scale, 0.1f, scale), 1, new Vec3(0, scale / 2, 0)); - + Material matFloor = new Material(Shader.Default); + matFloor.SetTexture("diffuse", Tex.FromFile("floor.png")); + matFloor.SetFloat("tex_scale", 32); + Cursors cursors = new Cursors(this); @@ -53,10 +56,10 @@ public class Mono { oriel.Start(3); - // Oriel otherOriel = new Oriel(); - // otherOriel.Start(4); + // Oriel otherOriel = new Oriel(); + // otherOriel.Start(4); - MonoNet net = new MonoNet(this); + MonoNet net = new MonoNet(this); net.Start(); ColorCube colorCube = new ColorCube(); @@ -99,7 +102,7 @@ public class Mono { while (SK.Step(() => { Renderer.CameraRoot = Matrix.T(pos); - cube.Draw(mat, floor.GetPose().ToMatrix(floorScale), Color.White * 0.666f); + cube.Draw(matFloor, floor.GetPose().ToMatrix(floorScale), Color.White * 0.666f); if (lefty) { domCon = Input.Controller(Handed.Left); subCon = Input.Controller(Handed.Right); } else { domCon = Input.Controller(Handed.Right); subCon = Input.Controller(Handed.Left); } @@ -404,7 +407,6 @@ public class Mono { draggingOriel = true; } } else { - oriel.bounds.center = Vec3.Lerp(net.me.cursor0, net.me.cursor3, 0.5f); // float distX = Math.Abs(net.me.cursor0.x - net.me.cursor3.x); @@ -416,7 +418,7 @@ public class Mono { draggingOriel = false; } - oriel.Step(); + oriel.Step(net.me.cursor0); // otherOriel.bounds.center = Vec3.Forward * -2; // otherOriel.Step(); @@ -597,7 +599,7 @@ public class Lerper { [StructLayout(LayoutKind.Sequential)] struct BufferData { - public Vec3 windDirection; + public Vec3 position; // public Vec3[] tri; public float windStrength; } @@ -612,18 +614,14 @@ public class Oriel { MaterialBuffer buffer; public void Start(int bufferIndex) { - bounds = new Bounds(Vec3.Forward * 2, new Vec3(1f, 0.5f, 0.5f)); + bounds = new Bounds(Vec3.Forward * 0, new Vec3(1f, 0.5f, 0.5f)); _dimensions = bounds.dimensions; buffer = new MaterialBuffer(bufferIndex); } BufferData data = new BufferData(); - public void Step() { - data.windDirection = new Vec3( - (float)Math.Sin(Time.Total * 1) / 6, - (float)Math.Cos(Time.Total * 2) / 6, - (float)Math.Sin(Time.Total * 3) / 6 - ); + public void Step(Vec3 p0) { + data.position = p0; // data.a = new Color(1.0f, 0.5f, 0.5f); // data.b = new Color(0.5f, 1.0f, 0.5f); // data.c = new Color(0.5f, 0.5f, 1.0f); @@ -632,7 +630,7 @@ public class Oriel { // new Vec3(0, 0, 1), // new Vec3(1, 0, 0), // }; - + data.windStrength = (1 + (float)Math.Sin(Time.Total)) / 2; buffer.Set(data); @@ -644,7 +642,7 @@ public class Oriel { mat.FaceCull = Cull.None; mat.SetVector("_dimensions", bounds.dimensions); - mat.SetVector("_center", bounds.center); + mat.SetVector("_center", Vec3.Zero); // mat.Wireframe = true; Matrix m = Matrix.TRS(bounds.center, Quat.Identity, bounds.dimensions);