-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retain loaded position of an imported obj file #283
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
using System.Collections.Generic; | ||
using System.Linq; | ||
using Netherlands3D.Coordinates; | ||
using Netherlands3D.Twin.FloatingOrigin; | ||
using Netherlands3D.Twin.Layers.LayerTypes; | ||
using Netherlands3D.Twin.Layers.Properties; | ||
using Netherlands3D.Twin.Projects; | ||
|
@@ -11,22 +12,28 @@ | |
|
||
namespace Netherlands3D.Twin.Layers | ||
{ | ||
[RequireComponent(typeof(WorldTransform))] | ||
public class HierarchicalObjectLayerGameObject : LayerGameObject, IPointerClickHandler, ILayerWithPropertyPanels, ILayerWithPropertyData | ||
{ | ||
private ToggleScatterPropertySectionInstantiator toggleScatterPropertySectionInstantiator; | ||
[SerializeField] private UnityEvent<GameObject> objectCreated = new(); | ||
private List<IPropertySectionInstantiator> propertySections = new(); | ||
private TransformLayerPropertyData transformPropertyData; | ||
private Vector3 previousPosition; | ||
private Coordinate previousPosition; | ||
private Quaternion previousRotation; | ||
private Vector3 previousScale; | ||
private WorldTransform worldTransform; | ||
|
||
LayerPropertyData ILayerWithPropertyData.PropertyData => transformPropertyData; | ||
|
||
protected void Awake() | ||
{ | ||
var coord = new Coordinate(transform.position); | ||
transformPropertyData = new TransformLayerPropertyData(coord, transform.eulerAngles, transform.localScale); | ||
worldTransform = GetComponent<WorldTransform>(); | ||
transformPropertyData = new TransformLayerPropertyData( | ||
worldTransform.Coordinate, | ||
worldTransform.Rotation.eulerAngles, | ||
transform.localScale | ||
); | ||
|
||
propertySections = GetComponents<IPropertySectionInstantiator>().ToList(); | ||
toggleScatterPropertySectionInstantiator = GetComponent<ToggleScatterPropertySectionInstantiator>(); | ||
|
@@ -47,8 +54,8 @@ protected override void OnDisable() | |
protected override void Start() | ||
{ | ||
base.Start(); | ||
previousPosition = transform.position; | ||
previousRotation = transform.rotation; | ||
previousPosition = worldTransform.Coordinate; | ||
previousRotation = worldTransform.Rotation; | ||
previousScale = transform.localScale; | ||
|
||
objectCreated.Invoke(gameObject); | ||
|
@@ -68,62 +75,72 @@ protected void OnDestroy() | |
|
||
private void UpdatePosition(Coordinate newPosition) | ||
{ | ||
if (newPosition.ToUnity() != transform.position) | ||
transform.position = newPosition.ToUnity(); | ||
if (newPosition.ToUnity() == worldTransform.Coordinate.ToUnity()) return; | ||
|
||
worldTransform.Coordinate = newPosition; | ||
transform.position = worldTransform.Coordinate.ToUnity(); | ||
} | ||
|
||
private void UpdateRotation(Vector3 newAngles) | ||
{ | ||
if (newAngles != transform.eulerAngles) | ||
transform.eulerAngles = newAngles; | ||
if (newAngles == transform.eulerAngles) return; | ||
|
||
worldTransform.Rotation = Quaternion.Euler(newAngles); | ||
transform.rotation = worldTransform.Rotation; | ||
} | ||
|
||
private void UpdateScale(Vector3 newScale) | ||
{ | ||
if (newScale != transform.localScale) | ||
transform.localScale = newScale; | ||
if (newScale == transform.localScale) return; | ||
|
||
transform.localScale = newScale; | ||
} | ||
|
||
public virtual void LoadProperties(List<LayerPropertyData> properties) | ||
{ | ||
var transformProperty = (TransformLayerPropertyData)properties.FirstOrDefault(p => p is TransformLayerPropertyData); | ||
if (transformProperty != null) | ||
var transformProperty = properties.OfType<TransformLayerPropertyData>().FirstOrDefault(); | ||
if (transformProperty == default(TransformLayerPropertyData)) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. waarom niet expliciet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Als je in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ja, default is alleen null als de Type nullable is. maar voor mij geeft een verder is dit geen blokkerende issue, maar ik vroeg me gewoon af wat het voordeel hiervan was, al begrijp ik jouw uitleg nog niet helemaal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FirstOrDefault geeft geen null terug als die niks vind, die geeft -zoals de methode naam aangeeft- de default terug als die niks vind. Toevallig is dat voor nullable type 'null'; maar het is met FirstOrDefault vaak beter -zeker in combinatie met ofType omdat dan default daadwerkelijk over die type gaat- om te checken op Het verschil zit hem in dit |
||
|
||
// unsubscribe events from previous property object, resubscribe to new object at the end of this if block | ||
if (transformPropertyData != null) | ||
{ | ||
if (transformPropertyData != null) //unsubscribe events from previous property object, resubscribe to new object at the end of this if block | ||
{ | ||
transformPropertyData.OnPositionChanged.RemoveListener(UpdatePosition); | ||
transformPropertyData.OnRotationChanged.RemoveListener(UpdateRotation); | ||
transformPropertyData.OnScaleChanged.RemoveListener(UpdateScale); | ||
} | ||
|
||
this.transformPropertyData = transformProperty; //take existing TransformProperty to overwrite the unlinked one of this class | ||
|
||
UpdatePosition(this.transformPropertyData.Position); | ||
UpdateRotation(this.transformPropertyData.EulerRotation); | ||
UpdateScale(this.transformPropertyData.LocalScale); | ||
|
||
transformPropertyData.OnPositionChanged.AddListener(UpdatePosition); | ||
transformPropertyData.OnRotationChanged.AddListener(UpdateRotation); | ||
transformPropertyData.OnScaleChanged.AddListener(UpdateScale); | ||
transformPropertyData.OnPositionChanged.RemoveListener(UpdatePosition); | ||
transformPropertyData.OnRotationChanged.RemoveListener(UpdateRotation); | ||
transformPropertyData.OnScaleChanged.RemoveListener(UpdateScale); | ||
} | ||
|
||
// take existing TransformProperty to overwrite the unlinked one of this class | ||
this.transformPropertyData = transformProperty; | ||
|
||
UpdatePosition(this.transformPropertyData.Position); | ||
UpdateRotation(this.transformPropertyData.EulerRotation); | ||
UpdateScale(this.transformPropertyData.LocalScale); | ||
|
||
// Reset the previous[X] to prevent a reset of the | ||
previousPosition = transformPropertyData.Position; | ||
previousRotation = Quaternion.Euler(transformPropertyData.EulerRotation); | ||
previousScale = transformPropertyData.LocalScale; | ||
|
||
transformPropertyData.OnPositionChanged.AddListener(UpdatePosition); | ||
transformPropertyData.OnRotationChanged.AddListener(UpdateRotation); | ||
transformPropertyData.OnScaleChanged.AddListener(UpdateScale); | ||
} | ||
|
||
private void Update() | ||
{ | ||
// We cannot use transform.hasChanged, because this flag is not correctly set when adjusting this transform using runtimeTransformHandles, instead we have to compare the values directly | ||
// Check for position change | ||
if (transform.position != previousPosition) | ||
if (worldTransform.Coordinate.ToUnity() != previousPosition.ToUnity()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kan er niet een directe coordinate vergelijking gedaan worden (of worden toegevoegd)? dit lijkt me een beetje vatbaar voor floating point errors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. De Coordinate class heeft geen Equality operator implementatie; daar liep ik tegenaan toen ik dit stuk omschreef. Maar ik wilde de scope ook niet heel erg vergroten door operator overrides in de Coordinate converter package toe te voegen |
||
{ | ||
var rdCoordinate = new Coordinate(CoordinateSystem.Unity, transform.position.x, transform.position.y, transform.position.z); | ||
transformPropertyData.Position = rdCoordinate; | ||
previousPosition = transform.position; | ||
transformPropertyData.Position = worldTransform.Coordinate; | ||
previousPosition = worldTransform.Coordinate; | ||
} | ||
|
||
// Check for rotation change | ||
if (transform.rotation != previousRotation) | ||
if (worldTransform.Rotation != previousRotation) | ||
{ | ||
transformPropertyData.EulerRotation = transform.eulerAngles; | ||
previousRotation = transform.rotation; | ||
transformPropertyData.EulerRotation = worldTransform.Rotation.eulerAngles; | ||
previousRotation = worldTransform.Rotation; | ||
} | ||
|
||
// Check for scale change | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,10 @@ public class WorldTransform : MonoBehaviour, IHasCoordinate | |
{ | ||
[SerializeField] private WorldTransformShifter worldTransformShifter; | ||
[SerializeField] private CoordinateSystem referenceCoordinateSystem = CoordinateSystem.RDNAP; | ||
public Coordinate Coordinate { | ||
get; | ||
set; | ||
} | ||
public Quaternion Rotation | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
public Coordinate Coordinate { get; set; } = new(CoordinateSystem.RDNAP); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ik meen me te herinneren dat deze constructor kapot was. Kan je even met Martijn dubbelchecken of dat opgelost is? Ik heb hier een keer uren aan besteed omdat ik niet begreep wat er fout was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, ik heb geen problemen ervaren bij testen, maar goed dat je dat dan aangeeft. @mvangog: weet jij dit toevallig nog? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. het coordinaat wrdt nu geinitiliseerd as rdnap (is 3d) met 0 waarden. het gevaar hierbij is dat, als deze om een of andere reden gebruikt gaat worden met transformaties, er rare dingen gaan gebeuren omdat er 3 waarden aanwezig horen te zijn. (dat is volgens mij het probleem waar Tom toen tegenaan liep, of het was rdnap met 2 waarden) |
||
|
||
public Quaternion Rotation { get; set; } = Quaternion.identity; | ||
public CoordinateSystem ReferenceCoordinateSystem => referenceCoordinateSystem; | ||
public Origin Origin => Origin.current; | ||
|
||
|
@@ -56,7 +51,6 @@ private void OnEnable() | |
{ | ||
ShiftTo(Coordinate, Coordinate); | ||
} | ||
|
||
} | ||
|
||
private void OnDisable() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gaat dit goed met de bolling van de aarde? moet er niet ook een ToUnity() functie worden aangeroepen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worldTransform.Rotation is een Quaternion die -als ik @mvangog goed begrepen heb- de rotatie van de gameobject is na toepassen van de bolling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maar dan zou de gebolde rotatie nu toepast worden op het gameObject, waardoor een object op de evenaar dus 90º gedraaid zou zijn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Ik zou momenteel niet zo goed weten hoe dit te testen; we doen nu nog alles met RD waardoor de bolling niet wordt toegepast..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En anders draai ik de rotatie dingen even terug; voor de zekerheid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worldtransform.rotation is de rotatie van het gameobject in zijn eigen coordinatenstelsel. (dus vóór het toepassen van de "bolling", zoals mike het noemt)
de rotatie in unity wordt uitgerekend met worldtransform.coordinate.RotationToUnityUp() * worldtransform.rotation