Skip to content

Instantly share code, notes, and snippets.

@Lance5057
Created June 5, 2024 02:50
Show Gist options
  • Select an option

  • Save Lance5057/736caa251eec4141aa9dc178874b09bb to your computer and use it in GitHub Desktop.

Select an option

Save Lance5057/736caa251eec4141aa9dc178874b09bb to your computer and use it in GitHub Desktop.
Simple Runtime Heightmap editor for TerraBrush
using Godot;
using Godot.Collections;
using TerraBrush;
public partial class BuildController : Node3D
{
[Export]
public Camera3D camera;
[Export]
public Node3D pointer;
[Export]
public TerraBrush.TerraBrush terrain;
[Export]
private Image heightMap;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
Color increment = new Color(1, 0, 0, 1);
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseMotion m)
{
movePointer(m);
}
if (@event is InputEventMouseButton button)
{
if (button.ButtonIndex == MouseButton.Left)
{
ZoneResource zone = terrain.TerrainZones.Zones[0];
if (heightMap == null)
heightMap = zone.HeightMapTexture.GetImage();
Vector3 zonePos = getZonePosition(zone, terrain.ZonesSize);
Vector2I pixel = new Vector2I((int)(pointer.Position.X - zonePos.X + (terrain.ZonesSize/2)), (int)(pointer.Position.Z - zonePos.Z + (terrain.ZonesSize / 2)));
GD.Print(pixel);
for (int x = 0; x < 4; x++)
for(int y = 0; y < 4; y++)
{
heightMap.SetPixel(x + pixel.X, y + pixel.Y, increment);
}
zone.HeightMapTexture.Update(heightMap);
terrain.TerrainZones.UpdateImageTextures();
}
}
}
Vector3 getZonePosition(ZoneResource zone, int ZonesSize)
{
return new Vector3(zone.ZonePosition.X * ZonesSize/2, 0, zone.ZonePosition.Y * ZonesSize);
}
private void movePointer(InputEventMouseMotion m)
{
Dictionary result = cameraRayCast(m.Position);
if (result.Count > 0)
{
GD.Print(result["position"]);
Vector3 p = (Vector3)result["position"];
pointer.Position = new Vector3(Mathf.Round(p.X), p.Y, Mathf.Round(p.Z));
}
}
private Dictionary cameraRayCast(Vector2 mousePos)
{
Vector3 from = camera.ProjectRayOrigin(mousePos);
Vector3 to = from + camera.ProjectRayNormal(mousePos) * 10000;
PhysicsDirectSpaceState3D space = GetWorld3D().DirectSpaceState;
PhysicsRayQueryParameters3D ray = PhysicsRayQueryParameters3D.Create(from, to);
Godot.Collections.Dictionary result = space.IntersectRay(ray);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment