-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageToVoxelGenerator.cs
56 lines (44 loc) · 1.72 KB
/
ImageToVoxelGenerator.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/* ImageToVoxelGenerator
* *********************
* Class designed to create a world based off an black and white texture
*/
public class ImageToVoxelGenerator : MonoBehaviour {
public QuadtreeComponent QuadtreeComponent;
private static string MapNameKey = "MAP_NAME";
private string _mapName;
private string _tutorialMapName;
private float _thresHold = 0.1f;
private Texture2D _image;
void Start()
{
//Load the chosen map from the player preferences and create a level
if(PlayerPrefs.HasKey(MapNameKey))
_mapName = PlayerPrefs.GetString(MapNameKey);
_image = Resources.Load<Texture2D>("Textures/Maps/" + _mapName);
Generate();
}
//Use an image texture to create the world.
//Only inserting data into the quadtree where a pixel value is white.
private void Generate()
{
int cells = (int)Mathf.Pow(2, QuadtreeComponent.Depth);
for(int x = 0; x <= cells;++x)
{
for (int y = 0; y <= cells; ++y)
{
Vector2 position = QuadtreeComponent.transform.position;
position.x += ((x - cells / 2) / (float)cells) * QuadtreeComponent.Size;
position.y += ((y - cells / 2) / (float)cells) * QuadtreeComponent.Size;
Color pixel = _image.GetPixelBilinear(x / (float)cells, y / (float)cells);
if(pixel.r > _thresHold && pixel.g > _thresHold && pixel.b > _thresHold)
{
QuadtreeComponent.Quadtree.InsertExplosion(position, 0.0001f, 1);
}
}
}
}
}