using UnityEngine; using System; using System.Linq; using System.IO; using System.Collections; using System.Collections.Generic; using SimpleJSON; [Serializable] public class AssetSpace { public string folderPath = "C:/dofdev/Pixelgon/Assets/AssetSpace"; public string name = "draft"; public List loadedNames = new List(); public Dictionary assets = new Dictionary(); // Animation // Some discrete number of steps (frames) // That are lerped through (one loop) int frames = 24; float loopTimeScale = 6f; float loopTime = 0; // 0-1 public void Init(PolyVector polyVector) { // Update Mesh Dictionary // loop through files in folder loadedNames.Clear(); assets.Clear(); string[] filePaths = Directory.GetFiles(folderPath, "*.json", SearchOption.TopDirectoryOnly); for (int i = 0; i < filePaths.Length; i++) { name = Path.GetFileNameWithoutExtension(filePaths[i]); loadedNames.Add(name); Pixelgon pgon = Load(); Mesh mesh = new Mesh(); mesh = polyVector.ToMesh(pgon.vectors); Asset asset = new Asset(); asset.pos = Enumerable.Repeat(Vector3.zero, frames).ToArray(); asset.rot = Enumerable.Repeat(Quaternion.identity, frames).ToArray(); asset.mesh = mesh; assets.Add(name, asset); } // load draft.space file } string held = null; Quaternion oldOffConRot; public void Frame(Input input, Render render) { // Looper loopTime += Time.deltaTime / loopTimeScale; if (loopTime >= 1) { loopTime = 0; } float frameDelta = loopTime * (frames - 1); // 0.1 * 24 = 2.4 int currentFrame = Mathf.FloorToInt(frameDelta); // 2.4 -> 2 (for index) frameDelta = frameDelta - currentFrame; // 2.4 - 2 = 0.4 (for lerp delta t) Vector3 cursorPos = input.stretchCursor; if (input.mainCon.trigger.held) { if (held != null) { Asset asset = assets[held]; if (input.mainCon.one.down) { // switch to pixelgon scene to edit } asset.pos[currentFrame] = cursorPos; asset.pos[frames - 1] = Vector3.Lerp(asset.pos[0], asset.pos[frames - 2], 0.5f); // Quaternion rotDelta = input.offCon.physical.Rot() * Quaternion.Inverse(oldOffConRot); // assets[held].rot = rotDelta * assets[held].rot; Graphics.DrawMesh(asset.mesh, cursorPos, asset.rot[currentFrame], render.polyVector.matVector, 0 ); } } else { held = null; } float closest = 1000f; int closestIndex = -1; for (int i = 0; i < loadedNames.Count; i++) { Asset asset = assets[loadedNames[i]]; Vector3 assetPos = Vector3.Lerp(asset.pos[currentFrame], asset.pos[currentFrame + 1], frameDelta); Quaternion assetRot = Quaternion.Lerp(asset.rot[currentFrame], asset.rot[currentFrame + 1], frameDelta); if (loadedNames[i] != held) { Graphics.DrawMesh(asset.mesh, assetPos, assetRot, render.polyVector.matVector, 0 ); } float reachDist = Vector3.Distance(assetPos, cursorPos); if (reachDist < closest) { closest = reachDist; closestIndex = i; } } if (closestIndex > -1) { if (input.mainCon.trigger.down) { held = loadedNames[closestIndex]; Asset asset = assets[held]; for (int i = 0; i < asset.pos.Length; i++) { asset.pos[i] = cursorPos; } } } oldOffConRot = input.offCon.physical.Rot(); } public void Save(Pixelgon pixelgon) { // Cleanup Empty Vectors and re-index quads int dup = 0; for (int i = pixelgon.vectors.Count - 1; i >= 0; i--) { if (pixelgon.vectors[i] == Vector3.zero) { if (dup++ > 0) { for (int j = 0; j < pixelgon.quads.Count; j++) { if (pixelgon.quads[j] >= i) { pixelgon.quads[j]--; } } pixelgon.vectors.RemoveAt(i); } } else { dup = 0; } } string destination = folderPath + "/" + name + ".json"; File.WriteAllText(destination, JsonUtility.ToJson(pixelgon)); } public Pixelgon Load() { string destination = folderPath + "/" + name + ".json"; if (!File.Exists(destination)) { Debug.LogError("File not found"); return null; } JSONNode N = JSON.Parse(File.ReadAllText(destination)); Pixelgon pixelgon = new Pixelgon(); pixelgon.vectors.Clear(); for (int i = 0; i < N["vectors"].Count; i++) { pixelgon.vectors.Add( new Vector3(N["vectors"][i][0].AsFloat, N["vectors"][i][1].AsFloat, N["vectors"][i][2].AsFloat) ); } pixelgon.quads.Clear(); for (int i = 0; i < N["quads"].Count; i++) { pixelgon.quads.Add(N["quads"][i].AsInt); } return pixelgon; } } [Serializable] public class Asset { public Mesh mesh; public Vector3[] pos; public Quaternion[] rot; }