I'm trying to port my project to godot, but there is no concept of transform in Godot. Now here it is.
To use it:
using UnityPortWarpper;
node3D.transform().TransformPoint(localPoint);
node3D.gameObject().SetActive(false);
Node3D can be replace to any node that inherent from Node3D.
To use this port from Unity c#, "transform" become "transform()". I wish I can remove "()", but I haven't found a way to write extension properties.
I'm a beginner here, so please point out any mistakes, or if this is a bad practice
`
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityPortWarpper
{
public class Transform
{
public Node3D node;
public Transform(Node3D node)
{
this.node = node;
if (!nodeTransformDictionary.ContainsKey(node))
{
nodeTransformDictionary.Add(node, this);
}
else if (nodeTransformDictionary[node] != this)
{
nodeTransformDictionary[node] = this;
}
}
public Vector3 position { get { return node.GlobalPosition; } set { node.GlobalPosition = value; } }
public Vector3 localPosition { get { return node.Position; } set { node.Position = value; } }
public Quaternion rotation { get { return node.GlobalTransform.Basis.GetRotationQuaternion(); } }//set no equivalent?
public Quaternion localRotation { get { return node.Quaternion; } set { node.Quaternion = value; } }
public Vector3 eulerAngles { get { return node.GlobalRotationDegrees; } set { node.GlobalRotationDegrees = value; } }
public Vector3 localScale { get { return node.Scale; } set { node.Scale = value; } }
public Vector3 lossyScale { get { return node.GlobalTransform.Basis.Scale; } }
public Vector3 forward => -node.GlobalTransform.Basis.Z;
public Vector3 up => node.GlobalTransform.Basis.Y;
public Vector3 right => node.GlobalTransform.Basis.X;
public void LookAt(Vector3 position, Vector3? up = null) => node.LookAt(position, up);
public void Rotate(Vector3 eulers)
{
node.RotateX(eulers.X); node.RotateY(eulers.Y); node.RotateZ(eulers.Z);
}
public void Translate(Vector3 globalTranslation) => node.GlobalTranslate(globalTranslation);
public Vector3 TransformPoint(Vector3 localPoint) => node.GlobalTransform * localPoint;
public Vector3 InverseTransformPoint(Vector3 worldPoint) => worldPoint * node.GlobalTransform;//or node.GlobalTransform.affine_inverse() * worldPoint
public Vector3 TransformDirection(Vector3 localDirection) => node.Transform.Basis * localDirection;
public Vector3 InverseTransformDirection(Vector3 localDirection) => localDirection * node.Transform.Basis;
public Vector3 TransformVector(Vector3 localVector)
{
return TransformPoint(localVector) - position;
}
public GameObject gameObject => node.gameObject();
#region my method
public void SetGlobalScale(Vector3 globalScale)
{
node.GlobalScale(globalScale);
// if (transform.lossyScale == globalScale)
// {
// return;
// }
// transform.localScale = Vector3.one;
// transform.localScale = new Vector3(globalScale.x / transform.lossyScale.x, globalScale.y / transform.lossyScale.y, globalScale.z / transform.lossyScale.z);
//
}
public void IterateAllChidren(Action<Transform> iterateAction, bool recursive = true)
{
foreach (Node child in node.GetChildren())
{
if (child is Node3D)
{
Node3D child3D = (Node3D)child;
iterateAction(child3D.transform());
if (recursive)
child3D.transform().IterateAllChidren(iterateAction, true);
}
}
}
#endregion
public static bool operator ==(Transform g1, Transform g2) => g1.node == g2.node;
public static bool operator !=(Transform g1, Transform g2) => g1.node != g2.node;
public override bool Equals(object obj)
{
return obj is Transform && Equals((Transform)obj);
}
public override int GetHashCode()
{
return node.GetHashCode();
}
public static Dictionary<Node3D, Transform> nodeTransformDictionary = new Dictionary<Node3D, Transform>();
}
public class GameObject
{
public Node3D node;
public bool activeSelf => node.Visible;
public bool activeInHierarchy => node.Visible && node.IsInsideTree();
public GameObject(Node3D node)
{
this.node = node;
if (!nodeGameObjectDictionary.ContainsKey(node))
{
nodeGameObjectDictionary.Add(node, this);
}
else if (nodeGameObjectDictionary[node] != this)
{
nodeGameObjectDictionary[node] = this;
}
}
public void SetActive(bool active)
{
node.SetProcess(active);
node.SetPhysicsProcess(active);
}
public Transform transform => node.transform();
public static bool operator ==(GameObject g1, GameObject g2) => g1.node == g2.node;
public static bool operator !=(GameObject g1, GameObject g2) => g1.node != g2.node;
public override bool Equals(object obj)
{
return obj is GameObject && Equals((GameObject)obj);
}
public override int GetHashCode()
{
return node.GetHashCode();
}
public static Dictionary<Node3D, GameObject> nodeGameObjectDictionary = new Dictionary<Node3D, GameObject>();
}
public static class Extensions
{
public static Transform transform(this Node3D node)
{
if (Transform.nodeTransformDictionary.TryGetValue(node, out Transform t))
{
return t;
}
else
{
return new Transform(node);
}
}
public static GameObject gameObject(this Node3D node)
{
if (GameObject.nodeGameObjectDictionary.TryGetValue(node, out GameObject g))
{
return g;
}
else
{
return new GameObject(node);
}
}
}
//public does not work
//{
// public Transform transform
// {
// get
// {
// Transform.nodeTransformDictionary.TryGetValue(this, out Transform t);
// if (t == null)
// {
// t = new Transform(this);
// }
// return t;
// }
// }
// public static GameObject gameObject
// {
// get
// {
// GameObject.nodeGameObjectDictionary.TryGetValue(this, out GameObject g);
// if (g == null)
// {
// g = new GameObject(this);
// }
// return g;
// }
// }
//}
}
`