Преглед на файлове

Add Matrix Transform demo

Aaron Franke преди 5 години
родител
ревизия
21e6dc8576

+ 15 - 0
misc/matrix_transform/2D.tscn

@@ -0,0 +1,15 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://marker/AxisMarker2D.tscn" type="PackedScene" id=1]
+
+[node name="2D" type="Node2D"]
+
+[node name="Scale100" type="Node2D" parent="."]
+rotation = 2.98023e-08
+scale = Vector2( 100, 100 )
+
+[node name="AxisMarker" parent="Scale100" instance=ExtResource( 1 )]
+
+[node name="AxisMarker2" parent="Scale100/AxisMarker" instance=ExtResource( 1 )]
+position = Vector2( 1.5, 1 )
+rotation = 0.349066

+ 10 - 0
misc/matrix_transform/3D.tscn

@@ -0,0 +1,10 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://marker/AxisMarker3D.tscn" type="PackedScene" id=1]
+
+[node name="3D" type="Spatial"]
+
+[node name="AxisMarker" parent="." instance=ExtResource( 1 )]
+
+[node name="AxisMarker" parent="AxisMarker" instance=ExtResource( 1 )]
+transform = Transform( 0.921381, -0.293412, 0.254887, 0.254887, 0.951251, 0.173648, -0.293412, -0.0950286, 0.951251, 1, 1, -1 )

+ 25 - 0
misc/matrix_transform/README.md

