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
|
2023-01-13 01:36:09 +01:00
|
|
|
var albedo_map: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return albedo_map
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 01:36:09 +01:00
|
|
|
self._material.set_shader_parameter("ALBEDO_MAP_0", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
albedo_map = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
2023-01-13 01:36:09 +01:00
|
|
|
var albedo_map_b: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return albedo_map_b
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 01:36:09 +01:00
|
|
|
self._material.set_shader_parameter("ALBEDO_MAP_B", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
albedo_map_b = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
2023-01-13 01:36:09 +01:00
|
|
|
var albedo_map_g: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return albedo_map_g
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 01:36:09 +01:00
|
|
|
self._material.set_shader_parameter("ALBEDO_MAP_G", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
albedo_map_g = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
2023-01-13 01:36:09 +01:00
|
|
|
var albedo_map_r: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return albedo_map_r
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 01:36:09 +01:00
|
|
|
self._material.set_shader_parameter("ALBEDO_MAP_R", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
albedo_map_r = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
2023-01-13 01:36:09 +01:00
|
|
|
var normal_map: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return normal_map
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 01:36:09 +01:00
|
|
|
self._material.set_shader_parameter("NORMAL_MAP_0", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
normal_map = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
2023-01-13 01:36:09 +01:00
|
|
|
var normal_map_b: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return normal_map_b
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 01:36:09 +01:00
|
|
|
self._material.set_shader_parameter("NORMAL_MAP_B", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
normal_map_b = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
2023-01-13 01:36:09 +01:00
|
|
|
var normal_map_g: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return normal_map_g
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 01:36:09 +01:00
|
|
|
self._material.set_shader_parameter("NORMAL_MAP_G", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
normal_map_g = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@export
|
2023-01-13 01:36:09 +01:00
|
|
|
var normal_map_r: Texture2D = null:
|
2023-01-10 02:36:09 +01:00
|
|
|
get:
|
2023-01-13 01:36:09 +01:00
|
|
|
return normal_map_r
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
set(value):
|
2023-01-13 19:00:13 +01:00
|
|
|
self._material.set_shader_parameter("NORMAL_MAP_R", value)
|
2023-01-10 02:36:09 +01:00
|
|
|
|
2023-01-13 01:36:09 +01:00
|
|
|
normal_map_r = value
|
2023-01-10 02:36:09 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
##
|
|
|
|
@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)
|