Example #1
0
        /// <summary>
        /// Handle additional <see cref="UpdateMessage"/> data.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msg"></param>
        /// <param name="packet"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        protected override Error PostHandleMessage(GameObject obj, UpdateMessage msg, PacketBuffer packet, BinaryReader reader)
        {
            TextMesh       textMesh  = obj.GetComponent <TextMesh>();
            ShapeComponent shapeComp = obj.GetComponent <ShapeComponent>();

            if (textMesh != null && shapeComp != null)
            {
                textMesh.fontSize = (int)msg.Attributes.ScaleZ;
                textMesh.color    = shapeComp.Colour;
            }

            // Convert position to Unity position.
            obj.transform.localPosition = FrameTransform.RemoteToUnity(obj.transform.localPosition, ServerInfo.CoordinateFrame);

            if (shapeComp != null && (msg.Flags & (ushort)Text3DFlag.SceenFacing) != (shapeComp.ObjectFlags & (ushort)Text3DFlag.SceenFacing))
            {
                // Screen facing flag changed.
                if ((msg.Flags & (ushort)Text3DFlag.SceenFacing) != 0)
                {
                    // Need to use -forward for text.
                    ScreenFacing.AddToManager(obj, -Vector3.forward, Vector3.up);
                }
                else
                {
                    // Need to use -forward for text.
                    ScreenFacing.RemoveFromManager(obj);
                }
            }
            return(new Error());
        }
Example #2
0
        /// <summary>
        /// Encode transform attributes for text.
        /// </summary>
        /// <param name="attr"></param>
        /// <param name="obj"></param>
        /// <param name="comp"></param>
        protected override void EncodeAttributes(ref ObjectAttributes attr, GameObject obj, ShapeComponent comp)
        {
            Transform transform = obj.transform;
            // Convert position to Unity position.
            Vector3 pos = FrameTransform.UnityToRemote(obj.transform.position, ServerInfo.CoordinateFrame);

            attr.X         = pos.x;
            attr.Y         = pos.y;
            attr.Z         = pos.z;
            attr.RotationX = transform.localRotation.x;
            attr.RotationY = transform.localRotation.y;
            attr.RotationZ = transform.localRotation.z;
            attr.RotationW = transform.localRotation.w;
            attr.ScaleX    = 1.0f;
            attr.ScaleY    = 1.0f;
            attr.ScaleZ    = 12.0f;

            TextMesh text = obj.GetComponent <TextMesh>();

            if (text != null)
            {
                attr.ScaleZ = text.fontSize;
            }
            if (comp != null)
            {
                attr.Colour = ShapeComponent.ConvertColour(comp.Colour);
            }
            else
            {
                attr.Colour = 0xffffffu;
            }
        }
Example #3
0
        /// <summary>
        /// Handle additional <see cref="CreateMessage"/> data.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msg"></param>
        /// <param name="packet"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        protected override Error PostHandleMessage(GameObject obj, CreateMessage msg, PacketBuffer packet, BinaryReader reader)
        {
            // Convert position to Unity position.
            obj.transform.localPosition = FrameTransform.RemoteToUnity(obj.transform.localPosition, ServerInfo.CoordinateFrame);

            // Read the text in the buffer.
            int textLength = reader.ReadUInt16();

            if (textLength > 0)
            {
                TextMesh textMesh = obj.GetComponent <TextMesh>();
                if (textMesh == null)
                {
                    textMesh = obj.AddComponent <TextMesh>();
                }

                byte[] textBytes = reader.ReadBytes(textLength);
                textMesh.text = System.Text.Encoding.UTF8.GetString(textBytes);
                ShapeComponent shapeComp = obj.GetComponent <ShapeComponent>();
                if (shapeComp != null)
                {
                    textMesh.fontSize = (int)msg.Attributes.ScaleZ;
                    textMesh.color    = shapeComp.Colour;
                }
            }

            if ((msg.Flags & (ushort)Text3DFlag.SceenFacing) != 0)
            {
                // Need to use -forward for text.
                ScreenFacing.AddToManager(obj, -Vector3.forward, Vector3.up);
            }

            return(base.PostHandleMessage(obj, msg, packet, reader));
        }
Example #4
0
//  public void Transform()
//  {
//    GameObject toUnityObj = new GameObject();
//    GameObject fromUnityObj = new GameObject();
//
//    foreach (CoordinateFrame frame in Enum.GetValues(typeof(CoordinateFrame)))
//    {
//      FrameTransform.SetFrameRotation(toUnityObj.transform, frame);
//    }
//    Debug.Log("ok");
//  }

    public void FrameConsistency()
    {
        Matrix4x4 toUnity = new Matrix4x4();
        string    message;
        bool      shouldChangeHands;
        bool      changedHands;
        bool      ok = true;

        foreach (CoordinateFrame frame in Enum.GetValues(typeof(CoordinateFrame)))
        {
            //string FrameName = frame.ToString();
            FrameTransform.SetFrameRotation(ref toUnity, frame);
            shouldChangeHands = frame < CoordinateFrame.LeftHanded;
            changedHands      = FrameTransform.EffectsHandChange(toUnity);
            ok = ok && shouldChangeHands == changedHands;

            message = string.Format("{0} R? {1} : Ok? {2}", frame.ToString(), shouldChangeHands, changedHands);
            if (shouldChangeHands == changedHands)
            {
                Debug.Log(message);
            }
            else
            {
                Debug.LogError(message);
            }
        }

        if (!ok)
        {
            throw new Exception("failed");
        }
    }
