forked from sideeffects/HoudiniEngineForUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHEU_TerrainUtility.cs
More file actions
1415 lines (1224 loc) · 56.2 KB
/
HEU_TerrainUtility.cs
File metadata and controls
1415 lines (1224 loc) · 56.2 KB
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) <2020> Side Effects Software Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. The name of Side Effects Software may not be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HoudiniEngineUnity
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Typedefs (copy these from HEU_Common.cs)
using HAPI_NodeId = System.Int32;
using HAPI_PartId = System.Int32;
using HAPI_StringHandle = System.Int32;
public static class HEU_TerrainUtility
{
/// <summary>
/// Creates terrain from given volumeInfo for the given gameObject.
/// If gameObject has a valid Terrain component, then it is reused.
/// Similarly, if the Terrain component has a valid TerrainData, or if the given terrainData is valid, then it is used.
/// Otherwise a new TerrainData is created and set to the Terrain.
/// Populates the volumePositionOffset with the heightfield offset position.
/// Returns true if successfully created the terrain, otherwise false.
/// </summary>
/// <param name="session">Houdini Engine session to query heightfield data from</param>
/// <param name="volumeInfo">Volume info pertaining to the heightfield to generate the Terrain from</param>
/// <param name="geoID">The geometry ID</param>
/// <param name="partID">The part ID (height layer)</param>
/// <param name="gameObject">The target GameObject containing the Terrain component</param>
/// <param name="terrainData">A valid TerrainData to use, or if empty, a new one is created and populated</param>
/// <param name="volumePositionOffset">Heightfield offset</param>
/// <param name="bakedMaterialPath">Folder path for caching material output</param>
/// <returns>True if successfully popupated the terrain</returns>
public static bool GenerateTerrainFromVolume(HEU_SessionBase session, ref HAPI_VolumeInfo volumeInfo, HAPI_NodeId geoID, HAPI_PartId partID,
GameObject gameObject, ref TerrainData terrainData, out Vector3 volumePositionOffset, ref Terrain terrain, string bakedMaterialPath)
{
volumePositionOffset = Vector3.zero;
if (volumeInfo.zLength == 1 && volumeInfo.tupleSize == 1)
{
// Heightfields will be converted to terrain in Unity.
// Unity requires terrainData.heightmapResolution to be square power of two plus 1 (eg. 513, 257, 129, 65).
// Houdini gives volumeInfo.xLength and volumeInfo.yLength which are the number of height values per dimension.
// Note that volumeInfo.xLength and volumeInfo.yLength is equal to Houdini heightfield size / grid spacing.
// The heightfield grid spacing is given as volumeTransformMatrix.scale but divided by 2 (grid spacing / 2 = volumeTransformMatrix.scale).
// It is recommended to use grid spacing of 2.
// Use the volumeInfo.transform to get the actual heightfield position and size.
Matrix4x4 volumeTransformMatrix = HEU_HAPIUtility.GetMatrixFromHAPITransform(ref volumeInfo.transform, false);
Vector3 position = HEU_HAPIUtility.GetPosition(ref volumeTransformMatrix);
Vector3 scale = HEU_HAPIUtility.GetScale(ref volumeTransformMatrix);
// Calculate real terrain size in both Houdini and Unity.
// The height values will be mapped over this terrain size.
float gridSpacingX = scale.x * 2f;
float gridSpacingY = scale.y * 2f;
// Subtracting 1 to account for the corner sampling.
// In HEU_InputInterfaceTerrain::GenerateTerrainDataFromGameObject we add voxel size to
// account for corner sampling as well. This ensures maintaining the size
// when roundtripping the terrain.
float terrainSizeX = Mathf.Round((volumeInfo.xLength - 1) * gridSpacingX);
float terrainSizeY = Mathf.Round((volumeInfo.yLength - 1) * gridSpacingY);
//HEU_Logger.LogFormat("volumeInfo: {0}x{1}", volumeInfo.xLength, volumeInfo.yLength);
//HEU_Logger.LogFormat("GS = {0}x{1}, Size = {2}x{3}", gridSpacingX, gridSpacingY, terrainSizeX, terrainSizeY);
//HEU_Logger.LogFormat("HeightField Pos:{0}, Scale:{1}", position, scale.ToString("{0.00}"));
//HEU_Logger.LogFormat("HeightField tileSize:{0}, xLength:{1}, yLength:{2}", volumeInfo.tileSize.ToString("{0.00}"), volumeInfo.xLength.ToString("{0.00}"), volumeInfo.yLength.ToString("{0.00}"));
//HEU_Logger.LogFormat("HeightField Terrain Size x:{0}, y:{1}", terrainSizeX.ToString("{0.00}"), terrainSizeY.ToString("{0.00}"));
//HEU_Logger.LogFormat("HeightField minX={0}, minY={1}, minZ={2}", volumeInfo.minX.ToString("{0.00}"), volumeInfo.minY.ToString("{0.00}"), volumeInfo.minZ.ToString("{0.00}"));
bool bNewTerrain = false;
bool bNewTerrainData = false;
terrain = gameObject.GetComponent<Terrain>();
if (terrain == null)
{
terrain = gameObject.AddComponent<Terrain>();
bNewTerrain = true;
}
#if !HEU_TERRAIN_COLLIDER_DISABLED
TerrainCollider collider = HEU_GeneralUtility.GetOrCreateComponent<TerrainCollider>(gameObject);
#endif
// This ensures to reuse existing terraindata, and only creates new if none exist
if (terrain.terrainData == null)
{
if (terrainData == null)
{
terrainData = new TerrainData();
bNewTerrainData = true;
}
}
if (terrainData != null)
{
terrain.terrainData = terrainData;
}
else if (terrain.terrainData != null)
{
terrainData = terrain.terrainData;
}
else
{
return false;
}
// Look up terrain material, if specified, on the height layer
string specifiedTerrainMaterialName = HEU_GeneralUtility.GetMaterialAttributeValueFromPart(session,
geoID, partID);
SetTerrainMaterial(terrain, specifiedTerrainMaterialName, bakedMaterialPath);
#if !HEU_TERRAIN_COLLIDER_DISABLED
collider.terrainData = terrainData;
#endif
if (bNewTerrain)
{
#if UNITY_2018_3_OR_NEWER
terrain.allowAutoConnect = true;
// This has to be set after setting material
terrain.drawInstanced = true;
#endif
}
// Heightmap resolution must be square power-of-two plus 1.
// Unity will automatically resize terrainData.heightmapResolution so need to handle the changed size (if Unity changed it).
int heightMapResolution = volumeInfo.xLength;
const int UNITY_MINIMUM_HEIGHTMAP_RESOLUTION = 33;
if (heightMapResolution < UNITY_MINIMUM_HEIGHTMAP_RESOLUTION || heightMapResolution < UNITY_MINIMUM_HEIGHTMAP_RESOLUTION)
{
HEU_Logger.LogWarningFormat("Unity Terrain has a minimum heightmap resolution of {0}. This HDA heightmap size is {1}x{2}."
+ "\nPlease resample the heightmap resolution to a value higher than this.",
UNITY_MINIMUM_HEIGHTMAP_RESOLUTION, heightMapResolution, heightMapResolution);
return false;
}
terrainData.heightmapResolution = heightMapResolution;
int mapWidth = volumeInfo.xLength;
int mapHeight = volumeInfo.yLength;
// Get the converted height values from Houdini and find the min and max height range.
float minHeight = 0;
float maxHeight = 0;
float heightRange = 0;
bool bUseHeightRangeOverride = true;
float[] normalizedHeights = GetNormalizedHeightmapFromPartWithMinMax(session, geoID, partID,
volumeInfo.xLength, volumeInfo.yLength, ref minHeight, ref maxHeight, ref heightRange,
bUseHeightRangeOverride);
int terrainResizedDelta = terrainData.heightmapResolution - heightMapResolution;
if (terrainResizedDelta < 0)
{
HEU_Logger.LogWarningFormat("Note that Unity automatically resized terrain resolution to {0} from {1}. Use terrain size of power of two plus 1, and grid spacing of 2.", heightMapResolution, terrainData.heightmapResolution);
float[] resampledHeights = HEU_TerrainUtility.ResampleData(normalizedHeights, heightMapResolution, heightMapResolution, terrainData.heightmapResolution, terrainData.heightmapResolution);
normalizedHeights = resampledHeights;
heightMapResolution = terrainData.heightmapResolution;
}
else if (terrainResizedDelta > 0)
{
HEU_Logger.LogWarningFormat("Unsupported terrain size. Use terrain size of power of two plus 1, and grid spacing of 2. Given size is {0} but Unity resized it to {1}.", heightMapResolution, terrainData.heightmapResolution);
}
float[,] unityHeights = ConvertHeightMapHoudiniToUnity(heightMapResolution, heightMapResolution, normalizedHeights);
// The terrainData.baseMapResolution is not set here, but rather left to whatever default Unity uses
// The terrainData.alphamapResolution is set later when setting the alphamaps.
if (bNewTerrainData)
{
// 32 is the default for resolutionPerPatch
const int detailResolution = 1024;
const int resolutionPerPatch = 32;
terrainData.SetDetailResolution(detailResolution, resolutionPerPatch);
}
// Note SetHeights must be called before setting size in next line, as otherwise
// the internal terrain size will not change after setting the size.
terrainData.SetHeights(0, 0, unityHeights);
// Note that Unity uses a default height range of 600 when a flat terrain is created.
// Without a non-zero value for the height range, user isn't able to draw heights.
// Therefore, set 600 as the value if height range is currently 0 (due to flat heightfield).
if (heightRange == 0)
{
heightRange = terrainData.size.y > 1 ? terrainData.size.y : 600;
}
terrainData.size = new Vector3(terrainSizeX, heightRange, terrainSizeY);
terrain.Flush();
// Unity Terrain has origin at bottom left, whereas Houdini uses centre of terrain.
// Use volume bounds to set position offset when using split tiles
float xmin, xmax, zmin, zmax, ymin, ymax, xcenter, ycenter, zcenter;
session.GetVolumeBounds(geoID, partID, out xmin, out ymin, out zmin, out xmax, out ymax, out zmax, out xcenter,
out ycenter, out zcenter);
//HEU_Logger.LogFormat("xmin: {0}, xmax: {1}, ymin: {2}, ymax: {3}, zmin: {4}, zmax: {5}, xc: {6}, yc: {7}, zc: {8}",
// xmin, xmax, ymin, ymax, zmin, zmax, xcenter, ycenter, zcenter);
//HEU_Logger.LogFormat("heightMapResolution: {0}, mapWidth: {1}, mapHeight: {2}", heightMapResolution, mapWidth, mapHeight);
// Use y position from attribute if user has set it
float ypos = position.y + minHeight;
float userYPos;
if (HEU_GeneralUtility.GetAttributeFloatSingle(session, geoID, partID,
HEU_Defines.DEFAULT_UNITY_HEIGHTFIELD_YPOS, out userYPos))
{
ypos = userYPos;
}
float sx = Mathf.Abs(Mathf.Abs(xmax - xmin) - terrainSizeX);
volumePositionOffset = new Vector3(position.x + (terrainSizeX - sx), ypos, position.z);
return true;
}
else
{
HEU_Logger.LogWarning("Non-heightfield volume type not supported!");
}
return false;
}
/// <summary>
/// Sets a material on the given Terrain object.
/// Currently sets the default Terrain material from the plugin settings, if its valid.
/// </summary>
/// <param name="terrain">The terrain to set material for</param>
public static void SetTerrainMaterial(Terrain terrain, string specifiedMaterialName, string bakedMaterialPath = "")
{
// Use material specified in Plugin settings.
string terrainMaterialPath = string.IsNullOrEmpty(specifiedMaterialName) ? HEU_PluginSettings.DefaultTerrainMaterial :
specifiedMaterialName;
if (!string.IsNullOrEmpty(terrainMaterialPath))
{
Material material = HEU_MaterialFactory.LoadUnityMaterial(terrainMaterialPath);
if (material != null)
{
#if UNITY_2019_2_OR_NEWER
terrain.materialTemplate = material;
#else
terrain.materialType = Terrain.MaterialType.Custom;
terrain.materialTemplate = material;
#endif
}
else
{
HEU_Logger.LogWarning("Warning: Specified material does not exist!");
}
}
else
{
Material material = null;
// Create new material with the default shader
if (!string.IsNullOrEmpty(bakedMaterialPath))
{
material = HEU_MaterialFactory.GetNewMaterialWithShader(bakedMaterialPath, GetDefaultTerrainShaderName(), terrain.gameObject.name + "-Material", false);
}
// Use the hardcoded paths as a last resort
if (material == null)
{
string defaultTerrainMaterialPath = GetDefaultTerrainMaterialPath();
material = HEU_MaterialFactory.LoadUnityMaterial(defaultTerrainMaterialPath);
}
if (material != null)
{
#if UNITY_2019_2_OR_NEWER
terrain.materialTemplate = material;
#else
terrain.materialType = Terrain.MaterialType.Custom;
terrain.materialTemplate = material;
#endif
}
else
{
HEU_Logger.LogWarning("Warning: Specified material does not exist!");
}
}
// TODO: If none specified, guess based on Render settings?
}
public static string GetDefaultTerrainShaderName()
{
HEU_PipelineType pipeline = HEU_RenderingPipelineDefines.GetPipeline();
if (pipeline == HEU_PipelineType.HDRP)
{
return HEU_Defines.DEFAULT_TERRAIN_SHADER_HDRP;
}
else if (pipeline == HEU_PipelineType.URP)
{
return HEU_Defines.DEFAULT_TERRAIN_SHADER_URP;
}
else
{
return HEU_Defines.DEFAULT_TERRAIN_SHADER;
}
}
public static string GetDefaultTerrainMaterialPath()
{
HEU_PipelineType pipeline = HEU_RenderingPipelineDefines.GetPipeline();
if (pipeline == HEU_PipelineType.HDRP)
{
return HEU_Defines.DEFAULT_TERRAIN_MATERIAL_PATH_HDRP;
}
else if (pipeline == HEU_PipelineType.URP)
{
return HEU_Defines.DEFAULT_TERRAIN_MATERIAL_PATH_URP;
}
else
{
return HEU_Defines.DEFAULT_TERRAIN_MATERIAL_PATH;
}
}
/// <summary>
/// Retrieves the heightmap from Houdini for the given volume part, converts to Unity coordinates,
/// normalizes to 0 and 1, along with min and max height values, as well as the range.
/// </summary>
/// <param name="session">Current Houdini session</param>
/// <param name="geoID">Geometry object ID</param>
/// <param name="partID">The volume part ID</param>
/// <param name="heightMapSize">Size of each dimension of the heightmap (assumes equal sides).</param>
/// <param name="minHeight">Found minimum height value in the heightmap.</param>
/// <param name="maxHeight">Found maximum height value in the heightmap.</param>
/// <param name="heightRange">Found height range in the heightmap.</param>
/// <returns>The converted heightmap from Houdini.</returns>
public static float[] GetNormalizedHeightmapFromPartWithMinMax(HEU_SessionBase session, HAPI_NodeId geoID, HAPI_PartId partID,
int heightMapWidth, int heightMapHeight, ref float minHeight, ref float maxHeight, ref float heightRange, bool bUseHeightRangeOverride)
{
minHeight = float.MaxValue;
maxHeight = float.MinValue;
heightRange = 1;
HAPI_VolumeInfo volumeInfo = new HAPI_VolumeInfo();
bool bResult = session.GetVolumeInfo(geoID, partID, ref volumeInfo);
if (!bResult)
{
return null;
}
int volumeXLength = volumeInfo.xLength;
int volumeYLength = volumeInfo.yLength;
// Number of heightfield values
int totalHeightValues = volumeXLength * volumeYLength;
float[] heightValues = new float[totalHeightValues];
if (!GetHeightmapFromPart(session, volumeXLength, volumeYLength, geoID, partID, ref heightValues, ref minHeight, ref maxHeight))
{
return null;
}
heightRange = (maxHeight - minHeight);
//HEU_Logger.LogFormat("HF min={0}, max={1}, range={2}", minHeight, maxHeight, heightRange);
// Use the override height range if user has set via attribute
bool bHeightRangeOverriden = false;
if (bUseHeightRangeOverride)
{
float userHeightRange = GetHeightRangeFromHeightfield(session, geoID, partID);
if (userHeightRange > 0)
{
heightRange = userHeightRange;
bHeightRangeOverriden = true;
}
}
if (heightRange == 0f)
{
// Always use a non-zero height range, otherwise user can't paint height on Terrain.
heightRange = 1f;
}
//HEU_Logger.LogFormat("{0} : {1}", HEU_SessionManager.GetString(volumeInfo.nameSH, session), heightRange);
const int UNITY_MAX_HEIGHT_RANGE = 65536;
if (Mathf.RoundToInt(heightRange) > UNITY_MAX_HEIGHT_RANGE)
{
HEU_Logger.LogWarningFormat("Unity Terrain has maximum height range of {0}. This HDA height range is {1}, so it will be maxed out at {0}.\nPlease resize to within valid range!",
UNITY_MAX_HEIGHT_RANGE, Mathf.RoundToInt(heightRange));
heightRange = UNITY_MAX_HEIGHT_RANGE;
}
// Remap height values to fit terrain size
int paddingWidth = heightMapWidth - volumeXLength;
int paddingLeft = Mathf.CeilToInt(paddingWidth * 0.5f);
int paddingRight = heightMapWidth - paddingLeft;
//HEU_Logger.LogFormat("Padding: Width={0}, Left={1}, Right={2}", paddingWidth, paddingLeft, paddingRight);
int paddingHeight = heightMapHeight - volumeYLength;
int paddingTop = Mathf.CeilToInt(paddingHeight * 0.5f);
int paddingBottom = heightMapHeight - paddingTop;
//HEU_Logger.LogFormat("Padding: Height={0}, Top={1}, Bottom={2}", paddingHeight, paddingTop, paddingBottom);
// Normalize the height values into the range between 0 and 1, inclusive.
float inverseHeightRange = 1f / heightRange;
float normalizeMinHeight = minHeight;
if (minHeight >= 0f && minHeight <= 1f && maxHeight >= 0f && maxHeight <= 1f)
{
// Its important to leave the values alone if they are already normalized.
// So these values don't actually do anything in the normalization calculation below.
inverseHeightRange = 1f;
normalizeMinHeight = 0f;
}
// Set height values at centre of the terrain, with padding on the sides if we resized
float[] resizedHeightValues = new float[heightMapWidth * heightMapHeight];
for (int y = 0; y < heightMapHeight; ++y)
{
for (int x = 0; x < heightMapWidth; ++x)
{
if (y >= paddingTop && y < (paddingBottom) && x >= paddingLeft && x < (paddingRight))
{
int ay = x - paddingLeft;
int ax = y - paddingTop;
float f = heightValues[ay + ax * volumeXLength];
if (!bHeightRangeOverriden)
{
f -= normalizeMinHeight;
}
f *= inverseHeightRange;
// Flip for right-hand to left-handed coordinate system
int ix = x;
int iy = heightMapHeight - (y + 1);
// Unity expects height array indexing to be [y, x].
resizedHeightValues[iy + ix * heightMapWidth] = f;
}
}
}
return resizedHeightValues;
}
/// <summary>
/// Returns the DetailLayer values for the given heightfield part.
/// Converts from HF float[] values into int[,] array.
/// </summary>
/// <param name="session">Houdini Engine session to query</param>
/// <param name="geoID">The geometry ID in Houdini</param>
/// <param name="partID">The part ID in Houdini</param>
/// <param name="detailResolution">Out value specifying the detail resolution acquired
/// from the heightfield size.</param>
/// <returns>The DetailLayer values</returns>
public static int[,] GetDetailMapFromPart(HEU_SessionBase session, HAPI_NodeId geoID, HAPI_PartId partID,
out int detailResolution)
{
detailResolution = 0;
HAPI_VolumeInfo volumeInfo = new HAPI_VolumeInfo();
bool bResult = session.GetVolumeInfo(geoID, partID, ref volumeInfo);
if (!bResult)
{
return null;
}
int volumeXLength = volumeInfo.xLength;
int volumeYLength = volumeInfo.yLength;
// Unity requires square size
if (volumeXLength != volumeYLength)
{
HEU_Logger.LogErrorFormat("Detail layer size must be square. Got {0}x{1} instead. Unable to apply detail layer.",
volumeXLength, volumeYLength);
return null;
}
// Use the size of the volume as the detail resolution size
detailResolution = volumeXLength;
// Number of heightfield values
int totalHeightValues = volumeXLength * volumeYLength;
// Get the floating point values from the heightfield
float[] heightValues = new float[totalHeightValues];
bResult = HEU_GeneralUtility.GetArray2Arg(geoID, partID, session.GetHeightFieldData, heightValues, 0, totalHeightValues);
if (!bResult)
{
return null;
}
// Convert float[] to int[,]. No conversion or scaling done since just want to use values as they are.
int[,] intMap = new int[volumeXLength, volumeYLength];
for (int y = 0; y < volumeYLength; ++y)
{
for (int x = 0; x < volumeXLength; ++x)
{
float f = heightValues[x + y * volumeXLength];
// Flip for right-hand to left-handed coordinate system
int ix = x;
int iy = volumeYLength - (y + 1);
intMap[ix, iy] = (int)f;
}
}
return intMap;
}
/// <summary>
/// Retrieve the heightmap from Houdini for given part (volume), along with min and max height values.
/// </summary>
/// <param name="session">Current Houdini session.</param>
/// <param name="xLength">Length of x dimension of the heightmap.</param>
/// <param name="yLength">Length of y dimension of the heightmap.</param>
/// <param name="geoID">Geometry object ID.</param>
/// <param name="partID">The volume part ID.</param>
/// <param name="heightValues">The raw heightmap from Houdini.</param>
/// <param name="minHeight">Found minimum height value in the heightmap.</param>
/// <param name="maxHeight">Found maximum height value in the heightmap.</param>
/// <returns>The heightmap from Houdini.</returns>
public static bool GetHeightmapFromPart(HEU_SessionBase session, int xLength, int yLength, HAPI_NodeId geoID, HAPI_PartId partID,
ref float[] heightValues, ref float minHeight, ref float maxHeight)
{
// Get the height values from Houdini and find the min and max height range.
int totalHeightValues = xLength * yLength;
heightValues = new float[totalHeightValues];
bool bResult = HEU_GeneralUtility.GetArray2Arg(geoID, partID, session.GetHeightFieldData, heightValues, 0, totalHeightValues);
if (!bResult)
{
return false;
}
minHeight = heightValues[0];
maxHeight = minHeight;
for (int i = 0; i < totalHeightValues; ++i)
{
float f = heightValues[i];
if (f > maxHeight)
{
maxHeight = f;
}
else if (f < minHeight)
{
minHeight = f;
}
}
return true;
}
/// <summary>
/// Convert the given heightValues (linear array) into a multi-dimensional array that Unity
/// needs for uploading as the heightmap for terrain.
/// </summary>
/// <param name="heightMapSize">Size of each dimension of the heightmap (assumes equal sides).</param>
/// <param name="heightValues">List of height values that will be converted to the heightmap.</param>
/// <returns>Converted heightmap for Unity terrain.</returns>
public static float[,] ConvertHeightMapHoudiniToUnity(int heightMapWidth, int heightMapHeight, float[] heightValues)
{
float[,] unityHeights = new float[heightMapWidth, heightMapHeight];
for (int y = 0; y < heightMapHeight; ++y)
{
for (int x = 0; x < heightMapWidth; ++x)
{
float f = heightValues[y + x * heightMapWidth];
unityHeights[x, y] = f;
}
}
return unityHeights;
}
/// <summary>
/// Converts the given heightfields (linear array) into a multi-dimensional array that Unity
/// needs for uploading splatmap values for terrain.
/// Assumes the values have already been converted to Unity coordinates, and normalized between 0 and 1.
/// </summary>
/// <param name="heightMapSize">Size of each dimension of the heightmap (assumes equal sides).</param>
/// <param name="heightFields">List of height values that will be converted to alphamap.</param>
/// <returns>Converted heightfields into Unity alphamap array.</returns>
public static float[,,] ConvertHeightFieldToAlphaMap(int heightMapWidth, int heightMapHeight, List<float[]> heightFields)
{
int numMaps = heightFields.Count;
// Assign height floats to alpha map, with strength applied.
float[,,] alphamap = new float[heightMapWidth, heightMapHeight, numMaps];
for (int y = 0; y < heightMapHeight; ++y)
{
for (int x = 0; x < heightMapWidth; ++x)
{
for (int m = numMaps - 1; m >= 0; --m)
{
float a = heightFields[m][y + heightMapWidth * x];
a = Mathf.Clamp01(a);
alphamap[x, y, m] = a;
}
}
}
return alphamap;
}
/// <summary>
/// Returns a new alphamap for Unity terrain consisting of heightfield values that have
/// already be converted to Unity format, with strengths multiplied.
/// </summary>
/// <param name="heightMapSize">Size of each dimension of the heightmap</param>
/// <param name="existingAlphaMaps">Existing alphamaps to reuse (could be null)</param>
/// <param name="heightFields">Converted heightfields to use for alphamaps</param>
/// <param name="strengths">Strength values to multiply the alphamap by</param>
/// <param name="alphaMapIndices">List of indices for each alphamap dictating whether to use existingAlphaMaps values or use heightFields values.
/// Negative values signal existing indices, while positive (>0) values signal heightField indices. Indices are offset by -1, and +1, respectively.</param>
/// <returns>Converted alphamap</returns>
public static float[,,] AppendConvertedHeightFieldToAlphaMap(int heightMapWidth, int heightMapHeight, float[,,] existingAlphaMaps, List<float[]> heightFields, float[] strengths, List<int> alphaMapIndices)
{
// Assign height floats to alpha map, with strength applied.
int numMaps = alphaMapIndices.Count;
int index = 0;
float[,,] alphaMap = new float[heightMapWidth, heightMapHeight, numMaps];
for (int y = 0; y < heightMapHeight; ++y)
{
for (int x = 0; x < heightMapWidth; ++x)
{
for (int m = 0; m < numMaps; m++)
{
index = alphaMapIndices[m];
if (index < 0)
{
// Use existing alphamap
index = (index * -1) - 1; // index is negative and off by -1
alphaMap[x, y, m] = existingAlphaMaps[x, y, index];
}
else if (index > 0)
{
// Use heightfield
index -= 1; // index is off by +1
float a = heightFields[index][y + heightMapWidth * x];
a = Mathf.Clamp01(a) * strengths[index];
alphaMap[x, y, m] = a;
}
}
}
}
return alphaMap;
}
/// <summary>
/// Get the volume position offset based on volume dimensions and bounds.
/// </summary>
public static Vector3 GetVolumePositionOffset(HEU_SessionBase session, HAPI_NodeId geoID, HAPI_PartId partID,
Vector3 volumePosition, float terrainSizeX, float heightMapSize, int mapWidth, int mapHeight, float minHeight)
{
// Use volume bounds to set position offset when using split tiles
float xmin, xmax, zmin, zmax, ymin, ymax, xcenter, ycenter, zcenter;
session.GetVolumeBounds(geoID, partID, out xmin, out ymin, out zmin, out xmax, out ymax, out zmax, out xcenter,
out ycenter, out zcenter);
// Offset position is based on size of heightfield
float offsetX = (float)heightMapSize / (float)mapWidth;
float offsetZ = (float)heightMapSize / (float)mapHeight;
return new Vector3((terrainSizeX + xmin) * offsetX, minHeight + volumePosition.y, zmin * offsetZ);
}
/// <summary>
/// Returns list of HEU_TreePrototypeInfo formed by querying data from given part.
/// </summary>
/// <param name="session">Houdini Engine session</param>
/// <param name="geoID">Geometry object</param>
/// <param name="partID">Part ID</param>
/// <returns>Returns list of HEU_TreePrototypeInfo or null if none found.</returns>
public static List<HEU_TreePrototypeInfo> GetTreePrototypeInfosFromPart(HEU_SessionBase session, HAPI_NodeId geoID, HAPI_PartId partID)
{
List<HEU_TreePrototypeInfo> treePrototypes = new List<HEU_TreePrototypeInfo>();
// Each TreePrototype data is stored as a string attribute, under the 'HEU_Defines.HEIGHTFIELD_TREEPROTOTYPE + index'
// name. So check and parse until no more valid attributes found.
int index = 0;
while (true)
{
// Does this attribute exist?
string attrName = HEU_Defines.HEIGHTFIELD_TREEPROTOTYPE + index.ToString();
if (!HEU_GeneralUtility.HasAttribute(session, geoID, partID, attrName, HAPI_AttributeOwner.HAPI_ATTROWNER_PRIM))
{
break;
}
index++;
// Get the string value
HAPI_AttributeInfo treeAttrInfo = new HAPI_AttributeInfo();
string[] protoAttrString = HEU_GeneralUtility.GetAttributeStringData(session, geoID, partID,
attrName, ref treeAttrInfo);
if (protoAttrString == null || protoAttrString.Length == 0 || string.IsNullOrEmpty(protoAttrString[0]))
{
break;
}
// Parse the attribute string value:
// Only expecting a single element here, comma-separated for the asset path and bend factor:
// => asset_path,bend_factor
string[] properties = protoAttrString[0].Split(',');
if (properties.Length > 0 && !string.IsNullOrEmpty(properties[0]))
{
HEU_TreePrototypeInfo prototype = new HEU_TreePrototypeInfo();
prototype._prefabPath = properties[0];
if (properties.Length >= 2)
{
float.TryParse(properties[1], out prototype._bendfactor);
}
treePrototypes.Add(prototype);
}
}
return treePrototypes.Count > 0 ? treePrototypes : null;
}
/// <summary>
/// Grab the scatter data for the given part.
/// This finds the properties of TreeInstances via attributes.
/// </summary>
/// <param name="session">Houdini session</param>
/// <param name="geoID">Geometry ID</param>
/// <param name="partID">Part (volume layer) ID</param>
/// <param name="pointCount">Number of expected scatter points</param>
public static void PopulateScatterTrees(HEU_SessionBase session, HAPI_NodeId geoID, HAPI_PartId partID, int pointCount,
ref HEU_VolumeScatterTrees scatterTrees, bool throwWarningIfNoTileAttribute)
{
// The HEU_VolumeScatterTrees might already have been created when the volumecache was queried.
// The "height" layer might have had prototype data which is set in _scatterTrees.
if (scatterTrees == null)
{
scatterTrees = new HEU_VolumeScatterTrees();
}
// Get prototype indices. These indices refer to _scatterTrees._treePrototypes.
HAPI_AttributeInfo indicesAttrInfo = new HAPI_AttributeInfo();
int[] indices = new int[0];
if (HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_Defines.HEIGHTFIELD_TREEINSTANCE_PROTOTYPEINDEX, ref indicesAttrInfo, ref indices, session.GetAttributeIntData))
{
if (indices != null && indices.Length == pointCount)
{
scatterTrees._prototypeIndices = indices;
}
else
{
HEU_Logger.LogWarningFormat("Scatter instance index count for attribute {0} is not valid. Expected {1} but got {2}",
HEU_Defines.HEIGHTFIELD_TREEINSTANCE_PROTOTYPEINDEX, pointCount, (indices != null ? indices.Length : 0));
}
}
HAPI_AttributeInfo tileAttrInfo = new HAPI_AttributeInfo();
int[] tiles = new int[0];
if (!HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_Defines.HAPI_HEIGHTFIELD_TILE_ATTR, ref tileAttrInfo, ref tiles, session.GetAttributeIntData))
{
if (!HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_Defines.HEIGHTFIELD_UNITY_TILE, ref tileAttrInfo, ref tiles, session.GetAttributeIntData))
{
if (throwWarningIfNoTileAttribute)
{
HEU_Logger.LogWarning("Multiple tiles detected but attribute tile or unity_hf_tile was not found! This will cause tree instances to default to the first tile. Set unity_hf_tile to the tile index to prevent this.");
}
tiles = new int[scatterTrees._prototypeIndices.Length];
for (int i = 0; i < scatterTrees._prototypeIndices.Length; i++)
{
tiles[i] = -1;
}
}
}
if (tiles != null && tiles.Length == scatterTrees._prototypeIndices.Length)
{
scatterTrees._terrainTiles = tiles;
}
// Using the UVs as position of the instances, since they are properly mapped to the terrain tile.
// Also getting other attributes for the TreeInstances, if they are set.
HAPI_AttributeInfo uvAttrInfo = new HAPI_AttributeInfo();
float[] uvs = new float[0];
if (!HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_HAPIConstants.HAPI_ATTRIB_UV, ref uvAttrInfo, ref uvs, session.GetAttributeFloatData))
{
HEU_Logger.LogWarning("UVs for scatter instances not found or valid.");
}
if (uvs != null && uvs.Length == (pointCount * uvAttrInfo.tupleSize))
{
// Get height scales
HAPI_AttributeInfo heightAttrInfo = new HAPI_AttributeInfo();
float[] heightscales = new float[0];
HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_Defines.HEIGHTFIELD_TREEINSTANCE_HEIGHTSCALE, ref heightAttrInfo, ref heightscales, session.GetAttributeFloatData);
// Get width scales
HAPI_AttributeInfo widthAttrInfo = new HAPI_AttributeInfo();
float[] widthscales = new float[0];
HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_Defines.HEIGHTFIELD_TREEINSTANCE_WIDTHSCALE, ref widthAttrInfo, ref widthscales, session.GetAttributeFloatData);
// Get orientation
HAPI_AttributeInfo orientAttrInfo = new HAPI_AttributeInfo();
float[] orients = new float[0];
HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_Defines.HAPI_ATTRIB_ORIENT, ref orientAttrInfo, ref orients, session.GetAttributeFloatData);
// Get color
HAPI_AttributeInfo colorAttrInfo = new HAPI_AttributeInfo();
float[] colors = new float[0];
HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_HAPIConstants.HAPI_ATTRIB_COLOR, ref colorAttrInfo, ref colors, session.GetAttributeFloatData);
// Get lightmap color
HAPI_AttributeInfo lightmapColorAttrInfo = new HAPI_AttributeInfo();
float[] lightmapColors = new float[0];
HEU_GeneralUtility.GetAttribute(session, geoID, partID, HEU_Defines.HEIGHTFIELD_TREEINSTANCE_LIGHTMAPCOLOR, ref lightmapColorAttrInfo, ref lightmapColors, session.GetAttributeFloatData);
scatterTrees._positions = new Vector3[pointCount];
if (heightAttrInfo.exists && (heightscales.Length == pointCount))
{
scatterTrees._heightScales = heightscales;
}
if (widthAttrInfo.exists && (widthscales.Length == pointCount))
{
scatterTrees._widthScales = widthscales;
}
if (orientAttrInfo.exists && (orients.Length == orientAttrInfo.tupleSize * pointCount))
{
scatterTrees._rotations = new float[pointCount];
}
if (colorAttrInfo.exists && (colors.Length == colorAttrInfo.tupleSize * pointCount))
{
scatterTrees._colors = new Color32[pointCount];
}
if (lightmapColorAttrInfo.exists && (lightmapColors.Length == lightmapColorAttrInfo.tupleSize * pointCount))
{
scatterTrees._lightmapColors = new Color32[pointCount];
}
float a = 1.0f;
for (int i = 0; i < pointCount; ++i)
{
scatterTrees._positions[i] = new Vector3(1.0f - uvs[i * uvAttrInfo.tupleSize + 1],
0,
uvs[i * uvAttrInfo.tupleSize + 0]);
if (scatterTrees._colors != null)
{
a = colorAttrInfo.tupleSize == 4 ? colors[i * colorAttrInfo.tupleSize + 3] : 1.0f;
scatterTrees._colors[i] =
new Color32((byte)(colors[i * colorAttrInfo.tupleSize + 0] * 255),
(byte)(colors[i * colorAttrInfo.tupleSize + 1] * 255),
(byte)(colors[i * colorAttrInfo.tupleSize + 2] * 255),
(byte)(a * 255));
}
if (scatterTrees._lightmapColors != null)
{
a = lightmapColorAttrInfo.tupleSize == 4 ? lightmapColors[i * lightmapColorAttrInfo.tupleSize + 3] : 1.0f;
scatterTrees._lightmapColors[i] =
new Color32((byte)(lightmapColors[i * lightmapColorAttrInfo.tupleSize + 0] * 255),
(byte)(lightmapColors[i * lightmapColorAttrInfo.tupleSize + 1] * 255),
(byte)(lightmapColors[i * lightmapColorAttrInfo.tupleSize + 2] * 255),
(byte)(a * 255));
}
if (scatterTrees._rotations != null)
{
Quaternion quaternion = new Quaternion(
orients[i * orientAttrInfo.tupleSize + 0],
orients[i * orientAttrInfo.tupleSize + 1],
orients[i * orientAttrInfo.tupleSize + 2],
orients[i * orientAttrInfo.tupleSize + 3]);
Vector3 euler = quaternion.eulerAngles;
euler.y = -euler.y;
euler.z = -euler.z;
scatterTrees._rotations[i] = euler.y * Mathf.Deg2Rad;
}
}
}
}
/// <summary>
/// Apply the cached scatter prototypes and instances to the given TerrainData.
/// </summary>
public static void ApplyScatterTrees(TerrainData terrainData, HEU_VolumeScatterTrees scatterTrees, int tileIndex)
{
#if UNITY_2019_1_OR_NEWER
if (scatterTrees == null || scatterTrees._treePrototypInfos == null || scatterTrees._treePrototypInfos.Count == 0)
{
return;
}
// Load and set TreePrototypes
GameObject prefabGO;
List<TreePrototype> treePrototypes = new List<TreePrototype>();
for (int i = 0; i < scatterTrees._treePrototypInfos.Count; ++i)
{
prefabGO = HEU_AssetDatabase.LoadAssetAtPath(scatterTrees._treePrototypInfos[i]._prefabPath, typeof(GameObject)) as GameObject;
if (prefabGO != null)
{
TreePrototype prototype = new TreePrototype();
prototype.prefab = prefabGO;
prototype.bendFactor = scatterTrees._treePrototypInfos[i]._bendfactor;
treePrototypes.Add(prototype);
//HEU_Logger.LogFormat("Added Tree Prototype: {0} - {1}", scatterTrees._treePrototypInfos[i]._prefabPath, scatterTrees._treePrototypInfos[i]._bendfactor);
}
}
terrainData.treePrototypes = treePrototypes.ToArray();
terrainData.RefreshPrototypes();
if (scatterTrees._positions != null && scatterTrees._positions.Length > 0
&& scatterTrees._prototypeIndices != null && scatterTrees._prototypeIndices.Length == scatterTrees._positions.Length)
{
TreeInstance[] treeInstances = new TreeInstance[scatterTrees._positions.Length];
for (int i = 0; i < scatterTrees._positions.Length; ++i)
{
if (scatterTrees._terrainTiles != null && i < scatterTrees._terrainTiles.Length && scatterTrees._terrainTiles[i] != -1 && scatterTrees._terrainTiles[i] != tileIndex)
{
continue;
}
treeInstances[i] = new TreeInstance();
treeInstances[i].color = scatterTrees._colors != null ? scatterTrees._colors[i] : new Color32(255, 255, 255, 255);
treeInstances[i].heightScale = scatterTrees._heightScales != null ? scatterTrees._heightScales[i] : 1f;
treeInstances[i].lightmapColor = scatterTrees._lightmapColors != null ? scatterTrees._lightmapColors[i] : new Color32(255, 255, 255, 255);
treeInstances[i].position = scatterTrees._positions[i];
treeInstances[i].prototypeIndex = scatterTrees._prototypeIndices[i];
treeInstances[i].rotation = scatterTrees._rotations[i];
treeInstances[i].widthScale = scatterTrees._widthScales != null ? scatterTrees._widthScales[i] : 1f;
}
terrainData.SetTreeInstances(treeInstances, true);
}
#endif
}
/// <summary>
/// Fill up the given detailPrototype with values from the specified heightfield part.
/// </summary>
/// <param name="session">Houdini Engine session to query</param>
/// <param name="geoID">The geometry ID in Houdini</param>
/// <param name="partID">The part ID in Houdini</param>
/// <param name="detailPrototype">The detail prototype object to populate</param>
public static void PopulateDetailPrototype(HEU_SessionBase session, HAPI_NodeId geoID, HAPI_PartId partID,
ref HEU_DetailPrototype detailPrototype)
{
// Get the detail prototype properties from attributes on this layer
if (detailPrototype == null)
{
detailPrototype = new HEU_DetailPrototype();
}
HAPI_AttributeInfo prefabAttrInfo = new HAPI_AttributeInfo();
string[] prefabPaths = HEU_GeneralUtility.GetAttributeStringData(session, geoID, partID,
HEU_Defines.HEIGHTFIELD_DETAIL_PROTOTYPE_PREFAB, ref prefabAttrInfo);
if (prefabAttrInfo.exists && prefabPaths.Length >= 1)
{