-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageToHeightmapEditor.cs
More file actions
56 lines (48 loc) · 1.78 KB
/
Copy pathImageToHeightmapEditor.cs
File metadata and controls
56 lines (48 loc) · 1.78 KB
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
using UnityEngine;
using UnityEditor;
public class ImageToHeightmapEditor : EditorWindow
{
private Texture2D heightmapImage;
private Terrain terrain;
private float heightMultiplier = 1.0f;
[UnityEditor.MenuItem("TerrainTools/Image To Heightmap")]
public static void ShowWindow()
{
GetWindow<ImageToHeightmapEditor>("Image To Heightmap");
}
private void OnGUI()
{
GUILayout.Label("Heightmap Settings", EditorStyles.boldLabel);
heightmapImage = (Texture2D)EditorGUILayout.ObjectField("Heightmap Image", heightmapImage, typeof(Texture2D), false);
terrain = (Terrain)EditorGUILayout.ObjectField("Terrain", terrain, typeof(Terrain), true);
heightMultiplier = EditorGUILayout.FloatField("Height Multiplier", heightMultiplier);
if (GUILayout.Button("Apply Heightmap"))
{
if (heightmapImage != null && terrain != null)
{
ApplyHeightmap();
}
else
{
EditorUtility.DisplayDialog("Error", "Please assign a heightmap image and a terrain.", "OK");
}
}
}
private void ApplyHeightmap()
{
int width = heightmapImage.width;
int height = heightmapImage.height;
float[,] heights = new float[width, height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixel = heightmapImage.GetPixel(x, y);
float heightValue = pixel.r * heightMultiplier;
heights[y, x] = heightValue;
}
}
terrain.terrainData.heightmapResolution = width + 1;
terrain.terrainData.SetHeights(0, 0, heights);
}
}