From dc53042f920345f91c950ebc30e4532d94008270 Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Sat, 3 Sep 2022 12:08:12 -0400 Subject: [PATCH] Add relative to parent option --- NewHorizons/Builder/Props/DetailBuilder.cs | 16 +++ NewHorizons/Builder/Props/DialogueBuilder.cs | 11 ++- NewHorizons/Builder/Props/NomaiTextBuilder.cs | 97 +++++++++++-------- .../Builder/Props/ProjectionBuilder.cs | 61 ++++++------ NewHorizons/Builder/Props/RemoteBuilder.cs | 64 ++++-------- NewHorizons/Builder/Props/SignalBuilder.cs | 4 +- NewHorizons/External/Modules/PropModule.cs | 40 ++++++++ NewHorizons/External/Modules/SignalModule.cs | 5 + 8 files changed, 178 insertions(+), 120 deletions(-) diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index ae7a63f7..08d0d6ce 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -159,6 +159,22 @@ namespace NewHorizons.Builder.Props } } + if (detail.isRelativeToParent) + { + prop.transform.localPosition = detail.position == null ? Vector3.zero : detail.position; + if (detail.alignToNormal) + { + // Apply the rotation after aligning it with normal + var up = prop.transform.parent.InverseTransformPoint(prop.transform.position).normalized; + prop.transform.rotation = Quaternion.FromToRotation(Vector3.up, up); + prop.transform.rotation *= rot; + } + else + { + prop.transform.localRotation = rot; + } + } + return prop; } diff --git a/NewHorizons/Builder/Props/DialogueBuilder.cs b/NewHorizons/Builder/Props/DialogueBuilder.cs index f917c973..7ad5dcc8 100644 --- a/NewHorizons/Builder/Props/DialogueBuilder.cs +++ b/NewHorizons/Builder/Props/DialogueBuilder.cs @@ -102,8 +102,15 @@ namespace NewHorizons.Builder.Props { conversationZone.transform.parent = planetGO.transform.Find(info.pathToAnimController); } - - conversationZone.transform.position = planetGO.transform.TransformPoint(info.position); + + if (info.isRelativeToParent) conversationZone.transform.localPosition = info.position; + else conversationZone.transform.position = planetGO.transform.TransformPoint(info.position); + + if (!string.IsNullOrEmpty(info.rename)) + { + conversationZone.name = info.rename; + } + conversationZone.SetActive(true); return dialogueTree; diff --git a/NewHorizons/Builder/Props/NomaiTextBuilder.cs b/NewHorizons/Builder/Props/NomaiTextBuilder.cs index 7743d754..d6816b54 100644 --- a/NewHorizons/Builder/Props/NomaiTextBuilder.cs +++ b/NewHorizons/Builder/Props/NomaiTextBuilder.cs @@ -136,19 +136,40 @@ namespace NewHorizons.Builder.Props } } - nomaiWallTextObj.transform.position = planetGO.transform.TransformPoint(info.position); - if (info.normal != null) + var pos = (Vector3)(info.position ?? Vector3.zero); + if (info.isRelativeToParent) { - // In global coordinates (normal was in local coordinates) - var up = (nomaiWallTextObj.transform.position - planetGO.transform.position).normalized; - var forward = planetGO.transform.TransformDirection(info.normal).normalized; + nomaiWallTextObj.transform.localPosition = pos; + if (info.normal != null) + { + // In global coordinates (normal was in local coordinates) + var up = (nomaiWallTextObj.transform.position - planetGO.transform.position).normalized; + var forward = planetGO.transform.TransformDirection(info.normal).normalized; - nomaiWallTextObj.transform.up = up; - nomaiWallTextObj.transform.forward = forward; + nomaiWallTextObj.transform.up = up; + nomaiWallTextObj.transform.forward = forward; + } + if (info.rotation != null) + { + nomaiWallTextObj.transform.localRotation = Quaternion.Euler(info.rotation); + } } - if (info.rotation != null) + else { - nomaiWallTextObj.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(info.rotation)); + nomaiWallTextObj.transform.position = planetGO.transform.TransformPoint(pos); + if (info.normal != null) + { + // In global coordinates (normal was in local coordinates) + var up = (nomaiWallTextObj.transform.position - planetGO.transform.position).normalized; + var forward = planetGO.transform.TransformDirection(info.normal).normalized; + + nomaiWallTextObj.transform.up = up; + nomaiWallTextObj.transform.forward = forward; + } + if (info.rotation != null) + { + nomaiWallTextObj.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(info.rotation)); + } } nomaiWallTextObj.SetActive(true); @@ -211,7 +232,9 @@ namespace NewHorizons.Builder.Props } } - customScroll.transform.position = planetGO.transform.TransformPoint(info.position ?? Vector3.zero); + var pos = info.position ?? Vector3.zero; + if (info.isRelativeToParent) customScroll.transform.localPosition = pos; + else customScroll.transform.position = planetGO.transform.TransformPoint(pos); var up = planetGO.transform.InverseTransformPoint(customScroll.transform.position).normalized; customScroll.transform.rotation = Quaternion.FromToRotation(customScroll.transform.up, up) * customScroll.transform.rotation; @@ -264,7 +287,9 @@ namespace NewHorizons.Builder.Props } } - computerObject.transform.position = planetGO.transform.TransformPoint(info?.position ?? Vector3.zero); + var pos = info.position ?? Vector3.zero; + if (info.isRelativeToParent) computerObject.transform.localPosition = pos; + else computerObject.transform.position = planetGO.transform.TransformPoint(pos); var up = computerObject.transform.position - planetGO.transform.position; if (info.normal != null) up = planetGO.transform.TransformDirection(info.normal); @@ -291,29 +316,14 @@ namespace NewHorizons.Builder.Props { var detailInfo = new PropModule.DetailInfo() { - position = info.position + position = info.position, + parentPath = info.parentPath, + isRelativeToParent = info.isRelativeToParent, + rename = info.rename }; var computerObject = DetailBuilder.Make(planetGO, sector, _preCrashComputerPrefab, detailInfo); computerObject.SetActive(false); - if (!string.IsNullOrEmpty(info.rename)) - { - computerObject.name = info.rename; - } - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - computerObject.transform.SetParent(newParent, true); - } - else - { - Logger.LogWarning($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - var up = computerObject.transform.position - planetGO.transform.position; if (info.normal != null) up = planetGO.transform.TransformDirection(info.normal); computerObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, up) * computerObject.transform.rotation; @@ -380,11 +390,15 @@ namespace NewHorizons.Builder.Props } } - cairnObject.transform.position = planetGO.transform.TransformPoint(info?.position ?? Vector3.zero); + var pos = info.position ?? Vector3.zero; + if (info.isRelativeToParent) cairnObject.transform.localPosition = pos; + else cairnObject.transform.position = planetGO.transform.TransformPoint(pos); if (info.rotation != null) { - cairnObject.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(info.rotation)); + var rot = Quaternion.Euler(info.rotation); + if (info.isRelativeToParent) cairnObject.transform.localRotation = rot; + else cairnObject.transform.rotation = planetGO.transform.TransformRotation(rot); } else { @@ -429,18 +443,13 @@ namespace NewHorizons.Builder.Props var detailInfo = new PropModule.DetailInfo { parentPath = info.parentPath, rotation = info.rotation, - position = info.position + position = info.position, + isRelativeToParent = info.isRelativeToParent, + rename = info.rename }; var recorderObject = DetailBuilder.Make(planetGO, sector, prefab, detailInfo); recorderObject.SetActive(false); - if (!string.IsNullOrEmpty(info.rename)) - { - recorderObject.name = info.rename; - } - - recorderObject.transform.position = planetGO.transform.TransformPoint(info?.position ?? Vector3.zero); - if (info.rotation == null) { var up = recorderObject.transform.position - planetGO.transform.position; @@ -490,11 +499,15 @@ namespace NewHorizons.Builder.Props } } - trailmarkerObject.transform.position = planetGO.transform.TransformPoint(info?.position ?? Vector3.zero); + var pos = info.position ?? Vector3.zero; + if (info.isRelativeToParent) trailmarkerObject.transform.localPosition = pos; + else trailmarkerObject.transform.position = planetGO.transform.TransformPoint(pos); if (info.rotation != null) { - trailmarkerObject.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler(info.rotation)); + var rot = Quaternion.Euler(info.rotation); + if (info.isRelativeToParent) trailmarkerObject.transform.localRotation = rot; + else trailmarkerObject.transform.rotation = planetGO.transform.TransformRotation(rot); } else { diff --git a/NewHorizons/Builder/Props/ProjectionBuilder.cs b/NewHorizons/Builder/Props/ProjectionBuilder.cs index f6cb399d..128e8ff3 100644 --- a/NewHorizons/Builder/Props/ProjectionBuilder.cs +++ b/NewHorizons/Builder/Props/ProjectionBuilder.cs @@ -79,8 +79,18 @@ namespace NewHorizons.Builder.Props } } - slideReelObj.transform.position = planetGO.transform.TransformPoint((Vector3)(info.position ?? Vector3.zero)); - slideReelObj.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero))); + var pos = (Vector3)(info.position ?? Vector3.zero); + var rot = Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero)); + if (info.isRelativeToParent) + { + slideReelObj.transform.localPosition = pos; + slideReelObj.transform.localRotation = rot; + } + else + { + slideReelObj.transform.position = planetGO.transform.TransformPoint(pos); + slideReelObj.transform.rotation = planetGO.transform.TransformRotation(rot); + } // Now we replace the slides int slidesCount = info.slides.Length; @@ -184,8 +194,18 @@ namespace NewHorizons.Builder.Props } } - autoProjector.transform.position = planetGO.transform.TransformPoint((Vector3)(info.position ?? Vector3.zero)); - autoProjector.transform.rotation = planetGO.transform.TransformRotation(Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero))); + var pos = (Vector3)(info.position ?? Vector3.zero); + var rot = Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero)); + if (info.isRelativeToParent) + { + autoProjector.transform.localPosition = pos; + autoProjector.transform.localRotation = rot; + } + else + { + autoProjector.transform.position = planetGO.transform.TransformPoint(pos); + autoProjector.transform.rotation = planetGO.transform.TransformRotation(rot); + } // Now we replace the slides int slidesCount = info.slides.Length; @@ -226,23 +246,13 @@ namespace NewHorizons.Builder.Props var detailInfo = new PropModule.DetailInfo() { position = info.position, + rotation = info.rotation, + parentPath = info.parentPath, + isRelativeToParent = info.isRelativeToParent, scale = 2 }; var g = DetailBuilder.Make(planetGO, sector, prefab, detailInfo); - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - g.transform.SetParent(newParent, true); - } - else - { - Logger.LogWarning($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - if (g == null) { Logger.LogWarning($"Tried to make a vision torch target but couldn't. Do you have the DLC installed?"); @@ -292,23 +302,12 @@ namespace NewHorizons.Builder.Props var detailInfo = new PropModule.DetailInfo() { position = info.position, - rotation = info.rotation + rotation = info.rotation, + parentPath = info.parentPath, + isRelativeToParent = info.isRelativeToParent }; var standingTorch = DetailBuilder.Make(planetGO, sector, prefab, detailInfo); - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = planetGO.transform.Find(info.parentPath); - if (newParent != null) - { - standingTorch.transform.SetParent(newParent, true); - } - else - { - Logger.LogWarning($"Cannot find parent object at path: {planetGO.name}/{info.parentPath}"); - } - } - if (standingTorch == null) { Logger.LogWarning($"Tried to make a vision torch target but couldn't. Do you have the DLC installed?"); diff --git a/NewHorizons/Builder/Props/RemoteBuilder.cs b/NewHorizons/Builder/Props/RemoteBuilder.cs index b82ec0e5..d5a951b9 100644 --- a/NewHorizons/Builder/Props/RemoteBuilder.cs +++ b/NewHorizons/Builder/Props/RemoteBuilder.cs @@ -153,31 +153,14 @@ namespace NewHorizons.Builder.Props var detailInfo = new PropModule.DetailInfo() { position = info.position, - rotation = info.rotation + rotation = info.rotation, + parentPath = info.parentPath, + isRelativeToParent = info.isRelativeToParent, + rename = info.rename }; var whiteboard = DetailBuilder.Make(go, sector, _whiteboardPrefab, detailInfo); whiteboard.SetActive(false); - if (!string.IsNullOrEmpty(info.rename)) - { - whiteboard.name = info.rename; - } - - whiteboard.transform.parent = sector?.transform ?? go.transform; - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = go.transform.Find(info.parentPath); - if (newParent != null) - { - whiteboard.transform.parent = newParent; - } - else - { - Logger.LogWarning($"Cannot find parent object at path: {go.name}/{info.parentPath}"); - } - } - var decalMat = new Material(_decalMaterial); decalMat.SetTexture("_MainTex", decal); decalMat.SetTexture("_EmissionMap", decal); @@ -218,31 +201,14 @@ namespace NewHorizons.Builder.Props var detailInfo = new PropModule.DetailInfo() { position = info.position, - rotation = info.rotation + rotation = info.rotation, + parentPath = info.parentPath, + isRelativeToParent = info.isRelativeToParent, + rename = info.rename }; var platform = DetailBuilder.Make(go, sector, _remoteCameraPlatformPrefab, detailInfo); platform.SetActive(false); - if (!string.IsNullOrEmpty(info.rename)) - { - platform.name = info.rename; - } - - platform.transform.parent = sector?.transform ?? go.transform; - - if (!string.IsNullOrEmpty(info.parentPath)) - { - var newParent = go.transform.Find(info.parentPath); - if (newParent != null) - { - platform.transform.parent = newParent; - } - else - { - Logger.LogWarning($"Cannot find parent object at path: {go.name}/{info.parentPath}"); - } - } - var decalMat = new Material(_decalMaterial); decalMat.SetTexture("_MainTex", decal); decalMat.SetTexture("_EmissionMap", decal); @@ -292,8 +258,18 @@ namespace NewHorizons.Builder.Props } } - shareStone.transform.position = go.transform.TransformPoint((Vector3)(info.position ?? Vector3.zero)); - shareStone.transform.rotation = go.transform.TransformRotation(Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero))); + var pos = (Vector3)(info.position ?? Vector3.zero); + var rot = Quaternion.Euler((Vector3)(info.rotation ?? Vector3.zero)); + if (info.isRelativeToParent) + { + shareStone.transform.localPosition = pos; + shareStone.transform.localRotation = rot; + } + else + { + shareStone.transform.position = go.transform.TransformPoint(pos); + shareStone.transform.rotation = go.transform.TransformRotation(rot); + } shareStone.GetComponent()._connectedPlatform = id; diff --git a/NewHorizons/Builder/Props/SignalBuilder.cs b/NewHorizons/Builder/Props/SignalBuilder.cs index 72895387..cb004310 100644 --- a/NewHorizons/Builder/Props/SignalBuilder.cs +++ b/NewHorizons/Builder/Props/SignalBuilder.cs @@ -154,7 +154,9 @@ namespace NewHorizons.Builder.Props } } - signalGO.transform.position = planetGO.transform.TransformPoint(info.position != null ? (Vector3)info.position : Vector3.zero); + var pos = (Vector3)(info.position ?? Vector3.zero); + if (info.isRelativeToParent) signalGO.transform.localPosition = pos; + else signalGO.transform.position = planetGO.transform.TransformPoint(pos); signalGO.layer = LayerMask.NameToLayer("AdvancedEffectVolume"); var source = signalGO.AddComponent(); diff --git a/NewHorizons/External/Modules/PropModule.cs b/NewHorizons/External/Modules/PropModule.cs index 932b1bc0..bafbc853 100644 --- a/NewHorizons/External/Modules/PropModule.cs +++ b/NewHorizons/External/Modules/PropModule.cs @@ -200,6 +200,11 @@ namespace NewHorizons.External.Modules /// public string parentPath; + /// + /// Whether the positional and rotational coordinates are relative to parent instead of the root planet object. + /// + public bool isRelativeToParent; + /// /// Should this detail stay loaded even if you're outside the sector (good for very large props) /// @@ -425,6 +430,16 @@ namespace NewHorizons.External.Modules /// Relative path to the xml file defining the dialogue. /// public string xmlFile; + + /// + /// Whether the positional and rotational coordinates are relative to the animation controller instead of the root planet object. + /// + public bool isRelativeToParent; + + /// + /// An optional rename of this object + /// + public string rename; } [JsonObject] @@ -525,6 +540,11 @@ namespace NewHorizons.External.Modules /// public string parentPath; + /// + /// Whether the positional and rotational coordinates are relative to parent instead of the root planet object. + /// + public bool isRelativeToParent; + /// /// An optional rename of this object /// @@ -615,6 +635,11 @@ namespace NewHorizons.External.Modules /// The relative path from the planet to the parent of this slideshow. Optional (will default to the root sector). /// public string parentPath; + + /// + /// Whether the positional and rotational coordinates are relative to parent instead of the root planet object. + /// + public bool isRelativeToParent; } [JsonObject] @@ -814,6 +839,11 @@ namespace NewHorizons.External.Modules /// public string parentPath; + /// + /// Whether the positional and rotational coordinates are relative to parent instead of the root planet object. + /// + public bool isRelativeToParent; + /// /// An optional rename of this object /// @@ -877,6 +907,11 @@ namespace NewHorizons.External.Modules /// public string parentPath; + /// + /// Whether the positional and rotational coordinates are relative to parent instead of the root planet object. + /// + public bool isRelativeToParent; + /// /// An optional rename of this object /// @@ -916,6 +951,11 @@ namespace NewHorizons.External.Modules /// public string parentPath; + /// + /// Whether the positional and rotational coordinates are relative to parent instead of the root planet object. + /// + public bool isRelativeToParent; + /// /// An optional rename of this object /// diff --git a/NewHorizons/External/Modules/SignalModule.cs b/NewHorizons/External/Modules/SignalModule.cs index 3397df9a..255ebc9b 100644 --- a/NewHorizons/External/Modules/SignalModule.cs +++ b/NewHorizons/External/Modules/SignalModule.cs @@ -80,6 +80,11 @@ namespace NewHorizons.External.Modules /// The relative path from the planet to the parent of this signal. Optional (will default to the root sector). /// public string parentPath; + + /// + /// Whether the positional and rotational coordinates are relative to parent instead of the root planet object. + /// + public bool isRelativeToParent; } } } \ No newline at end of file