oriels
This commit is contained in:
parent
796803506c
commit
a38f2ec889
3 changed files with 89 additions and 36 deletions
BIN
Assets/floor.png
Normal file
BIN
Assets/floor.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.9 KiB |
|
@ -10,7 +10,7 @@ Texture2D tex; // : register(t0);
|
||||||
SamplerState tex_s; // : register(s0);
|
SamplerState tex_s; // : register(s0);
|
||||||
|
|
||||||
cbuffer BufferData : register(b3) {
|
cbuffer BufferData : register(b3) {
|
||||||
float3 windDirection;
|
float3 position;
|
||||||
float windStrength;
|
float windStrength;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -85,38 +85,55 @@ float sdSphere(float3 p, float s) {
|
||||||
return length(p) - 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
|
// n must be normalized
|
||||||
return dot(p,n) + h;
|
return dot(p,n) + h;
|
||||||
}
|
}
|
||||||
|
|
||||||
float sdBox(float3 p, float3 b)
|
float sdBox(float3 p, float3 b) {
|
||||||
{
|
|
||||||
float3 q = abs(p) - b;
|
float3 q = abs(p) - b;
|
||||||
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
|
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);
|
p = abs(p);
|
||||||
return (p.x + p.y + p.z - s) * 0.57735027;
|
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) {
|
float map(float3 pos) {
|
||||||
// pos.x = _center.x + pos.x;
|
// pos.x = _center.x + pos.x;
|
||||||
// pos.y = _center.y + pos.y;
|
// pos.y = _center.y + pos.y;
|
||||||
// pos.z = _center.z - pos.z;
|
// 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);
|
// 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);
|
// 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);
|
float blendd = lerp(octo, frame, windStrength);
|
||||||
return min(phere, octo);
|
return min(plane, blendd);
|
||||||
// return octo;
|
|
||||||
|
// return opRep(pos - _center, float3(0, 0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
float3 calcNormal(float3 pos)
|
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);
|
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 RayMarch(vec3 ro, vec3 rd) {
|
||||||
// float dO=0.;
|
// 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);
|
// input.color = float4(float3(1,1,1) * max(tri_raycast(input.world, ray), 0.0), 1);
|
||||||
|
|
||||||
// raymarch
|
// raymarch
|
||||||
float tmax = 3.0;
|
float tmax = 100;
|
||||||
float t = 0.0;
|
float t = 0.0;
|
||||||
for (int i = 0; i < 256; i++) {
|
for (int i = 0; i < 256; i++) {
|
||||||
float3 pos = ro + t * rd;
|
float3 pos = ro + t * rd;
|
||||||
|
@ -186,16 +223,34 @@ float4 ps(psIn input) : SV_TARGET {
|
||||||
}
|
}
|
||||||
|
|
||||||
// shading/lighting
|
// shading/lighting
|
||||||
float3 col = float3(0.0, 0.0, 0.0);
|
float3 col = float3(0.5, 0.75, 0.9);
|
||||||
if (t < tmax)
|
if (t < tmax)
|
||||||
{
|
{
|
||||||
float3 pos = ro + t * rd;
|
float3 pos = ro + t * rd;
|
||||||
|
float3 light = float3(0.0, 1.0, 0.0);
|
||||||
|
float3 lightDir = normalize(light - pos);
|
||||||
float3 nor = calcNormal(pos);
|
float3 nor = calcNormal(pos);
|
||||||
float dif = clamp(dot(nor, float3(0.7, 0.6, 0.4)), 0.0, 1.0);
|
float dif = clamp(dot(nor, lightDir), 0.0, 1.0);
|
||||||
float amb = 0.5 + 0.5 * dot(nor, float3(0.0, 0.8, 0.6));
|
float amb = 0.5 + 0.5 * dot(nor, lightDir);
|
||||||
float ao = calcAO(pos, nor);
|
float ao = calcAO(pos, nor);
|
||||||
dif *= ao;
|
float sh = calcShadow(pos, light);
|
||||||
col = float3(0.2, 0.3, 0.4) * amb + float3(0.8, 0.7, 0.5) * dif;
|
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);
|
// input.color = float4(float3(1,1,1) * max(t, 0.0), 1);
|
||||||
|
|
32
Program.cs
32
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));
|
floor.AddBox(new Vec3(0.1f, scale / 2, scale), 1, new Vec3(scale / 2, scale / 4, 0));
|
||||||
// and ceiling
|
// and ceiling
|
||||||
floor.AddBox(new Vec3(scale, 0.1f, scale), 1, new Vec3(0, scale / 2, 0));
|
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);
|
Cursors cursors = new Cursors(this);
|
||||||
|
|
||||||
|
@ -53,10 +56,10 @@ public class Mono {
|
||||||
oriel.Start(3);
|
oriel.Start(3);
|
||||||
|
|
||||||
|
|
||||||
// Oriel otherOriel = new Oriel();
|
// Oriel otherOriel = new Oriel();
|
||||||
// otherOriel.Start(4);
|
// otherOriel.Start(4);
|
||||||
|
|
||||||
MonoNet net = new MonoNet(this);
|
MonoNet net = new MonoNet(this);
|
||||||
net.Start();
|
net.Start();
|
||||||
|
|
||||||
ColorCube colorCube = new ColorCube();
|
ColorCube colorCube = new ColorCube();
|
||||||
|
@ -99,7 +102,7 @@ public class Mono {
|
||||||
while (SK.Step(() => {
|
while (SK.Step(() => {
|
||||||
Renderer.CameraRoot = Matrix.T(pos);
|
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); }
|
if (lefty) { domCon = Input.Controller(Handed.Left); subCon = Input.Controller(Handed.Right); }
|
||||||
else { domCon = Input.Controller(Handed.Right); subCon = Input.Controller(Handed.Left); }
|
else { domCon = Input.Controller(Handed.Right); subCon = Input.Controller(Handed.Left); }
|
||||||
|
@ -404,7 +407,6 @@ public class Mono {
|
||||||
draggingOriel = true;
|
draggingOriel = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
oriel.bounds.center = Vec3.Lerp(net.me.cursor0, net.me.cursor3, 0.5f);
|
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);
|
float distX = Math.Abs(net.me.cursor0.x - net.me.cursor3.x);
|
||||||
|
@ -416,7 +418,7 @@ public class Mono {
|
||||||
draggingOriel = false;
|
draggingOriel = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
oriel.Step();
|
oriel.Step(net.me.cursor0);
|
||||||
|
|
||||||
// otherOriel.bounds.center = Vec3.Forward * -2;
|
// otherOriel.bounds.center = Vec3.Forward * -2;
|
||||||
// otherOriel.Step();
|
// otherOriel.Step();
|
||||||
|
@ -597,7 +599,7 @@ public class Lerper {
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
struct BufferData {
|
struct BufferData {
|
||||||
public Vec3 windDirection;
|
public Vec3 position;
|
||||||
// public Vec3[] tri;
|
// public Vec3[] tri;
|
||||||
public float windStrength;
|
public float windStrength;
|
||||||
}
|
}
|
||||||
|
@ -612,18 +614,14 @@ public class Oriel {
|
||||||
MaterialBuffer<BufferData> buffer;
|
MaterialBuffer<BufferData> buffer;
|
||||||
|
|
||||||
public void Start(int bufferIndex) {
|
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;
|
_dimensions = bounds.dimensions;
|
||||||
buffer = new MaterialBuffer<BufferData>(bufferIndex);
|
buffer = new MaterialBuffer<BufferData>(bufferIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferData data = new BufferData();
|
BufferData data = new BufferData();
|
||||||
public void Step() {
|
public void Step(Vec3 p0) {
|
||||||
data.windDirection = new Vec3(
|
data.position = p0;
|
||||||
(float)Math.Sin(Time.Total * 1) / 6,
|
|
||||||
(float)Math.Cos(Time.Total * 2) / 6,
|
|
||||||
(float)Math.Sin(Time.Total * 3) / 6
|
|
||||||
);
|
|
||||||
// data.a = new Color(1.0f, 0.5f, 0.5f);
|
// data.a = new Color(1.0f, 0.5f, 0.5f);
|
||||||
// data.b = new Color(0.5f, 1.0f, 0.5f);
|
// data.b = new Color(0.5f, 1.0f, 0.5f);
|
||||||
// data.c = new Color(0.5f, 0.5f, 1.0f);
|
// data.c = new Color(0.5f, 0.5f, 1.0f);
|
||||||
|
@ -632,7 +630,7 @@ public class Oriel {
|
||||||
// new Vec3(0, 0, 1),
|
// new Vec3(0, 0, 1),
|
||||||
// new Vec3(1, 0, 0),
|
// new Vec3(1, 0, 0),
|
||||||
// };
|
// };
|
||||||
|
|
||||||
data.windStrength = (1 + (float)Math.Sin(Time.Total)) / 2;
|
data.windStrength = (1 + (float)Math.Sin(Time.Total)) / 2;
|
||||||
buffer.Set(data);
|
buffer.Set(data);
|
||||||
|
|
||||||
|
@ -644,7 +642,7 @@ public class Oriel {
|
||||||
|
|
||||||
mat.FaceCull = Cull.None;
|
mat.FaceCull = Cull.None;
|
||||||
mat.SetVector("_dimensions", bounds.dimensions);
|
mat.SetVector("_dimensions", bounds.dimensions);
|
||||||
mat.SetVector("_center", bounds.center);
|
mat.SetVector("_center", Vec3.Zero);
|
||||||
// mat.Wireframe = true;
|
// mat.Wireframe = true;
|
||||||
|
|
||||||
Matrix m = Matrix.TRS(bounds.center, Quat.Identity, bounds.dimensions);
|
Matrix m = Matrix.TRS(bounds.center, Quat.Identity, bounds.dimensions);
|
||||||
|
|
Loading…
Add table
Reference in a new issue