170 lines
2.8 KiB
GDScript
170 lines
2.8 KiB
GDScript
@tool
|
|
class_name TerrainInstance3D extends GeometryInstance3D
|
|
|
|
const _TERRAIN_MAX := 1024
|
|
|
|
var _mesh := PlaneMesh.new()
|
|
|
|
var _material := ShaderMaterial.new()
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var size: Vector2i = Vector2i.ZERO:
|
|
get:
|
|
return size
|
|
|
|
set(value):
|
|
var width := clampi(value.x, 0, _TERRAIN_MAX)
|
|
var height := clampi(value.y, 0, _TERRAIN_MAX)
|
|
|
|
if (width != size.x) or (height != size.y):
|
|
if (width == 0) and (height == 0):
|
|
RenderingServer.instance_set_base(self.get_instance(), RID())
|
|
|
|
else:
|
|
self._mesh.subdivide_width = width
|
|
self._mesh.subdivide_depth = height
|
|
self._mesh.size = value
|
|
|
|
self._material.set_shader_parameter("SIZE", Vector2(value))
|
|
RenderingServer.instance_set_base(self.get_instance(), self._mesh)
|
|
|
|
size = Vector2i(width, height)
|
|
|
|
##
|
|
##
|
|
##
|
|
@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:
|
|
self._material.shader = preload("res://terrain_shader.gdshader")
|
|
|
|
self._mesh.surface_set_material(0, self._material)
|