Skip to content

Commit

Permalink
Fix detail layers were not rendering correctly when terrain is centered
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Dec 10, 2023
1 parent 0102124 commit f5707d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ For a more detailed list of past and incoming changes, see the commit history.
- Fixed `lod_scale` property getter was always returning 2 instead of the last set value
- Fixed LOD chunks sometimes overlapping when the heightmap is modified (issue #411)
- Fixed detail layers were rendering repeating instances outside the terrain when `map_scale` is not 1.0
- Fixed detail layers were not rendering correctly when terrain is in centered mode


1.7.2
Expand Down
3 changes: 2 additions & 1 deletion addons/zylann.hterrain/hterrain.gd
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ func get_internal_transform_unscaled():
var gt := global_transform
if centered and _data != null:
var half_size := 0.5 * (_data.get_resolution() - 1.0)
gt.origin += gt.basis * (-Vector3(half_size, 0, half_size))
# Map scale still has an effect on origin when the map is centered
gt.origin += gt.basis * (-Vector3(half_size, 0, half_size) * map_scale)
return gt


Expand Down
14 changes: 8 additions & 6 deletions addons/zylann.hterrain/hterrain_detail_layer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func _on_terrain_transform_changed(gt: Transform3D):
_logger.error("Detail layer is not child of a terrain!")
return

var terrain_transform : Transform3D = terrain.get_internal_transform()
var terrain_transform : Transform3D = terrain.get_internal_transform_unscaled()

# Update AABBs and transforms, because scale might have changed
for k in _chunks:
Expand Down Expand Up @@ -451,8 +451,8 @@ func process(delta: float, viewer_pos: Vector3):
terrain.get_internal_transform_unscaled()
var local_viewer_pos := terrain_transform_without_map_scale.affine_inverse() * viewer_pos

var viewer_cx := local_viewer_pos.x / CHUNK_SIZE
var viewer_cz := local_viewer_pos.z / CHUNK_SIZE
var viewer_cx := int(local_viewer_pos.x / CHUNK_SIZE)
var viewer_cz := int(local_viewer_pos.z / CHUNK_SIZE)

var cr := int(view_distance) / CHUNK_SIZE + 1

Expand Down Expand Up @@ -481,7 +481,8 @@ func process(delta: float, viewer_pos: Vector3):
_debug_cubes.clear()
for cz in range(cmin_z, cmax_z):
for cx in range(cmin_x, cmax_x):
_add_debug_cube(terrain, _get_chunk_aabb(terrain, Vector3(cx, 0, cz) * CHUNK_SIZE))
_add_debug_cube(terrain, _get_chunk_aabb(terrain, Vector3(cx, 0, cz) * CHUNK_SIZE),
terrain_transform_without_map_scale)

for cz in range(cmin_z, cmax_z):
for cx in range(cmin_x, cmax_x):
Expand Down Expand Up @@ -637,7 +638,7 @@ func _update_material():
mat.set_shader_parameter("u_terrain_globalmap", globalmap_texture)


func _add_debug_cube(terrain: Node3D, aabb: AABB):
func _add_debug_cube(terrain: Node3D, aabb: AABB, terrain_transform_without_scale: Transform3D):
var world : World3D = terrain.get_world_3d()

if _debug_wirecube_mesh == null:
Expand All @@ -653,7 +654,8 @@ func _add_debug_cube(terrain: Node3D, aabb: AABB):
debug_cube.set_mesh(_debug_wirecube_mesh)
debug_cube.set_world(world)
#aabb.position.y += 0.2*randf()
debug_cube.set_transform(Transform3D(Basis().scaled(aabb.size), aabb.position))
debug_cube.set_transform(terrain_transform_without_scale \
* Transform3D(Basis().scaled(aabb.size), aabb.position))

_debug_cubes.append(debug_cube)

Expand Down

0 comments on commit f5707d3

Please sign in to comment.