// Converts an OBJMesh (assumed to be a tri-mesh) to a bullet-friendly structure
    public static Mesh ToUnityMesh(OBJLoader.OBJMesh objMesh)
    {
        Mesh mesh = new Mesh();

        UnityEngine.Vector3[] newVertices = new UnityEngine.Vector3[objMesh.faces.Count * 3]; // must make a separate copy of each for normals to be correct
        int[] newTriangles = new int[objMesh.faces.Count * 3];

        for (int i = 0; i < objMesh.vertices.Count; i++)
        {
            newVertices[i] = objMesh.vertices[i];
        }
        for (int i = 0; i < objMesh.faces.Count; i++)
        {
            UnityEngine.Vector3 v1 = objMesh.vertices[objMesh.faces[i].indexes[0]];
            UnityEngine.Vector3 v2 = objMesh.vertices[objMesh.faces[i].indexes[1]];
            UnityEngine.Vector3 v3 = objMesh.vertices[objMesh.faces[i].indexes[2]];

            newVertices[i * 3 + 0] = v1;
            newVertices[i * 3 + 1] = v2;
            newVertices[i * 3 + 2] = v3;

            newTriangles[i * 3 + 0] = i * 3 + 0;
            newTriangles[i * 3 + 1] = i * 3 + 1;
            newTriangles[i * 3 + 2] = i * 3 + 2;
        }

        mesh.vertices  = newVertices;
        mesh.triangles = newTriangles;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();

        return(mesh);
    }
