public PlayerWireData(string playerDeviceId, TransformWireData head, TransformWireData left, TransformWireData right)
 {
     this.playerDeviceId             = playerDeviceId;
     this.playerTransformWireData[0] = head;
     this.playerTransformWireData[1] = left;
     this.playerTransformWireData[2] = right;
 }
Exemplo n.º 2
0
    private void SendPlayerPosition()
    {
        TransformWireData transformWireDataHead  = new TransformWireData(head);
        TransformWireData transformWireDataLeft  = new TransformWireData(leftHand);
        TransformWireData transformWireDataRight = new TransformWireData(rightHand);

        string         playerDeviceId = SystemInfo.deviceUniqueIdentifier;
        PlayerWireData playerWireData = new PlayerWireData(playerDeviceId, transformWireDataHead, transformWireDataLeft, transformWireDataRight);

        byte[] data = NetworkTransportManager.Serialize(playerWireData);
        netTransportManager.SendUnreliableData(data);
    }
Exemplo n.º 3
0
    public ModelWireData(Vector3[] vertices, Vector2[] uvs, int[] triangles, float[] materialColour, float materialGlossiness, float materialMetallic, byte[] textureData, int textureWidth, int textureHeight, TextureFormat textureFormat, string name, Transform transform) : base(transform)
    {
        //Mesh Parameters
        //creating 2d float array for v3s
        this.vertices       = new float[vertices.Length, 3];
        this.verticesLength = vertices.Length;
        for (int i = 0; i < vertices.Length; i++)
        {
            this.vertices[i, 0] = vertices[i].x;
            this.vertices[i, 1] = vertices[i].y;
            this.vertices[i, 2] = vertices[i].z;
        }

        //constructing v2 uv[]
        this.uvs       = new float[uvs.Length, 2];
        this.uvsLength = uvs.Length;
        for (int i = 0; i < uvs.Length; i++)
        {
            this.uvs[i, 0] = uvs[i].x;
            this.uvs[i, 1] = uvs[i].y;
        }

        //constructing int[]
        this.triangles       = new int[triangles.Length];
        this.trianglesLength = triangles.Length;
        for (int i = 0; i < triangles.Length; i++)
        {
            this.triangles = triangles;
        }

        //confirmation constructor worked.
        Debug.Log("Finished constructing vertices of length: " + this.vertices.Length);
        Debug.Log("Finished constructing uv of length: " + this.uvs.Length);
        Debug.Log("Finished constructing triangles of length: " + this.triangles.Length);

        //Material Parameters
        this.materialColour     = materialColour;
        this.materialGlossiness = materialGlossiness;
        this.materialMetallic   = materialMetallic;

        this.textureData   = textureData;
        this.textureWidth  = textureWidth;
        this.textureHeight = textureHeight;

        this.textureFormat = (int)textureFormat;

        //model name for checking if present
        this.name = name;

        //transform
        this.transform = new TransformWireData(transform);
    }
    /// <summary>
    /// Applies transform wiredata coordinates to a object transform. Bool to include scale.
    /// </summary>
    /// <param name="transformWireData"></param>
    /// <param name="transformTarget"></param>
    /// <param name="includeScale"></param>
    public static void ApplyTransform(TransformWireData transformWireData, Transform transformTarget, bool includeScale)
    {
        Vector3 newPosition = new Vector3(transformWireData.posX, transformWireData.posY, transformWireData.posZ);

        transformTarget.position = newPosition;

        Vector3    v3rotation  = new Vector3(transformWireData.rotX, transformWireData.rotY, transformWireData.rotZ);
        Quaternion newRotation = Quaternion.Euler(v3rotation);

        transformTarget.rotation = newRotation;

        if (includeScale)
        {
            Vector3 newScale = new Vector3(transformWireData.scaX, transformWireData.scaY, transformWireData.scaZ);
            transformTarget.localScale = newScale;
        }
    }
