Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_TerrainMeshEntity.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/Shapes", description: "Procedural box", color: "255 0 0 255", style: "box", dynamicBox: true)]
2 class SCR_TerrainMeshEntityClass: GenericEntityClass
3 {
4 };
5 
10 {
11  [Attribute(category: "Terrain Mesh", defvalue: "4", params: "2 32 1", uiwidget: UIWidgets.Slider)]
12  protected int m_iResolution;
13 
14  [Attribute(category: "Terrain Mesh", defvalue: "0.01", params: "0 1 0.01", uiwidget: UIWidgets.Slider)]
15  protected float m_fHeightCoef;
16 
17  [Attribute(category: "Terrain Mesh")]
18  protected ref array<float> m_aHeights;
19 
20  [Attribute(category: "Terrain Mesh", desc: "Material mapped on the mesh.", uiwidget: UIWidgets.ResourcePickerThumbnail, params: "emat")]
21  private ResourceName m_Material;
22 
23  void Generate(array<float> heights, ResourceName material)
24  {
25  int heightsCount = heights.Count();
26  if (heightsCount != m_iResolution * m_iResolution)
27  {
28  Print(string.Format("Resolution of height values (%1) doesn't match the resolution attribute (%2)", Math.Sqrt(heightsCount), m_iResolution), LogLevel.WARNING);
29  return;
30  }
31  int resolution1 = m_iResolution - 1;
32 
33  vector verts[2048];
34  float uvs[2048];
35  int indices[2048];
36  int vI, iI, uI, row, column;
37 
38  //--- Get positions
39  for (int y = 0; y < m_iResolution; y++)
40  {
41  for (int x = 0; x < m_iResolution; x++)
42  {
43  verts[vI] = Vector(
44  x / resolution1 - 0.5,
45  heights[vI] * m_fHeightCoef,
46  y / resolution1 - 0.5
47  );
48  vI++;
49 
50  uvs[uI] = x / resolution1; uI++;
51  uvs[uI] = 1 - y / resolution1; uI++;
52  }
53  }
54 
55  /*
56  6 - 7 - 8
57  | \ | \ |
58  3 - 4 - 5
59  | \ | \ |
60  0 - 1 - 2
61 
62  Vertex 1: 0-3-4
63  Vertex 2: 4-1-0
64  ...
65  */
66  for (int s = 0; s < vI - m_iResolution; s++)
67  {
68  column = s % m_iResolution;
69  if (column == resolution1)
70  {
71  row++;
72  continue;
73  }
74 
75  int pointBottomLeft = s;
76  int pointBottomRight = s + 1;
77  int pointTopLeft = pointBottomLeft + m_iResolution;
78  int pointTopRight = pointBottomRight + m_iResolution;
79 
80  indices[iI] = pointBottomLeft; iI++;
81  indices[iI] = pointTopLeft; iI++;
82  indices[iI] = pointTopRight; iI++;
83 
84  indices[iI] = pointTopRight; iI++;
85  indices[iI] = pointBottomRight; iI++;
86  indices[iI] = pointBottomLeft; iI++;
87  }
88 
89  int numVertices[] = {vI};
90  int numIndices[] = {iI};
91  string materials[] = {material};
92 
93  Resource res = MeshObject.Create(1, numVertices, numIndices, materials, 0);
94  MeshObject meshObject = res.GetResource().ToMeshObject();
95  meshObject.UpdateVerts(0, verts, uvs);
96  meshObject.UpdateIndices(0, indices);
97  SetObject(meshObject,"");
98  Physics.CreateStatic(this, 0xffffffff);
99  }
100 
101  void SCR_TerrainMeshEntity(IEntitySource src, IEntity parent)
102  {
103  Generate(m_aHeights, m_Material);
104  }
105 
106 #ifdef WORKBENCH
107  override array<ref WB_UIMenuItem> _WB_GetContextMenuItems()
108  {
109  return { new WB_UIMenuItem("Generate from the current terrain", 0) };
110  }
111  override void _WB_OnContextMenu(int id)
112  {
113  WorldEditor worldEditor = Workbench.GetModule(WorldEditor);
114  if (!worldEditor) return;
115 
116  WorldEditorAPI api = _WB_GetEditorAPI();
117  if (!api) return;
118 
119  BaseWorld world = api.GetWorld();
120  if (!world) return;
121 
122  vector min, max, size;
123  //world.GetBoundBox(mins, maxs);
124  worldEditor.GetTerrainBounds(min, max);
125  size = max - min;
126  float stepX = size[0] / m_iResolution;
127  float stepY = size[2] / m_iResolution;
128  float heightMax = max[1] - Math.Max(min[1], 0);
129 
130  int resolution1 = m_iResolution - 1;
131  bool addComma = false;
132  string value;
133  float posX, posY, posZ;
134  for (int y = 0; y < m_iResolution; y++)
135  {
136  for (int x = 0; x < m_iResolution; x++)
137  {
138  if (addComma) value += ",";
139  posX = min[0] + size[0] * x / resolution1;
140  posY = min[2] + size[2] * y / resolution1;
141  posZ = Math.Max(world.GetSurfaceY(posX, posY), 0) / heightMax;
142  value += posZ.ToString();
143  addComma = true;
144  }
145  }
146  Print(value);
147 
148  api.BeginEntityAction();
149  api.SetVariableValue(api.EntityToSource(this), null, "m_aHeights", value);
150  api.EndEntityAction();
151  }
152 #endif
153 };
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_TerrainMeshEntity
Definition: SCR_TerrainMeshEntity.c:9
SCR_TerrainMeshEntityClass
Definition: SCR_TerrainMeshEntity.c:2
m_Material
protected ResourceName m_Material
Definition: SCR_BaseAreaMeshComponent.c:27
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180