Example #2
0
    // Converts an OBJMesh (assumed to be a tri-mesh) to a bullet-friendly structure
    public static BulletOBJMesh BulletMeshFromUnity(OBJLoader.OBJMesh mesh)
    {
        BulletOBJMesh btmesh = new BulletOBJMesh();

        btmesh.vertices = new float[mesh.vertices.Count * 3];
        for (int i = 0; i < mesh.vertices.Count; i++)
        {
            UnityEngine.Vector3 curVert = mesh.vertices[i];
            btmesh.vertices[i * 3 + 0] = curVert[0];
            btmesh.vertices[i * 3 + 1] = curVert[1];
            btmesh.vertices[i * 3 + 2] = curVert[2];
        }

        List <int> indList = new List <int>();

        for (int i = 0; i < mesh.faces.Count; i++)
        {
            int[] faceIndices = mesh.faces[i].indexes;
            for (int j = 0; j < faceIndices.Length; j++)
            {
                int idx = faceIndices[j];
                indList.Add(faceIndices[j]);
            }
        }

        btmesh.indices = indList.ToArray();

        return(btmesh);
    }
    void LoadSim(int idx)
    {
        // read in results from json
        string predFile = Path.Combine(dataInDir, "eval_sim_" + idx.ToString());

        Debug.Log("Visualizing " + predFile);
        var jsonTextFile = Resources.Load <TextAsset>(predFile);

        m_curPred = PredInfo.CreateFromJSON(jsonTextFile.ToString());

        // read in mesh to use
        string meshFile = Path.Combine(m_baseModelDir, m_curPred.shape);

        meshFile += ".obj";
        Debug.Log(meshFile);

        OBJLoader.OBJMesh objLoaderMesh = OBJLoader.LoadOBJMesh(meshFile);
        Debug.Assert(objLoaderMesh.vertices.Count > 0);
        Debug.Assert(objLoaderMesh.faces.Count > 0);
        m_mesh = DataGenUtils.ToUnityMesh(objLoaderMesh);

        // create init object
        m_initObj = new GameObject("Sim" + idx.ToString() + "_Init");
        MeshFilter mfInit = m_initObj.AddComponent <MeshFilter>();

        mfInit.mesh = m_mesh;
        MeshRenderer mrInit = m_initObj.AddComponent <MeshRenderer>();

        mrInit.sharedMaterial           = initMat;
        m_initObj.transform.localScale  = m_curPred.scale;
        m_initObj.transform.position    = m_curPred.gt_pos[0];
        m_initObj.transform.eulerAngles = m_curPred.gt_rot[0];

        // create GT object
        m_gtObj = new GameObject("Sim" + idx.ToString() + "_GT");
        MeshFilter mf = m_gtObj.AddComponent <MeshFilter>();

        mf.mesh = m_mesh;
        MeshRenderer mr = m_gtObj.AddComponent <MeshRenderer>();

        mr.sharedMaterial             = gtMat;
        m_gtObj.transform.localScale  = m_curPred.scale;
        m_gtObj.transform.position    = m_curPred.gt_pos[0];
        m_gtObj.transform.eulerAngles = m_curPred.gt_rot[0];

        // create sampled object
        m_sampObj = new GameObject("Sim" + idx.ToString() + "_Samp");
        MeshFilter mf2 = m_sampObj.AddComponent <MeshFilter>();

        mf2.mesh = m_mesh;
        MeshRenderer mr2 = m_sampObj.AddComponent <MeshRenderer>();

        mr2.sharedMaterial              = sampMat;
        m_sampObj.transform.localScale  = m_curPred.scale;
        m_sampObj.transform.position    = m_curPred.samp_pos[0];
        m_sampObj.transform.eulerAngles = m_curPred.samp_rot[0];
    }
    // Loads the given OBJ file, creates a RigidBody with the given mess, and places
    // at the origin of the ground.
    protected void PrepareSimObj(string objFile, float mass, BulletSharp.Math.Vector3 inertia)
    {
        Debug.Log("Loading " + objFile + "...");
        // Load wavefront file
        OBJLoader.OBJMesh objloadermesh = OBJLoader.LoadOBJMesh(objFile);
        Debug.Assert(objloadermesh.vertices.Count > 0);
        Debug.Assert(objloadermesh.faces.Count > 0);
        //		Debug.Log("VERTS: " + objloadermesh.vertices.Count.ToString());
        //		Debug.Log("FACES: " + objloadermesh.faces.Count.ToString());

        m_btmesh = DataGenUtils.BulletMeshFromUnity(objloadermesh);
        Debug.Assert(m_btmesh.vertices.Length > 0);
        Debug.Assert(m_btmesh.indices.Length > 0);
        //		Debug.Log("btVERTS: " + (btmesh.vertices.Length / 3).ToString());
        //		Debug.Log("btFACES: " + (btmesh.indices.Length / 3).ToString());

        // Create a GImpactMeshShape for collider
        var triVtxarray = new TriangleIndexVertexArray(m_btmesh.indices, m_btmesh.vertices);

        m_cs = new GImpactMeshShape(triVtxarray);
        m_cs.LocalScaling = new BulletSharp.Math.Vector3(1);
        m_cs.Margin       = bodyMargin;
        m_cs.UpdateBound();
        AddCollisionShape(m_cs);

        // move it up so resting on the ground plane
        float miny = float.MaxValue;
        float cury;

        for (int i = 0; i < objloadermesh.vertices.Count; i++)
        {
            cury = objloadermesh.vertices[i][1];
            if (cury < miny)
            {
                miny = cury;
            }
        }
        miny               = -miny;
        m_rbInitTransVec   = new BulletSharp.Math.Vector3(0, miny + bodyMargin + m_groundMargin, 0);
        m_rbInitTrans      = Matrix.Translation(m_rbInitTransVec);   // * Matrix.RotationY(Random.Range(0.0f, 360.0f));
        m_rb               = CreateRigidBody(mass, inertia, m_rbInitTrans, m_cs, bodyMat, bodyFriction, viz: RENDER_MODE);
        m_rb.AngularFactor = angularFactor;
        m_rb.SetSleepingThresholds(linearSleepThresh, angularSleepThresh);
        if (DEBUG)
        {
            Debug.Log("LOADED MOMENT: " + m_rb.LocalInertia.ToString());
        }
        // if (DEBUG) Debug.Log("WORLD MOMENT: " + m_rb.InvInertiaTensorWorld.ToString());
//		Debug.Log("Min y: " + (-miny).ToString());
        if (DEBUG)
        {
            Debug.Log(m_rb.CenterOfMassPosition.ToString());
        }
    }