Example #5
0
    public void Conversion()
    {
        GameObject parent = new GameObject("Parent");
        GameObject child = new GameObject("Child");
        Matrix4x4  toUnity = new Matrix4x4();
        Matrix4x4  toRemote = new Matrix4x4();
        Vector3    v, v1, v2;

        bool stepOk = true;
        bool ok     = true;

        child.transform.SetParent(parent.transform);

        foreach (CoordinateFrame frame in Enum.GetValues(typeof(CoordinateFrame)))
        {
            //string FrameName = frame.ToString();
            FrameTransform.SetFrameRotation(ref toUnity, frame);
            FrameTransform.SetFrameRotationInverse(ref toRemote, frame);
            FrameTransform.SetFrameRotation(parent.transform, frame);

            for (int i = 0; i < TestVectors.Length; ++i)
            {
                v = TestVectors[i];
                child.transform.localPosition = v;
                v1 = toUnity.MultiplyPoint(v);
                v2 = toRemote.MultiplyPoint(v1);

                stepOk = ApproxEqual(v, v2);
                ok     = ok && stepOk;

                if (!stepOk)
                {
                    Debug.LogError(string.Format("Transformation {0} failed: {1} -> {2} -> {3}", frame, v, v1, v2));
                }

                v2     = child.transform.position;
                stepOk = ApproxEqual(v1, v2);
                ok     = ok && stepOk;

                if (!stepOk)
                {
                    Debug.LogError(string.Format("Transform/Matrix {0} disagreement: {1} : {2} <> {3}", frame, v, v2, v1));
                }
            }
        }

        GameObject.Destroy(parent);
        GameObject.Destroy(child);
        if (!ok)
        {
            throw new Exception("failed");
        }
    }
        /// <summary>
        /// Serialises the currently active objects in for playback from file.
        /// </summary>
        /// <param name="writer">The write to serialise to.</param>
        /// <param name="info">Statistics</param>
        /// <returns>An error code on failure.</returns>
        public override Error Serialise(BinaryWriter writer, ref SerialiseInfo info)
        {
            Error err = new Error();

            info.PersistentCount = info.TransientCount = 0;
            if (_cameras.Count > 0)
            {
                PacketBuffer  packet = new PacketBuffer(256);
                CameraMessage msg    = new CameraMessage();
                Vector3       v      = Vector3.zero;
                msg.Reserved1 = 0;
                msg.Reserved2 = 0;
                foreach (CameraInfo camera in _cameras.Values)
                {
                    ++info.PersistentCount;
                    msg.CameraID = camera.ID;
                    v            = FrameTransform.UnityToRemote(camera.transform.localPosition, ServerInfo.CoordinateFrame);
                    msg.X        = v.x;
                    msg.Y        = v.y;
                    msg.Z        = v.z;
                    v            = FrameTransform.UnityToRemote(camera.transform.forward, ServerInfo.CoordinateFrame);
                    msg.DirX     = v.x;
                    msg.DirY     = v.y;
                    msg.DirZ     = v.z;
                    v            = FrameTransform.UnityToRemote(camera.transform.up, ServerInfo.CoordinateFrame);
                    msg.UpX      = v.x;
                    msg.UpY      = v.y;
                    msg.UpZ      = v.z;
                    msg.Near     = camera.Near;
                    msg.Far      = camera.Far;
                    msg.FOV      = camera.FOV;
                    packet.Reset(RoutingID, 0);
                    msg.Write(packet);
                    packet.FinalisePacket();
                    packet.ExportTo(writer);
                }
            }

            return(err);
        }
        /// <summary>
        /// The primary message handling function.
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public override Error ReadMessage(PacketBuffer packet, BinaryReader reader)
        {
            CameraMessage msg = new CameraMessage();

            if (!msg.Read(reader))
            {
                return(new Error(ErrorCode.MalformedMessage));
            }

            CameraInfo camera = null;

            if (_cameras.ContainsKey(msg.CameraID))
            {
                camera = _cameras[msg.CameraID];
            }
            else
            {
                GameObject obj = new GameObject(string.Format("Camera {0}", msg.CameraID));
                camera = obj.AddComponent <CameraInfo>();
                _cameras.Add(msg.CameraID, camera);
                obj.transform.SetParent(_root.transform, false);
                camera.ID = msg.CameraID;
            }

            camera.Near = msg.Near;
            camera.Far  = msg.Far;
            camera.FOV  = msg.FOV;

            Transform transform = camera.transform;
            Vector3   dir       = FrameTransform.RemoteToUnity(new Vector3(msg.DirX, msg.DirY, msg.DirZ), ServerInfo.CoordinateFrame);
            Vector3   up        = FrameTransform.RemoteToUnity(new Vector3(msg.UpX, msg.UpY, msg.UpZ), ServerInfo.CoordinateFrame);

            transform.localPosition = FrameTransform.RemoteToUnity(new Vector3(msg.X, msg.Y, msg.Z), ServerInfo.CoordinateFrame);
            transform.LookAt(transform.position + dir, up);

            return(new Error());
        }