Skip to content

Commit

Permalink
add: cast shadow option in ScatterItem nodes, fix: bitmask now suppor…
Browse files Browse the repository at this point in the history
…t 32 layers
  • Loading branch information
HungryProton committed Sep 24, 2021
1 parent e43e820 commit 59c15c1
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 115 deletions.
2 changes: 1 addition & 1 deletion 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.7.6"
version="2.8.0"
script="plugin.gd"
8 changes: 7 additions & 1 deletion src/core/scatter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export var disable_updates_in_game := true
export var force_update_when_loaded := true
export var make_children_unselectable := true


var modifier_stack setget _set_modifier_stack
var undo_redo setget _set_undo_redo

Expand Down Expand Up @@ -58,6 +57,7 @@ func _get_property_list() -> Array:
type = TYPE_OBJECT,
hint_string = "ScatterModifierStack",
})

return list


Expand Down Expand Up @@ -246,6 +246,7 @@ func _create_multimesh() -> void:
offset += count


# TODO: Move this to scatter_item.gd?
func _setup_multi_mesh(item, count):
var instance: MultiMeshInstance = item.get_multimesh_instance()
if not instance:
Expand All @@ -257,6 +258,7 @@ func _setup_multi_mesh(item, count):
instance.multimesh = MultiMesh.new()

instance.translation = Vector3.ZERO
item.update_shadows()

var mesh_instance: MeshInstance = item.get_mesh_instance()
if not mesh_instance:
Expand Down Expand Up @@ -328,6 +330,10 @@ func _set_instancing(val: bool) -> void:
_delete_duplicates()
else:
_delete_multimeshes()

for item in _items:
item.use_instancing = val

update()


Expand Down
85 changes: 68 additions & 17 deletions src/core/scatter_item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ tool
extends Spatial


export(int) var proportion : int = 100 setget _set_proportion
export(NodePath) var local_item_path setget _set_local_path
export(String, FILE) var item_path : String setget _set_path
export(float) var scale_modifier : float = 1.0 setget _set_scale_modifier
export(bool) var ignore_initial_position := true setget _set_ignore_pos
export(bool) var ignore_initial_rotation := true setget _set_ignore_rot
export(bool) var ignore_initial_scale := true setget _set_ignore_scale
export var proportion := 100 setget _set_proportion
export var local_item_path: NodePath setget _set_local_path
export(String, FILE) var item_path setget _set_path
export var scale_modifier := 1.0 setget _set_scale_modifier
export var ignore_initial_position := true setget _set_ignore_pos
export var ignore_initial_rotation := true setget _set_ignore_rot
export var ignore_initial_scale := true setget _set_ignore_scale

var use_instancing := true setget _set_use_instancing
var cast_shadow := 1 setget _set_cast_shadow

var initial_position: Vector3
var initial_rotation: Vector3
Expand All @@ -22,6 +25,7 @@ var _parent
func _ready():
_parent = get_parent()
_restore_multimesh_materials()
use_instancing = _parent.use_instancing


func _get_configuration_warning() -> String:
Expand All @@ -35,11 +39,29 @@ func _get_configuration_warning() -> String:


func _get_property_list() -> Array:
return [{
var list = []

list.push_back({
"name": "materials",
"type": TYPE_ARRAY,
"usage": PROPERTY_USAGE_STORAGE
}]
})

if use_instancing: # Only display the option is instancing is used
list.push_back({
"name": "cast_shadow",
"type": TYPE_INT,
"hint": PROPERTY_HINT_ENUM,
"hint_string": "Off,On,Double-Sided,Shadows Only"
})
else: # but store the previous value if it's not
list.push_back({
"name": "cast_shadow",
"type": TYPE_INT,
"usage": PROPERTY_USAGE_STORAGE
})

return list


func _set(property, _value):
Expand Down Expand Up @@ -132,6 +154,25 @@ func delete_multimesh() -> void:
mmi.queue_free()


func update_shadows() -> void:
var mmi: MultiMeshInstance = get_multimesh_instance()
if not mmi:
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):
if node is MeshInstance:
return node
Expand Down Expand Up @@ -159,7 +200,7 @@ func _save_initial_data(mesh: MeshInstance) -> void:
materials.append(mesh.get_surface_material(i))


func _restore_multimesh_materials():
func _restore_multimesh_materials() -> void:
var mmi := get_multimesh_instance()
if not mmi:
return
Expand All @@ -174,36 +215,46 @@ func _restore_multimesh_materials():
mesh.surface_set_material(i, materials[i])


func _set_proportion(val):
func _set_proportion(val: int) -> void:
proportion = val
update()