@@ -0,0 +1,25 @@
+# Matrix Transform
+
+This demo project is a playground where you can visualize how transforms work.
+
+Do not "run" this project. You are only meant to use it within the Godot editor.
+
+For more information, see the [Matrices and Transforms](https://docs.godotengine.org/en/latest/tutorials/math/matrices_and_transforms.html) article.
+
+## How does it work?
+
+In both 2D and 3D, colored lines are drawn indicating the basis vectors as well as the origin vector. For 3D, this means cuboids. If you translate, rotate, scale, or shear the AxisMarker objects, you will be able to see how it affects the transform's component vectors, and all children objects are also appropriately transformed.
+
+You are encouraged to manipulate the AxisMarker objects both in the main viewport and in the inspector. You are encouraged to duplicate them in the hierarchy and parent them any way you wish.
+
+In 2D, red and green lines represent the X and Y axes, with blue representing the origin.
+
+In 3D, red, green, and blue lines represent the X, Y, and Z axes, with cyan representing the origin.
+
+One noteworthy implementation detail: to avoid jitter, the origin vector is a parent of a Node, and inherits the AxisMarker's parent's transform.
+
+## Screenshots
+
+![2D](screenshots/2D.png)
+
+![3D](screenshots/3D.png)

+ 7 - 0
misc/matrix_transform/default_env.tres

@@ -0,0 +1,7 @@
+[gd_resource type="Environment" load_steps=2 format=2]
+
+[sub_resource type="ProceduralSky" id=1]
+
+[resource]
+background_mode = 2
+background_sky = SubResource( 1 )

BIN
misc/matrix_transform/icon.png


+ 34 - 0
misc/matrix_transform/icon.png.import

@@ -0,0 +1,34 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://icon.png"
+dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=true
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=true
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+stream=false
+size_limit=0
+detect_3d=true
+svg/scale=1.0

+ 11 - 0
misc/matrix_transform/marker/AxisMarker2D.gd

@@ -0,0 +1,11 @@
+tool
+class_name AxisMarker2D, "res://marker/AxisMarker2D.svg"
+extends Node2D
+
+func _process(_delta):
+	var line: Line2D = get_child(0).get_child(0)
+	var marker_parent: Node2D = get_parent()
+	
+	line.points[1] = transform.origin
+	if marker_parent as Node2D != null:
+		line.transform = marker_parent.global_transform

+ 1 - 0
misc/matrix_transform/marker/AxisMarker2D.svg

@@ -0,0 +1 @@
+<svg height="16" viewBox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="m3.9844 15a1 1 0 0 1 -.69141-.29102l-2-2a1 1 0 0 1 0-1.4141 1 1 0 0 1 1.4141 0l.29297.29297v-8.5859002h8.5859l-.29297-.29297a1 1 0 0 1 0-1.4141 1 1 0 0 1 1.4141 0l2 2a1 1 0 0 1 0 1.4141l-2 2a1 1 0 0 1 -.7207.29102 1 1 0 0 1 -.69336-.29102 1 1 0 0 1 0-1.4141l.29297-.29297h-6.5859v6.5859002l.29297-.29297a1 1 0 0 1 1.4141 0 1 1 0 0 1 0 1.4141l-2 2a1 1 0 0 1 -.72266.29102z" fill="#a5b7f3"/></svg>

+ 34 - 0
misc/matrix_transform/marker/AxisMarker2D.svg.import

@@ -0,0 +1,34 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/AxisMarker2D.svg-a52a23070c2ca9dcdd860a47183ce756.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://marker/AxisMarker2D.svg"
+dest_files=[ "res://.import/AxisMarker2D.svg-a52a23070c2ca9dcdd860a47183ce756.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=true
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=true
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+stream=false
+size_limit=0
+detect_3d=true
+svg/scale=1.0

+ 40 - 0
misc/matrix_transform/marker/AxisMarker2D.tscn

@@ -0,0 +1,40 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://marker/AxisMarker2D.gd" type="Script" id=1]
+
+[node name="AxisMarker" type="Node2D"]
+script = ExtResource( 1 )
+
+[node name="Origin" type="Node" parent="."]
+
+[node name="Line" type="Line2D" parent="Origin"]
+points = PoolVector2Array( 0, 0, 0, 0 )
+width = 0.1
+default_color = Color( 0, 0.266667, 1, 1 )
+__meta__ = {
+"_edit_lock_": true
+}
+
+[node name="Center" type="Line2D" parent="."]
+points = PoolVector2Array( -0.05, 0, 0.05, 0 )
+width = 0.1
+default_color = Color( 0, 0.266667, 1, 1 )
+__meta__ = {
+"_edit_lock_": true
+}
+
+[node name="X" type="Line2D" parent="."]
+points = PoolVector2Array( 0.05, 0, 1, 0 )
+width = 0.1
+default_color = Color( 0.866667, 0, 0, 1 )
+__meta__ = {
+"_edit_lock_": true
+}
+
+[node name="Y" type="Line2D" parent="."]
+points = PoolVector2Array( 0, 0.05, 0, 1 )
+width = 0.1
+default_color = Color( 0, 0.866667, 0, 1 )
+__meta__ = {
+"_edit_lock_": true
+}

+ 17 - 0
misc/matrix_transform/marker/AxisMarker3D.gd

@@ -0,0 +1,17 @@
+tool
+class_name AxisMarker3D, "res://marker/AxisMarker3D.svg"
+extends Spatial
+
+func _process(_delta):
+	var holder: Spatial = get_child(0).get_child(0)
+	var cube: Spatial = holder.get_child(0)
+	# "Hide" the origin vector if the AxisMarker is at (0, 0, 0)
+	if translation == Vector3():
+		holder.transform = Transform()
+		cube.transform = Transform().scaled(Vector3.ONE * 0.0001)
+		return
+	
+	holder.transform = Transform(Basis(), translation / 2)
+	holder.transform = holder.transform.looking_at(translation, Vector3.UP)
+	holder.transform = get_parent().global_transform * holder.transform
+	cube.transform = Transform(Basis().scaled(Vector3(0.1, 0.1, translation.length())))

+ 1 - 0
misc/matrix_transform/marker/AxisMarker3D.svg

@@ -0,0 +1 @@
+<svg height="16" viewBox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="m3.9902 1a1 1 0 0 0 -.69141.29102l-2 2a1 1 0 0 0 0 1.4141 1 1 0 0 0 1.4141 0l.29297-.29297v8.5859h8.5859l-.29297.29297a1 1 0 0 0 0 1.4141 1 1 0 0 0 1.4141 0l2-2a1 1 0 0 0 0-1.4141l-2-2a1 1 0 0 0 -.72266-.29102 1 1 0 0 0 -.69141.29102 1 1 0 0 0 0 1.4141l.29297.29297h-5.1719l5.5859-5.5859v.41602a1 1 0 0 0 1 1 1 1 0 0 0 1-1v-2.8301a1 1 0 0 0 -1-1h-2.8301a1 1 0 0 0 -1 1 1 1 0 0 0 1 1h.41602l-5.5859 5.5859v-5.1719l.29297.29297a1 1 0 0 0 1.4141 0 1 1 0 0 0 0-1.4141l-2-2a1 1 0 0 0 -.72266-.29102z" fill="#fc9c9c"/></svg>

+ 34 - 0
misc/matrix_transform/marker/AxisMarker3D.svg.import

@@ -0,0 +1,34 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/AxisMarker3D.svg-613a31ba426aca266949d27353333ae4.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://marker/AxisMarker3D.svg"
+dest_files=[ "res://.import/AxisMarker3D.svg-613a31ba426aca266949d27353333ae4.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=true
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=true
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+stream=false
+size_limit=0
+detect_3d=true
+svg/scale=1.0

+ 50 - 0
misc/matrix_transform/marker/AxisMarker3D.tscn

@@ -0,0 +1,50 @@
+[gd_scene load_steps=7 format=2]
+
+[ext_resource path="res://marker/AxisMarker3D.gd" type="Script" id=1]
+
+[sub_resource type="CubeMesh" id=1]
+size = Vector3( 1, 1, 1 )
+
+[sub_resource type="SpatialMaterial" id=2]
+albedo_color = Color( 0, 1, 1, 1 )
+
+[sub_resource type="SpatialMaterial" id=3]
+albedo_color = Color( 1, 0, 0, 1 )
+
+[sub_resource type="SpatialMaterial" id=4]
+albedo_color = Color( 0, 1, 0, 1 )
+
+[sub_resource type="SpatialMaterial" id=5]
+albedo_color = Color( 0, 0, 1, 1 )
+
+[node name="AxisMarker" type="Spatial"]
+script = ExtResource( 1 )
+
+[node name="Origin" type="Node" parent="."]
+
+[node name="Holder" type="Spatial" parent="Origin"]
+
+[node name="Cube" type="MeshInstance" parent="Origin/Holder"]
+transform = Transform( 0.0001, 0, 0, 0, 0.0001, 0, 0, 0, 0.0001, 0, 0, 0 )
+mesh = SubResource( 1 )
+material/0 = SubResource( 2 )
+
+[node name="Center" type="MeshInstance" parent="."]
+transform = Transform( 0.1, 0, 0, 0, 0.1, 0, 0, 0, 0.1, 0, 0, 0 )
+mesh = SubResource( 1 )
+material/0 = SubResource( 2 )
+
+[node name="X" type="MeshInstance" parent="."]
+transform = Transform( 1, 0, 0, 0, 0.1, 0, 0, 0, 0.1, 0.55, 0, 0 )
+mesh = SubResource( 1 )
+material/0 = SubResource( 3 )
+
+[node name="Y" type="MeshInstance" parent="."]
+transform = Transform( 0.1, 0, 0, 0, 1, 0, 0, 0, 0.1, 0, 0.55, 0 )
+mesh = SubResource( 1 )
+material/0 = SubResource( 4 )
+
+[node name="Z" type="MeshInstance" parent="."]
+transform = Transform( 0.1, 0, 0, 0, 0.1, 0, 0, 0, 1, 0, 0, 0.55 )
+mesh = SubResource( 1 )
+material/0 = SubResource( 5 )

+ 35 - 0
misc/matrix_transform/project.godot

@@ -0,0 +1,35 @@
+; Engine configuration file.
+; It's best edited using the editor UI and not directly,
+; since the parameters that go here are not all obvious.
+;
+; Format:
+;   [section] ; section goes between []
+;   param=value ; assign values to parameters
+
+config_version=4
+
+_global_script_classes=[ {
+"base": "Node2D",
+"class": "AxisMarker2D",
+"language": "GDScript",
+"path": "res://marker/AxisMarker2D.gd"
+}, {
+"base": "Spatial",
+"class": "AxisMarker3D",
+"language": "GDScript",
+"path": "res://marker/AxisMarker3D.gd"
+} ]
+_global_script_class_icons={
+"AxisMarker2D": "res://marker/AxisMarker2D.svg",
+"AxisMarker3D": "res://marker/AxisMarker3D.svg"
+}
+
+[application]
+
+config/name="Matrix Transform"
+run/main_scene="res://3D.tscn"
+config/icon="res://icon.png"
+
+[rendering]
+
+environment/default_environment="res://default_env.tres"

+ 0 - 0
misc/matrix_transform/screenshots/.gdignore


BIN
misc/matrix_transform/screenshots/2D.png


BIN
misc/matrix_transform/screenshots/3D.png