2021-11-18 17:23:37 +01:00
tool
extends Node2D
var image : = Image . new ( )
var table : = ImageTexture . new ( )
2021-11-25 18:26:56 +01:00
onready var Util = preload ( " res://addons/summer_day/utilities/math_helper.gd " )
2021-11-18 17:23:37 +01:00
var corners = [
Vector2 ( - 0.4 , - 0.4 ) ,
Vector2 ( 0.1 , - 0.4 ) ,
Vector2 ( 0.1 , 0.1 ) ,
Vector2 ( - 0.4 , 0.1 ) ,
Vector2 ( - 0.1 , - 0.1 ) ,
Vector2 ( 0.4 , - 0.1 ) ,
Vector2 ( 0.4 , 0.4 ) ,
Vector2 ( - 0.1 , 0.4 ) ,
]
var picked_point = - 1
var rear_matrix : = [ ]
var front_matrix : = [ ]
var canvas_inv : Transform2D
var canvas_trans : Transform2D
# Called when the node enters the scene tree for the first time.
func _ready ( ) :
update_canvas_inv ( )
image . create ( 8 , 1 , false , Image . FORMAT_RGBAF )
table . create_from_image ( image , 0 )
#table.create(8, 1, Image.FORMAT_RGBAF, 0)
#image = table.get_data()
image . lock ( )
for i in range ( 8 ) :
image . set_pixel ( i , 0 ,
Color ( 1.0 , 0.0 , 0.0 ) )
image . unlock ( )
table . set_data ( image )
$ " ../Sprite " . texture = table
$ " ../CSGBox " . material . set_shader_param ( " table " , table )
_update_matrix ( )
func update_canvas_inv ( ) :
while true :
var viewport_size = get_viewport_rect ( ) . size
canvas_inv = Transform2D (
Vector2 ( 2.0 / viewport_size . x , 0.0 ) ,
Vector2 ( 0.0 , - 2.0 / viewport_size . y ) ,
Vector2 ( - 1.0 , 1.0 )
)
canvas_trans = canvas_inv . affine_inverse ( )
update ( )
yield ( get_viewport ( ) , " size_changed " )
func _input ( event ) :
var mouse_position = get_viewport ( ) . get_mouse_position ( )
if event is InputEventMouseButton :
if event . button_index == BUTTON_LEFT :
if event . pressed :
_pick_point ( canvas_inv . xform ( mouse_position ) )
else :
picked_point = - 1
if event is InputEventMouseMotion :
if picked_point != - 1 :
corners [ picked_point ] = canvas_inv . xform ( mouse_position )
_update_matrix ( )
update ( )
func _draw ( ) :
for i in range ( 4 ) :
draw_line (
2021-12-12 17:04:17 +01:00
corners [ i ] ,
corners [ wrapi ( i + 1 , 0 , 4 ) ] ,
2021-11-18 17:23:37 +01:00
Color . maroon )
for i in range ( 4 ) :
2021-12-12 17:04:17 +01:00
draw_circle ( corners [ i ] , 0.01 , Color . maroon )
2021-11-18 17:23:37 +01:00
for i in range ( 4 ) :
draw_line (
2021-12-12 17:04:17 +01:00
corners [ i ] ,
corners [ i + 4 ] ,
2021-11-18 17:23:37 +01:00
Color . yellow )
for i in range ( 4 ) :
draw_line (
2021-12-12 17:04:17 +01:00
corners [ i + 4 ] ,
corners [ wrapi ( i + 5 , 4 , 8 ) ] ,
2021-11-18 17:23:37 +01:00
Color . purple )
for i in range ( 4 ) :
2021-12-12 17:04:17 +01:00
draw_circle ( corners [ i + 4 ] , 0.01 , Color . purple )
2021-11-18 17:23:37 +01:00
draw_texture_rect ( table , Rect2 (
2021-12-12 17:04:17 +01:00
Vector2 ( 450.0 , 50.0 ) , Vector2 ( 256.0 , 32.0 )
2021-11-18 17:23:37 +01:00
) , false )
func _pick_point ( mouse_pos ) :
var picked_point_so_far = - 1
var nearest_distance = 20.0
for i in corners . size ( ) :
var distance = corners [ i ] . distance_to ( mouse_pos )
if distance < nearest_distance :
nearest_distance = distance
picked_point_so_far = i
picked_point = picked_point_so_far
func _update_matrix ( ) :
rear_matrix = Util . plane_3d_to_xy (
Vector3 ( - 1.0 , - 1.0 , 1.0 ) ,
Vector3 ( 1.0 , - 1.0 , 1.0 ) ,
Vector3 ( 1.0 , 1.0 , 1.0 ) ,
Vector3 ( - 1.0 , 1.0 , 1.0 ) ,
corners [ 0 ] , corners [ 1 ] , corners [ 2 ] , corners [ 3 ]
)
front_matrix = Util . plane_3d_to_xy (
Vector3 ( - 1.0 , - 1.0 , - 1.0 ) ,
Vector3 ( 1.0 , - 1.0 , - 1.0 ) ,
Vector3 ( 1.0 , 1.0 , - 1.0 ) ,
Vector3 ( - 1.0 , 1.0 , - 1.0 ) ,
corners [ 4 ] , corners [ 5 ] , corners [ 6 ] , corners [ 7 ]
)
# rear_matrix = [
# 0.5, 0.0, 0.0, 0.0,
# 0.0, 0.5, 0.0, 0.0,
# 0.0, 0.0, 0.5, 0.0,
# 0.0, 0.0, 0.0, 8.5,
# ]
#
# front_matrix = [
# 0.5, 0.0, 0.0, 0.0,
# 0.0, 0.5, 0.0, 20.0,
# 0.0, 0.0, 0.5, 0.0,
# 0.0, 0.0, 0.0, 8.5,
# ]
image . lock ( )
for i in range ( 4 ) :
image . set_pixel ( i , 0 ,
Color ( front_matrix [ i * 4 ] , front_matrix [ i * 4 + 1 ] ,
front_matrix [ i * 4 + 2 ] , front_matrix [ i * 4 + 3 ] ) )
for i in range ( 4 ) :
image . set_pixel ( i + 4 , 0 ,
Color ( rear_matrix [ i * 4 ] , rear_matrix [ i * 4 + 1 ] ,
rear_matrix [ i * 4 + 2 ] , rear_matrix [ i * 4 + 3 ] ) )
image . unlock ( )
table . set_data ( image )