Ejemplo n.º 1
0
    public void Awake()
    {
        Stopwatch sw = Stopwatch.StartNew();

        Vector3[] points = ori.mesh.vertices;
        if (useEight)
        {
            EightBlockTree eightTree = new EightBlockTree();
            points = eightTree.Build(points, 5);
        }

        QuickHull3D hull = new QuickHull3D();

        hull.Build(points);

        Vector3[] vertices = hull.GetVertices();

        int[] faceIndices = hull.GetFaces();

        Mesh mesh = new Mesh {
            vertices = vertices, triangles = faceIndices
        };

        col.mesh = mesh;
        sw.Stop();
        Debug.Log(sw.Elapsed);
    }
Ejemplo n.º 2
0
    public void Awake()
    {
        Vector3[]      v3s       = ori.mesh.vertices;
        EightBlockTree eightTree = new EightBlockTree();
        var            points    = eightTree.Build(v3s);


        foreach (var point in points)
        {
            Instantiate(prefab, point, Quaternion.identity);
        }
    }
    private Mesh CalcMesh(string _path, bool _useEightBlocks, int _blockLv)
    {
        var oriMesh = AssetDatabase.LoadAssetAtPath <GameObject>(_path);

        List <Vector3> allPoints = new List <Vector3>(1024);

        foreach (var mesh in oriMesh.GetComponentsInChildren <SkinnedMeshRenderer>())
        {
            allPoints.AddRange(mesh.sharedMesh.vertices);
        }

        foreach (var mesh in oriMesh.GetComponentsInChildren <MeshFilter>())
        {
            allPoints.AddRange(mesh.sharedMesh.vertices);
        }

        Vector3[] points = allPoints.ToArray();


        AppendLogLine(_path);
        AppendLogLine("  Ori Vertexs:", points.Length.ToString());

        if (_useEightBlocks)
        {
            EightBlockTree eightTree = new EightBlockTree();
            points = eightTree.Build(points, _blockLv);
            AppendLogLine("  EightBlockTree Vertexs:", points.Length.ToString());
        }

        QuickHull3D hull = new QuickHull3D();

        hull.Build(points);

        Vector3[] vertices = hull.GetVertices();

        int[] faceIndices = hull.GetFaces();

        Mesh newMesh = new Mesh {
            vertices = vertices, triangles = faceIndices
        };

        AppendLogLine("  End vertices:", vertices.Length.ToString());
        AppendLogLine("  End faceIndices:", faceIndices.Length.ToString());

        return(newMesh);
    }
Ejemplo n.º 4
0
    void Start()
    {
        MeshFilter[]      meshFilters = GetComponentsInChildren <MeshFilter>(); //包括了自己所以要跳过
        CombineInstance[] combine     = new CombineInstance[meshFilters.Length - 1];
        Material[]        mats        = new Material[meshFilters.Length - 1];
        Matrix4x4         matrix      = transform.worldToLocalMatrix;
        int index = 0;

        for (int i = 1; i < meshFilters.Length; i++)
        {
            MeshFilter   mf = meshFilters[i];
            MeshRenderer mr = meshFilters[i].GetComponent <MeshRenderer>();
            if (mr == null)
            {
                continue;
            }

            combine[index].mesh      = mf.sharedMesh;
            combine[index].transform = matrix * mf.transform.localToWorldMatrix;
            mr.enabled  = false;
            mats[index] = mr.sharedMaterial;
            index++;
        }

        MeshFilter thisMeshFilter = GetComponent <MeshFilter>();
        Mesh       mesh           = new Mesh {
            name = "Combined"
        };

        thisMeshFilter.mesh = mesh;
        mesh.CombineMeshes(combine, false);
        MeshRenderer thisMeshRenderer = GetComponent <MeshRenderer>();

        thisMeshRenderer.sharedMaterials = mats;
        thisMeshRenderer.enabled         = true;

        MeshCollider thisMeshCollider = GetComponent <MeshCollider>();

        if (thisMeshCollider != null)
        {
            Stopwatch sw     = Stopwatch.StartNew();
            Vector3[] points = mesh.vertices;


            EightBlockTree eightTree = new EightBlockTree();
            points = eightTree.Build(points, 8);

            QuickHull3D hull = new QuickHull3D();
            hull.Build(points);


            Vector3[] vertices = hull.GetVertices();

            int[] faceIndices = hull.GetFaces();

            Mesh colMesh = new Mesh {
                vertices = vertices, triangles = faceIndices
            };
            sw.Stop();
            Debug.Log(sw.Elapsed);

            thisMeshCollider.sharedMesh = colMesh;
        }
        else
        {
            thisMeshCollider            = gameObject.AddComponent <MeshCollider>();
            thisMeshCollider.sharedMesh = mesh;
            thisMeshCollider.convex     = true;
        }
    }