Skip to content

Commit

Permalink
Fix/grass (#47)
Browse files Browse the repository at this point in the history
* Updated plugin Scatter to 2.8.4

* Fixed bug where grass would not show up when 'test_map' was reloaded

Co-authored-by: Telmo Valverde <[email protected]>
  • Loading branch information
telmotrooper and telmotrooper authored May 21, 2022
1 parent 1ac0aa6 commit 19f2a6b
Show file tree
Hide file tree
Showing 34 changed files with 425 additions and 221 deletions.
301 changes: 185 additions & 116 deletions addons/scatter/demos/feature_list.tscn

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion addons/scatter/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="Scatter"
description="Scatter other scenes in a manually defined area"
author="HungryProton"
version="2.8.2"
version="2.8.4"
script="plugin.gd"
4 changes: 4 additions & 0 deletions addons/scatter/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func _enter_tree():

_editor_selection = get_editor_interface().get_selection()
_editor_selection.connect("selection_changed", self, "_on_selection_changed")

# warning-ignore:return_value_discarded
connect("scene_changed", self, "_on_scene_changed")


Expand Down Expand Up @@ -137,6 +139,8 @@ func _setup_options_panel() -> void:
])
_gizmo_options = preload("./src/tools/path_gizmo/gizmo_options.tscn").instance()
_options_root.add_child(_gizmo_options)

# warning-ignore:return_value_discarded
_gizmo_options.connect("snap_to_colliders_enabled", self, "_on_snap_to_colliders_enabled")
_gizmo_options.visible = false

Expand Down
19 changes: 10 additions & 9 deletions addons/scatter/presets/default.tscn
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://addons/scatter/src/core/scatter.gd" type="Script" id=1]
[ext_resource path="res://addons/scatter/src/core/scatter_item.gd" type="Script" id=2]
[ext_resource path="res://addons/scatter/demos/assets/grass/data/m_grass_52.tres" type="Material" id=3]

[sub_resource type="Curve3D" id=3]
[sub_resource type="Curve3D" id=1]
_data = {
"points": PoolVector3Array( 0, 0, 0, 0, 0, 0, -4.56338, 0, 4.05873, 0, 0, 0, 0, 0, 0, -3.38615, 4.48227e-05, -3.4064, 0, 0, 0, 0, 0, 0, 5.92637, 0, -4.04829, 0, 0, 0, 0, 0, 0, 4.98351, 0, 4.68679 ),
"tilts": PoolRealArray( 0, 0, 0, 0 )
}

