2023-01-10 02:36:09 +01:00
|
|
|
@tool
|
|
|
|
class_name TerrainInstance3D extends GeometryInstance3D
|
|
|
|
|
|
|
|
var _mesh := ArrayMesh.new()
|
|
|
|
|
|
|
|
var _material: ShaderMaterial = (func ():
|
|
|
|
var material := ShaderMaterial.new()
|
|
|
|
|
|
|
|
material.shader = preload("res://terrain_shader.gdshader")
|
|
|
|
|
|
|
|
return material).call()
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var size: Vector2i = Vector2i.ZERO:
|
|
|
|
get:
|
|
|
|
return size
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._mesh.clear_surfaces()
|
|
|
|
|
|
|
|
var width := value.x
|
|
|
|
var height := value.y
|
|
|
|
|
|
|
|
if (width != 0) and (height != 0):
|
|
|
|
if (width != size.x) or (height != size.y):
|
|
|
|
var surface_tool := SurfaceTool.new()
|
|
|
|
|
|
|
|
surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES)
|
|
|
|
|
|
|
|
var center_x := width / 2.0
|
|
|
|
var center_y := height / 2.0
|
|
|
|
|
|
|
|
for y in height + 1:
|
|
|
|
for x in width + 1:
|
|
|
|
surface_tool.set_uv(Vector2(x / float(width), y / float(height)))
|
|
|
|
surface_tool.set_color(Color(0.0, 0.0, 0.0, 0.0))
|
|
|
|
surface_tool.set_normal(Vector3.UP)
|
|
|
|
surface_tool.add_vertex(Vector3(x - center_x, 0.0, y - center_y))
|
|
|
|
|
|
|
|
var vert := 0
|
|
|
|
|
|
|
|
for y in height:
|
|
|
|
for x in width:
|
|
|
|
surface_tool.add_index(vert + width + 2)
|
|
|
|
surface_tool.add_index(vert + width + 1)
|
|
|
|
surface_tool.add_index(vert + 1)
|
|
|
|
surface_tool.add_index(vert + 1)
|
|
|
|
surface_tool.add_index(vert + width + 1)
|
|
|
|
surface_tool.add_index(vert + 0)
|
|
|
|
|
|
|
|
vert += 1
|
|
|
|
|
|
|
|
vert += 1
|
|
|
|
|
|
|
|
RenderingServer.instance_set_base(self.get_instance(),
|
|
|
|
surface_tool.commit(self._mesh).get_rid())
|
|
|
|
|
2023-01-10 02:45:50 +01:00
|
|
|
self._material.set_shader_parameter("SIZE", Vector2(value))
|
2023-01-10 02:36:09 +01:00
|
|
|
self._mesh.surface_set_material(0, self._material)
|
|
|
|
|
|
|
|
size = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_1_albedo: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_1_albedo
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_1_ALBEDO", value)
|
|
|
|
|
|
|
|
layer_1_albedo = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_1_normal_map: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_1_normal_map
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_1_NORMAL_MAP", value)
|
|
|
|
|
|
|
|
layer_1_normal_map = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_2_albedo: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_2_albedo
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_2_ALBEDO", value)
|
|
|
|
|
|
|
|
layer_2_albedo = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_2_normal_map: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_2_normal_map
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_2_NORMAL_MAP", value)
|
|
|
|
|
|
|
|
layer_2_normal_map = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_3_albedo: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_3_albedo
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_3_ALBEDO", value)
|
|
|
|
|
|
|
|
layer_3_albedo = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_3_normal_map: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_3_normal_map
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_3_NORMAL_MAP", value)
|
|
|
|
|
|
|
|
layer_3_normal_map = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_4_albedo: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_4_albedo
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_4_ALBEDO", value)
|
|
|
|
|
|
|
|
layer_4_albedo = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var layer_4_normal_map: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return layer_4_normal_map
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("LAYER_4_NORMAL_MAP", value)
|
|
|
|
|
|
|
|
layer_4_normal_map = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var terrain_map: Texture2D = null:
|
|
|
|
get:
|
|
|
|
return terrain_map
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("TERRAIN_MAP", value)
|
|
|
|
|
|
|
|
terrain_map = value
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var max_height := 100.0:
|
|
|
|
get:
|
|
|
|
return max_height
|
|
|
|
|
|
|
|
set(value):
|
|
|
|
self._material.set_shader_parameter("MAX_HEIGHT", value)
|
|
|
|
|
|
|
|
max_height = max(value, 0.0)
|
|
|
|
|
|
|
|
func _init() -> void:
|
|
|
|
RenderingServer.instance_set_base(self.get_instance(), self._mesh)
|