-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathTerrainTreeReplacer.cs
101 lines (75 loc) · 3.54 KB
/
TerrainTreeReplacer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class TerrainTreeReplacer : EditorWindow
{
private const string RootObjectName = "TREES_CONVERTED";
[MenuItem("Window/Tools/Terrain Tree Replacer")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(TerrainTreeReplacer));
}
private Terrain terrain;
private bool disableDrawTreesAndFoliage = false;
private int treeDivisions = 0;
private bool DivideTreesIntoGroups { get { return treeDivisions > 0; } }
void OnGUI()
{
GUILayout.Label("Replace Terrain Trees with Objects", EditorStyles.boldLabel);
terrain = EditorGUILayout.ObjectField("Terrain:", terrain, typeof(Terrain), true) as Terrain;
disableDrawTreesAndFoliage = EditorGUILayout.ToggleLeft("Disable Drawing Trees and Foliage", disableDrawTreesAndFoliage);
GUILayout.Label("Tree Division groups: " + treeDivisions);
treeDivisions = (int)GUILayout.HorizontalSlider(treeDivisions, 0, 10);
if (GUILayout.Button("Replace Terrain trees to Objects!")) Replace();
if (GUILayout.Button("Clear generated trees!")) Clear();
}
public void Replace()
{
if (terrain == null)
{
Debug.LogError("Please Assign Terrain");
return;
}
Clear();
GameObject treeParent = new GameObject(RootObjectName);
List<List<Transform>> treegroups = new List<List<Transform>>();
if (DivideTreesIntoGroups)
{
for (int i = 0; i < treeDivisions; i++)
{
treegroups.Add(new List<Transform>());
for (int j = 0; j < treeDivisions; j++)
{
GameObject treeGroup = new GameObject("TreeGroup_" + i + "_" + j);
treeGroup.transform.parent = treeParent.transform;
treegroups[i].Add(treeGroup.transform);
}
}
}
TerrainData terrainData = terrain.terrainData;
float xDiv = terrainData.size.x / (float)treeDivisions;
float zDiv = terrainData.size.z / (float)treeDivisions;
foreach (TreeInstance tree in terrainData.treeInstances)
{
GameObject treePrefab = terrainData.treePrototypes[tree.prototypeIndex].prefab;
Vector3 position = Vector3.Scale(tree.position, terrainData.size);
int xGroup = (int)(position.x / xDiv);
int zGroup = (int)(position.z / zDiv);
position += terrain.transform.position;
Vector2 lookRotationVector = new Vector2(Mathf.Cos(tree.rotation - Mathf.PI), Mathf.Sin(tree.rotation - Mathf.PI));
Quaternion rotation = Quaternion.LookRotation(new Vector3(lookRotationVector.x, 0, lookRotationVector.y), Vector3.up);
Vector3 scale = new Vector3(tree.widthScale, tree.heightScale, tree.widthScale);
GameObject spawnedTree = Instantiate(treePrefab, position, rotation) as GameObject;
spawnedTree.name = treePrefab.name;
spawnedTree.transform.localScale = scale;
if (DivideTreesIntoGroups) spawnedTree.transform.SetParent(treegroups[xGroup][zGroup]);
else spawnedTree.transform.SetParent(treeParent.transform);
}
if (disableDrawTreesAndFoliage) terrain.drawTreesAndFoliage = false;
}
public void Clear()
{
DestroyImmediate(GameObject.Find(RootObjectName));
}
}