[node name="Scatter" type="Path"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -2.25427, 0, 0 )
curve = SubResource( 3 )
curve = SubResource( 1 )
script = ExtResource( 1 )
modifier_stack = Object(Node,"_import_path":NodePath(""),"pause_mode":0,"process_priority":0,"script":Resource( "res://addons/scatter/src/core/modifier_stack.gd" ),"stack":[ Object(Node,"_import_path":NodePath(""),"pause_mode":0,"process_priority":0,"script":Resource( "res://addons/scatter/src/modifiers/distribute_inside_random.gd" ),"enabled":true,"display_name":"Distribute Inside (Random)","category":"Distribute","warning":"","warning_ignore_no_transforms":true,"warning_ignore_no_path":false,"override_global_seed":false,"custom_seed":0,"instance_count":100,"_rng":null)
, Object(Node,"_import_path":NodePath(""),"pause_mode":0,"process_priority":0,"script":Resource( "res://addons/scatter/src/modifiers/randomize_transforms.gd" ),"enabled":true,"display_name":"Randomize Transforms","category":"Edit","warning":"","warning_ignore_no_transforms":false,"warning_ignore_no_path":true,"override_global_seed":false,"custom_seed":0,"local_space":true,"position":Vector3( 1, 0, 1 ),"rotation":Vector3( 0, 360, 0 ),"scale":Vector3( 1, 1, 1 ),"_rng":null)
, Object(Node,"_import_path":NodePath(""),"pause_mode":0,"process_priority":0,"script":Resource( "res://addons/scatter/src/modifiers/project_on_floor.gd" ),"enabled":true,"display_name":"Project On Floor","category":"Edit","warning":"","warning_ignore_no_transforms":false,"warning_ignore_no_path":true,"ray_length":10.0,"ray_offset":4.0,"remove_points_on_miss":true,"align_with_floor_normal":false,"invert_ray_direction":false,"floor_direction":Vector3( 0, -1, 0 ),"max_slope":1.0,"mask":"1048575")
],"just_created":false,"undo_redo":null)
modifier_stack = Object(Node,"_import_path":NodePath(""),"pause_mode":0,"process_priority":0,"script":Resource( "res://addons/scatter/src/core/modifier_stack.gd" ),"stack":[ Object(Node,"_import_path":NodePath(""),"pause_mode":0,"process_priority":0,"script":Resource( "res://addons/scatter/src/modifiers/distribute_inside_random.gd" ),"enabled":true,"display_name":"Distribute Inside (Random)","category":"Distribute","warning":"","warning_ignore_no_transforms":true,"warning_ignore_no_path":false,"override_global_seed":false,"custom_seed":0,"instance_count":100,"_rng":Object(RandomNumberGenerator,"seed":-7543621026227883429,"state":-6733907183443918434,"script":null)
)
, Object(Node,"_import_path":NodePath(""),"pause_mode":0,"process_priority":0,"script":Resource( "res://addons/scatter/src/modifiers/randomize_transforms.gd" ),"enabled":true,"display_name":"Randomize Transforms","category":"Edit","warning":"","warning_ignore_no_transforms":false,"warning_ignore_no_path":true,"override_global_seed":false,"custom_seed":0,"local_space":true,"position":Vector3( 0.1, 0.1, 0.1 ),"rotation":Vector3( 5, 360, 5 ),"scale":Vector3( 1, 1, 1 ),"_rng":Object(RandomNumberGenerator,"seed":2725799953364770839,"state":1629781496755605418,"script":null)
)
],"just_created":false,"undo_redo":Object(UndoRedo,"script":null)
)


[node name="ScatterItem" type="Spatial" parent="."]
script = ExtResource( 2 )
materials = [ ExtResource( 3 ) ]
materials = [ ]
merge_target_meshes = false
cast_shadow = 1
5 changes: 3 additions & 2 deletions addons/scatter/src/common/poisson_disc_sampling.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

tool
extends Reference

var rng: RandomNumberGenerator
Expand Down Expand Up @@ -148,8 +149,8 @@ func _init_vars() -> void:
push_error("Unrecognized shape!!! Please input a valid shape")

_cell_size = _radius / sqrt(2)
_cols = max(floor(_sample_region_rect.size.x / _cell_size), 1)
_rows = max(floor(_sample_region_rect.size.y / _cell_size), 1)
_cols = int(max(floor(_sample_region_rect.size.x / _cell_size), 1))
_rows = int(max(floor(_sample_region_rect.size.y / _cell_size), 1))
# scale the cell size in each axis
_cell_size_scaled.x = _sample_region_rect.size.x / _cols
_cell_size_scaled.y = _sample_region_rect.size.y / _rows
Expand Down
8 changes: 7 additions & 1 deletion addons/scatter/src/common/util.gd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ static func create_mesh_from(mesh_instances: Array) -> Mesh:
arrays[ArrayMesh.ARRAY_VERTEX][j] = pos

array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
array_mesh.surface_set_material(total_surfaces, mesh.surface_get_material(i))

# Retrieve the material on the MeshInstance first, if none is defined,
# use the one from the mesh resource.
var material = mi.get_surface_material(i)
if not material:
material = mesh.surface_get_material(i)
array_mesh.surface_set_material(total_surfaces, material)

total_surfaces += 1

Expand Down
26 changes: 20 additions & 6 deletions addons/scatter/src/core/scatter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export var use_instancing := true setget _set_instancing
export var disable_updates_in_game := true
export var force_update_when_loaded := true
export var make_children_unselectable := true
export var preview_count := -1

var modifier_stack setget _set_modifier_stack
var undo_redo setget _set_undo_redo
var is_moving := false

var _transforms
var _items := []
Expand All @@ -23,6 +25,12 @@ func _ready() -> void:
_ensure_stack_exists()
_discover_items()

if _items.empty():
var item = Scatter.ScatterItem.new()
add_child(item)
item.set_owner(get_tree().get_edited_scene_root())
item.set_name("ScatterItem")

