feat: added base features of the place object tool, you can place objects with L and print the config files with semicolon.

This commit is contained in:
FreezeDriedMangoes 2022-05-12 12:51:37 -04:00
parent 398c5f9459
commit 07b20fc0eb
3 changed files with 177 additions and 30 deletions

View File

@ -0,0 +1,90 @@
using NewHorizons.Builder.Props;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace NewHorizons.Utility
{
class DebugPropPlacer
{
private struct PropPlacementData
{
public Vector3 pos;
public Vector3 rotation;
public string body;
}
// placeholder currentObject
public static string currentObject = "BrittleHollow_Body/Sector_BH/Sector_NorthHemisphere/Sector_NorthPole/Sector_HangingCity/Sector_HangingCity_District1/Props_HangingCity_District1/OtherComponentsGroup/Props_HangingCity_Building_10/Prefab_NOM_VaseThin";
private static List<PropPlacementData> props = new List<PropPlacementData>();
public static void PlaceObject(DebugRaycastData data)
{
if (!data.hitObject.name.EndsWith("_Body"))
{
Logger.Log("Cannot place object on non-body object: " + data.hitObject.name);
}
try
{
GameObject prop = DetailBuilder.MakeDetail(data.hitObject, data.hitObject.GetComponentInChildren<Sector>(), currentObject, data.pos, data.norm, 1, false);
string bodyName = data.hitObject.name.Substring(0, data.bodyName.Length-"_Body".Length);
props.Add(new PropPlacementData
{
pos = data.pos,
rotation = data.norm,
body = bodyName
}
);
}
catch (Exception e)
{
Logger.Log($"Failed to place object {currentObject} on body ${data.hitObject} at location ${data.pos}.");
}
}
public static void PrintConfig()
{
var groupedProps = props
.GroupBy(p => p.body)
.Select(grp => grp.ToList())
.ToList();
foreach (List<PropPlacementData> bodyProps in groupedProps)
{
string configFile =
"{" + Environment.NewLine +
" \"$schema\": \"https://raw.githubusercontent.com/xen-42/outer-wilds-new-horizons/master/NewHorizons/schema.json\"," + Environment.NewLine +
$" \"name\" : \"{bodyProps[0].body}\"," + Environment.NewLine +
" \"Props\" :" + Environment.NewLine +
" {" + Environment.NewLine +
" \"details\": [" + Environment.NewLine;
for(int i = 0; i < bodyProps.Count; i++)
{
PropPlacementData prop = bodyProps[i];
string positionString = $"\"x\":{prop.pos.x},\"y\":{prop.pos.y},\"z\":{prop.pos.z}";
string rotationString = $"\"x\":{prop.rotation.x},\"y\":{prop.rotation.y},\"z\":{prop.rotation.z}";
string endingString = i == bodyProps.Count-1 ? "" : ",";
configFile += " {\"path\" : \"" + "\", \"position\": {"+positionString+"}, \"rotation\": {"+rotationString+"}, \"scale\": 1}" + endingString + Environment.NewLine;
}
configFile +=
" ]" + Environment.NewLine +
" }" + Environment.NewLine +
"}";
Logger.Log(configFile);
}
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace NewHorizons.Utility
{
struct DebugRaycastData
{
public bool hit;
public Vector3 pos;
public Vector3 norm;
public string bodyName;
public string bodyPath;
public GameObject hitObject;
}
}

View File

@ -25,38 +25,75 @@ namespace NewHorizons.Utility
private void Update()
{
if (Main.Debug && Keyboard.current != null && Keyboard.current[Key.P].wasReleasedThisFrame)
if (!Main.Debug) return;
if (Keyboard.current == null) return;
if (Keyboard.current[Key.P].wasReleasedThisFrame)
{
// Raycast
_rb.DisableCollisionDetection();
int layerMask = OWLayerMask.physicalMask;
var origin = Locator.GetActiveCamera().transform.position;
var direction = Locator.GetActiveCamera().transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(origin, direction, out RaycastHit hitInfo, 100f, layerMask))
{
var pos = hitInfo.transform.InverseTransformPoint(hitInfo.point);
var norm = hitInfo.transform.InverseTransformDirection(hitInfo.normal);
var o = hitInfo.transform.gameObject;
var posText = $"{{\"x\": {pos.x}, \"y\": {pos.y}, \"z\": {pos.z}}}";
var normText = $"{{\"x\": {norm.x}, \"y\": {norm.y}, \"z\": {norm.z}}}";
if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere);
if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1);
if(_normalSphere2 != null) GameObject.Destroy(_normalSphere2);
_surfaceSphere = AddDebugShape.AddSphere(hitInfo.transform.gameObject, 0.1f, Color.green);
_normalSphere1 = AddDebugShape.AddSphere(hitInfo.transform.gameObject, 0.01f, Color.red);
_normalSphere2 = AddDebugShape.AddSphere(hitInfo.transform.gameObject, 0.01f, Color.red);
_surfaceSphere.transform.localPosition = pos;
_normalSphere1.transform.localPosition = pos + norm * 0.5f;
_normalSphere2.transform.localPosition = pos + norm;
Logger.Log($"Raycast hit \"position\": {posText}, \"normal\": {normText} on [{o.name}] at [{SearchUtilities.GetPath(o.transform)}]");
}
_rb.EnableCollisionDetection();
PrintRaycast();
}
if (Keyboard.current[Key.L].wasReleasedThisFrame)
{
PlaceObject();
}
if (Keyboard.current[Key.Semicolon].wasReleasedThisFrame)
{
DebugPropPlacer.PrintConfig();
}
}
internal void PlaceObject()
{
DebugRaycastData data = Raycast();
DebugPropPlacer.PlaceObject(data);
}
internal void PrintRaycast()
{
DebugRaycastData data = Raycast();
var posText = $"{{\"x\": {data.pos.x}, \"y\": {data.pos.y}, \"z\": {data.pos.z}}}";
var normText = $"{{\"x\": {data.norm.x}, \"y\": {data.norm.y}, \"z\": {data.norm.z}}}";
if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere);
if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1);
if(_normalSphere2 != null) GameObject.Destroy(_normalSphere2);
_surfaceSphere = AddDebugShape.AddSphere(data.hitObject, 0.1f, Color.green);
_normalSphere1 = AddDebugShape.AddSphere(data.hitObject, 0.01f, Color.red);
_normalSphere2 = AddDebugShape.AddSphere(data.hitObject, 0.01f, Color.red);
_surfaceSphere.transform.localPosition = data.pos;
_normalSphere1.transform.localPosition = data.pos + data.norm * 0.5f;
_normalSphere2.transform.localPosition = data.pos + data.norm;
Logger.Log($"Raycast hit \"position\": {posText}, \"normal\": {normText} on [{data.bodyName}] at [{data.bodyPath}]");
}
internal DebugRaycastData Raycast()
{
DebugRaycastData data = new DebugRaycastData();
_rb.DisableCollisionDetection();
int layerMask = OWLayerMask.physicalMask;
var origin = Locator.GetActiveCamera().transform.position;
var direction = Locator.GetActiveCamera().transform.TransformDirection(Vector3.forward);
data.hit = Physics.Raycast(origin, direction, out RaycastHit hitInfo, 100f, layerMask);
if (data.hit)
{
data.pos = hitInfo.transform.InverseTransformPoint(hitInfo.point);
data.norm = hitInfo.transform.InverseTransformDirection(hitInfo.normal);
var o = hitInfo.transform.gameObject;
data.bodyName = o.name;
data.bodyPath = SearchUtilities.GetPath(o.transform);
data.hitObject = hitInfo.transform.gameObject;
}
_rb.EnableCollisionDetection();
return data;
}
}
}