纳金网

标题: unity3d中将模型转变为地型的代码 [打印本页]

作者: 晃晃    时间: 2011-8-12 10:48
标题: unity3d中将模型转变为地型的代码
将模型转为地型的代码:

Object to Terrain.js



@MenuItem ("Terrain/Object to Terrain")

static function Object2Terrain () {



    // See if a valid object is selected



    var obj = Selection.activeObject as GameObject;



    if (obj == null) {



        EditorUtility.DisplayDialog("No object selected", "lease select an object.", "Cancel");



        return;



    }



    if (obj.GetComponent(MeshFilter) == null) {



        EditorUtility.DisplayDialog("No mesh selected", "lease select an object with a mesh.", "Cancel");



        return;



    }



    else if ((obj.GetComponent(MeshFilter) as MeshFilter).sharedMesh == null) {



        EditorUtility.DisplayDialog("No mesh selected", "lease select an object with a valid mesh.", "Cancel");



        return;  



    }



    if (Terrain.activeTerrain == null) {



        EditorUtility.DisplayDialog("No terrain found", "lease make sure a terrain exists.", "Cancel");



        return;



    }   



    var terrain = Terrain.activeTerrain.terrainData;



   



    // If there's no mesh collider, add one (and then remove it later when done)



    var addedCollider = false;



    var addedMesh = false;



    var objCollider = obj.collider as MeshCollider;



    if (objCollider == null) {



        objCollider = obj.AddComponent(MeshCollider);



        addedCollider = true;



    }



    else if (objCollider.sharedMesh == null) {



        objCollider.sharedMesh = (obj.GetComponent(MeshFilter) as MeshFilter).sharedMesh;



        addedMesh = true;



    }



   



    Undo.RegisterUndo (terrain, "Object to Terrain");
    var resolutionX = terrain.heightmapWidth;



    var resolutionZ = terrain.heightmapHeight;



    var heights = terrain.GetHeights(0, 0, resolutionX, resolutionZ);



   



    // Use bounds a bit smaller than the actual object; otherwise raycasting tends to miss at the edges



    var objectBounds = objCollider.bounds;



    var leftEdge = objectBounds.center.x - objectBounds.extents.x + .01;



    var bottomEdge = objectBounds.center.z - objectBounds.extents.z + .01;



    var stepX = (objectBounds.size.x - .019) / resolutionX;



    var stepZ = (objectBounds.size.z - .019) / resolutionZ;



   



    // Set up raycast vars



    var y = objectBounds.center.y + objectBounds.extents.y + .01;



    var hit : RaycastHit;



    var ray = new Ray(Vector3.zero, -Vector3.up);



    var rayDistance = objectBounds.size.y + .02;



    var heightFactor = 1.0 / rayDistance;



        



    // Do raycasting samples over the object to see what terrain heights should be



    var z = bottomEdge;



    for (zCount = 0; zCount < resolutionZ; zCount++) {



        var x = leftEdge;



        for (xCount = 0; xCount < resolutionX; xCount++) {



            ray.origin = Vector3(x, y, z);



            if (objCollider.Raycast(ray, hit, rayDistance)) {



                heights[zCount, xCount] = 1.0 - (y - hit.point.y)*heightFactor;



            }



            else {



                heights[zCount, xCount] = 0.0;



            }



            x += stepX;



        }



        z += stepZ;



    }



   



    terrain.SetHeights(0, 0, heights);



   



    if (addedMesh) {



        objCollider.sharedMesh = null;



    }



    if (addedCollider) {



        DestroyImmediate(objCollider);



    }



}
作者: Asen    时间: 2011-9-9 09:52

作者: tc    时间: 2012-1-6 19:22
这么后现代

作者: 晃晃    时间: 2012-3-31 23:23
灌水。。。

作者: 晃晃    时间: 2012-5-25 23:23
不错哦,顶一下......

作者: tc    时间: 2012-6-9 23:19
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!

作者: 菜刀吻电线    时间: 2012-6-21 23:18
好可爱的字,学习了

作者: 菜刀吻电线    时间: 2012-8-19 23:29
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5