Skip to content

Commit

Permalink
Add image cache layer to SpatialTree.gd (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood authored Feb 7, 2021
1 parent b7c681a commit 738448e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# next: v0.6.0

- Add image cache layer to SpatialTree.gd (#47, #48)

# v0.5.1

- Raise the camera in TheWoods (#46)
Expand Down
4 changes: 4 additions & 0 deletions ImageCache.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extends Node

# hash(TreeParams.id()) -> Image
var cache = {}
50 changes: 38 additions & 12 deletions SpatialTree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@ export var axiom = "F"
export var n = 4
export var delta = 22.5
export var rules = "F:FF-[-F+F+F]+[+F-F-F]"
export var image_cache_path: NodePath

const TreeParams = preload("res://TreeParams.gd")

func _ready():
$NativeHelp.set("base/rules", rules)
$NativeHelp.set("base/axiom", axiom)
$NativeHelp.set("base/n", n)
$NativeHelp.set("base/delta", delta)
$NativeHelp.set("base/stroke_width", stroke_width)
$NativeHelp.set("base/stroke_length", stroke_length)

var img_with_blank_space:Image = $NativeHelp.make_image()
if img_with_blank_space == null:
printerr("image wasn't created")
return
var img = img_with_blank_space.get_rect(img_with_blank_space.get_used_rect())
var make_image_start_time = OS.get_ticks_msec()
var img = _cached_make_image()
print("make image took %d" % (OS.get_ticks_msec() - make_image_start_time))

var translate_x = 0.5 - _guess_center_along_bottom(img)

Expand Down Expand Up @@ -79,3 +73,35 @@ func _guess_center_along_bottom(img: Image) -> float:
img.unlock()

return found_col * 1.0 / w * 1.0

func _cached_make_image():
var params = TreeParams.new()
params.stroke_length = stroke_length
params.stroke_width = stroke_width
params.n = n
params.delta = delta
params.axiom = axiom
params.rules = rules

var params_id = params.id()
var cache_node = get_node_or_null(image_cache_path)
if cache_node and cache_node.cache and cache_node.cache.has(params_id):
return cache_node.cache[params_id]
else:
$NativeHelp.set("base/rules", rules)
$NativeHelp.set("base/axiom", axiom)
$NativeHelp.set("base/n", n)
$NativeHelp.set("base/delta", delta)
$NativeHelp.set("base/stroke_width", stroke_width)
$NativeHelp.set("base/stroke_length", stroke_length)

var make_image_start_time = OS.get_ticks_msec()

var img_with_blank_space:Image = $NativeHelp.make_image()
if img_with_blank_space == null:
printerr("image wasn't created")
return
var final_img = img_with_blank_space.get_rect(img_with_blank_space.get_used_rect())
if cache_node:
cache_node.cache[params_id] = final_img
return final_img
17 changes: 8 additions & 9 deletions TheWoods.gd
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
extends Spatial

const SpatialTree = preload("res://SpatialTree.tscn")
const TreeParams = preload("res://TreeParams.gd")
const ImageCache = preload("res://ImageCache.gd")

const ROWS = 3
const ROWS = 10
const COLS = 10
const SPACING = 4

class TreeParams:
var stroke_width = 2.0
var stroke_length = 4.0
var axiom = "F"
var n = 4
var delta = 22.5
var rules = "F:FF-[-F+F+F]+[+F-F-F]"

var _samples = []

func _make_samples():
Expand Down Expand Up @@ -61,10 +55,15 @@ func _make_samples():

func _ready():
_make_samples()
var image_cache = ImageCache.new()
image_cache.name = "ImageCache"
add_child(image_cache)
var tree_image_cache_path = NodePath("%s/%s" % [get_path() , "/ImageCache"])

for i in range(0,ROWS):
for j in range(0, COLS):
var tree = SpatialTree.instance()
tree.image_cache_path = tree_image_cache_path
var sample = _samples[randi()%_samples.size()]
tree.n = sample.n
tree.rules = sample.rules
Expand Down
11 changes: 11 additions & 0 deletions TreeParams.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends Resource

var stroke_width = 2.0
var stroke_length = 4.0
var axiom = "F"
var n = 4
var delta = 22.5
var rules = "F:FF-[-F+F+F]+[+F-F-F]"

func id() -> String:
return "SW:%f_SL:%f_N:%d_D:%f_A:%s_R:%s" % [stroke_width, stroke_length, n, delta, axiom, rules]

0 comments on commit 738448e

Please sign in to comment.