Added table and cleaned up math helper.

This commit is contained in:
mono 2022-01-01 12:55:02 +01:00
parent feb61ee74c
commit cf32f494ad
14 changed files with 220 additions and 146 deletions

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=17 format=2]
[gd_scene load_steps=18 format=2]
[ext_resource path="res://TryingOutSomethingNew.gd" type="Script" id=1]
[ext_resource path="res://Node2D.gd" type="Script" id=2]
@ -6,7 +6,6 @@
[ext_resource path="res://addons/summer_day/display/display.gd" type="Script" id=4]
[ext_resource path="res://Bullshit/Main.gd" type="Script" id=5]
[ext_resource path="res://addons/summer_day/user_interface/canvas/rendering/space_camera.gd" type="Script" id=6]
[ext_resource path="res://Bullshit/THIS SHOULD FIX IThoefully.tres" type="Texture" id=7]
[sub_resource type="Shader" id=1]
code = "shader_type spatial;
@ -39,6 +38,21 @@ shader = SubResource( 1 )
shader_param/albedo = Color( 0.768627, 0.231373, 0.231373, 1 )
shader_param/ha = null
[sub_resource type="Image" id=16]
data = {
"data": PoolByteArray( 0, 0, 128, 62, 0, 0, 0, 0, 0, 0, 0, 0, 154, 153, 25, 62, 0, 0, 0, 0, 0, 0, 128, 62, 0, 0, 0, 0, 154, 153, 25, 62, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 128, 62, 0, 0, 0, 0, 0, 0, 0, 0, 154, 153, 25, 190, 0, 0, 0, 0, 0, 0, 128, 62, 0, 0, 0, 0, 154, 153, 25, 190, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63 ),
"format": "RGBAFloat",
"height": 1,
"mipmaps": false,
"width": 8
}
[sub_resource type="ImageTexture" id=15]
flags = 0
flags = 0
image = SubResource( 16 )
size = Vector2( 8, 1 )
[sub_resource type="CapsuleMesh" id=5]
radius = 0.9
mid_height = 2.0
@ -94,7 +108,7 @@ void fragment() {
[sub_resource type="ShaderMaterial" id=7]
shader = SubResource( 6 )
shader_param/canvas_transform = Basis( 0.89779, 0.0130737, -0.857315, 0.0243171, -1.66989, 0.724701, 0, 0, 1 )
shader_param/table = ExtResource( 7 )
shader_param/table = SubResource( 15 )
[sub_resource type="QuadMesh" id=8]
@ -147,7 +161,7 @@ script = ExtResource( 6 )
[node name="Sprite" type="Sprite" parent="."]
position = Vector2( 619.786, 73.103 )
texture = ExtResource( 7 )
texture = SubResource( 15 )
centered = false
[node name="Sprite3D" type="Sprite3D" parent="."]

View File

@ -13,7 +13,7 @@ func _create_model() -> PackedScene: # Virtual.
return null
func _create_cell() -> SummerDayCell: # Virtual.
func _create_cell(table: ImageTexture) -> SummerDayCell: # Virtual.
return null

View File

@ -3,13 +3,16 @@ extends Resource
class_name SummerDayDocument
signal resolution_changed(new_resolution)
signal track_added(track)
signal track_deleted(track) # Not used yet. Implement please.
export(Resource) var scene = _scene_generation()
export(Vector2) var resolution = Vector2(1920, 1080) setget _set_resolution
export(Array, Resource) var tracks
var raw_aspect_ratio setget , _get_raw_aspect_ratio
func setup_tracks():
for track in tracks:
@ -24,8 +27,31 @@ func add_track(blueprint: SummerDayBlueprint):
emit_signal("track_added", track)
func _scene_generation() -> SummerDayScene:
func create_scene() -> SummerDayScene:
var _scene = SummerDayScene.new()
_scene.resource_path = "res://production/test/scene_001.tres"
_scene.aspect_ratio = Vector2(12.0, 9.0)
_scene.resolution = Vector2(12.0, 9.0)
return _scene
func _set_resolution(new_resolution):
var handled_resolution = Vector2()
# Don't let the components drop below 0.01!
if new_resolution.x <= 0.01:
handled_resolution.x = 0.01
else:
handled_resolution.x = new_resolution.x
if new_resolution.y <= 0.01:
handled_resolution.y = 0.01
else:
handled_resolution.y = new_resolution.y
resolution = handled_resolution
emit_signal("resolution_changed", resolution)
func _get_raw_aspect_ratio():
return resolution.x / resolution.y

View File

@ -0,0 +1,46 @@
tool
extends Resource
class_name SummerDayTable
export(int) var allocation_size # How many matrices per frame.
export(ImageTexture) var texture
export(Image) var image
var dirty_flag = false
func create():
var row_size = allocation_size * 4
image.create(row_size, 1, false, Image.FORMAT_RGBAF)
texture.create_from_image(image, 0)
_presage_changes()
for i in range(row_size):
image.set_pixel(i, 0,
Color(1.0, 0.0, 0.0))
flush()
# Position in matrix-indices, not in pixels.
func apply_matrix(matrix: SummerDayMatrix4, row: int, position: int):
_presage_changes()
var color_list = matrix.get_values_as_colors()
for i in range(4):
image.set_pixel(
position * 4 + i,
row, color_list[i]
)
func flush():
if dirty_flag:
image.unlock()
texture.set_data(image)
dirty_flag = false
# This needs to be called BEFORE anything, that makes changes to the image!
func _presage_changes():
if !dirty_flag:
image.lock()
dirty_flag = true

View File

@ -3,12 +3,13 @@ extends Resource
class_name SummerDayTrack # Don't mistake with SummerDaySceneTrack!
export(Resource) var blueprint
export(Resource) var blueprint # SummerDayResource
export(Resource) var table = SummerDayTable.new()
export(Array, Resource) var cells
func add_cell(at: int):
var new_cell = blueprint._create_cell()
var new_cell = blueprint._create_cell(table)
cells.append(new_cell)

View File

@ -2,7 +2,6 @@ tool
extends EditorPlugin
signal document_changed(new_doc)
signal sd_scene_changed(new_scene)
const MainScreen = preload("res://addons/summer_day/main_screen.tscn")
const Timeline = preload("res://addons/summer_day/user_interface/timeline/timeline.tscn")
@ -24,15 +23,9 @@ func _enter_tree():
Canvas.tool_context = tool_context
self.connect("document_changed",
Canvas, "_on_SummerDay_document_changed")
self.connect("sd_scene_changed",
Canvas, "_on_SummerDay_scene_changed"
)
self.connect("document_changed",
tool_context, "_on_SummerDay_document_changed"
)
self.connect("sd_scene_changed",
tool_context, "_on_SummerDay_scene_changed"
)
# Add the main panel to the editor's main viewport.
get_editor_interface().get_editor_viewport().add_child(main_screen_instance)
# Hide the main panel. Very much required.
@ -86,7 +79,6 @@ func handles(object):
func edit(object):
emit_signal("document_changed", object)
emit_signal("sd_scene_changed", object.scene)
func get_plugin_name():

View File

@ -3,7 +3,6 @@ class_name SummerDayToolContext
var edited_document: SummerDayDocument
var edited_scene: SummerDayScene
var canvas_state: SummerDayCanvasState
var position: Vector2
@ -11,8 +10,3 @@ var position: Vector2
func _on_SummerDay_document_changed(new_document):
edited_document = new_document
pass
func _on_SummerDay_scene_changed(new_scene):
edited_scene = new_scene

View File

@ -6,7 +6,6 @@ onready var MeshContainer := $Render/RenderLayer/Container
onready var EditHints := $Render/ForegroundLayer/EditHints
var document: SummerDayDocument
var scene: SummerDayScene
var canvas_state := SummerDayCanvasState.new()
var tool_pool := SummerDayCanvasToolPool.new()
@ -53,26 +52,21 @@ func _on_SummerDay_document_changed(new_doc):
new_doc.connect("track_added",
MeshContainer, "_on_Document_add_track")
new_doc.setup_tracks()
func _on_SummerDay_scene_changed(new_scene):
if new_scene != scene:
scene = new_scene
canvas_state.update_fill_transform(scene)
scene.connect("aspect_ratio_changed",
canvas_state.update_fill_transform(new_doc)
new_doc.connect("resolution_changed",
self, "_on_scene_aspect_ratio_changed")
$Render/Label.text = str(scene) + "\n" + str(scene.aspect_ratio)
canvas_state.update_fill_transform(scene)
$Render/Label.text = str(new_doc) + "\n" + str(new_doc.resolution)
canvas_state.update_fill_transform(new_doc)
func _on_Canvas_resized():
canvas_state.rect_size = rect_size
canvas_state.update_canvas_to_clip_space()
canvas_state.update_fill_transform(scene)
canvas_state.update_fill_transform(document)
func _on_scene_aspect_ratio_changed(new_aspect_ratio):
canvas_state.update_fill_transform(scene)
canvas_state.update_fill_transform(document)
func _on_view_transform_changed(canvas_transform, spatial_transform):

View File

@ -26,11 +26,11 @@ func _init():
connect("canvas_transform_changed", self, "_update_view_transformation")
func update_fill_transform(scene: SummerDayScene):
if scene == null:
func update_fill_transform(document: SummerDayDocument):
if document == null:
fill_transform = Transform2D.IDENTITY
else:
var aspect_ratio = scene.aspect_ratio.x / scene.aspect_ratio.y
var aspect_ratio = document.resolution.x / document.resolution.y
var size_ratio = rect_size.x / rect_size.y
@ -51,7 +51,7 @@ func update_fill_transform(scene: SummerDayScene):
translation
)
_update_canvas_polygon(scene)
_update_canvas_polygon(document)
emit_signal("fill_transform_changed", fill_transform)
@ -79,15 +79,15 @@ func _set_canvas_transform(new_canvas_transform):
emit_signal("canvas_transform_changed", canvas_transform)
func _update_canvas_polygon(scene: SummerDayScene):
if scene == null:
func _update_canvas_polygon(document: SummerDayDocument):
if document == null:
_canvas_polygon = PoolVector2Array()
return
_canvas_polygon = PoolVector2Array(
[
Vector2.ZERO,
Vector2(scene.raw_aspect_ratio, 0.0),
Vector2(scene.raw_aspect_ratio, 1.0),
Vector2(document.raw_aspect_ratio, 0.0),
Vector2(document.raw_aspect_ratio, 1.0),
Vector2(0.0, 1.0)
]
)

View File

@ -4,7 +4,7 @@ class_name SummerDayMathHelper
static func matrix_to_points(
p01: Vector2, p02: Vector2, p03: Vector2, p04: Vector2
) -> Array:
) -> Basis:
var j = p01.x - p02.x - p03.x + p04.x
if j == 0.0:
j = 0.00000001
@ -28,11 +28,11 @@ static func matrix_to_points(
var b = (p01.x * (g + h + i) - p02.x * (g - h + i)) / 2
var a = p01.x * (g + h + i) - c - b
return [
a, d, g,
b, e, h,
c, f, i
]
return Basis(
Vector3(a, d, g),
Vector3(b, e, h),
Vector3(c, f, i)
)
static func basis_from_to_points(
@ -40,44 +40,13 @@ static func basis_from_to_points(
d01: Vector2, d02: Vector2, d03: Vector2, d04: Vector2
):
var matrix_to_source = matrix_to_points(s01, s02, s03, s04)
var basis_to_source = array_to_basis(matrix_to_source)
var basis_from_source = basis_to_source.inverse()
var matrix_from_source = matrix_to_source.inverse()
var matrix_to_dest = matrix_to_points(d01, d02, d03, d04)
var basis_to_dest = array_to_basis(matrix_to_dest)
var from_source_to_dest = basis_to_dest * basis_from_source
var from_source_to_dest = matrix_to_dest * matrix_from_source
return from_source_to_dest
static func array_to_basis(arr: Array) -> Basis:
return Basis(
Vector3(arr[0], arr[1], arr[2]),
Vector3(arr[3], arr[4], arr[5]),
Vector3(arr[6], arr[7], arr[8])
)
static func basis_to_array(basis: Basis) -> Array:
return [
basis.x.x, basis.x.y, basis.x.z,
basis.y.x, basis.y.y, basis.y.z,
basis.z.x, basis.z.y, basis.z.z
]
static func mult_4x4_matrices(left: Array, right: Array) -> Array:
var result = []
for y in range(4):
for x in range(4):
var sum = 0.0
for i in range(4):
sum += left[_c2i(x, i)] * right[_c2i(i, y)]
result.append(sum)
return result
# Coordinates on a 4x4 field to index.
static func _c2i(x: int, y: int) -> int:
return y * 4 + x
# This just calculates a rotation-matrix that flattens the z-coordinates of
# the given Quad. You could also say that it makes the normal of the Quad
@ -109,6 +78,7 @@ static func plane_to_xy_basis(
return basis.inverse()
# This basically takes four Vector3 that form a Quad somewhere in the
# space and a second (this time two-dimensional) Quad to calculate
# the transformation-matrix that is required to get from the former
@ -147,16 +117,6 @@ static func plane_3d_to_xy(
xy_points[0], xy_points[1], xy_points[2], xy_points[3],
d_01, d_02, d_03, d_04)
# print(basis_2d)
#
# for i in range(4):
# var flat_coord = xy_points[i]
# var ext_coord = Vector3(flat_coord.x, flat_coord.y, 1.0)
# var trans_coord = basis_2d.xform(ext_coord)
# var cut_coord = Vector2(trans_coord.x, trans_coord.y)
# print(cut_coord)
# print("")
var transform_2d = Transform(
Vector3(basis_2d.x.x, basis_2d.y.x, 0.0),
Vector3(basis_2d.x.y, basis_2d.y.y, 0.0),
@ -164,57 +124,11 @@ static func plane_3d_to_xy(
Vector3(basis_2d.x.z, basis_2d.y.z, 0.0)
)
# var transform_2d = Transform(
# Vector3(basis_2d.x.x, basis_2d.x.y, basis_2d.x.z),
# Vector3(basis_2d.y.x, basis_2d.y.y, basis_2d.y.z),
# Vector3.ZERO,
# Vector3(0.0, 0.0, 0.0)
# )
var transform = transform_2d * to_plane_transform
# return [
# transform.basis.x.x, transform.basis.x.y, transform.basis.x.z,
# transform.origin.x,
# transform.basis.y.x, transform.basis.y.y, transform.basis.y.z,
# transform.origin.y,
# transform.basis.z.x, transform.basis.z.y, transform.basis.z.z,
# transform.origin.z,
# basis_2d.z.x, basis_2d.z.y, 0.0, 1.0
# ]
var cool = mult_4x4_matrices(
# [
# basis_2d.x.x, basis_2d.x.y, 0.0, basis_2d.x.z,
# basis_2d.y.x, basis_2d.y.y, 0.0, basis_2d.y.z,
# 0.0, 0.0, 0.0, 0.0,
# basis_2d.z.x, basis_2d.z.y, 0.0, basis_2d.z.z,
# ],
[
basis_2d.x.x, basis_2d.y.x, 0.0, basis_2d.z.x,
basis_2d.x.y, basis_2d.y.y, 0.0, basis_2d.z.y,
0.0, 0.0, 0.0, 0.0,
basis_2d.x.z, basis_2d.y.z, 0.0, basis_2d.z.z,
],
[
to_plane_transform.basis.x.x, to_plane_transform.basis.x.y,
to_plane_transform.basis.x.z, to_plane_transform.origin.x,
to_plane_transform.basis.y.x, to_plane_transform.basis.y.y,
to_plane_transform.basis.y.z, to_plane_transform.origin.y,
to_plane_transform.basis.z.x, to_plane_transform.basis.z.y,
to_plane_transform.basis.z.z, to_plane_transform.origin.z,
0.0, 0.0, 0.0, 1.0
]
var cool = SummerDayMatrix4.mult(
SummerDayMatrix4.from_Basis(basis_2d),
SummerDayMatrix4.from_Transform(to_plane_transform)
)
# cool = [
# cool[0], cool[4], cool[8], cool[12],
# cool[1], cool[5], cool[9], cool[13],
# cool[2], cool[6], cool[10], cool[14],
# cool[3], cool[7], cool[11], cool[15],
# ]
return cool

View File

@ -0,0 +1,82 @@
extends Reference
class_name SummerDayMatrix4
var values: Array
func _init(initial_values: Array = [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
]):
if initial_values.size() != 16:
push_error(
"Instance: " + str(self) + " \"initial_values.size() != 16\""
)
var misplaced_type_found = false
for i in initial_values:
if !(i is float):
misplaced_type_found = true
if misplaced_type_found:
push_error(
"Instance: " + str(self)
+ " \"initial_values\" contains non-float values!"
)
values = initial_values
static func from_Basis(from: Basis) -> SummerDayMatrix4:
var _SummerDayMatrix4 = load("res://addons/summer_day/utilities/matrix_4.gd")
return _SummerDayMatrix4.new([
from.x.x, from.y.x, 0.0, from.z.x,
from.x.y, from.y.y, 0.0, from.z.y,
0.0, 0.0, 0.0, 0.0, # <-- Change to 0.0, 0.0, 1.0, 0.0,
from.x.z, from.y.z, 0.0, from.z.z,
])
static func from_Transform(from: Transform) -> SummerDayMatrix4:
var _SummerDayMatrix4 = load("res://addons/summer_day/utilities/matrix_4.gd")
return _SummerDayMatrix4.new([
from.basis.x.x, from.basis.x.y, from.basis.x.z, from.origin.x,
from.basis.y.x, from.basis.y.y, from.basis.y.z, from.origin.y,
from.basis.z.x, from.basis.z.y, from.basis.z.z, from.origin.z,
0.0, 0.0, 0.0, 1.0
])
static func mult(
left: SummerDayMatrix4, right: SummerDayMatrix4
) -> SummerDayMatrix4:
var _SummerDayMatrix4 = load("res://addons/summer_day/utilities/matrix_4.gd")
var result = []
for y in range(4):
for x in range(4):
var sum = 0.0
for i in range(4):
sum += left.get_value(x, i) * right.get_value(i, y)
result.append(sum)
return _SummerDayMatrix4.new(result)
# Get value per coordinates.
func get_value(x: int, y: int) -> float:
return values[y * 4 + x]
func get_values_as_colors() -> Array:
var list = []
for y in range(4):
list.append(
Color(
get_value(0, y),
get_value(1, y),
get_value(2, y),
get_value(3, y)
)
)
return list

View File

@ -1,7 +1,6 @@
[gd_resource type="Resource" load_steps=10 format=2]
[gd_resource type="Resource" load_steps=9 format=2]
[ext_resource path="res://addons/summer_day/data/document/document.gd" type="Script" id=1]
[ext_resource path="res://production/test/scene_001.tres" type="Resource" id=2]
[ext_resource path="res://addons/summer_day/data/document/cell.gd" type="Script" id=3]
[ext_resource path="res://production/blueprints/mannequin/mannequin.tres" type="Resource" id=4]
[ext_resource path="res://addons/summer_day/data/document/track.gd" type="Script" id=5]
@ -21,5 +20,5 @@ cells = [ SubResource( 2 ) ]
[resource]
script = ExtResource( 1 )
scene = ExtResource( 2 )
resolution = Vector2( 1920, 1080 )
tracks = [ SubResource( 3 ) ]

View File

@ -55,5 +55,5 @@ void fragment() {
[resource]
shader = SubResource( 1 )
shader_param/canvas_transform = Basis( 0.423164, 0.356665, -0.252057, 0.508395, -0.603185, -0.0647715, 0, 0, 1 )
shader_param/canvas_transform = Basis( -0.100636, -0.496363, 0.0452502, -0.707523, 0.143448, 0.352804, 0, 0, 1 )
shader_param/table = ExtResource( 1 )

View File

@ -104,6 +104,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://addons/summer_day/utilities/math_helper.gd"
}, {
"base": "Reference",
"class": "SummerDayMatrix4",
"language": "GDScript",
"path": "res://addons/summer_day/utilities/matrix_4.gd"
}, {
"base": "SummerDayTool",
"class": "SummerDayPenCanvasTool",
"language": "GDScript",
@ -134,6 +139,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://addons/summer_day/data/scene/track.gd"
}, {
"base": "Resource",
"class": "SummerDayTable",
"language": "GDScript",
"path": "res://addons/summer_day/data/document/table.gd"
}, {
"base": "Control",
"class": "SummerDayTimeline",
"language": "GDScript",
@ -194,12 +204,14 @@ _global_script_class_icons={
"SummerDayHandCanvasTool": "",
"SummerDayMainScreen": "",
"SummerDayMathHelper": "",
"SummerDayMatrix4": "",
"SummerDayPenCanvasTool": "",
"SummerDayRotationCanvasTool": "",
"SummerDayScene": "",
"SummerDaySceneFrame": "",
"SummerDayScenePicker": "",
"SummerDaySceneTrack": "",
"SummerDayTable": "",
"SummerDayTimeline": "",
"SummerDayTool": "",
"SummerDayToolContext": "",