matrix is a reserved name

This commit is contained in:
spatialfree 2022-03-21 21:47:14 -04:00
parent d115cbdce8
commit 908c6c3db8
8 changed files with 188 additions and 57 deletions

View file

@ -3,6 +3,7 @@
// --name = dofdev/oriel
float3 _center;
float3 _dimensions;
float3 _light;
float4x4 _matrix;
struct vsIn {
@ -81,9 +82,11 @@ psOut ps(psIn input) {
clip(distance(input.campos, input.world) - distance(input.campos, ro));
float t = 1 - (1 + dot(input.normal, normalize(input.world - _center))) / 2;
float3 light = lerp(float3(0.05, 0.05, 0.1), float3(0.95, 0.95, 0.9), t);
o.color = float4(o.color.rgb * light, 1);
// normalize(input.world - _center)
float t = 1 - (1 + dot(input.normal, _light)) / 2;
// float3 light = lerp(float3(0.05, 0.05, 0.1), float3(0.95, 0.95, 0.9), t);
// o.color = float4(o.color.rgb * light, 1);
o.color = float4(o.color.rgb * t, 1);
// backface

View file

@ -11,6 +11,12 @@ float tex_scale;
Texture2D diffuse : register(t0);
SamplerState diffuse_s : register(s0);
cbuffer BufferData : register(b3) {
float4x4 oriel_matrix;
float3 dimensions;
float time;
};
struct vsIn {
float4 pos : SV_Position;
float3 norm : NORMAL0;
@ -19,8 +25,11 @@ struct vsIn {
};
struct psIn {
float4 pos : SV_Position;
float3 world : WORLD;
float2 uv : TEXCOORD0;
float4 color : COLOR0;
float3 camdir : TEXCOORD1;
float3 campos : TEXCOORD2;
uint view_id : SV_RenderTargetArrayIndex;
};
@ -29,8 +38,11 @@ psIn vs(vsIn input, uint id : SV_InstanceID) {
o.view_id = id % sk_view_count;
id = id / sk_view_count;
float4 world = mul(input.pos, sk_inst[id].world);
o.pos = mul(world, sk_viewproj[o.view_id]);
o.camdir = sk_camera_dir[o.view_id].xyz;
o.campos = sk_camera_pos[o.view_id].xyz;
o.world = mul(input.pos, sk_inst[id].world).xyz;
o.pos = mul(float4(o.world, 1), sk_viewproj[o.view_id]);
float3 normal = normalize(mul(input.norm, (float3x3)sk_inst[id].world));
@ -40,8 +52,50 @@ psIn vs(vsIn input, uint id : SV_InstanceID) {
return o;
}
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 raymarch(float3 ro, float3 rd) {
ro = mul(float4(ro, 1), oriel_matrix).xyz;
rd = mul(float4(rd, 0), oriel_matrix).xyz;
float dist = 0.0;
for (int i = 0; i < 256; i++) {
float3 pos = ro + dist * rd;
float step = sdBox(pos, dimensions / 2.0);
if (step < 0.0001 || dist > 100) break; // 100 == distmax
dist += step;
}
return dist;
}
float4 ps(psIn input) : SV_TARGET {
float4 col = diffuse.Sample(diffuse_s, input.uv);
float value = (col.r + col.r + col.g + col.g + col.g + col.b) / 6;
return float4(value, value, value, 1);
float3 ro = input.campos;
float3 rd = normalize(input.world - ro);
float ol = raymarch(ro, rd);
clip(-(100 - (ol + 1)));
// if ((100 - (ol + 1)) > 0) {
// col *= 0.1;
// }
ro += ol * rd;
// clip((distance(input.campos, input.world) - distance(input.campos, ro)) * -1);
// if ((distance(input.campos, input.world) - distance(input.campos, ro)) >= 0) {
// col *= 0.1;
// }
// if (input.world.y < bufferCenter.y) {
// col *= 0.1;
// }
// float value = (col.r + col.r + col.g + col.g + col.g + col.b) / 6;
// return float4(value, value, value, 1);
return col;
}

View file

@ -132,8 +132,8 @@ public class Glove {
Material mat = Default.Material;
Model model = Model.FromFile("skinned_test.glb", Shader.Default);
public void Render(Pose glove, Pose virtualGlove, Pose wrist, float stretch, float twist, bool chirality) {
Lines.Add(pullPoint, glove.position, new Color(1, 0, 1), 0.005f);
Lines.Add(glove.position, virtualGlove.position, new Color(0, 1, 1), 0.005f);
Lines.Add(pullPoint, glove.position, new Color(1, 0, 1, 0.1f), 0.005f);
Lines.Add(glove.position, virtualGlove.position, new Color(0, 1, 1, 0.1f), 0.005f);
// Twist
float twistAbs = Math.Abs(twist);
@ -155,8 +155,8 @@ public class Glove {
}
Lines.Add(linePoints);
mesh.Draw(mat, glove.ToMatrix(new Vec3(0.02f, 0.08f, 0.08f) / 1));
mesh.Draw(mat, virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) / 1));
// mesh.Draw(mat, glove.ToMatrix(new Vec3(0.02f, 0.08f, 0.08f) / 1));
mesh.Draw(mat, virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) / 3));
// ModelNode top = model.FindNode("Top");

View file

@ -367,7 +367,7 @@ public class Peer {
mono.rCon.pos,
mono.lCon.pos,
mono.lGlove.virtualGlove.position,
Color.White
new Color(1, 1, 1, 0.1f)
);
for (int i = 0; i < blocks.Length; i++) {

View file

@ -11,7 +11,7 @@ if (!SK.Initialize(settings))
Environment.Exit(1);
Input.HandSolid(Handed.Max, false);
Input.HandVisible(Handed.Max, false);
Input.HandVisible(Handed.Max, true);
// TextStyle style = Text.MakeStyle(Font.FromFile("DMMono-Regular.ttf"), 0.1f, Color.White);
Monolith mono = new Monolith();
@ -491,10 +491,12 @@ public static class PullRequest {
);
}
static Bounds _bounds = new Bounds();
public static bool ActuallyContains(this Bounds bounds, Quat ori, Vec3 pos, float radius) {
_bounds.dimensions = bounds.dimensions;
Vec3 p = ori.Inverse * (pos - bounds.center);
return _bounds.Contains(p, p, radius);
public static float Lerp(float a, float b, float t) {
return a + (b - a) * t;
}
static Pose _pose = new Pose();
public static Pose WorldPose(this Pose pose, float scale = 1) {
return pose;
}
}

View file

@ -10,10 +10,11 @@ public class Oriel {
static Model model = Model.FromFile("colorball.glb");
Mesh meshCube, meshFrame;
public Bounds bounds; Vec3 _dimensions;
public Bounds bounds;
public Quat ori;
public Matrix matrix;
public float crown = 0.0666f;
public bool drawAxis;
public bool drawAxis = false;
bool adjusting = false;
// bool scalingOriel = false;
@ -21,16 +22,16 @@ public class Oriel {
// bool rotatingOriel = false;
Quat qOffset = Quat.Identity;
Vec3 vOffset = Vec3.Zero;
Vec3 lOffset = Vec3.Zero;
Vec3 anchor = Vec3.Zero;
Matrix mOffset = Matrix.Identity;
public Oriel() {
bounds = new Bounds(
Input.Head.position + new Vec3(0, 0f, -1.5f),
Input.Head.position + new Vec3(-0.5f, 0, -1f),
new Vec3(0.8f, 0.5f, 0.5f)
);
_dimensions = bounds.dimensions;
ori = Quat.Identity;
matFrame.SetMat(102, Cull.None, true);
@ -43,49 +44,75 @@ public class Oriel {
Gen();
}
public class Transform {
public string name;
public Pose pose;
public float scale;
public Transform() {
}
public Vec3 LocalPos() {
return pose.position;
}
}
Vec3 detect = Vec3.Zero;
int detectCount = 0;
public void Step(Monolith mono) {
Matrix matrix = Matrix.TR(bounds.center, ori).Inverse;
matrix = Matrix.TR(bounds.center, ori).Inverse;
Vec3 rGlovePos = mono.rGlove.virtualGlove.position;
Quat rGloveRot = mono.rGlove.virtualGlove.orientation;
// Vec3 lGlovePos = mono.lGlove.virtualGlove.position;
// face detection = (1 axis)
// edge detection = (2 axis)
// corner detection = (3 axis)
// Pose pose = new Pose();
Vec3 localPos = matrix.Transform(rGlovePos);
Vec3 localPos = ori.Inverse * (rGlovePos - bounds.center);
if (!mono.rCon.triggerBtn.held) {
float margin = PullRequest.Lerp(0.03f, 0.5f, bounds.dimensions.y / 2);
Vec3 newDetect = Vec3.Zero;
if ((_dimensions.x / 2) - MathF.Abs(localPos.x) < 0.1f) newDetect.x = 1 * MathF.Sign(localPos.x);
if ((_dimensions.y / 2) - MathF.Abs(localPos.y) < 0.1f) newDetect.y = 1 * MathF.Sign(localPos.y);
if ((_dimensions.z / 2) - MathF.Abs(localPos.z) < 0.1f) newDetect.z = 1 * MathF.Sign(localPos.z);
if ((bounds.dimensions.x / 2) - MathF.Abs(localPos.x) < 0) newDetect.x = 1 * MathF.Sign(localPos.x);
if ((bounds.dimensions.y / 2) - MathF.Abs(localPos.y) < 0) newDetect.y = 1 * MathF.Sign(localPos.y);
if ((bounds.dimensions.z / 2) - MathF.Abs(localPos.z) < 0) newDetect.z = 1 * MathF.Sign(localPos.z);
if (newDetect.x != detect.x || newDetect.y != detect.y || newDetect.z != detect.z) {
detect = newDetect;
detectCount = (int)(MathF.Abs(detect.x) + MathF.Abs(detect.y) + MathF.Abs(detect.z));
Console.WriteLine(detectCount + ": " + detect);
// Console.WriteLine(detectCount + ": " + detect);
}
if (!bounds.ActuallyContains(ori, rGlovePos, 0.1f) || bounds.ActuallyContains(ori, rGlovePos, 0f)) {
Vec3 dim = new Vec3(
bounds.dimensions.x + 0.1f,
bounds.dimensions.y + 0.1f,
bounds.dimensions.z + 0.1f
);
Bounds arounds = new Bounds(Vec3.Zero, dim);
if (!arounds.Contains(localPos) || bounds.Contains(bounds.center + localPos)) {
detect = Vec3.Zero;
detectCount = 0;
}
vOffset = rGlovePos - bounds.center;
lOffset = ori.Inverse * vOffset;
qOffset = (ori.Inverse * rGloveRot).Normalized;
mOffset = matrix;
// qOffset = Quat.LookAt(Vec3.Zero, vOffset.Normalized);
anchor = bounds.center + ori * -(detect * bounds.dimensions / 2);
anchor = bounds.center + ori * -(detect * bounds.dimensions / 2);
adjusting = false;
} else {
if (detectCount == 1) { // Move
bounds.center = rGlovePos - vOffset;
ori = (rGloveRot * qOffset.Inverse).Normalized;
bounds.center = rGlovePos - ori * lOffset;
} else if (detectCount == 2) { // Rotate
localPos = mOffset.Transform(rGlovePos);
Vec3 dir = new Vec3(
@ -154,21 +181,22 @@ public class Oriel {
matPanes["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(matrix);
meshCube.Draw(matPanes,
Matrix.TRS(bounds.center, ori, bounds.dimensions),
new Color(0.1f, 0.1f, 0.4f)
new Color(0.0f, 0.0f, 0.5f)
);
matOriel.SetVector("_center", bounds.center);
matOriel.SetVector("_dimensions", bounds.dimensions);
matOriel.SetVector("_light", ori * new Vec3(0.6f, -0.9f, 0.3f));
matOriel["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(matrix);
Matrix orielSimMatrix = Matrix.TRS(new Vec3(0, -bounds.dimensions.y / 2, 0), Quat.Identity, Vec3.One * 0.1f).Inverse;
Matrix orielSimMatrix = Matrix.TRS(new Vec3(0, -bounds.dimensions.y / 2, 0), Quat.Identity, Vec3.One * 0.5f * bounds.dimensions.y).Inverse;
if (drawAxis) {
@ -179,13 +207,13 @@ public class Oriel {
}
Mesh.Quad.Draw(matOriel,
Matrix.TRS(Vec3.Zero, Quat.FromAngles(90, 0, 0), Vec3.One * 20f) * orielSimMatrix.Inverse * matrix.Inverse,
new Color(0.3f, 0.9f, 0.3f)
Matrix.TRS(Vec3.Zero, Quat.FromAngles(90, 0, 0), Vec3.One * 100f) * orielSimMatrix.Inverse * matrix.Inverse,
new Color(1.0f, 1.0f, 1.0f) * 0.3f
);
meshCube.Draw(matOriel,
mono.rGlove.virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) * 1.05f),
new Color(0.5f, 0.5f, 0.9f)
mono.rGlove.virtualGlove.ToMatrix(new Vec3(0.025f, 0.1f, 0.1f) / 3 * 1.05f),
new Color(0.3f, 0.3f, 0.6f)
);
// draw relative to oriel matrix
@ -201,29 +229,33 @@ public class Oriel {
player.Move(playerPos + simOffset, Quat.Identity);
meshCube.Draw(matOriel,
Matrix.TRS(player.GetPose().position + (player.GetPose().orientation * Vec3.Up * 0.5f) - simOffset, player.GetPose().orientation, new Vec3(0.4f, 1f, 0.2f)) * orielSimMatrix.Inverse * matrix.Inverse,
new Color(0.9f, 0.5f, 0.5f)
new Color(1.0f, 0.0f, 0.05f)
);
// FULLSTICK
Vec3 Fullstick() {
Controller con = mono.lCon.device;
Quat rot = Quat.FromAngles(con.stick.y * -90, 0, con.stick.x * 90);
Vec3 dir = Vec3.Up * (con.IsStickClicked ? -1 : 1);
return con.aim.orientation * rot * dir;
}
Vec3 fullstick = Fullstick();
sword.Move(playerPos + simOffset + fullstick, Quat.LookAt(Vec3.Zero, fullstick, Vec3.Up));
meshCube.Draw(matOriel,
Matrix.TRS(sword.GetPose().position + (Vec3.Up * 0.7f) + (sword.GetPose().orientation * Vec3.Forward * 0.5f) - simOffset, sword.GetPose().orientation, new Vec3(0.1f, 0.03f, 1f)) * orielSimMatrix.Inverse * matrix.Inverse,
new Color(0.9f, 0.5f, 0.5f)
);
// Vec3 Fullstick() {
// Controller con = mono.lCon.device;
// Quat rot = Quat.FromAngles(con.stick.y * -90, 0, con.stick.x * 90);
// Vec3 dir = Vec3.Up * (con.IsStickClicked ? -1 : 1);
// return con.aim.orientation * rot * dir;
// }
// Vec3 fullstick = Fullstick();
// sword.Move(playerPos + simOffset + fullstick, Quat.LookAt(Vec3.Zero, fullstick, Vec3.Up));
// meshCube.Draw(matOriel,
// Matrix.TRS(sword.GetPose().position + (Vec3.Up * 0.7f) + (sword.GetPose().orientation * Vec3.Forward * -0.5f) - simOffset, sword.GetPose().orientation, new Vec3(0.1f, 0.03f, 1f)) * orielSimMatrix.Inverse * matrix.Inverse,
// new Color(0.9f, 0.5f, 0.5f)
// );
for (int i = 0; i < enemies.Count; i++) {
Solid enemy = enemies[i];
Pose pose = enemy.GetPose();
// move towards player
enemy.Move(pose.position + (playerPos - pose.position).Normalized * Time.Elapsedf * 0.5f, pose.orientation);
meshCube.Draw(matOriel,
Matrix.TRS(enemy.GetPose().position - simOffset, enemy.GetPose().orientation, new Vec3(0.4f, 1f, 0.2f)) * orielSimMatrix.Inverse * matrix.Inverse,
Color.White * 0.32f
Matrix.TRS(pose.position - simOffset, pose.orientation, new Vec3(0.4f, 1f, 0.2f) * 0.99f) * orielSimMatrix.Inverse * matrix.Inverse,
Color.White * 0.62f
);
}
}
@ -272,7 +304,7 @@ public class Oriel {
player.AddBox(new Vec3(0.4f, 1f, 0.2f), 1, new Vec3(0, 0.5f, 0));
sword = new Solid(simOffset + Vec3.Up, Quat.Identity, SolidType.Normal);
sword.AddBox(new Vec3(0.1f, 0.03f, 1f), 1, new Vec3(0, 0.5f, 0));
sword.AddBox(new Vec3(0.1f, 0.03f, 1f), 1, new Vec3(0, 0, -0.5f));
for (int i = 0; i < 32; i++) {
Solid solid = new Solid(

View file

@ -1,14 +1,32 @@
using System;
using System.Runtime.InteropServices;
using StereoKit;
[StructLayout(LayoutKind.Sequential)]
struct BufferData {
public Matrix matrix;
public Vec3 dimensions;
public float time;
}
public class Scene {
Monolith mono;
MaterialBuffer<BufferData> buffer;
BufferData data = new BufferData();
Material matFloor = new Material(Shader.Default);
Model room = Model.FromFile("room/room.glb", Shader.FromFile("room.hlsl"));
Solid floor;
public Scene(Monolith mono) {
this.mono = mono;
// Shader.FromFile("room.hlsl")
buffer = new MaterialBuffer<BufferData>(3); // index
floor = new Solid(World.BoundsPose.position, Quat.Identity, SolidType.Immovable);
scale = 64f;
floorScale = new Vec3(scale, 0.1f, scale);
@ -29,8 +47,30 @@ public class Scene {
public void Step() {
// PullRequest.BlockOut(floor.GetPose().ToMatrix(floorScale), Color.White * 0.333f, matFloor);
data.matrix = (Matrix)System.Numerics.Matrix4x4.Transpose(mono.oriel.matrix);
data.dimensions = mono.oriel.bounds.dimensions;
buffer.Set(data);
// PullRequest.BlockOut(floor.GetPose().ToMatrix(floorScale), Color.White * 0.333f, matFloor);
foreach (ModelNode node in room.Visuals) {
// Console.WriteLine(i + " - " + node.Name);
// node.Material.SetVector("_center", mono.oriel.bounds.center);
// node.Material.SetVector("_dimensions", mono.oriel.bounds.dimensions);
// node.Material["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(mono.oriel.matrix);
// Console.WriteLine("Shader: " + node.Material.Shader.Name);
// node.Mesh.Draw(matRoom, Matrix.TRS(new Vec3(0, World.BoundsPose.position.y, -1), Quat.Identity, Vec3.One));
// Console.WriteLine(matRoom.ParamCount + " test " + node.Material.ParamCount);
}
// room.RootNode.Material.SetVector("_center", mono.oriel.bounds.center);
// room.RootNode.Material.SetVector("_dimensions", mono.oriel.bounds.dimensions);
// room.RootNode.Material["_matrix"] = (Matrix)System.Numerics.Matrix4x4.Transpose(mono.oriel.matrix);
// Shader.
room.Draw(Matrix.TRS(new Vec3(0, World.BoundsPose.position.y, -1), Quat.Identity, Vec3.One));
}

View file

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="StereoKit" Version="0.3.5-preview.3" />
<PackageReference Include="StereoKit" Version="0.3.6-preview.3" />
<!-- <PackageReference Include="System.Speech" Version="5.0.0" /> -->
</ItemGroup>