{"id":517,"date":"2010-03-27T12:39:33","date_gmt":"2010-03-27T12:39:33","guid":{"rendered":""},"modified":"2013-02-27T12:22:23","modified_gmt":"2013-02-27T04:22:23","slug":"517","status":"publish","type":"post","link":"https:\/\/1vr.cn\/?p=517","title":{"rendered":"\u5c063D\u5efa\u6a21\u8f6f\u4ef6\u5efa\u7acb\u7684\u5730\u5f62\u5bfc\u5165Unity3D\u4e2d\u4f5c\u4e3a\u5730\u5f62."},"content":{"rendered":"<p>\u5c06\u4e0b\u9762\u7684\u811a\u672c\u653e\u5728\u9879\u76ee\u6587\u4ef6\u7684\u8d44\u6e90\u76ee\u5f55\u7684Editor\u6587\u4ef6\u5939\u4e0b.<br \/>\u5f53<strong>Terrain<\/strong>\u83dc\u5355\u4e0b\u51fa\u73b0<strong>Object to Terrain<\/strong>\u65f6,\u5728\u573a\u666f\u89c6\u56fe\u6216\u8005\u5c42\u6b21\u9762\u677f\u9009\u62e9\u4e00\u4e2a\u5bf9\u8c61,\u8fd9\u4e2a\u5bf9\u8c61\u5c06\u8f6c\u53d8\u5916\u9ad8\u5ea6\u56fe\u7528\u4e8e\u5730\u5f62.\u8fd9\u4e2a\u5bf9\u8c61\u5fc5\u987b\u5305\u542b\u7f51\u683c.\u5982\u56fe:<br \/><img decoding=\"async\" src=\"wp-content\/uploads\/attachments\/month_1003\/20100327144910d.png\" \/><br \/>\u8fd9\u4e2a\u51fd\u6570\u7528\u7684\u662f\u548c\u7f51\u683c\u8f74\u76f8\u540c\u7684\u5305\u56f4\u76d2,\u5982\u679c\u5bf9\u8c61\u5728x\u8f74\u548cz\u8f74\u6709\u65cb\u8f6c.\u6216\u8005y\u8f74\u670990\u5ea6\u4ee5\u4e0a\u65cb\u8f6c\u65f6.\u53ef\u80fd\u4f1a\u6709\u5947\u602a\u7684\u7ed3\u679c.<br \/>\u4f60\u53ef\u80fd\u8981\u8c03\u6574\u5730\u5f62\u7684\u9ad8\u5ea6\u5339\u914d\u4f60\u7684\u7f51\u683c\u5bf9\u8c61(\u53ef\u4ee5\u4f7f\u7528Terrain\u83dc\u5355\u4e0b\u7684Set Resolution\u2026).<\/p>\n<p>\u4e0b\u9762\u4e3aObject2Terrain.js\u811a\u672c\u5185\u5bb9<\/p>\n<p><code>@MenuItem (\"Terrain\/Object to Terrain\")<\/p>\n<p>static function Object2Terrain () {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ See if a valid object is selected<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var obj = Selection.activeObject as GameObject;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (obj == null) { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EditorUtility.DisplayDialog(\"No object selected\", \"Please select an object.\", \"Cancel\");<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (obj.GetComponent(MeshFilter) == null) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EditorUtility.DisplayDialog(\"No mesh selected\", \"Please select an object with a mesh.\", \"Cancel\");<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;else if ((obj.GetComponent(MeshFilter) as MeshFilter).sharedMesh == null) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EditorUtility.DisplayDialog(\"No mesh selected\", \"Please select an object with a valid mesh.\", \"Cancel\");<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (Terrain.activeTerrain == null) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EditorUtility.DisplayDialog(\"No terrain found\", \"Please make sure a terrain exists.\", \"Cancel\");<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;var terrain = Terrain.activeTerrain.terrainData;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ If there's no mesh collider, add one (and then remove it later when done)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var addedCollider = false;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var addedMesh = false;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var objCollider = obj.collider as MeshCollider;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (objCollider == null) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objCollider = obj.AddComponent(MeshCollider);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedCollider = true;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;else if (objCollider.sharedMesh == null) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objCollider.sharedMesh = (obj.GetComponent(MeshFilter) as MeshFilter).sharedMesh;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedMesh = true;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Undo.RegisterUndo (terrain, \"Object to Terrain\");<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;var resolutionX = terrain.heightmapWidth;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var resolutionZ = terrain.heightmapHeight;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var heights = terrain.GetHeights(0, 0, resolutionX, resolutionZ);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Use bounds a bit smaller than the actual object; otherwise raycasting tends to miss at the edges<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var objectBounds = objCollider.bounds;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var leftEdge = objectBounds.center.x - objectBounds.extents.x + .01;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var bottomEdge = objectBounds.center.z - objectBounds.extents.z + .01;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var stepX = (objectBounds.size.x - .019) \/ resolutionX;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var stepZ = (objectBounds.size.z - .019) \/ resolutionZ;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Set up raycast vars<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var y = objectBounds.center.y + objectBounds.extents.y + .01;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var hit : RaycastHit;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var ray = new Ray(Vector3.zero, -Vector3.up);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var rayDistance = objectBounds.size.y + .02;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var heightFactor = 1.0 \/ rayDistance;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Do raycasting samples over the object to see what terrain heights should be<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var z = bottomEdge;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;for (zCount = 0; zCount &lt; resolutionZ; zCount++) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var x = leftEdge;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (xCount = 0; xCount &lt; resolutionX; xCount++) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ray.origin = Vector3(x, y, z);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (objCollider.Raycast(ray, hit, rayDistance)) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;heights[zCount, xCount] = 1.0 - (y - hit.point.y)*heightFactor;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;heights[zCount, xCount] = 0.0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x += stepX;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z += stepZ;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;terrain.SetHeights(0, 0, heights);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (addedMesh) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objCollider.sharedMesh = null;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (addedCollider) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DestroyImmediate(objCollider);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u4e0b\u9762\u7684\u811a\u672c\u653e\u5728\u9879\u76ee\u6587\u4ef6\u7684\u8d44\u6e90\u76ee\u5f55\u7684Editor\u6587\u4ef6\u5939\u4e0b.\u5f53Terrain\u83dc\u5355\u4e0b\u51fa\u73b0Object to Ter &hellip; <a href=\"https:\/\/1vr.cn\/?p=517\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u5c063D\u5efa\u6a21\u8f6f\u4ef6\u5efa\u7acb\u7684\u5730\u5f62\u5bfc\u5165Unity3D\u4e2d\u4f5c\u4e3a\u5730\u5f62.<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-517","post","type-post","status-publish","format-standard","hentry","without-featured-image"],"_links":{"self":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=517"}],"version-history":[{"count":2,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":822,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/517\/revisions\/822"}],"wp:attachment":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}