Skip to content

Commit

Permalink
scattershapes now using collections
Browse files Browse the repository at this point in the history
  • Loading branch information
daleblackwood committed Feb 18, 2023
1 parent c19af30 commit 165da04
Show file tree
Hide file tree
Showing 8 changed files with 897 additions and 800 deletions.
6 changes: 3 additions & 3 deletions addons/goshapes/MeshShaper/BlockShaper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extends Shaper
var watcher_cap := ResourceWatcher.new(mark_dirty)

## The Shaper used for the building the cap
@export var cap_shaper: Resource:
@export var cap_shaper: CapShaper:
set(value):
cap_shaper = value
watcher_cap.watch(cap_shaper)
Expand All @@ -16,7 +16,7 @@ var watcher_cap := ResourceWatcher.new(mark_dirty)
var watcher_wall := ResourceWatcher.new(mark_dirty)

## The Shaper used for the building the walls
@export var wall_shaper: Resource:
@export var wall_shaper: WallShaper:
set(value):
wall_shaper = value
watcher_wall.watch(wall_shaper)
Expand All @@ -26,7 +26,7 @@ var watcher_wall := ResourceWatcher.new(mark_dirty)
var watcher_bottom := ResourceWatcher.new(mark_dirty)

## The Shaper used for the building the base
@export var bottom_shaper: Resource:
@export var bottom_shaper: BottomShaper:
set(value):
bottom_shaper = value
watcher_bottom.watch(bottom_shaper)
Expand Down
44 changes: 44 additions & 0 deletions addons/goshapes/ShatterShaper/ScatterCollection.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@tool
class_name ScatterCollection
extends SceneSource
## Multiple scene item collections

## the scene objects (prefabs) to fetch
@export var prefabs: Array[PackedScene] = []:
set(value):
prefabs = value
is_dirty = true
emit_changed()


## the weights to bias which is selected
@export var weights: Array[int] = []:
set(value):
weights = value
is_dirty = true
emit_changed()


var is_dirty = false
var total_weight = 0


func get_resource(pick: int = -1) -> PackedScene:
if is_dirty:
total_weight = 0
for i in range(prefabs.size()):
total_weight += 1 if i >= weights.size() else weights[i]
if pick < 0:
pick = randi_range(0, total_weight)
else:
pick = pick % total_weight
var w := 0
for i in range(prefabs.size()):
w += 1 if i >= weights.size() else weights[i]
if w >= pick:
return prefabs[i]
return null


func has_resource() -> bool:
return prefabs.size() > 0
20 changes: 20 additions & 0 deletions addons/goshapes/ShatterShaper/ScatterItem.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@tool
class_name ScatterItem
extends SceneSource
## A single scene item

## the scene objects (prefabs) to fetch
@export var prefab: PackedScene = null:
set(value):
prefab = value
emit_changed()


func get_resource(pick: int = 0) -> PackedScene:
return prefab


func has_resource() -> bool:
return prefab != null


19 changes: 14 additions & 5 deletions addons/goshapes/ShatterShaper/ScatterShaper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ extends Shaper
# An upper limit for instance counts for protection
const INSTANCE_CAP = 2000

var watcher_scene_source := ResourceWatcher.new(emit_changed)

## The object to scatter about
@export var object: PackedScene = null:
@export var scene_source: SceneSource = null:
set(value):
if object != value:
object = value
if scene_source != value:
scene_source = value
watcher_scene_source.watch(scene_source)
emit_changed()


Expand Down Expand Up @@ -99,6 +102,8 @@ var watcher_noise := ResourceWatcher.new(emit_changed)

func _init():
super._init()
if not scene_source:
scene_source = ScatterItem.new()
watcher_noise.watch(noise)


Expand All @@ -114,6 +119,9 @@ class ShatterBuilder extends ShapeBuilder:


func build(host: Node3D, path: PathData) -> void:
if not style.scene_source or not style.scene_source.has_resource():
printerr("No scene(s) attached to ScatterShaper.")
return
var rng = RandomNumberGenerator.new()
rng.seed = style.seed
var curve = path.curve
Expand All @@ -128,7 +136,6 @@ class ShatterBuilder extends ShapeBuilder:
max_z = maxf(max_z, p.z)
var inc = style.spread
var density = style.density
var object = style.object
var place_on_ground = style.place_on_ground
var random_angle = style.random_angle
var scale_variance = style.scale_variance
Expand All @@ -141,7 +148,6 @@ class ShatterBuilder extends ShapeBuilder:
polygon.set(i, Vector2(path.points[i].x, path.points[i].z))
var x = min_x
var instances = 0
var object_name = ResourceUtils.get_type(object)
var collision_layer = style.collision_layer
var placement_mask = path.placement_mask
while x < max_x:
Expand All @@ -152,6 +158,7 @@ class ShatterBuilder extends ShapeBuilder:
if instances > INSTANCE_CAP:
printerr("Exceeded %d scatter instance cap" % INSTANCE_CAP)
return
var r_inst = rng.randi()
var r_density = rng.randf()
var r_x = rng.randf()
var r_z = rng.randf()
Expand Down Expand Up @@ -181,6 +188,8 @@ class ShatterBuilder extends ShapeBuilder:
continue
else:
pos.y = curve.get_closest_point(pos).y
var object = style.scene_source.get_resource()
var object_name = ResourceUtils.get_type(object)
var inst = object.instantiate()
instances += 1
inst.name = "%s%d" % [object_name, instances]
Expand Down
11 changes: 11 additions & 0 deletions addons/goshapes/base/SceneSource.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@tool
class_name SceneSource
extends Resource
# A base type for fetching scenes

func get_resource(pick: int = 0) -> PackedScene:
return null


func has_resource() -> bool:
return false
1,388 changes: 701 additions & 687 deletions addons/goshapes/samples/SampleIsland.tscn

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion addons/goshapes/samples/SampleLineShape.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ ground_placement_mask = 0

[sub_resource type="Resource" id="Resource_ex5rf"]
script = ExtResource("12_4qbsa")
object = ExtResource("12_b8nom")
density = 0.485
spread = 8.0
seed = 0
Expand Down
208 changes: 104 additions & 104 deletions addons/goshapes/samples/SampleMountain.tscn

Large diffs are not rendered by default.

0 comments on commit 165da04

Please sign in to comment.