public void RecordStreams(float deltaT)
        {
            foreach (RefID user_id in current_users)
            {
                //Encode the streams
                BinaryWriterX writer = output_writers[user_id];

                //WRITE deltaT
                writer.Write(deltaT); //float
                int node_index = 0;
                //foreach (var item in avatar_stream_drivers[user_id])
                foreach (var item in avatar_object_slots[user_id])
                {
                    BodyNode node = item.Item1;
                    //UniLog.Log(node);
                    //TransformStreamDriver driver = item.Item2;
                    TransformStreamDriver driver       = avatar_stream_drivers[user_id][node_index].Item2;
                    IAvatarObject         avatarObject = item.Item2.Equipped?.Target;
                    Slot slot = null;
                    if (node == BodyNode.Root)
                    {
                        slot = driver.Slot;
                    }
                    else if (avatarObject != null)
                    {
                        slot = avatarObject.Slot;
                    }
                    else
                    {
                        slot = tracked_device_positioners[user_id][node_index].Item2.BodyNodeRoot.Target;
                    }

                    //WRITE the transform

                    //scale stream;
                    if (driver.ScaleStream.Target != null)
                    {
                        float3 scale = slot.LocalScale;
                        scale = slot.Parent.LocalScaleToSpace(scale, driver.Slot.Parent);
                        if (node == BodyNode.Root)
                        {
                            //scale = driver.TargetScale;
                            scale = slot.Parent.LocalScaleToGlobal(scale);
                        }
                        writer.Write((float)(scale.x));
                        writer.Write((float)(scale.y));
                        writer.Write((float)(scale.z));
                    }
                    //position stream;
                    if (driver.PositionStream.Target != null)
                    {
                        float3 position = slot.LocalPosition;
                        position = slot.Parent.LocalPointToSpace(position, driver.Slot.Parent);
                        if (node == BodyNode.Root)
                        {
                            //position = driver.TargetPosition;
                            position = slot.Parent.LocalPointToGlobal(position);
                        }
                        writer.Write(position.x);
                        writer.Write(position.y);
                        writer.Write(position.z);
                    }
                    //rotation stream;
                    if (driver.RotationStream.Target != null)
                    {
                        floatQ rotation = slot.LocalRotation;
                        rotation = slot.Parent.LocalRotationToSpace(rotation, driver.Slot.Parent);
                        if (node == BodyNode.Root)
                        {
                            //rotation = driver.TargetRotation;
                            rotation = slot.Parent.LocalRotationToGlobal(rotation);
                        }
                        writer.Write(rotation.x);
                        writer.Write(rotation.y);
                        writer.Write(rotation.z);
                        writer.Write(rotation.w);
                    }
                    node_index++;
                }
                //WRITE finger pose
                if (finger_stream_drivers[user_id] != null)
                {
                    //Left Hand
                    for (int index = 0; index < FingerPoseStreamManager.FINGER_NODE_COUNT; ++index)
                    {
                        BodyNode node = (BodyNode)(18 + index);
                        float3   position;
                        floatQ   rotation;
                        bool     was_succesful = finger_stream_drivers[user_id].TryGetFingerData(node, out position, out rotation);
                        //WRITE whether finger data was obtained
                        writer.Write(was_succesful);
                        writer.Write(rotation.x);
                        writer.Write(rotation.y);
                        writer.Write(rotation.z);
                        writer.Write(rotation.w);
                    }
                    //Right Hand
                    for (int index = 0; index < FingerPoseStreamManager.FINGER_NODE_COUNT; ++index)
                    {
                        BodyNode node = (BodyNode)(47 + index);
                        float3   position;
                        floatQ   rotation;
                        bool     was_succesful = finger_stream_drivers[user_id].TryGetFingerData(node, out position, out rotation);
                        //WRITE whether finger data was obtained
                        writer.Write(was_succesful);
                        writer.Write(rotation.x);
                        writer.Write(rotation.y);
                        writer.Write(rotation.z);
                        writer.Write(rotation.w);
                    }
                }
            }
        }
