按比例缩小网格以适合Unity3D中的容器

问题描述 投票:1回答:1

我目前正在开发一个项目,在这个项目中,人们应该能够在运行时加载他们自己的模型并在其上行走。为此,我想缩小网格以适应容器,这将是水平区域。

enter image description here Example 2 of problem

我使用ObjImporter得到导入部分,但我似乎无法弄清楚如何缩小导入的网格以适应框。

这是我到目前为止所尝试的:

public void ModelPicker() {
    Mesh mesh = new Mesh();

    //Choose an Obj or dxf file 
    string filePath = UnityEditor.EditorUtility.OpenFilePanel ("Select model","C:\\", "obj;*.dxf;" ); 
    if(filePath != "") {
        string[] extention = filePath.Split ('.'); //Get the extention of the chosen file
        string[] filename = filePath.Split ('/'); // Get the file name + extention of the chosen file

        if (extention [extention.Length - 1] == "obj") { //If the file had an "obj" extention, use ObjImporter
            mesh = OI.ImportFile (filePath);
        } else if (extention [extention.Length - 1] == "dxf") { //If the file had an "dxf" extention, use DxfToMesh
        //TODO  mesh = DxfToMesh (filePath);
        } else {
            GameObject.Find ("FilePathText").GetComponent<UnityEngine.UI.Text> ().text = "Format not supported.";   //if the file had an unsupported extention
        }

        if (mesh != null) {
            GameObject em = GameObject.Find ("emptyModel");
            em.GetComponent<MeshFilter> ().mesh = mesh; //Display the loaded model 
            GameObject.Find ("FilePathText").GetComponent<UnityEngine.UI.Text> ().text = filename[filename.Length -1]; //Display filename

            //Should decrease size to fit in the box 
            int counter = 0; // to make sure the loop will end eventually
            em.renderer.bounds.SetMinMax(em.transform.parent.collider.bounds.min, em.transform.parent.collider.bounds.max);
            Debug.Log("max: " + em.renderer.bounds.max + "  |  min: " + em.renderer.bounds.min);
            Debug.Log("parent max: " + em.transform.collider.bounds.max + "  |  parent min: " + em.transform.collider.bounds.min);
            while(em.renderer.collider.bounds.Intersects(em.transform.parent.collider.bounds) && counter < 100)
            {
                Debug.Log (em.transform.localScale );
                em.transform.localScale *= 0.9f;
                counter++;
                if(counter == 100) {
                    Debug.Log("max: " + em.renderer.bounds.max + "  |  min: " + em.renderer.bounds.min);
                }
            }
            GameObject.Find("SimulateButton").GetComponent<Button>().interactable = true;
        }
    }
}

emptyModel是包含导入模型的GameObject。它有一个带有boxcollider且没有网格的父级。

c# unity3d
1个回答
2
投票

好吧,我现在已经弄明白了。

我可以从网格的extends中获取x,y,z值以获得最远的点。然后在while循环中,我检查这些点是否都在父母的bounds中。如果它们不在bounds内,我将比例乘以0.9。然后它会重复,直到它放在盒子里

public void ModelPicker() {
    Mesh mesh = new Mesh();

    //Choose an Obj or dxf file 
    string filePath = UnityEditor.EditorUtility.OpenFilePanel ("Select model","C:\\", "obj;*.dxf;" ); 
    if(filePath != "") {
        string[] extention = filePath.Split ('.'); //Get the extention of the chosen file
        string[] filename = filePath.Split ('/'); // Get the file name + extention of the chosen file

        if (extention [extention.Length - 1] == "obj") { //If the file had an "obj" extention, use ObjImporter
            mesh = OI.ImportFile (filePath);
        } else if (extention [extention.Length - 1] == "dxf") { //If the file had an "dxf" extention, use DxfToMesh
        //  mesh = DxfToMesh (filePath);
        } else {
            GameObject.Find ("FilePathText").GetComponent<UnityEngine.UI.Text> ().text = "Format not supported.";   //if the file had an unsupported extention
        }

        if (mesh != null) {
            GameObject em = GameObject.Find ("emptyModel");
            em.transform.localScale = new Vector3(1,1,1);
            em.GetComponent<MeshFilter> ().mesh = mesh; //Display the loaded model 
            GameObject.Find ("FilePathText").GetComponent<UnityEngine.UI.Text> ().text = filename[filename.Length -1]; //Display filename
            Vector3 parentSize = em.transform.parent.GetComponent<BoxCollider>().bounds.size;

            while(em.transform.GetComponent<MeshFilter>().renderer.bounds.extents.x > parentSize.x || 
                  em.transform.GetComponent<MeshFilter>().renderer.bounds.extents.y > parentSize.y || 
                  em.transform.GetComponent<MeshFilter>().renderer.bounds.extents.z > parentSize.z  
                 ) {
                em.transform.localScale *= 0.9f;
            }
            GameObject.Find("SimulateButton").GetComponent<Button>().interactable = true;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.