forked from JTippetts/U3DTerrainEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerrainEdit.h
259 lines (207 loc) · 9.9 KB
/
TerrainEdit.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// C++ Implementation of Terrain Editing Component
#ifndef TERRAINEDIT_H
#define TERRAINEDIT_H
#include <Urho3D/Urho3D.h>
#include <Urho3D/Container/ArrayPtr.h>
#include <Urho3D/Graphics/Terrain.h>
#include <Urho3D/Graphics/Texture2D.h>
#include <Urho3D/Resource/Image.h>
#include <Urho3D/Container/Str.h>
#include <Urho3D/Graphics/CustomGeometry.h>
#include "ThirdParty/accidental-noise-library/anl.h"
using namespace Urho3D;
using namespace anl;
struct BrushSettings
{
float radius, max, power, hardness;
BrushSettings() : radius(0), max(0), power(0), hardness(0) {}
BrushSettings(float r, float mx, float p, float h) :
radius(r), max(mx), power(p), hardness(h) {}
};
struct MaskSettings
{
bool usemask0, usemask1, usemask2;
bool invertmask0, invertmask1, invertmask2;
MaskSettings() : usemask0(false), usemask1(false), usemask2(false), invertmask0(false), invertmask1(false), invertmask2(false) {}
MaskSettings(bool um0, bool im0, bool um1, bool im1, bool um2, bool im2) : usemask0(um0), usemask1(um1), usemask2(um2), invertmask0(im0),
invertmask1(im1), invertmask2(im2) {}
};
class TerrainEdit// : public Object
{
//URHO3D_OBJECT(TerrainEdit, Object);
public:
//TerrainEdit(Context *context);
TerrainEdit();
bool Initialize(Scene *scene, int tw, int th, int bw, int bh, Vector3 spacing, bool use16bit=true);
void SetTerrainSize(int w, int h, Vector3 spacing, bool use16bit=true);
void SetBlendMaskSize(int w, int h);
void SetTerrainSpacing(Vector3 spacing);
Vector2 WorldToNormalized(Vector3 world);
Vector3 NormalizedToWorld(Vector2 normalized);
IntVector2 WorldToTerrain(Vector3 world);
IntVector2 WorldToBlend(Vector3 world);
Vector3 TerrainToWorld(IntVector2 terrain);
Vector3 BlendToWorld(IntVector2 blend);
Vector2 TerrainToNormalized(IntVector2 terrain);
Vector2 BlendToNormalized(IntVector2 blend);
IntVector2 NormalizedToBlend(Vector2 norm);
IntVector2 NormalizedToTerrain(Vector2 norm);
void SetHeightValue(int x, int y, float val);
float GetHeightValue(int x, int y);
float GetHeightValueFromNormalized(Vector2 nrm);
int GetTerrainWidth()
{
if (hmap_) return hmap_->GetWidth();
return 0;
}
int GetTerrainHeight()
{
if (hmap_) return hmap_->GetHeight();
return 0;
}
int GetBlendWidth()
{
if (blend0_) return blend0_->GetWidth();
return 0;
}
int GetBlendHeight()
{
if (blend0_) return blend0_->GetHeight();
return 0;
}
float GetHeightValue(Vector3 worldpos);
void GetHeightMap(CArray2Dd &buffer);
void SetHeightBuffer(CArray2Dd &buffer, MaskSettings &masksettings);
void SetLayerBuffer(CArray2Dd &buffer, int layer, MaskSettings &masksettings);
void SetLayerBufferMax(CArray2Dd &buffer, int layer, MaskSettings &masksettings);
void BlendHeightBuffer(CArray2Dd &buffer, CArray2Dd &blend, MaskSettings &masksettings);
void SetMaskBuffer(CArray2Dd &buffer, int which);
void ApplyHeightBrush(float x, float z, float dt, BrushSettings &brush, MaskSettings &masksettings);
void ApplyBlendBrush(float x, float z, int layer, float dt, BrushSettings &brush, MaskSettings &masksettings);
void ApplyMaskBrush(float x, float z, int which, float dt, BrushSettings &brush, MaskSettings &masksettings);
void ApplySmoothBrush(float x, float z, float dt, BrushSettings &brush, MaskSettings &masksettings);
void SetBrushCursorHeight(CustomGeometry *brush, float groundx, float groundz);
void InvertMask(int which);
void ClearMask(int which);
void ClearAllMasks();
void InvertLayer(int which);
void ClearLayer(int which);
void ClearAllLayers();
void InvertHeight();
void ClearHeight();
Material *GetMaterial()
{
return material_;
}
Terrain *GetTerrain()
{
return terrain_;
}
void SetMaterialSettings(bool triplanar, bool smoothing, bool normalmapping, bool reduce);
void SaveHeightMap(const String &filename);
void SaveBlend0(const String &filename);
void SaveBlend1(const String &filename);
void SaveMask(const String &filename);
void LoadHeightMap(const String &filename);
void LoadBlend0(const String &filename);
void LoadBlend1(const String &filename);
void LoadMask(const String &filename);
void GetSteepness(CArray2Dd &buffer, float threshold, float fade);
void GetCavityMap(CArray2Dd &buffer, float sampleradius, float scale, float bias, float intensity, unsigned int iterations);
Vector3 GetTerrainSpacing();
void SaveTerrainNormalMap(const String &filename);
protected:
Node *terrainNode_;
Terrain *terrain_;
Material *material_;
Image *hmap_, *blend0_, *blend1_, *mask_;
SharedPtr<Texture2D> blendtex0_, blendtex1_, masktex_;
bool use16bit_;
bool triplanar_, smoothing_, normalmapping_;
float DoAmbientOcclusion(Vector2 tcoord, Vector2 uv, Vector3 p, Vector3 cnorm, float scale, float bias, float intensity);
};
Vector2 WorldToNormalized(Image *height, Terrain *terrain, Vector3 world);
Vector3 NormalizedToWorld(Image *height, Terrain *terrain, Vector2 normalized);
void SetHeightValue(Image *height, int x, int y, float val);
float GetHeightValue(Image *height, int x, int y);
void ApplyHeightBrush(Terrain *terrain, Image *height, Image *mask, float x, float z, float radius, float max, float power, float hardness, bool usemask0, bool usemask1, bool usemask2, float dt);
void ApplyBlendBrush(Terrain *terrain, Image *height, Image *blend, Image *mask, float x, float z, float radius, float mx, float power, float hardness, int layer, bool usemask0, bool usemask1, bool usemask2, float dt);
void ApplyBlendBrush8(Terrain *terrain, Image *height, Image *blend0, Image *blend1, Image *mask, float x, float z, float radius, float mx, float power, float hardness, int layer, bool usemask0, bool usemask1, bool usemask2, float dt);
void ApplyMaskBrush(Terrain *terrain, Image *height, Image *mask, float x, float z, float radius, float mx, float power, float hardness, float dt, int which);
void ApplySmoothBrush(Terrain *terrain, Image *height, Image *mask, float x, float z, float radius, float max, float power, float hardness, bool usemask0, bool usemask1, bool usemask2, float dt);
void ApplySpeckleBrush(Terrain *terrain, Image *height, Image *color, Image *mask, float x, float z, float radius, float mx, float power, float hardness, Color c1, Color c2, bool usemask, float dt);
void SetBrushCursorHeight(Terrain *terrain, CustomGeometry *brush, float groundx, float groundz);
void InvertMask(Image *mask);
void RenderANLKernelToHeight(Image *height, Image *mask, CKernel *kernel, double lowRange=0, double highRange=1, bool useMask=false, bool invertMask=false);
struct RasterVertex
{
float x_, y_;
float val_;
RasterVertex() : x_(0), y_(0), val_(0) {}
RasterVertex(float x, float y, float val): x_(x), y_(y), val_(val) {}
RasterVertex(const RasterVertex &rhs) : x_(rhs.x_), y_(rhs.y_), val_(rhs.val_) {}
~RasterVertex() {}
void operator=(const RasterVertex &rhs)
{
x_=rhs.x_;
y_=rhs.y_;
val_=rhs.val_;
}
RasterVertex operator-(const RasterVertex &rhs)
{
RasterVertex v;
v.x_=x_-rhs.x_;
v.y_=y_-rhs.y_;
v.val_=val_-rhs.val_;
return v;
}
RasterVertex operator+(const RasterVertex &rhs)
{
RasterVertex v;
v.x_=x_+rhs.x_;
v.y_=y_+rhs.y_;
v.val_=val_+rhs.val_;
return v;
}
RasterVertex operator*(const RasterVertex &rhs)
{
RasterVertex v;
v.x_=x_*rhs.x_;
v.y_=y_*rhs.y_;
v.val_=val_*rhs.val_;
return v;
}
RasterVertex operator*(float t)
{
RasterVertex v;
v.x_=x_*t;
v.y_=y_*t;
v.val_=val_*t;
return v;
}
};
typedef TArray1D<RasterVertex> RasterVertexList;
//typedef TArray2D<float> CArray2Dd;
void RasterizeTriangle(CArray2Dd *buffer, RasterVertex v1, RasterVertex v2, RasterVertex v3);
void RasterizeQuadStrip(CArray2Dd *buffer, RasterVertexList *strip);
void BlendHeightWithRasterizedBuffer(Image *height, CArray2Dd *buffer, CArray2Dd *blend, Image *mask=0, bool useMask=false, bool invertMask=false);
void BlendColorWithRasterizedBuffer(Image *img, CArray2Dd *buffer, Color endColor, Image *mask=0, bool useMask=false, bool invertMask=false);
void BlendRasterizedBuffer8(Image *img, Image *other, CArray2Dd *buffer, int layer, Image *mask=0, bool useMask=false, bool invertMask=false);
void BlendRasterizedBuffer8Max(Image *img, Image *other, CArray2Dd *buffer, int layer, Image *mask=0, bool useMask=false, bool invertMask=false);
void TessellateLineList(RasterVertexList *in, RasterVertexList *out, int steps);
void ApplyBedFunction(CArray2Dd *buffer, float hardness, bool quintic);
void BuildQuadStrip(RasterVertexList *in, RasterVertexList *out, float width);
void BuildQuadStripVarying(RasterVertexList *in, RasterVertexList *out, float startwidth, float endwidth);
void RenderANLKernelToBuffer(CArray2Dd *buffer, CKernel *kernel, float lowrange=0, float highrange=1);
void RenderANLKernelToImage(Image *buffer, CKernel *kernel, float lowrange=0, float highrange=1);
void SetHeightFromRasterBuffer(Image *height, CArray2Dd *buffer, Image *mask=0, bool useMask=false, bool invertMask=false);
void DistortBuffer(CArray2Dd *buffer, CArray2Dd *xd, CArray2Dd *yd, double power);
void CopyImageInto(Image *dest, Image *src, int x, int y);
bool IsPowerOfTwo(int n);
Image* GetNextImageLevel(Image *i);
void ExtractLayerToBuffer(Image *blend1, Image *blend2, CArray2Dd *buffer, int layer);
float GetLayerBlend(Image *blend1, Image *blend2, int x, int y, int layer);
void SetLayerBlend(Image *blend1, Image *blend2, int x, int y, int layer, float v);
bool LoadImage(Context *c, Image *i, const char *fname);
void SteepnessTerrain(Image *blend0, Image *blend1, Image *hmap, Terrain *terrain, Image *mask, float threshold, float fade, unsigned int layer, bool usemask, bool invertmask);
#endif