170 lines
2.6 KiB
GDScript
170 lines
2.6 KiB
GDScript
@tool
|
|
class_name TerrainInstance3D extends GeometryInstance3D
|
|
|
|
const _DETAIL := 2
|
|
|
|
var _mesh := PlaneMesh.new()
|
|
|
|
var _material := ShaderMaterial.new()
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var size: Vector2i = Vector2i.ZERO:
|
|
get:
|
|
return size
|
|
|
|
set(value):
|
|
var width := maxi(value.x, 0)
|
|
var height := maxi(value.y, 0)
|
|
|
|
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 * _DETAIL
|
|
self._mesh.subdivide_depth = height * _DETAIL
|
|
self._mesh.size = value
|
|
|
|
self._material.set_shader_parameter("SIZE", Vector2(value))
|
|
RenderingServer.instance_set_base(self.get_instance(), self._mesh)
|
|
|
|
size = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var albedo_map: Texture2D = null:
|
|
get:
|
|
return albedo_map
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("ALBEDO_MAP_0", value)
|
|
|
|
albedo_map = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var albedo_map_b: Texture2D = null:
|
|
get:
|
|
return albedo_map_b
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("ALBEDO_MAP_B", value)
|
|
|
|
albedo_map_b = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var albedo_map_g: Texture2D = null:
|
|
get:
|
|
return albedo_map_g
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("ALBEDO_MAP_G", value)
|
|
|
|
albedo_map_g = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var albedo_map_r: Texture2D = null:
|
|
get:
|
|
return albedo_map_r
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("ALBEDO_MAP_R", value)
|
|
|
|
albedo_map_r = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var normal_map: Texture2D = null:
|
|
get:
|
|
return normal_map
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("NORMAL_MAP_0", value)
|
|
|
|
normal_map = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var normal_map_b: Texture2D = null:
|
|
get:
|
|
return normal_map_b
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("NORMAL_MAP_B", value)
|
|
|
|
normal_map_b = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var normal_map_g: Texture2D = null:
|
|
get:
|
|
return normal_map_g
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("NORMAL_MAP_G", value)
|
|
|
|
normal_map_g = value
|
|
|
|
##
|
|
##
|
|
##
|
|
@export
|
|
var normal_map_r: Texture2D = null:
|
|
get:
|
|
return normal_map_r
|
|
|
|
set(value):
|
|
self._material.set_shader_parameter("NORMAL_MAP_R", value)
|
|
|
|
normal_map_r = 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/terrain_shader.gdshader")
|
|
|
|
self._mesh.surface_set_material(0, self._material)
|