Skip to content

Commit

Permalink
Center the image in 3D (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood authored Feb 5, 2021
1 parent 89d763b commit a34318f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Proc3D.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
[node name="Proc3D" type="Spatial"]

[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2.18523 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.681981, 3.2168 )
fov = 92.2227

[node name="SpatialTree" type="Spatial" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.533641, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29621, 0 )
script = ExtResource( 1 )

[node name="NativeHelp" type="Spatial" parent="SpatialTree"]
script = ExtResource( 2 )
base/axiom = "X"
base/delta = 22.5
base/stroke_length = 10.0
base/rules = "F:FF-[-F+F+F]+[+F-F-F]"
base/axiom = "F"
base/n = 4
base/stroke_width = 4.0
base/n = 7
base/delta = 25.7
base/rules = "X:F[+X][-X]FX;F:FF"

[node name="Tween" type="Tween" parent="SpatialTree"]
repeat = true
Expand Down
44 changes: 36 additions & 8 deletions SpatialTree.gd
Original file line number Diff line number Diff line change
@@ -1,41 +1,69 @@
extends Spatial

func _ready():
var img:Image = $NativeHelp.make_image()
if img == null:
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 translate_x = 0.5 - _guess_center_along_bottom(img)

var resize_y_ratio = 1.0 * img.get_height() / img.get_width()

for rotate_y in [0, 90]:
_make_opposite_faces(img, resize_y_ratio, rotate_y)
_make_opposite_faces(img, resize_y_ratio, rotate_y, translate_x)

$Tween.interpolate_property(self,"rotation_degrees:y", 0, 360, 4, Tween.TRANS_LINEAR)
$Tween.start()

func _make_opposite_faces(img: Image, resize_y_ratio: float, rot_y: float):
func _make_opposite_faces(img: Image, resize_y_ratio: float, rot_y: float, translate_x: float):
var tex = ImageTexture.new()
tex.create_from_image(img)

var first_sm = SpatialMaterial.new()
first_sm.albedo_texture = tex
first_sm.flags_transparent = true

var first_face = _make_mesh_instance(first_sm, resize_y_ratio, rot_y)
var first_face = _make_mesh_instance(first_sm, resize_y_ratio, rot_y, translate_x)
add_child(first_face)

var second_face = _make_mesh_instance(first_sm, resize_y_ratio, rot_y, true)
var second_face = _make_mesh_instance(first_sm, resize_y_ratio, rot_y, translate_x, true)
add_child(second_face)

func _make_mesh_instance(spatial_mat: SpatialMaterial, resize_y_ratio: float, rot_y: float, flip_faces: bool = false):
func _make_mesh_instance(spatial_mat: SpatialMaterial, resize_y_ratio: float, rot_y: float, translate_x: float, flip_faces: bool = false) -> MeshInstance:
var pm = PlaneMesh.new()
pm.size = Vector2(pm.size.x, pm.size.y * resize_y_ratio)
var size = Vector2(pm.size.x, pm.size.y * resize_y_ratio)
pm.size = size
var mi = MeshInstance.new()
pm.material = spatial_mat
mi.mesh = pm
mi.rotate_x(deg2rad(90))
mi.rotate_y(deg2rad(rot_y))
mi.translate(Vector3(translate_x * size.x, 0, 0))
if flip_faces:
pm.flip_faces = true
return mi


const DEFAULT_CENTER = 0.5
# this is a poor approximation of what the "center"
# of a tree image is assumed to be: the first pixel
# which is NOT transparent, along the final row in the
# image. the answer is returned as a percentage value
func _guess_center_along_bottom(img: Image) -> float:
if img.is_empty():
return DEFAULT_CENTER
var w = img.get_width()
var last_row = img.get_height() - 1
var found_col = 0

img.lock()
for x in range(0,w):
var c = img.get_pixel(x,last_row)
if c.a != 0:
found_col = x
break
img.unlock()

return found_col * 1.0 / w * 1.0
5 changes: 0 additions & 5 deletions gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ This rust library provides functionality to draw L-System
Trees in 2D, and to expose the ImageTexture that will be
assigned to various meshes made in 3D.

## trivia

We might have written the 3D mesh-assignment using rust
bindings, but it was hard to understand how to manipulate
albedo texture on a [spatial material](https://docs.rs/gdnative/0.9.2/gdnative/api/struct.SpatialMaterial.html).
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _global_script_class_icons={
[application]

config/name="forest"
run/main_scene="res://Forest.tscn"
run/main_scene="res://Proc3D.tscn"
config/icon="res://icon.png"

[display]
Expand Down

0 comments on commit a34318f

Please sign in to comment.