-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureGenerator.cs
160 lines (137 loc) · 3.79 KB
/
TextureGenerator.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* LyapunovTerrain
*
* Author : Eiichi Takebuchi(GRGSIBERIA)
* License: MIT License
* Email : nanashi4129@gmail.com
* Twitter: https://twitter.com/#!/GRGSIBERIA
* Blog : http://blogs.yahoo.co.jp/nanashi_hippie
*
* (c) Eiichi Takebuchi(GRGSIBERIA) 2012-
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Threading;
namespace LyapunovTerrain
{
/// <summary>
/// リアプノフ・フラクタルから自動生成したテクスチャ
/// </summary>
public class TextureGenerator
{
class Setup
{
/// <summary>
/// 画素Xの大きさ
/// </summary>
public uint sizeX;
/// <summary>
/// 画素Yの大きさ
/// </summary>
public uint sizeY;
/// <summary>
/// リアプノフ指数の二次元配列
/// </summary>
public float[,] result;
/// <summary>
/// 自動生成したテクスチャ
/// </summary>
public Texture2D texture;
/// <summary>
/// 最小値
/// </summary>
public float min;
/// <summary>
/// 最大値
/// </summary>
public float max;
/// <summary>
/// 1から見た最小値と最大値の差分の比率
/// </summary>
public float difference;
/// <summary>
/// 一番大きな色
/// </summary>
public Color maxColor;
/// <summary>
/// 一番小さな色
/// </summary>
public Color minColor;
/// <summary>
/// 大小の色の差分
/// </summary>
public Color diffColor;
/// <summary>
/// 色の変化
/// </summary>
public AnimationCurve curve;
}
/// <summary>
/// リアプノフ指数のテクスチャを自動生成する
/// </summary>
/// <param name="gen">生成したフラクタル</param>
public static Texture2D CreateTexture(LyapunovGenerator gen, Color maxColor, Color minColor, AnimationCurve curve)
{
Setup setup = new Setup();
setup.sizeX = gen.XLength;
setup.sizeY = gen.YLength;
setup.result = gen.Result;
setup.texture = new Texture2D((int)setup.sizeX, (int)setup.sizeY);
setup.min = gen.MinValue;
setup.max = gen.MaxValue;
setup.difference = 1 / (0 - setup.min + setup.max);
setup.minColor = minColor;
setup.maxColor = maxColor;
// 差分色を正規化してるところ
setup.diffColor = setup.maxColor - setup.minColor;
setup.curve = curve;
// ここでテクスチャの色を埋めて行く
SetTextureColor(ref setup, gen);
return setup.texture;
}
/// <summary>
/// テクスチャの色を設定する
/// </summary>
static void SetTextureColor(ref Setup setup, LyapunovGenerator gen)
{
for (uint x = 0; x < setup.sizeX; x++)
{
for (uint y = 0; y < setup.sizeY; y++)
{
// 0から1の間で正規化されたリアプノフ指数
float normalizedLyapunov = gen.Normalized((int)x, (int)y);
Color color = setup.diffColor * setup.curve.Evaluate(normalizedLyapunov) + setup.minColor;
color.a = 1;
setup.texture.SetPixel((int)x, (int)y, color);
}
}
setup.texture.Apply();
}
/// <summary>
/// 高さマップを生成する
/// </summary>
/// <param name="gen">生成したリアプノフ</param>
/// <param name="curve">色曲線</param>
/// <returns>高さマップ</returns>
public static Texture2D CreateHeightMap(LyapunovGenerator gen, AnimationCurve curve)
{
Texture2D tex = new Texture2D((int)gen.XLength, (int)gen.YLength);
float diff = 1f / (gen.MaxValue - gen.MinValue);
Color[] color = new Color[gen.XLength * gen.YLength];
for (int i = 0; i < (int)gen.XLength; i++)
{
for (int j = 0; j < (int)gen.YLength; j++)
{
float val = curve.Evaluate(gen.Result[i, j] * diff);
color[i * gen.XLength + j] = new Color(val, val, val, 1);
}
}
tex.SetPixels(color);
tex.Apply();
return tex;
}
}
}