Beispiel #2
0
        public void PlayStreamsInternal()
        {
            if (!avatars_finished_loading)
            {
                return;
            }
            World currentWorld = metagen_comp.World;

            //currentWorld.RunSynchronously(() =>
            //{
            try
            {
                foreach (RefID user_id in user_ids)
                {
                    //Decode the streams
                    Task readTask = output_readers[user_id].MoveNext();
                    if (Task.WhenAny(readTask, Task.Delay(2000)).Result != readTask)
                    {
                        continue;
                    }
                    IEnumerator <float> reader = output_readers[user_id].Current.Transforms.GetEnumerator();
                    //UniLog.Log(output_readers[user_id].Current.Transforms);
                    //await default(ToWorld);

                    //READ deltaT
                    //float deltaT = reader.ReadSingle();
                    int node_index = 0;
                    //foreach (var item in fake_proxies[user_id])
                    foreach (var item in avatar_pose_nodes[user_id])
                    {
                        BodyNode node = item.Item1;
                        var      available_streams = avatar_stream_channels[user_id][node];
                        //AvatarObjectSlot comp = item.Item2;
                        AvatarObjectSlot avatarObject = fake_proxies[user_id][node_index].Item2;
                        IAvatarObject    comp         = item.Item2;
                        Slot             slot         = comp != null ? comp.Slot : null;
                        if (node == BodyNode.Root)
                        {
                            slot = avatarObject.Slot;
                        }

                        //READ transform
                        float x, y, z, w;
                        //Scale stream
                        if (available_streams.Item1)
                        {
                            reader.MoveNext();
                            x = reader.Current;
                            reader.MoveNext();
                            y = reader.Current;
                            reader.MoveNext();
                            z = reader.Current;
                            reader.MoveNext();
                            float3 scale = new float3(x, y, z);
                            if (slot != null)
                            {
                                //scale = avatarObject.Slot.Parent.LocalScaleToSpace(scale, slot.Parent);
                                //slot.LocalScale = scale;
                                //slot.GlobalScale = scale;
                            }
                            //UniLog.Log(slot.LocalScale.ToString());
                        }
                        //Position stream
                        if (available_streams.Item2)
                        {
                            x = reader.Current;
                            reader.MoveNext();
                            y = reader.Current;
                            reader.MoveNext();
                            z = reader.Current;
                            reader.MoveNext();
                            float3 position = new float3(x, y, z);
                            //UniLog.Log(position.x);
                            //UniLog.Log(position.y);
                            //UniLog.Log(position.z);
                            if (slot != null)
                            {
                                //position = avatarObject.Slot.Parent.LocalPointToSpace(position, slot.Parent);
                                //slot.LocalPosition = position;
                                slot.GlobalPosition = position;
                            }
                            //UniLog.Log(slot.LocalPosition.ToString());
                        }
                        //Rotation stream
                        if (available_streams.Item3)
                        {
                            x = reader.Current;
                            reader.MoveNext();
                            y = reader.Current;
                            reader.MoveNext();
                            z = reader.Current;
                            reader.MoveNext();
                            w = reader.Current;
                            floatQ rotation = new floatQ(x, y, z, w);
                            if (slot != null)
                            {
                                //rotation = avatarObject.Slot.Parent.LocalRotationToSpace(rotation, slot.Parent);
                                //rotation = avatarObject.Slot.Parent.GlobalRotationToLocal(rotation);
                                //slot.LocalRotation = rotation;
                                slot.GlobalRotation = rotation;
                            }
                            //UniLog.Log(slot.LocalRotation.ToString());
                        }
                        node_index++;
                    }

                    //READ finger pose
                    var finger_slot = finger_slots[user_id];
                    if (hands_are_tracked[user_id])
                    {
                        //UniLog.Log("UPDATING HANDS");
                        //FingerPlayerSource finger_source = finger_sources[user_id];
                        float x, y, z, w;
                        //Left Hand
                        HandPoser hand_poser = hand_posers[user_id][Chirality.Left];
                        floatQ    lookRot    = floatQ.LookRotation(hand_poser.HandForward, hand_poser.HandUp);
                        for (int index = 0; index < FingerPoseStreamManager.FINGER_NODE_COUNT; ++index)
                        {
                            BodyNode node = (BodyNode)(18 + index);
                            //READ whether finger data was obtained
                            //bool was_succesful = reader.ReadBoolean();
                            x = reader.Current;
                            reader.MoveNext();
                            y = reader.Current;
                            reader.MoveNext();
                            z = reader.Current;
                            reader.MoveNext();
                            w = reader.Current;
                            reader.MoveNext();
                            if (finger_slot.ContainsKey(node))
                            {
                                floatQ rot = new floatQ(x, y, z, w);
                                rot = lookRot * rot;
                                Slot root = hand_poser.HandRoot.Target ?? hand_poser.Slot;
                                rot = finger_slot[node].Parent.SpaceRotationToLocal(rot, root);
                                rot = rot * finger_compensations[user_id][node];
                                finger_slot[node].LocalRotation = rot;
                            }
                        }
                        //Right Hand
                        hand_poser = hand_posers[user_id][Chirality.Right];
                        lookRot    = floatQ.LookRotation(hand_poser.HandForward, hand_poser.HandUp);
                        for (int index = 0; index < FingerPoseStreamManager.FINGER_NODE_COUNT; ++index)
                        {
                            BodyNode node = (BodyNode)(47 + index);
                            //READ whether finger data was obtained
                            //bool was_succesful = reader.ReadBoolean();
                            x = reader.Current;
                            reader.MoveNext();
                            y = reader.Current;
                            reader.MoveNext();
                            z = reader.Current;
                            reader.MoveNext();
                            w = reader.Current;
                            reader.MoveNext();
                            //finger_source.UpdateFingerPose(node, new floatQ(x, y, z, w));
                            if (finger_slot.ContainsKey(node))
                            {
                                floatQ rot = new floatQ(x, y, z, w);
                                rot = lookRot * rot;
                                Slot root = hand_poser.HandRoot.Target ?? hand_poser.Slot;
                                rot = finger_slot[node].Parent.SpaceRotationToLocal(rot, root);
                                rot = rot * finger_compensations[user_id][node];
                                finger_slot[node].LocalRotation = rot;
                            }
                        }
                    }
                    //await default(ToBackground);
                }
                if (generateAnimation)
                {
                    animationRecorder.RecordFrame();
                }
                if (generateBvh)
                {
                    bvhRecorder.RecordFrame();
                }
            }
            catch (Exception e)
            {
                UniLog.Log("OwO: " + e.Message);
                //this.StopPlaying();
                metagen_comp.StopPlaying();
            }
            //});
        }