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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
|
#include "UnityPrefix.h"
#include "Heightmap.h"
#if ENABLE_TERRAIN
#include "Runtime/Terrain/TerrainRenderer.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Utilities/Utility.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxHeightField.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxHeightFieldDesc.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxHeightFieldShape.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxHeightFieldShapeDesc.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxHeightFieldSample.h"
#include "Runtime/Dynamics/PhysicsManager.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxScene.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxActor.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxActorDesc.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxCapsuleShape.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxCapsuleShapeDesc.h"
#include "Runtime/Terrain/DetailDatabase.h"
#include "Runtime/Dynamics/NxWrapperUtility.h"
#include "math.h"
#include "Runtime/Graphics/Transform.h"
#include "Runtime/Filters/Mesh/LodMesh.h"
#include "Runtime/Terrain/TerrainData.h"
#include "Runtime/BaseClasses/SupportedMessageOptimization.h"
#include "Runtime/Utilities/BitUtility.h"
#include "Runtime/BaseClasses/IsPlaying.h"
#include "Runtime/Terrain/TerrainIndexGenerator.h"
#include "Runtime/Misc/BuildSettings.h"
#include "Runtime/Dynamics/TerrainCollider.h"
#include "Runtime/Interfaces/IPhysics.h"
enum { kMaxHeight = 32766 };
using namespace std;
inline UInt32 GetLowMaterialIndex (UInt32 index)
{
return index & 0x7F;
}
inline UInt32 GetHighMaterialIndex (UInt32 index)
{
return index >> 7;
}
static void UpdatePatchMeshInternal (
const Heightmap& heightmap,
const StrideIterator<Vector3f>& vertices,
const StrideIterator<Vector3f>& normals,
const StrideIterator<Vector2f>& uvs,
int xPatch, int yPatch, int mipLevel, int edgeMask, TerrainRenderer *renderer)
{
Vector3f hmScale = heightmap.GetScale();
float skipScale = 1 << mipLevel;
float scale = hmScale.y / (float)(kMaxHeight);
Vector2f uvscale;
uvscale.x = 1.0F / (heightmap.GetWidth() - 1) * skipScale;
uvscale.y = 1.0F / (heightmap.GetHeight() - 1) * skipScale;
Vector2f uvoffset;
uvoffset.x = xPatch * (1 << mipLevel) * (kPatchSize -1);
uvoffset.x /= (heightmap.GetWidth() - 1);
uvoffset.y = yPatch * (1 << mipLevel) * (kPatchSize -1);
uvoffset.y /= (heightmap.GetHeight() - 1);
int skip = 1 << mipLevel;
int xBase = xPatch * (kPatchSize -1);
int yBase = yPatch * (kPatchSize -1);
StrideIterator<Vector3f> itVertices = vertices;
StrideIterator<Vector3f> itNormals = normals;
StrideIterator<Vector2f> itUVs = uvs;
for (int x=0;x<kPatchSize;x++)
{
for (int y=0;y<kPatchSize;y++)
{
int sampleIndex = (y + yBase) + (x + xBase) * heightmap.GetHeight();
sampleIndex *= skip;
float height = heightmap.GetRawHeight(sampleIndex);
height *= scale;
int index = y + x * kPatchSize;
// Vertex
itVertices[index].x = (x + xBase) * hmScale.x * skipScale;
itVertices[index].y = height;
itVertices[index].z = (y + yBase) * hmScale.z * skipScale;
// UV
itUVs[index].x = (float)x * uvscale.x + uvoffset.x;
itUVs[index].y = (float)y * uvscale.y + uvoffset.y;
// Normal
Vector3f normal = heightmap.CalculateNormalSobelRespectingNeighbors ((x + xBase) * skip, (y + yBase) * skip, renderer);
itNormals[index] = normal;
}
}
}
Heightmap::Heightmap (TerrainData *owner)
: m_Scale(1.0f,1.0f,1.0f)
{
m_TerrainData = owner;
#if ENABLE_PHYSICS
m_NxHeightField = NULL;
#endif
}
Heightmap::~Heightmap ()
{
#if ENABLE_PHYSICS
CleanupNx ();
#endif
}
/// Calculates the index of the patch given it's level and x, y index
int Heightmap::GetPatchIndex (int x, int y, int level) const
{
int index = 0;
for (int i=0;i<level;i++)
{
int size = 1 << (m_Levels - i);
index += size * size;
}
int width = 1 << (m_Levels - level);
index += width * y;
index += x;
return index;
}
float Heightmap::InterpolatePatchHeight (float* data, float fx, float fy) const
{
int lx = (int)(fx * kPatchSize);
int ly = (int)(fy * kPatchSize);
int hx = lx + 1;
if (hx >= kPatchSize)
hx = kPatchSize - 1;
int hy = ly + 1;
if (hy >= kPatchSize)
hy = kPatchSize - 1;
float s00 = GetPatchHeight (data, lx, ly);
float s01 = GetPatchHeight (data, lx, hy);
float s10 = GetPatchHeight (data, hx, ly);
float s11 = GetPatchHeight (data, hx, hy);
float dx = fx * kPatchSize - lx;
float dy = fy * kPatchSize - ly;
float x = Lerp(s00, s10, dx);
float y = Lerp(s01, s11, dx);
float value = Lerp(x, y, dy);
return value;
}
Vector3f Heightmap::CalculateNormalSobelRespectingNeighbors (int x, int y, const TerrainRenderer *renderer) const
{
Vector3f normal;
float dY, dX;
// Do X sobel filter
dX = GetHeightRespectingNeighbors (x-1, y-1, renderer) * -1.0F;
dX += GetHeightRespectingNeighbors (x-1, y , renderer) * -2.0F;
dX += GetHeightRespectingNeighbors (x-1, y+1, renderer) * -1.0F;
dX += GetHeightRespectingNeighbors (x+1, y-1, renderer) * 1.0F;
dX += GetHeightRespectingNeighbors (x+1, y , renderer) * 2.0F;
dX += GetHeightRespectingNeighbors (x+1, y+1, renderer) * 1.0F;
dX /= m_Scale.x;
// Do Y sobel filter
dY = GetHeightRespectingNeighbors (x-1, y-1, renderer) * -1.0F;
dY += GetHeightRespectingNeighbors (x , y-1, renderer) * -2.0F;
dY += GetHeightRespectingNeighbors (x+1, y-1, renderer) * -1.0F;
dY += GetHeightRespectingNeighbors (x-1, y+1, renderer) * 1.0F;
dY += GetHeightRespectingNeighbors (x , y+1, renderer) * 2.0F;
dY += GetHeightRespectingNeighbors (x+1, y+1, renderer) * 1.0F;
dY /= m_Scale.z;
// Cross Product of components of gradient reduces to
normal.x = -dX;
if (IS_CONTENT_NEWER_OR_SAME (kUnityVersion4_3_a1))
normal.y = 8;
else
// 5 here is just wrong!
normal.y = 5;
normal.z = -dY;
normal = NormalizeFast (normal);
return normal;
}
Vector3f Heightmap::CalculateNormalSobel (int x, int y) const
{
Vector3f normal;
float dY, dX;
// Do X sobel filter
dX = GetHeight (x-1, y-1) * -1.0F;
dX += GetHeight (x-1, y ) * -2.0F;
dX += GetHeight (x-1, y+1) * -1.0F;
dX += GetHeight (x+1, y-1) * 1.0F;
dX += GetHeight (x+1, y ) * 2.0F;
dX += GetHeight (x+1, y+1) * 1.0F;
dX /= m_Scale.x;
// Do Y sobel filter
dY = GetHeight (x-1, y-1) * -1.0F;
dY += GetHeight (x , y-1) * -2.0F;
dY += GetHeight (x+1, y-1) * -1.0F;
dY += GetHeight (x-1, y+1) * 1.0F;
dY += GetHeight (x , y+1) * 2.0F;
dY += GetHeight (x+1, y+1) * 1.0F;
dY /= m_Scale.z;
// Cross Product of components of gradient reduces to
normal.x = -dX;
if (IS_CONTENT_NEWER_OR_SAME (kUnityVersion4_3_a1))
normal.y = 8;
else
// 5 here is just wrong!
normal.y = 5;
normal.z = -dY;
normal = NormalizeFast (normal);
return normal;
}
float Heightmap::ComputeMaximumHeightError (int xPatch, int yPatch, int level) const
{
// Lod zero never has error
if (level == 0)
return 0.0F;
float* data = new float[kPatchSize * kPatchSize];
GetPatchData (xPatch, yPatch, level, data);
float deltaMax = 0.0F;
int skip = 1 << level;
int xBase = xPatch * (kPatchSize - 1) * skip;
int yBase = yPatch * (kPatchSize - 1) * skip;
int size = (kPatchSize - 1) * skip + 1;
float normalizeScale = 1.0F / (kPatchSize * skip);
for (int x=0;x<size;x++)
{
for (int y=0;y<size;y++)
{
float fx = (float)x * normalizeScale;
float fy = (float)y * normalizeScale;
float interpolatedHeight = InterpolatePatchHeight (data, fx, fy);
float realHeight = GetHeight (x + xBase, y + yBase);
float delta = Abs(realHeight - interpolatedHeight);
deltaMax = max(deltaMax, delta);
}
}
delete[] data;
return deltaMax;
}
Vector3f Heightmap::GetSize () const
{
return Vector3f (m_Scale.x * (m_Width - 1), m_Scale.y, m_Scale.z * (m_Height - 1));
}
void Heightmap::SetSize (const Vector3f& size)
{
m_Scale.x = size.x / (m_Width - 1);
m_Scale.y = size.y;
m_Scale.z = size.z / (m_Height - 1);
PrecomputeError(0, 0, m_Width, m_Height, false);
#if ENABLE_PHYSICS
UpdateNx ();
RecreateShapes();
#endif
m_TerrainData->SetDirty();
m_TerrainData->UpdateUsers (TerrainData::kHeightmap);
}
void Heightmap::AwakeFromLoad ()
{
#if ENABLE_PHYSICS
CreateNx ();
RecreateShapes ();
#endif
}
#if ENABLE_PHYSICS
void Heightmap::RecreateShapes ()
{
for (TerrainColliderList::iterator i=m_TerrainColliders.begin();i != m_TerrainColliders.end();)
{
TerrainCollider& col = **i;
i++;
col.Create(NULL);
}
}
#endif
// Precompute error only on a part of the heightmap
// if forceHighestLod is enabled we simply set the error to infinity
// This casues the heightmap to be rendered at full res (Used while editing)
void Heightmap::PrecomputeError (int minX, int minY, int width, int height, bool forceHighestLod)
{
for (int level=0;level <= m_Levels;level++)
{
for (int y=0;y<GetPatchCountY(level);y++)
{
for (int x=0;x<GetPatchCountX(level);x++)
{
int skip = 1 << level;
int curXBase = x * (kPatchSize - 1) * skip;
int curYBase = y * (kPatchSize - 1) * skip;
int curPatchSize = kPatchSize * skip;
// Are we in the bounds horizontally
if (curXBase + curPatchSize < minX)
continue;
if (curXBase > minX + width)
continue;
// Are we in the bounds vertically
if (curYBase + curPatchSize < minY)
continue;
if (curYBase > minY + height)
continue;
if (forceHighestLod)
{
m_PrecomputedError[GetPatchIndex(x, y, level)] = std::numeric_limits<float>::infinity();
}
else
{
float error = ComputeMaximumHeightError(x, y, level);
m_PrecomputedError[GetPatchIndex(x, y, level)] = error;
}
RecalculateMinMaxHeight(x, y, level);
}
}
}
m_TerrainData->SetDirty();
}
/// After editing is complete we need to recompute the error for the modified patches
/// We also update the min&max height of each patch
void Heightmap::RecomputeInvalidPatches(UNITY_TEMP_VECTOR(int)& recomputedPatches)
{
recomputedPatches.clear();
for (int level=0;level <= m_Levels;level++)
{
for (int y=0;y<GetPatchCountY(level);y++)
{
for (int x=0;x<GetPatchCountX(level);x++)
{
int patchIndex = GetPatchIndex(x, y, level);
if (m_PrecomputedError[patchIndex] == std::numeric_limits<float>::infinity())
{
float error = ComputeMaximumHeightError(x, y, level);
m_PrecomputedError[patchIndex] = error;
RecalculateMinMaxHeight(x, y, level);
recomputedPatches.push_back(patchIndex);
}
}
}
}
if (!recomputedPatches.empty())
m_TerrainData->SetDirty();
}
float Heightmap::GetMaximumHeightError (int x, int y, int level) const
{
return m_PrecomputedError[GetPatchIndex(x, y, level)];
}
float Heightmap::Bilerp (const float* corners, float u, float v)
{
// Corners are laid out like this
/// 0 1
/// 2 3
if (u > v)
{
float z00 = corners[0];
float z01 = corners[1];
float z11 = corners[3];
return z00 + (z01-z00) * u + (z11 - z01) * v;
}
else
{
float z00 = corners[0];
float z10 = corners[2];
float z11 = corners[3];
return z00 + (z11-z10) * u + (z10 - z00) * v;
}
}
/// The scaled interpolated height at the normalized coordinate x, y [0...1]
/// Out of bounds x and y will be clamped
float Heightmap::GetInterpolatedHeight (float x, float y) const
{
float fx = x * (m_Width - 1);
float fy = y * (m_Height - 1);
int lx = (int)fx;
int ly = (int)fy;
float u = fx - lx;
float v = fy - ly;
if (u > v)
{
float z00 = GetHeight (lx+0, ly+0);
float z01 = GetHeight (lx+1, ly+0);
float z11 = GetHeight (lx+1, ly+1);
return z00 + (z01-z00) * u + (z11 - z01) * v;
}
else
{
float z00 = GetHeight (lx+0, ly+0);
float z10 = GetHeight (lx+0, ly+1);
float z11 = GetHeight (lx+1, ly+1);
return z00 + (z11-z10) * u + (z10 - z00) * v;
}
}
// Gets the interpolated normal of the terrain at a
float Heightmap::GetSteepness (float x, float y) const
{
float steepness = Dot(GetInterpolatedNormal(x, y), Vector3f(0.0F, 1.0F, 0.0F));
steepness = Rad2Deg(acos (steepness));
return steepness;
}
// Gets the interpolated normal of the terrain at a
Vector3f Heightmap::GetInterpolatedNormal (float x, float y) const
{
float fx = x * (m_Width - 1);
float fy = y * (m_Height - 1);
int lx = (int)fx;
int ly = (int)fy;
Vector3f n00 = CalculateNormalSobel (lx+0, ly+0);
Vector3f n10 = CalculateNormalSobel (lx+1, ly+0);
Vector3f n01 = CalculateNormalSobel (lx+0, ly+1);
Vector3f n11 = CalculateNormalSobel (lx+1, ly+1);
float u = fx - lx;
float v = fy - ly;
Vector3f s = Lerp(n00, n10, u);
Vector3f t = Lerp(n01, n11, u);
Vector3f value = Lerp(s, t, v);
value = NormalizeFast(value);
return value;
}
void Heightmap::GetHeights (int xBase, int yBase, int width, int height, float* heights) const
{
float toNormalize = 1.0F / kMaxHeight;
for (int x=0;x<width;x++)
{
for (int y=0;y<height;y++)
{
NxHeightFieldSample sample = reinterpret_cast<const NxHeightFieldSample&> (m_Heights[(y + yBase) + (x + xBase) * m_Height]);
float height = sample.height;
height *= toNormalize;
heights[y * width + x] = height;
}
}
}
#define SUPPORT_PHYSX_UPDATE_BLOCKS 1
void Heightmap::SetHeights (int xBase, int yBase, int width, int height, const float* heights, bool delayLodComputation)
{
UInt32 min = 0;
float normalizedTo16 = kMaxHeight;
#if ENABLE_PHYSICS && SUPPORT_PHYSX_UPDATE_BLOCKS
NxHeightFieldSample* nxSamples = new NxHeightFieldSample[width * height];
int materialIndex = GetLowMaterialIndex(GetMaterialIndex());
#endif
for (int x=0;x<width;x++)
{
for (int y=0;y<height;y++)
{
float nHeight = heights[y * width + x] * normalizedTo16;
SInt32 iheight = RoundfToInt(nHeight);
iheight = clamp<SInt32> (iheight, min, kMaxHeight);
// Update height value
m_Heights[(y + yBase) + (x + xBase) * m_Height] = iheight;
#if ENABLE_PHYSICS && SUPPORT_PHYSX_UPDATE_BLOCKS
// Build update buffer for novodex
NxHeightFieldSample sample;
sample.height = iheight;
sample.materialIndex0 = materialIndex;
sample.materialIndex1 = materialIndex;
sample.tessFlag = 1;
sample.unused = 0;
nxSamples[height * x + y] = sample;
#endif
}
}
#if ENABLE_PHYSICS
#if SUPPORT_PHYSX_UPDATE_BLOCKS
if (m_NxHeightField)
{
m_NxHeightField->updateBlock(xBase, yBase, width, height, height * sizeof(NxHeightFieldSample), nxSamples);
RecreateShapes();
}
delete[] nxSamples;
#else
UpdateNx ();
RecreateShapes();
#endif
#endif
PrecomputeError(xBase, yBase, width, height, delayLodComputation);
m_TerrainData->SetDirty();
m_TerrainData->UpdateUsers (delayLodComputation ? TerrainData::kDelayedHeightmapUpdate : TerrainData::kHeightmap);
}
int Heightmap::GetAdjustedSize (int size) const
{
int levels = HighestBit( NextPowerOfTwo( size / kPatchSize ) );
levels = max<int>(1, levels);
return (1 << levels) * (kPatchSize - 1) + 1;
}
#if ENABLE_PHYSICS
void Heightmap::SetPhysicMaterial(PPtr<PhysicMaterial> mat)
{
m_DefaultPhysicMaterial = mat;
UpdateNx ();
RecreateShapes();
}
#endif
void Heightmap::SetResolution (int resolution)
{
m_Levels = HighestBit( NextPowerOfTwo( resolution / kPatchSize ) );
m_Levels = max<int>(1, m_Levels);
m_Width = (1 << m_Levels) * (kPatchSize - 1) + 1;
m_Height = (1 << m_Levels) * (kPatchSize - 1) + 1;
UInt32 materialIndex = GetLowMaterialIndex(GetMaterialIndex());//
NxHeightFieldSample sample;
sample.height = 0;
sample.materialIndex0 = materialIndex;
sample.materialIndex1 = materialIndex;
sample.tessFlag = 1;
sample.unused = 0;
m_Heights.clear();
m_Heights.resize(m_Width * m_Height, reinterpret_cast<UInt32&> (sample));
m_PrecomputedError.clear();
m_PrecomputedError.resize(GetTotalPatchCount());
m_MinMaxPatchHeights.clear();
m_MinMaxPatchHeights.resize(GetTotalPatchCount() * 2);
#if ENABLE_PHYSICS
UpdateNx ();
RecreateShapes();
#endif
m_TerrainData->SetDirty();
m_TerrainData->UpdateUsers (TerrainData::kHeightmap);
}
float Heightmap::GetHeight (int x, int y) const
{
x = clamp<int>(x, 0, m_Width-1);
y = clamp<int>(y, 0, m_Height-1);
float scale = m_Scale.y / (float)(kMaxHeight);
NxHeightFieldSample sample = reinterpret_cast<const NxHeightFieldSample&> (m_Heights[y + x * m_Height]);
return sample.height * scale;
}
float Heightmap::GetHeightRespectingNeighbors (int x, int y, const TerrainRenderer *renderer) const
{
const Heightmap *lookup = this;
if(x<0)
{
if(renderer && renderer->m_LeftNeighbor && renderer->m_LeftNeighbor->GetTerrainData())
{
renderer = renderer->m_LeftNeighbor;
lookup = &(renderer->GetTerrainData()->GetHeightmap());
x += lookup->m_Width - 1;
}
}
if(x >= lookup->m_Width)
{
if(renderer && renderer->m_RightNeighbor && renderer->m_RightNeighbor->GetTerrainData())
{
x -= lookup->m_Width - 1;
renderer = renderer->m_RightNeighbor;
lookup = &(renderer->GetTerrainData()->GetHeightmap());
}
}
if(y<0)
{
if(renderer && renderer->m_BottomNeighbor && renderer->m_BottomNeighbor->GetTerrainData())
{
renderer = renderer->m_BottomNeighbor;
lookup = &(renderer->GetTerrainData()->GetHeightmap());
y += lookup->m_Height - 1;
}
}
if(y >= lookup->m_Height)
{
if(renderer && renderer->m_TopNeighbor && renderer->m_TopNeighbor->GetTerrainData())
{
y -= lookup->m_Height - 1;
renderer = renderer->m_TopNeighbor;
lookup = &(renderer->GetTerrainData()->GetHeightmap());
}
}
return lookup->GetHeight(x,y);
}
void Heightmap::UpdatePatchIndices (Mesh& mesh, int xPatch, int yPatch, int level, int edgeMask)
{
unsigned int count;
unsigned short *tris = TerrainIndexGenerator::GetOptimizedIndexStrip(edgeMask, count);
mesh.SetIndicesComplex (tris, count, 0, kPrimitiveTriangleStripDeprecated, Mesh::k16BitIndices|Mesh::kDontSupportSubMeshVertexRanges);
}
void Heightmap::UpdatePatchMesh (Mesh& mesh, int xPatch, int yPatch, int mipLevel, int edgeMask, TerrainRenderer *renderer)
{
int vertexCount = kPatchSize * kPatchSize;
mesh.ResizeVertices (vertexCount, mesh.GetAvailableChannels() | VERTEX_FORMAT3(Vertex, TexCoord0, Normal));
UpdatePatchMeshInternal(*this,
mesh.GetVertexBegin(),
mesh.GetNormalBegin(),
mesh.GetUvBegin(),
xPatch, yPatch, mipLevel, edgeMask, renderer);
mesh.SetBounds(GetBounds(xPatch, yPatch, mipLevel));
mesh.SetChannelsDirty (mesh.GetAvailableChannels(), false);
}
void Heightmap::UpdatePatchIndices (VBO& vbo, SubMesh& subMesh, int xPatch, int yPatch, int level, int edgeMask)
{
unsigned int count;
unsigned short *tris = TerrainIndexGenerator::GetOptimizedIndexStrip(edgeMask, count);
IndexBufferData ibd = {
tris,
count,
1 << kPrimitiveTriangleStripDeprecated
};
vbo.UpdateIndexData(ibd);
subMesh.indexCount = count;
subMesh.topology = kPrimitiveTriangleStripDeprecated;
}
void Heightmap::UpdatePatchMesh (VBO& vbo, SubMesh& subMesh, int xPatch, int yPatch, int mipLevel, int edgeMask, TerrainRenderer *renderer)
{
const VertexChannelsLayout& format = VertexDataInfo::kVertexChannelsDefault;
UInt8 normalOffset = format.channels[kShaderChannelVertex].dimension * GetChannelFormatSize(format.channels[kShaderChannelVertex].format);
UInt8 uvOffset = format.channels[kShaderChannelNormal].dimension
* GetChannelFormatSize(format.channels[kShaderChannelNormal].format)
+ normalOffset;
UInt8 stride = format.channels[kShaderChannelTexCoord0].dimension
* GetChannelFormatSize(format.channels[kShaderChannelTexCoord0].format)
+ uvOffset;
VertexBufferData vbd;
vbd.channels[kShaderChannelVertex].format = format.channels[kShaderChannelVertex].format;
vbd.channels[kShaderChannelVertex].dimension = format.channels[kShaderChannelVertex].dimension;
vbd.channels[kShaderChannelNormal].format = format.channels[kShaderChannelNormal].format;
vbd.channels[kShaderChannelNormal].dimension = format.channels[kShaderChannelNormal].dimension;
vbd.channels[kShaderChannelNormal].offset = normalOffset;
vbd.channels[kShaderChannelTexCoord0].format = format.channels[kShaderChannelTexCoord0].format;
vbd.channels[kShaderChannelTexCoord0].dimension = format.channels[kShaderChannelTexCoord0].dimension;
vbd.channels[kShaderChannelTexCoord0].offset = uvOffset;
// UV1 is used for lightmapping and it shares with UV0
vbd.channels[kShaderChannelTexCoord1] = vbd.channels[kShaderChannelTexCoord0];
vbd.streams[0].channelMask = VERTEX_FORMAT4(Vertex, Normal, TexCoord0, TexCoord1);
vbd.streams[0].stride = stride;
vbd.vertexCount = kPatchSize * kPatchSize;
vbd.bufferSize = VertexDataInfo::AlignStreamSize(vbd.vertexCount * stride);
UInt8* buffer;
ALLOC_TEMP_ALIGNED(buffer, UInt8, vbd.bufferSize, VertexDataInfo::kVertexDataAlign);
vbd.buffer = buffer;
UpdatePatchMeshInternal(*this,
StrideIterator<Vector3f>(vbd.buffer, stride),
StrideIterator<Vector3f>(vbd.buffer + normalOffset, stride),
StrideIterator<Vector2f>(vbd.buffer + uvOffset, stride),
xPatch, yPatch, mipLevel, edgeMask, renderer);
vbo.UpdateVertexData(vbd);
subMesh.localAABB = GetBounds(xPatch, yPatch, mipLevel);
subMesh.vertexCount = vbd.vertexCount;
}
void Heightmap::GetPatchData (int xPatch, int yPatch, int mipLevel, float* heights) const
{
int skip = 1 << mipLevel;
int xBase = xPatch * (kPatchSize -1);
int yBase = yPatch * (kPatchSize -1);
float scale = m_Scale.y / (float)(kMaxHeight);
for (int x=0;x<kPatchSize;x++)
{
for (int y=0;y<kPatchSize;y++)
{
int index = (y + yBase) + (x + xBase) * m_Height;
index *= skip;
float height = m_Heights[index];
height *= scale;
heights[y + x * kPatchSize] = height;
}
}
}
void Heightmap::RecalculateMinMaxHeight (int xPatch, int yPatch, int mipLevel)
{
int totalSamples = kPatchSize * kPatchSize;
float* heights = new float[totalSamples];
GetPatchData (xPatch, yPatch, mipLevel, heights);
float minHeight = std::numeric_limits<float>::infinity();
float maxHeight = -std::numeric_limits<float>::infinity();
for (int i=0;i<totalSamples;i++)
{
minHeight = min(minHeight, heights[i]);
maxHeight = max(maxHeight, heights[i]);
}
int patchIndex = GetPatchIndex(xPatch, yPatch, mipLevel);
m_MinMaxPatchHeights[patchIndex * 2 + 0] = minHeight / m_Scale.y ;
m_MinMaxPatchHeights[patchIndex * 2 + 1] = maxHeight / m_Scale.y;
delete[] heights;
}
AABB Heightmap::GetBounds () const
{
return GetBounds(0, 0, m_Levels);
}
AABB Heightmap::GetBounds (int xPatch, int yPatch, int mipLevel) const
{
int patchIndex = GetPatchIndex(xPatch, yPatch, mipLevel);
float minHeight = m_MinMaxPatchHeights[patchIndex * 2 + 0];
float maxHeight = m_MinMaxPatchHeights[patchIndex * 2 + 1];
Vector3f min;
min.x = (xPatch) * (1 << mipLevel) * (kPatchSize -1) * m_Scale.x;
min.z = (yPatch) * (1 << mipLevel) * (kPatchSize -1) * m_Scale.z;
min.y = minHeight * m_Scale.y;
Vector3f max;
max.x = (xPatch + 1) * (1 << mipLevel) * (kPatchSize -1) * m_Scale.x;
max.z = (yPatch + 1) * (1 << mipLevel) * (kPatchSize -1) * m_Scale.z;
max.y = maxHeight * m_Scale.y;
MinMaxAABB bounds = MinMaxAABB (min, max);
return bounds;
}
int Heightmap::GetMaterialIndex () const
{
#if ENABLE_PHYSICS
const PhysicMaterial* material = m_DefaultPhysicMaterial;
if (material)
return material->GetMaterialIndex ();
else
#endif
return 0;
}
#if ENABLE_PHYSICS
NxHeightField* Heightmap::GetNxHeightField ()
{
if (m_NxHeightField == NULL)
CreateNx();
return m_NxHeightField;
}
void Heightmap::CreateNx ()
{
IPhysics* physicsModule = GetIPhysics();
if (!physicsModule)
return;
if (m_NxHeightField == NULL)
{
NxHeightFieldDesc desc;
BuildDesc(desc);
m_NxHeightField = physicsModule->CreateNxHeightField(desc);
free(desc.samples);
}
else
UpdateNx ();
}
// builds an nsdesc of the heightmap (you need to deallocate the samples array yourself)
void Heightmap::BuildDesc (NxHeightFieldDesc& desc)
{
NxHeightFieldSample* nxSamples = (NxHeightFieldSample*)malloc (sizeof(NxHeightFieldSample) * m_Width * m_Height);
AssertIf(!nxSamples);
desc.nbRows = m_Width;
desc.nbColumns = m_Height;
desc.samples = nxSamples;
desc.sampleStride = 4;
// the threshold seems to be the difference between two samples in 16 height space
// Thus a value of less than 1 doesn't make a difference
desc.convexEdgeThreshold = 4;
int materialIndex = GetLowMaterialIndex(GetMaterialIndex());
for (int x=0;x<m_Width;x++)
{
for (int y=0;y<m_Height;y++)
{
// Build update buffer for novodex
NxHeightFieldSample sample;
sample.height = m_Heights[m_Height * x + y];
sample.materialIndex0 = materialIndex;
sample.materialIndex1 = materialIndex;
sample.tessFlag = 1;
sample.unused = 0;
nxSamples[m_Height * x + y] = sample;
}
}
}
void Heightmap::UpdateNx ()
{
if (!m_NxHeightField)
return;
NxHeightFieldDesc desc;
BuildDesc(desc);
m_NxHeightField->loadFromDesc(desc);
free(desc.samples);
}
void Heightmap::CleanupNx ()
{
if (m_NxHeightField)
{
GetIPhysics()->ReleaseHeightField(*m_NxHeightField);
m_NxHeightField = NULL;
}
}
#endif
#endif
|