if force_update_when_loaded:
yield(get_tree(), "idle_frame")
_do_update()
Expand Down Expand Up @@ -63,7 +71,7 @@ func _get(property):
return null


func _set(property, value):
func _set(property, _value):
if not Engine.editor_hint:
return false

Expand Down Expand Up @@ -96,8 +104,15 @@ func _do_update() -> void:

_discover_items()
if not _items.empty():
_transforms = Scatter.Transforms.new()
_transforms.set_path(self)
if not _transforms:
_transforms = Scatter.Transforms.new()
_transforms.set_path(self)

_transforms.clear()
if is_moving:
_transforms.max_count = preview_count
else:
_transforms.max_count = -1

if use_instancing:
modifier_stack.update(_transforms, global_seed)
Expand Down Expand Up @@ -204,8 +219,7 @@ func _create_instance(item, root):

func _delete_duplicates():
for item in _items:
if item.has_node("Duplicates"):
item.get_node("Duplicates").queue_free()
item.delete_duplicates()


func _create_multimesh() -> void:
Expand Down Expand Up @@ -330,7 +344,7 @@ func _set_undo_redo(val) -> void:


func _set_modifier_stack(val) -> void:
if not is_instance_valid(val):
if not val or not is_instance_valid(val):
return

if modifier_stack:
Expand Down
1 change: 1 addition & 0 deletions addons/scatter/src/core/scatter_exclude_path.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends "scatter_path.gd"


func _ready():
# warning-ignore:return_value_discarded
connect("curve_updated", self, "update")


Expand Down
26 changes: 12 additions & 14 deletions addons/scatter/src/core/scatter_item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,15 @@ func get_multimesh_instance() -> MultiMeshInstance:


func delete_multimesh() -> void:
var mmi = get_multimesh_instance()
if mmi:
mmi.queue_free()
for c in get_children():
if c is MultiMeshInstance:
c.queue_free()


func delete_duplicates() -> void:
for c in get_children():
if c.name.begins_with("Duplicates"):
c.queue_free()


func update_shadows() -> void:
Expand All @@ -172,17 +178,6 @@ func update_shadows() -> void:
return

mmi.cast_shadow = cast_shadow
return

match cast_shadow:
0:
mmi.cast_shadows = GeometryInstance.SHADOW_CASTING_SETTING_OFF
1:
mmi.cast_shadows = GeometryInstance.SHADOW_CASTING_SETTING_ON
2:
mmi.cast_shadows = GeometryInstance.SHADOW_CASTING_SETTING_DOUBLE_SIDED
3:
mmi.cast_shadows = GeometryInstance.SHADOW_CASTING_SETTING_SHADOWS_ONLY


func _get_mesh_from_scene(node):
Expand Down Expand Up @@ -250,6 +245,9 @@ func _restore_multimesh_materials() -> void:
return

var mesh: Mesh = mmi.multimesh.mesh
if not mesh:
return

var surface_count = mesh.get_surface_count()

if not mesh or surface_count > materials.size():
Expand Down
53 changes: 26 additions & 27 deletions addons/scatter/src/core/scatter_path.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extends Path
signal curve_updated


export var bake_interval := 1.0 setget _set_bake_interval
export var curve_tolerance_degrees := 5.0 setget _set_curve_tolerance_degrees

var polygon : PolygonPathFinder
var baked_points : PoolVector3Array
Expand All @@ -20,7 +20,8 @@ var _previous_transform: Transform

func _ready():
set_notify_transform(true)
self.connect("curve_changed", self, "_on_curve_changed")
# warning-ignore:return_value_discarded
connect("curve_changed", self, "_on_curve_changed")
_update_from_curve()


Expand Down Expand Up @@ -77,7 +78,6 @@ func _update_from_curve():
bounds_min = null
var connections = PoolIntArray()
var polygon_points = PoolVector2Array()
baked_points = PoolVector3Array()

if not curve:
curve = Curve3D.new()
Expand All @@ -89,17 +89,16 @@ func _update_from_curve():
if not polygon:
polygon = PolygonPathFinder.new()

var length: float = curve.get_baked_length()
var steps := int(max(3, round(length / bake_interval)))
baked_points = curve.tessellate(4, curve_tolerance_degrees)

