2023-01-10 02:36:09 +01:00
|
|
|
@tool
|
|
|
|
class_name TerrainInstance3D extends GeometryInstance3D
|
|
|
|
|
2023-01-11 17:58:58 +01:00
|
|
|
const _DETAIL := 2
|
|
|
|
|
2023-01-10 19:02:26 +01:00
|
|
|
var _mesh := PlaneMesh.new()
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-10 19:02:26 +01:00
|
|
|
var _material := ShaderMaterial.new()
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
|
|
|
var size: Vector2i = Vector2i.ZERO:
|
|
|
|
get:
|
|
|
|
return size
|
|
|
|
|
|
|
|
set(value):
|
2023-01-11 12:38:32 +01:00
|
|
|
var width := maxi(value.x, 0)
|
|
|
|
var height := maxi(value.y, 0)
|
2023-01-11 12:37:49 +01:00
|
|
|
|
|
|
|
if (width != size.x) or (height != size.y):
|
|
|
|
if (width == 0) and (height == 0):
|
|
|
|
RenderingServer.instance_set_base(self.get_instance(), RID())
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-11 12:37:49 +01:00
|
|
|
else:
|
2023-01-11 17:58:58 +01:00
|
|
|
self._mesh.subdivide_width = width * _DETAIL
|
|
|
|
self._mesh.subdivide_depth = height * _DETAIL
|
2023-01-10 19:02:26 +01:00
|
|
|
self._mesh.size = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-10 02:45:50 +01:00
|
|
|
self._material.set_shader_parameter("SIZE", Vector2(value))
|
2023-01-11 12:37:49 +01:00
|
|
|
RenderingServer.instance_set_base(self.get_instance(), self._mesh)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-11 17:58:58 +01:00
|
|
|
size = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@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:
|
2023-01-10 19:02:26 +01:00
|
|
|
self._material.shader = preload("res://terrain_shader.gdshader")
|
|
|
|
|
|
|
|
self._mesh.surface_set_material(0, self._material)
|