Exemplo n.º 5
0
    public void OnData(int hostId, int connId, int chanId, byte[] data, int dataSize, NetworkError error)
    {
        if (chanId == netTransportManager.reliableFragmentedSequencedChannelId)
        {
            List <byte> addBytes = new List <byte>(data);
            receivedBytes.AddRange(addBytes);

            //TODO: Will break if send a modelMeshData thats a multiple of 1024 bytes
            if (dataSize < ChunkSize)
            {
                byte[]          totalData     = receivedBytes.ToArray();
                BinaryFormatter bf            = new BinaryFormatter();
                MemoryStream    ms            = new MemoryStream(totalData);
                ModelWireData   modelWireData = bf.Deserialize(ms) as ModelWireData;
                Debug.Log(modelWireData.verticesLength);
                Debug.Log(modelWireData.trianglesLength);

                if (GameObject.Find(modelWireData.name))
                {
                    Debug.Log("model already exists locally. No generation required");
                    return;
                }

                //loop 2d float array, make into vectors and add to new vertices array
                //genVert is size of the v3[] length metadata of the modelWireData object. Not the size of the 2d float array.
                Vector3[] genVertices = new Vector3[modelWireData.verticesLength];
                for (int i = 0; i < modelWireData.verticesLength; i++)
                {
                    //0 = x, 1 = y, 2 = z
                    genVertices[i] = new Vector3(modelWireData.vertices[i, 0], modelWireData.vertices[i, 1], modelWireData.vertices[i, 2]);
                }
                Debug.Log("generated vertices length: " + genVertices.Length);

                //loop through uvs into v2 array
                Vector2[] genUvs = new Vector2[modelWireData.uvsLength];
                for (int i = 0; i < modelWireData.uvsLength; i++)
                {
                    //0 = uv.x, 1 = uv.y
                    genUvs[i] = new Vector2(modelWireData.uvs[i, 0], modelWireData.uvs[i, 1]);
                }
                Debug.Log("generated uv length: " + genUvs.Length);

                //assign received triangle array to genTriangles array
                int[] genTriangles = modelWireData.triangles;
                Debug.Log("Generated triangles " + genTriangles.Length);

                //creating mesh with the generated vertices and triangles
                Mesh genMesh = new Mesh
                {
                    vertices  = genVertices,
                    uv        = genUvs,
                    triangles = genTriangles
                };

                //calculating mesh properties for rendering purposes
                genMesh.RecalculateNormals();
                genMesh.RecalculateBounds();
                genMesh.RecalculateTangents();

                //Adding generated mesh to container game object
                GameObject genGo = new GameObject
                {
                    name = modelWireData.name,
                    tag  = "Model"
                };
                MeshFilter genGoMeshFilter = genGo.AddComponent <MeshFilter>();
                genGoMeshFilter.mesh = genMesh;

                //Adding mesh renderer and assigning generated mesh to mesh renderer.
                MeshRenderer genGoMeshRenderer = genGo.AddComponent <MeshRenderer>();

                //Material reconstruction
                Material genMaterial = genGoMeshRenderer.material = new Material(Shader.Find("Standard"));
                genMaterial.name = "generatedMaterial";
                //Generate colour from float array [r=0, g=1, b=2, g=3]
                Color genColour = new Color(modelWireData.materialColour[0], modelWireData.materialColour[1], modelWireData.materialColour[2], modelWireData.materialColour[3]);
                genMaterial.color = genColour;
                genMaterial.SetFloat("_Glossiness", modelWireData.materialGlossiness);
                genMaterial.SetFloat("_Metallic", modelWireData.materialMetallic);

                //Texture reconstruction
                byte[]        texBytes      = modelWireData.textureData;
                TextureFormat textureFormat = (TextureFormat)modelWireData.textureFormat;
                Texture2D     genTex2D      = new Texture2D(modelWireData.textureWidth, modelWireData.textureHeight, textureFormat, false);
                genTex2D.LoadRawTextureData(texBytes);
                Debug.Log("generated texture " + genTex2D.width + " x " + genTex2D.height);
                genMaterial.mainTexture = genTex2D;
                genTex2D.Apply();

                //Transform reconstruction
                TransformWireData.ApplyTransform(modelWireData.transform, genGo.transform, true);

                //Empty list for new model
                receivedBytes.Clear();
            }
        }
        else if (chanId == netTransportManager.unreliableChannelId)
        {
            MemoryStream    memoryStream           = new MemoryStream(data);
            BinaryFormatter binaryFormatter        = new BinaryFormatter();
            PlayerWireData  receivedPlayerWireData = binaryFormatter.Deserialize(memoryStream) as PlayerWireData;

            //If player doesn't exist in game then create one
            GameObject player = GameObject.Find(receivedPlayerWireData.playerDeviceId);
            if (player == null)
            {
                player      = Instantiate(playerPrefab);
                player.name = receivedPlayerWireData.playerDeviceId;
                player.tag  = "Player";
            }

            //set transforms of the new player
            TransformWireData[] transformsArray = receivedPlayerWireData.playerTransformWireData;
            for (int i = 0; i < transformsArray.Length; i++)
            {
                TransformWireData.ApplyTransform(transformsArray[i], player.transform.GetChild(i), false);
            }
        }
    }