for i in steps:
# Get a point on the curve
var coords_3d = curve.interpolate_baked((float(i) / (steps - 2)) * length)
var coords = _get_projected_coords(coords_3d)
var steps := baked_points.size()

for i in baked_points.size():
var point = baked_points[i]
var projected_point = _get_projected_coords(point)

# Store polygon data
baked_points.append(coords_3d)
polygon_points.append(coords)
polygon_points.push_back(projected_point)
connections.append(i)
if i == steps - 1:
connections.append(0)
Expand All @@ -108,21 +107,21 @@ func _update_from_curve():

# Check for bounds
if i == 0:
bounds_min = coords_3d
bounds_max = coords_3d
bounds_min = point
bounds_max = point
else:
if coords_3d.x > bounds_max.x:
bounds_max.x = coords_3d.x
if coords_3d.x < bounds_min.x:
bounds_min.x = coords_3d.x
if coords_3d.y > bounds_max.y:
bounds_max.y = coords_3d.y
if coords_3d.y < bounds_min.y:
bounds_min.y = coords_3d.y
if coords_3d.z > bounds_max.z:
bounds_max.z = coords_3d.z
if coords_3d.z < bounds_min.z:
bounds_min.z = coords_3d.z
if point.x > bounds_max.x:
bounds_max.x = point.x
if point.x < bounds_min.x:
bounds_min.x = point.x
if point.y > bounds_max.y:
bounds_max.y = point.y
if point.y < bounds_min.y:
bounds_min.y = point.y
if point.z > bounds_max.z:
bounds_max.z = point.z
if point.z < bounds_min.z:
bounds_min.z = point.z

polygon.setup(polygon_points, connections)
size = Vector3(bounds_max.x - bounds_min.x, bounds_max.z - bounds_min.z, bounds_max.z - bounds_min.z)
Expand All @@ -131,8 +130,8 @@ func _update_from_curve():
emit_signal("curve_updated")


func _set_bake_interval(val) -> void:
bake_interval = val
func _set_curve_tolerance_degrees(val) -> void:
curve_tolerance_degrees = val
_update_from_curve()


Expand Down
10 changes: 9 additions & 1 deletion addons/scatter/src/core/transforms.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ extends Reference


var list := []
var path # ScatterPath object
var path setget set_path
var max_count := -1


func add(count: int) -> void:
Expand All @@ -19,12 +20,19 @@ func remove(count: int) -> void:


func resize(count: int) -> void:
if max_count >= 0:
count = int(min(count, max_count))

var size = list.size()
if count > size:
add(count - size)
else:
remove(size - count)


func clear() -> void:
list = []


func set_path(p: Path) -> void:
path = p
5 changes: 2 additions & 3 deletions addons/scatter/src/modifiers/add_around_point_grid.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ func _process_transforms(transforms, global_seed) -> void:
var size = bounds_max - bounds_min
var half_size = size * 0.5
var center: Vector3 = bounds_min + half_size
var width: int = ceil(size.x / x_spacing)
var length: int = ceil(size.z / z_spacing)
var width := int(ceil(size.x / x_spacing))
var length := int(ceil(size.z / z_spacing))
var max_count: int = width * length
var gt = transforms.path.get_global_transform()

for i in max_count:
var pos = Vector3.ZERO
Expand Down
3 changes: 2 additions & 1 deletion addons/scatter/src/modifiers/base_modifier.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tool
extends Node


Expand Down Expand Up @@ -45,7 +46,7 @@ func process_transforms(transforms, global_seed) -> void:


# Override in inherited class
func _process_transforms(transforms, global_seed) -> void:
func _process_transforms(_transforms, _global_seed) -> void:
pass


Expand Down
2 changes: 1 addition & 1 deletion addons/scatter/src/modifiers/base_point_modifier.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func is_inside(pos: Vector3, p) -> bool:
return distance_to_point <= max_distance


func _process_transforms(transforms, global_seed) -> void:
func _process_transforms(transforms, _global_seed) -> void:
if node_name.empty():
warning += "You must select a node for this modifier to work."
_notify_warning_changed()
Expand Down
Loading

0 comments on commit 19f2a6b

Please sign in to comment.