Example #5
0
    // Converts an OBJMesh (assumed to be a tri-mesh) to a bullet-friendly structure
    public static Mesh ToUnityMesh(OBJLoader.OBJMesh objMesh)
    {
        Mesh mesh = new Mesh();

        UnityEngine.Vector3[] newVertices = new UnityEngine.Vector3[objMesh.faces.Count * 3]; // must make a separate copy of each for normals to be correct
        //UnityEngine.Vector2[] newUV = new UnityEngine.Vector2[objMesh.faces.Count * 3];
        int[] newTriangles = new int[objMesh.faces.Count * 3];

        for (int i = 0; i < objMesh.vertices.Count; i++)
        {
            newVertices[i] = objMesh.vertices[i];
        }
        for (int i = 0; i < objMesh.faces.Count; i++)
        {
            UnityEngine.Vector3 v1 = objMesh.vertices[objMesh.faces[i].indexes[0]];
            UnityEngine.Vector3 v2 = objMesh.vertices[objMesh.faces[i].indexes[1]];
            UnityEngine.Vector3 v3 = objMesh.vertices[objMesh.faces[i].indexes[2]];

            newVertices[i * 3 + 0] = v1;
            newVertices[i * 3 + 1] = v2;
            newVertices[i * 3 + 2] = v3;

            newTriangles[i * 3 + 0] = i * 3 + 0;
            newTriangles[i * 3 + 1] = i * 3 + 1;
            newTriangles[i * 3 + 2] = i * 3 + 2;

            //UnityEngine.Vector3 normal = UnityEngine.Vector3.Cross(v3 - v1, v2 - v1);
            //UnityEngine.Quaternion rot = UnityEngine.Quaternion.Inverse(UnityEngine.Quaternion.LookRotation(normal));

            //         // Assign the uvs, applying a scale factor to control the texture tiling.
            //float scaleFactor = 2.0f;
            //newUV[i*3 + 0]     = (Vector2)(rot * v1) * scaleFactor;
            //newUV[i*3 + 1] = (Vector2)(rot * v2) * scaleFactor;
            //newUV[i*3 + 2] = (Vector2)(rot * v3) * scaleFactor;
        }

        mesh.vertices = newVertices;
        //mesh.uv = newUV;
        mesh.triangles = newTriangles;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();

        //mesh.uv = UnityEditor.Unwrapping.GeneratePerTriangleUV(mesh);

        return(mesh);
    }
Example #6
0
    // returns false if we don't want to use this sim
    bool LoadSim(int predIdx, int idx, bool createGt)
    {
        // read in results from json
        string predFile = Path.Combine(m_predDataDirs[predIdx], "eval_sim_" + idx.ToString() + ".json");

        Debug.Log("Visualizing " + predFile);
        string jsonTextFile = ReadFile(predFile);

        m_curPreds.Add(PredInfo.CreateFromJSON(jsonTextFile));

        if (showTopplingOnly && !m_curPreds[predIdx].toppled)
        {
            // don't want to use this
            return(false);
        }

        if (showNonTopplingOnly && m_curPreds[predIdx].toppled)
        {
            return(false);
        }

        // find mesh to use
        string meshFile = "";

        for (int i = 0; i < m_baseModelDirs.Count; i++)
        {
            meshFile  = Path.Combine(m_baseModelDirs[i], m_curPreds[predIdx].shape);
            meshFile += ".obj";
            if (File.Exists(meshFile))
            {
                break;
            }
        }
        Debug.Log(meshFile);
        if (meshFile == "")
        {
            Debug.Log("Couldn't find mesh " + m_curPreds[predIdx].shape + "!");
            Application.Quit();
        }

        OBJLoader.OBJMesh objLoaderMesh = OBJLoader.LoadOBJMesh(meshFile);
        Debug.Assert(objLoaderMesh.vertices.Count > 0);
        Debug.Assert(objLoaderMesh.faces.Count > 0);
        m_mesh = DataGenUtils.ToUnityMesh(objLoaderMesh);

        // create GT object
        if (createGt && !hideGroundTruth)
        {
            m_gtObj = new GameObject("Sim" + idx.ToString() + "_GT");
            MeshFilter mf = m_gtObj.AddComponent <MeshFilter>();
            mf.mesh = m_mesh;
            MeshRenderer mr = m_gtObj.AddComponent <MeshRenderer>();
            mr.sharedMaterial             = gtMat;
            m_gtObj.transform.localScale  = m_curPreds[predIdx].scale;
            m_gtObj.transform.position    = m_curPreds[predIdx].gt_pos[0];
            m_gtObj.transform.eulerAngles = m_curPreds[predIdx].gt_euler_rot[0];
        }

        // create sampled object
        m_predObjs.Add(new GameObject("Sim" + idx.ToString() + "_Pred"));
        MeshFilter mf2 = m_predObjs[predIdx].AddComponent <MeshFilter>();

        mf2.mesh = m_mesh;
        MeshRenderer mr2 = m_predObjs[predIdx].AddComponent <MeshRenderer>();

        mr2.sharedMaterial = predMats[predIdx];
        m_predObjs[predIdx].transform.localScale  = m_curPreds[predIdx].scale;
        m_predObjs[predIdx].transform.position    = m_curPreds[predIdx].pred_pos[0];
        m_predObjs[predIdx].transform.eulerAngles = m_curPreds[predIdx].pred_rot[0];

        return(true);
    }