GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Silverback/LevelEditor/Engine/Terrain.cs at master · SilverbackSoPra/Silverback

Expand file tree

/

Terrain.cs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
324 lines (241 loc) · 11.6 KB
/

Terrain.cs

Copy path

File metadata and controls

324 lines (241 loc) · 11.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using LevelEditor.Engine.Mesh;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace LevelEditor.Engine
{
[Serializable()]
public sealed class Terrain : IActor, ISerializable
{
public Actor Actor { get; }
public int Size { get; }
[XmlIgnore]
public Grass mGrass;
private const float TerrainScale = 1.0f;
private readonly float TerrainMaxHeight = 40.0f;
private const float TextureRepitions = 1.0f;
private readonly int mHeight;
private readonly int mWidth;
public readonly string mHeightMapPath;
public readonly string mTexturePath;
private readonly ContentManager mContent;
private readonly GraphicsDevice mGraphics;
public void Update(GameTime gameTime)
{
throw new NotImplementedException();
}
public Terrain(ContentManager content, GraphicsDevice device, string heightMapPath, string texturePath)
{
mContent = content;
mGraphics = device;
mHeightMapPath = heightMapPath;
mTexturePath = texturePath;
var meshData = new MeshData();
var heightMapStream = new FileStream(heightMapPath, FileMode.Open, FileAccess.Read);
var heightmap = Texture2D.FromStream(device, heightMapStream);
heightMapStream.Close();
mWidth = heightmap.Width;
mHeight = heightmap.Height;
meshData.mVertices = new VertexPositionTexture[mWidth * mHeight];
meshData.mIndices = new int[(mWidth * 2 + 2) * (mHeight - 1)];
var subData = new MeshData.SubData();
var material = new Material();
var textureStream = new FileStream(texturePath, FileMode.Open, FileAccess.Read);
material.mDiffuseMap = Texture2D.FromStream(device, textureStream);
material.DiffuseMapStream = textureStream;
material.mGraphicsDevice = device;
textureStream.Close();
material.mDiffuseColor = new Vector3(1.0f);
subData.mIndicesOffset = 0;
subData.mMaterialIndex = 0;
subData.mNumPrimitives = (heightmap.Width * 2 + 2) * (heightmap.Height - 1);
meshData.mMaterials.Add(material);
meshData.mSubDatas.Add(subData);
meshData.mPrimitiveType = PrimitiveType.TriangleStrip;
meshData.mTotalNumPrimitives = (heightmap.Width * 2 + 2) * (heightmap.Height - 1);
meshData.mBoundingSphere.Radius = (float)Math.Sqrt(Math.Pow(heightmap.Width / 2.0f, 2) + Math.Pow(heightmap.Height / 2.0f, 2));
meshData.mIsTerrain = true;
Actor = new Actor(new Mesh.Mesh(mGraphics, meshData),null) {mCastShadow = false};
Size = mWidth;
LoadVertices(heightmap);
LoadIndices();
Actor.mMesh.UpdateData();
mGrass = new Grass(this);
mGrass.Generate();
}
public void Reload()
{
var meshData = Actor.mMesh.mMeshData;
var heightMapStream = new FileStream(mHeightMapPath, FileMode.Open, FileAccess.Read);
var heightmap = Texture2D.FromStream(mGraphics, heightMapStream);
heightMapStream.Close();
if (heightmap.Height == mHeight && heightmap.Width == mWidth)
{
var textureStream = new FileStream(mTexturePath, FileMode.Open, FileAccess.Read);
meshData.mMaterials[0].mDiffuseMap = Texture2D.FromStream(mGraphics, textureStream);
textureStream.Close();
LoadVertices(heightmap);
Actor.mMesh.UpdateData();
mGrass.Generate();
}
}
public float GetHeight(Vector3 location)
{
float height;
var x = mWidth / 2f + location.X;
var z = mHeight / 2f + location.Z;
if (x < 0 || z < 0 || x >= (mWidth - 1) * TerrainScale || z >= (mHeight - 1) * TerrainScale)
{
return 0.0f;
}
var position = new Vector2((int)Math.Floor(x / TerrainScale), (int)Math.Floor(z / TerrainScale));
if (position.X < 0 || position.Y < 0 ||
(int)position.X >= mWidth * TerrainScale || (int)position.Y >= mHeight * TerrainScale)
{
return 0.0f;
}
var coord = new Vector2(x % TerrainScale / TerrainScale, z % TerrainScale / TerrainScale);
if (coord.X > coord.Y)
{
height = BarryCentric(new Vector3(0.0f,
Actor.mMesh.mMeshData.mVertices[(int)(position.X + mWidth * position.Y)].Position.Y,
0.0f),
new Vector3(1.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + 1 + mWidth * position.Y)].Position.Y, 0.0f),
new Vector3(1.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + 1 + mWidth * (position.Y + 1))].Position.Y, 1.0f),
coord);
}
else
{
height = BarryCentric(new Vector3(0.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + mWidth * position.Y)].Position.Y, 0.0f),
new Vector3(1.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + 1 + mWidth * (position.Y + 1))].Position.Y, 1.0f),
new Vector3(0.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + mWidth * (position.Y + 1))].Position.Y, 1.0f),
coord);
}
return height;
}
public float GetSlope(Vector3 location)
{
var x = mWidth / 2f + location.X;
var z = mHeight / 2f + location.Z;
if (x < 0 || z < 0 || x >= (mWidth - 1) * TerrainScale || z >= (mHeight - 1) * TerrainScale)
{
return 0.0f;
}
var position = new Vector2((int)Math.Floor(x / TerrainScale), (int)Math.Floor(z / TerrainScale));
if (position.X < 0 || position.Y < 0 ||
(int)position.X >= mWidth * TerrainScale || (int)position.Y >= mHeight * TerrainScale)
{
return 0.0f;
}
var northWest = new Vector3(0.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + mWidth * position.Y)].Position.Y, 0.0f);
var northEast = new Vector3(1.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + 1 + mWidth * position.Y)].Position.Y, 0.0f);
var southWest = new Vector3(0.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + mWidth * (position.Y + 1))].Position.Y, 1.0f);
var southEast = new Vector3(1.0f, Actor.mMesh.mMeshData.mVertices[(int)(position.X + 1 + mWidth * (position.Y + 1))].Position.Y, 1.0f);
var min = northWest;
min = min.Y > northEast.Y ? northEast : min;
min = min.Y > southWest.Y ? southWest : min;
min = min.Y > southEast.Y ? southEast : min;
var max = northWest;
max = max.Y < northEast.Y ? northEast : max;
max = max.Y < southWest.Y ? southWest : max;
max = max.Y < southEast.Y ? southEast : max;
var distance = Vector2.Distance(new Vector2(min.X, min.Z), new Vector2(max.X, max.Z));
var difference = max.Y - min.Y;
return (float)Math.Sinh(difference / distance);
}
private float GetHeightFromData(VertexPositionTexture[] vertices, int x, int z)
{
if (x < mWidth && z < mHeight && z >= 0 && x >= 0)
{
return vertices[z * mWidth + x].Position.Y;
}
else
{
return vertices[0].Position.Y;
}
}
private static float BarryCentric(Vector3 p1, Vector3 p2, Vector3 p3, Vector2 pos)
{
var det = (p2.Z - p3.Z) * (p1.X - p3.X) + (p3.X - p2.X) * (p1.Z - p3.Z);
var l1 = ((p2.Z - p3.Z) * (pos.X - p3.X) + (p3.X - p2.X) * (pos.Y - p3.Z)) / det;
var l2 = ((p3.Z - p1.Z) * (pos.X - p3.X) + (p1.X - p3.X) * (pos.Y - p3.Z)) / det;
var l3 = 1.0f - l1 - l2;
return l1 * p1.Y + l2 * p2.Y + l3 * p3.Y;
}
private void LoadVertices(Texture2D heightMap)
{
var newHeightVal = new Color[mWidth * mHeight];
heightMap.GetData(newHeightVal);
var meshData = Actor.mMesh.mMeshData;
for (int z = 0; z < mHeight; z++)
{
for (int x = 0; x < mWidth; x++)
{
//Position
meshData.mVertices[x + z * mWidth].Position.X = (x - mWidth / 2.0f) * TerrainScale;
meshData.mVertices[x + z * mWidth].Position.Y = newHeightVal[x + z * mWidth].G / 255.0f * TerrainMaxHeight;
meshData.mVertices[x + z * mWidth].Position.Z = (z - mHeight / 2.0f) * TerrainScale;
//Texture
meshData.mVertices[x + z * mWidth].TextureCoordinate.X = (float)x / mWidth * TextureRepitions;
meshData.mVertices[x + z * mWidth].TextureCoordinate.Y = (float)z / mHeight * TextureRepitions;
}
}
}
private void LoadIndices()
{
var meshData = Actor.mMesh.mMeshData;
//Calculates the triangle strip
var i = 0;
for (int z = 0; z < mHeight - 1; z++)
{
meshData.mIndices[i++] = z * mWidth;
for (int x = 0; x < mWidth; x++)
{
meshData.mIndices[i++] = z * mWidth + x;
meshData.mIndices[i++] = (z + 1) * mWidth + x;
}
meshData.mIndices[i++] = (z + 1) * mWidth + (mWidth - 1);
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Actor", Actor);
info.AddValue("Size", Size);
info.AddValue("mGrass", mGrass);
info.AddValue("TerrainMaxHeight", TerrainMaxHeight);
info.AddValue("mHeight", mHeight);
info.AddValue("mWidth", mWidth);
info.AddValue("mHeightMapPath", mHeightMapPath);
info.AddValue("mTexturePath", mTexturePath);
info.AddValue("mContent", mContent);
info.AddValue("mGraphics", mGraphics);
}
public Terrain(SerializationInfo info, StreamingContext context)
{
Actor = (Actor)info.GetValue("Actor", typeof(Actor));
Size = (int)info.GetValue("Size", typeof(int));
mGrass = (Grass)info.GetValue("mGrass", typeof(Grass));
TerrainMaxHeight = (float)info.GetValue("TerrainMaxHeight", typeof(float));
mHeight = (int)info.GetValue("mHeight", typeof(int));
mWidth = (int)info.GetValue("mWidth", typeof(int));
mHeightMapPath = (string)info.GetValue("mHeightMapPath", typeof(string));
mTexturePath = (string)info.GetValue("mTexturePath", typeof(string));
mContent = (ContentManager)info.GetValue("mContent", typeof(ContentManager));
mGraphics = (GraphicsDevice)info.GetValue("mGraphics", typeof(GraphicsDevice));
}
public Terrain()
{
mGrass = new Grass(this);
}
[OnDeserialized()]
internal void OnSerializedMethod(StreamingContext context)
{
// Setting this as parent property for Child object
Actor.IActor = this;
}
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.