From 39acb700e1cb5bca28e833193e36ed75e5f82574 Mon Sep 17 00:00:00 2001 From: spatialfree Date: Wed, 8 Jul 2020 16:57:56 -0700 Subject: [PATCH] new quad tool implemented but not integrated into serialization/save system --- Assets/Scenes/Main.unity | 13 ++- Assets/Scripts/AssetSpace.cs | 3 +- Assets/Scripts/Monolith.cs | 15 +++- Assets/Scripts/Pixelgon.cs | 2 +- Assets/Scripts/Render.cs | 56 +++++++++++- Assets/Scripts/Tools/ToolNewQuad.cs | 125 +++++++++++--------------- Assets/Scripts/Tools/ToolNewVector.cs | 4 +- 7 files changed, 139 insertions(+), 79 deletions(-) diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index ab8c59c..b801af2 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -196,11 +196,14 @@ MonoBehaviour: held: 0 up: 0 twistCursor: {x: 0, y: 0, z: 0} + stretchCursor: {x: 0, y: 0, z: 0} pixelgon: vectors: [] + quads: assetSpace: folderPath: C:/dofdev/Pixelgon/Assets/AssetSpace name: draft + loadedNames: [] toolNewVector: cursorPos: {x: 0, y: 0, z: 0} vector: [] @@ -211,6 +214,12 @@ MonoBehaviour: fromCorner: {x: 0, y: 0, z: 0} toCorner: {x: 0, y: 0, z: 0} selected: + toolNewQuad: + cursorPos: {x: 0, y: 0, z: 0} + indexQuad: 0 + quad: + tris: + uvs: [] toolEraser: cursorPos: {x: 0, y: 0, z: 0} selected: @@ -244,9 +253,11 @@ MonoBehaviour: type: 3} meshBoxSelect: {fileID: -9071155252399151903, guid: dc6240c783ee7974e8811c10e071e0cc, type: 3} + meshToolNewQuad: {fileID: 6254183033293086303, guid: dc6240c783ee7974e8811c10e071e0cc, + type: 3} meshToolEraser: {fileID: -2868880653068191277, guid: dc6240c783ee7974e8811c10e071e0cc, type: 3} - scene: 0 + scene: 1 --- !u!1 &162671503 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/AssetSpace.cs b/Assets/Scripts/AssetSpace.cs index 7c85551..a09c7b4 100644 --- a/Assets/Scripts/AssetSpace.cs +++ b/Assets/Scripts/AssetSpace.cs @@ -82,7 +82,8 @@ public class AssetSpace if (input.mainCon.one.held) { - assets[held].rot *= input.offCon.physical.Rot() * Quaternion.Inverse(oldOffConRot); + Quaternion rotDelta = input.offCon.physical.Rot() * Quaternion.Inverse(oldOffConRot); + assets[held].rot = rotDelta * assets[held].rot; // assets[held].rot *= Quaternion.LookRotation(cursorPos - assets[held].pos) * // Quaternion.Inverse(Quaternion.LookRotation(oldCursorPos - assets[held].pos)); diff --git a/Assets/Scripts/Monolith.cs b/Assets/Scripts/Monolith.cs index 069f0e3..522d939 100644 --- a/Assets/Scripts/Monolith.cs +++ b/Assets/Scripts/Monolith.cs @@ -15,9 +15,12 @@ public class Monolith : MonoBehaviour [Button] public void Load() { pixelgon = assetSpace.Load(); } // Tools/Systems - // public ToolNewQuad toolNewQuad; public ToolNewVector toolNewVector; public ToolEditVector toolEditVector; + + public ToolNewQuad toolNewQuad; + // public ToolEditQuad toolEditQuad; + public ToolEraser toolEraser; // stretch-silo @@ -49,8 +52,12 @@ public class Monolith : MonoBehaviour { toolNewVector.Reset(); toolEditVector.Reset(); + + toolNewQuad.Reset(); + toolEraser.Reset(); - if (toolIndex < 2) + + if (toolIndex < 3) { toolIndex++; } @@ -71,6 +78,10 @@ public class Monolith : MonoBehaviour render.ToolEditVector(toolEditVector); break; case 2: + toolNewQuad.Frame(input, pixelgon); + render.ToolNewQuad(toolNewQuad, pixelgon); + break; + case 3: toolEraser.Frame(input, pixelgon); render.ToolEraser(toolEraser); break; diff --git a/Assets/Scripts/Pixelgon.cs b/Assets/Scripts/Pixelgon.cs index 07cfdfd..b6c2777 100644 --- a/Assets/Scripts/Pixelgon.cs +++ b/Assets/Scripts/Pixelgon.cs @@ -7,6 +7,6 @@ using UnityEngine; public class Pixelgon { // Just Data - // public List quads = new List(); // one material, one texture, map uv accordingly public List vectors = new List(); // mapped to one List with null break vectors + public List quads = new List(); } \ No newline at end of file diff --git a/Assets/Scripts/Render.cs b/Assets/Scripts/Render.cs index 48c677f..1012822 100644 --- a/Assets/Scripts/Render.cs +++ b/Assets/Scripts/Render.cs @@ -13,6 +13,7 @@ public class Render List selectedJoints = new List(); Matrix4x4 orielM4 = new Matrix4x4(); Matrix4x4 axisM4 = new Matrix4x4(); + Mesh quads; public void Pixelgon(Pixelgon pixelgon) { orielM4.SetTRS(Vector3.zero, Quaternion.identity, new Vector3(0.6f, 0.4f, 0.4f)); @@ -33,6 +34,43 @@ public class Render } selectedJoints.Clear(); + if (quads == null) + { + quads = new Mesh(); + } + + // w/triangle seam between the two closest points + List tris = new List(); + for (int i = 0; i < pixelgon.quads.Count; i += 4) + { + if (Vector3.Distance(pixelgon.vectors[pixelgon.quads[i + 0]], pixelgon.vectors[pixelgon.quads[i + 2]]) + < Vector3.Distance(pixelgon.vectors[pixelgon.quads[i + 1]], pixelgon.vectors[pixelgon.quads[i + 3]])) + { + tris.Add(pixelgon.quads[i + 0]); + tris.Add(pixelgon.quads[i + 1]); + tris.Add(pixelgon.quads[i + 2]); + + tris.Add(pixelgon.quads[i + 2]); + tris.Add(pixelgon.quads[i + 3]); + tris.Add(pixelgon.quads[i + 0]); + } + else + { + tris.Add(pixelgon.quads[i + 0]); + tris.Add(pixelgon.quads[i + 1]); + tris.Add(pixelgon.quads[i + 3]); + + tris.Add(pixelgon.quads[i + 3]); + tris.Add(pixelgon.quads[i + 1]); + tris.Add(pixelgon.quads[i + 2]); + } + } + + quads.SetVertices(pixelgon.vectors); + quads.SetTriangles(tris, 0); + + Graphics.DrawMesh(quads, Vector3.zero, Quaternion.identity, matVertexLit, 0); + // Now for pixels! // float pixelsPerMeter = 24; // int texDiameter = Mathf.RoundToInt( @@ -107,6 +145,22 @@ public class Render selectedJoints.AddRange(toolEditVector.selected); } + public Mesh meshToolNewQuad; + Mesh newQuad; + public void ToolNewQuad(ToolNewQuad toolNewQuad, Pixelgon pixelgon) + { + Graphics.DrawMesh(meshToolNewQuad, + toolNewQuad.cursorPos, + Quaternion.identity, + matVertexLit, 0 + ); + + if (toolNewQuad.indexQuad > 0) + { + selectedJoints.AddRange(toolNewQuad.quad); + } + } + public Mesh meshToolEraser; public void ToolEraser(ToolEraser toolEraser) { @@ -180,7 +234,7 @@ public class Render Quaternion.identity, matCursor, 0 ); - + Graphics.DrawMesh(meshCursor, input.twistCursor, Quaternion.identity, diff --git a/Assets/Scripts/Tools/ToolNewQuad.cs b/Assets/Scripts/Tools/ToolNewQuad.cs index 3dd85f4..6f520d3 100644 --- a/Assets/Scripts/Tools/ToolNewQuad.cs +++ b/Assets/Scripts/Tools/ToolNewQuad.cs @@ -6,49 +6,47 @@ using UnityEngine; [Serializable] public class ToolNewQuad { - public Transform planar; - [HideInInspector] public Vector3 cursorPos; - [HideInInspector] - public Mesh mesh; - [HideInInspector] - public Vector3[] quad = new Vector3[4]; + [HideInInspector] public int indexQuad = 0; + [HideInInspector] + public int[] quad = new int[4]; - int[] tris = new int[6]; - Vector2[] uvs = new Vector2[4]; + // [HideInInspector] + // public Vector2[] uvs = new Vector2[4]; + + public void Reset() + { + indexQuad = 0; + + quad = new int[4]; + } public void Frame(Input input, Pixelgon pixelgon) { - List quads = new List(); - // pixelgon.quads; // for convenience cursorPos = input.twistCursor; // can do more ^-^ - // vertice snapping - if (input.mainCon.one.held) - { - Vector3 closestVertice = Vector3.one * 666; // lol - for (int i = 0; i < quads.Count; i++) - { - for (int j = 0; j < quads[i].vertices.Length; j++) - { - if ((quads[i].vertices[j] - cursorPos).sqrMagnitude < - (closestVertice - cursorPos).sqrMagnitude) - { - closestVertice = quads[i].vertices[j]; - } - } - } + List vectors = pixelgon.vectors; - if ((closestVertice - cursorPos).magnitude < 0.02f) // hard coded + if (vectors.Count < 3) + { + Debug.Log("Not enough vectors, to form a Quad"); + return; + } + + // vertice snapping + int closestVector = 0; + for (int i = 0; i < vectors.Count; i++) + { + if ((vectors[i] - cursorPos).sqrMagnitude < (vectors[closestVector] - cursorPos).sqrMagnitude) { - cursorPos = closestVertice; + closestVector = i; } } - planar.position = (quad[0] + quad[1] + quad[2] + quad[3]) / 4; + cursorPos = vectors[closestVector]; switch (indexQuad) { @@ -57,14 +55,14 @@ public class ToolNewQuad { for (int i = 0; i < quad.Length; i++) { - quad[i] = cursorPos; + quad[i] = closestVector; } indexQuad++; } break; case (1): - quad[indexQuad] = cursorPos; + quad[indexQuad] = closestVector; if (input.mainCon.trigger.down) { @@ -72,7 +70,7 @@ public class ToolNewQuad } break; case (2): - quad[indexQuad] = cursorPos; + quad[indexQuad] = closestVector; if (input.mainCon.trigger.down) { @@ -80,56 +78,41 @@ public class ToolNewQuad } break; case (3): - Vector3 v3 = Vector3.Cross(quad[0] - quad[1], quad[2] - quad[1]).normalized; - planar.rotation = Quaternion.LookRotation(v3, quad[2] - quad[1]); - cursorPos = planar.InverseTransformPoint(cursorPos); - cursorPos.z = 0; - cursorPos = planar.TransformPoint(cursorPos); - - quad[indexQuad] = cursorPos; + quad[indexQuad] = closestVector; if (input.mainCon.trigger.down) { - // w/triangle seam between the two closest points - if (Vector3.Distance(quad[0], quad[2]) < Vector3.Distance(quad[1], quad[3])) - { - tris[0] = 0; tris[3] = 2; - tris[1] = 1; tris[4] = 3; - tris[2] = 2; tris[5] = 0; - } - else - { - tris[0] = 0; tris[3] = 3; - tris[1] = 1; tris[4] = 1; - tris[2] = 3; tris[5] = 2; - } + // int furthest = 0; + // for (int i = 1; i < quad.Length; i++) + // { + // if ((vectors[quad[i]] - planar.position).magnitude > + // (vectors[quad[furthest]] - planar.position).magnitude) + // { + // furthest = i; + // } + // } - int furthest = 0; - for (int i = 1; i < quad.Length; i++) - { - if ((quad[i] - planar.position).magnitude > - (quad[furthest] - planar.position).magnitude) - { - furthest = i; - } - } + // float scale = planar.InverseTransformPoint(vectors[quad[furthest]]).magnitude; - float scale = planar.InverseTransformPoint(quad[furthest]).magnitude; + // for (int i = 0; i < quad.Length; i++) + // { + // uvs[i] = ((Vector2)planar.InverseTransformPoint( + // vectors[quad[i]]) / (scale * 2)) + new Vector2(0.5f, 0.5f + // ); + // } + + // only tris! + // that makes everything so much easier + // the abstraction then follows the model + // vectors = vertices + // quads = tri pairs + // pixels = uvs for (int i = 0; i < quad.Length; i++) { - uvs[i] = ((Vector2)planar.InverseTransformPoint( - quad[i]) / (scale * 2)) + new Vector2(0.5f, 0.5f - ); + pixelgon.quads.Add(quad[i]); } - mesh = new Mesh(); - mesh.SetVertices(quad); - mesh.SetTriangles(tris, 0); - mesh.SetUVs(0, uvs); - - quads.Add(mesh); - indexQuad = 0; } break; diff --git a/Assets/Scripts/Tools/ToolNewVector.cs b/Assets/Scripts/Tools/ToolNewVector.cs index 4ecdc81..a788979 100644 --- a/Assets/Scripts/Tools/ToolNewVector.cs +++ b/Assets/Scripts/Tools/ToolNewVector.cs @@ -79,6 +79,8 @@ public class ToolNewVector vector.Add(cursorPos); } + vector[vector.Count - 1] = cursorPos; + if (input.mainCon.two.down) { if (vector.Count > 2) @@ -90,8 +92,6 @@ public class ToolNewVector vector.Clear(); } } - - vector[vector.Count - 1] = cursorPos; } if (input.mainCon.trigger.up)