- 最后登录
- 2017-4-1
- 注册时间
- 2011-7-26
- 阅读权限
- 90
- 积分
- 24690
- 纳金币
- 24658
- 精华
- 6
|
一,在unity3d 的Project面板中新建一个文件夹,并为其命名为Editor。并在其文件夹下新建一个Javascript文件,命名为MountainsCreate.
二,把下面的代码拷贝到MountainsCreate.js上。
/*
*Use the tool.
*You can put 3D Max or maya model built into Unity3d in the mountains.
*/
@MenuItem ("Terrain/3DObject to Terrain")static function Object2Terrain ()
{
// See if a valid object is selected
var obj = Selection.activeObject as GameObject;
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 = ***e;
}else if (objCollider.sharedMesh == null) {
objCollider.sharedMesh = (obj.GetComponent(MeshFilter) as MeshFilter).sharedMesh;
addedMesh = ***e;
}
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);
}
}
三,把从3D Max或Maya中制作的地形对象导入到Unity3d资源文件夹中。模型名为Mountains.
四,点击工具栏菜单中的Terrain--->Create Terrain,创建一个地形。
五,把导入的地形从Unity3D Project面板中拖放到Hierarchy面板中。在Hierarchy面板中选中Mountains对象,然后选中工具栏菜单中的
Terrain--->3DObject to Terrain.此时就能看到您自己导入的修改化地形了。
来自unity3d8.com |
|