func _set_path(val):
func _set_path(val: String) -> void:
item_path = val
update()


func _set_scale_modifier(val):
func _set_scale_modifier(val: float) -> void:
scale_modifier = val
update()


func _set_local_path(val):
func _set_local_path(val: NodePath) -> void:
local_item_path = val
update()


func _set_ignore_pos(val):
func _set_ignore_pos(val: bool) -> void:
ignore_initial_position = val
update()


func _set_ignore_rot(val):
func _set_ignore_rot(val: bool) -> void:
ignore_initial_rotation = val
update()


func _set_ignore_scale(val):
func _set_ignore_scale(val: bool) -> void:
ignore_initial_scale = val
update()


func _set_use_instancing(val: bool) -> void:
use_instancing = val
property_list_changed_notify()


func _set_cast_shadow(val: int) -> void:
cast_shadow = val
update_shadows()
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ margin_right = 14.0
margin_bottom = 14.0
rect_min_size = Vector2( 14, 14 )
focus_mode = 0
size_flags_horizontal = 3
custom_styles/hover = SubResource( 1 )
custom_styles/pressed = SubResource( 2 )
custom_styles/normal = SubResource( 3 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@ extends "base_parameter.gd"


onready var _label: Label = $Label
onready var _root: Control = $MarginContainer/HBoxContainer/GridContainer
onready var _grid_1: Control = $MarginContainer/HBoxContainer/VBoxContainer/HBoxContainer/GridContainer1
onready var _grid_2: Control = $MarginContainer/HBoxContainer/VBoxContainer/HBoxContainer/GridContainer2
onready var _grid_3: Control = $MarginContainer/HBoxContainer/VBoxContainer/HBoxContainer2/GridContainer3
onready var _grid_4: Control = $MarginContainer/HBoxContainer/VBoxContainer/HBoxContainer2/GridContainer4
onready var _menu_button: MenuButton = $MarginContainer/HBoxContainer/MenuButton

var _buttons: Array
var _popup: PopupMenu
var _layer_count := 32


func _ready() -> void:
for c in _root.get_children():
if c is Button:
_buttons.push_front(c)
c.connect("pressed", self, "_on_button_pressed")
_buttons = []
var grids = [_grid_1, _grid_2, _grid_3, _grid_4]
for g in grids:
for c in g.get_children():
if c is Button:
_buttons.push_front(c)
c.connect("pressed", self, "_on_button_pressed")

_popup = _menu_button.get_popup()
_popup.clear()

var layer_name := ""
for i in 20:
if i != 0 and i % 5 == 0:
for i in _layer_count:
if i != 0 and i % 4 == 0:
_popup.add_separator("", 100 + i)

layer_name = ProjectSettings.get_setting("layer_names/3d_physics/layer_" + String(i + 1))
if layer_name.empty():
layer_name = "Layer " + String(i + 1)
_popup.add_check_item(layer_name, 19 - i)
_popup.add_check_item(layer_name, _layer_count - 1 - i)

_sync_popup_state()
_popup.connect("id_pressed", self, "_on_id_pressed")
Expand All @@ -41,12 +48,12 @@ func _set_value(val: String) -> void:
var binary_string: String = _dec2bin(int(val))
var length = binary_string.length()

if length < 20:
binary_string = binary_string.pad_zeros(20)
elif length > 20:
binary_string = binary_string.substr(length - 20, length)
if length < _layer_count:
binary_string = binary_string.pad_zeros(_layer_count)
elif length > _layer_count:
binary_string = binary_string.substr(length - _layer_count, length)

for i in 20:
for i in _layer_count:
_buttons[i].pressed = binary_string[i] == "1"

_sync_popup_state()
Expand Down Expand Up @@ -90,7 +97,7 @@ func _sync_popup_state() -> void:
if not _popup:
return

for i in 20:
for i in _layer_count:
var idx = _popup.get_item_index(i)
_popup.set_item_checked(idx, _buttons[i].pressed)

Expand All @@ -102,14 +109,15 @@ func _on_button_pressed() -> void:

func _on_id_pressed(id: int) -> void:
var idx = _popup.get_item_index(id)
print("id ", id, " - idx ", idx)
var checked = not _popup.is_item_checked(idx)
_buttons[id].pressed = checked
_popup.set_item_checked(idx, checked)
_on_button_pressed()


func _on_enable_all_pressed() -> void:
_set_value("1048575")
_set_value("4294967295")
_on_value_changed(null)


Expand Down
Loading

0 comments on commit 59c15c1

Please sign in to comment.