mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2025-12-16 13:30:07 +01:00
Simplify 3D in 2D demo
This commit is contained in:
@@ -1,45 +1,16 @@
|
||||
extends Node2D
|
||||
|
||||
# These first 3 members are for the sprite animation.
|
||||
const MAX_FRAME_FOR_SPRITE = 4
|
||||
const FRAME_SWITCH_TIME = 0.2
|
||||
|
||||
var frame_switch_timer = 0
|
||||
var viewport_initial_size = Vector2()
|
||||
|
||||
onready var viewport = $Viewport
|
||||
onready var sprite = $Sprite2D
|
||||
onready var viewport_sprite = $ViewportSprite
|
||||
|
||||
func _ready():
|
||||
# We want Godot to load everything but be hidden for a bit.
|
||||
viewport_sprite.modulate = Color(1, 1, 1, 0.01)
|
||||
$AnimatedSprite.play()
|
||||
#warning-ignore:return_value_discarded
|
||||
get_viewport().connect("size_changed", self, "_root_viewport_size_changed")
|
||||
viewport_initial_size = viewport.size
|
||||
|
||||
# Assign the sprite's texture to the viewport texture.
|
||||
viewport.set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)
|
||||
|
||||
# Let two frames pass to make sure the screen was captured.
|
||||
yield(get_tree(), "idle_frame")
|
||||
yield(get_tree(), "idle_frame")
|
||||
viewport_sprite.texture = viewport.get_texture()
|
||||
# Hide a little bit longer just in case.
|
||||
for _unused in range(50):
|
||||
yield(get_tree(), "idle_frame")
|
||||
viewport_sprite.modulate = Color.white # Default modulate color.
|
||||
|
||||
|
||||
# Simple frame-based animation.
|
||||
func _process(delta):
|
||||
frame_switch_timer += delta
|
||||
if frame_switch_timer >= FRAME_SWITCH_TIME:
|
||||
frame_switch_timer -= FRAME_SWITCH_TIME
|
||||
sprite.frame += 1
|
||||
if sprite.frame > MAX_FRAME_FOR_SPRITE:
|
||||
sprite.frame = 0
|
||||
|
||||
|
||||
# Called when the root's viewport size changes (i.e. when the window is resized).
|
||||
# This is done to handle multiple resolutions without losing quality.
|
||||
@@ -47,4 +18,4 @@ func _root_viewport_size_changed():
|
||||
# The viewport is resized depending on the window height.
|
||||
# To compensate for the larger resolution, the viewport sprite is scaled down.
|
||||
viewport.size = Vector2.ONE * get_viewport().size.y
|
||||
viewport_sprite.scale = Vector2.ONE * viewport_initial_size.y / get_viewport().size.y
|
||||
viewport_sprite.scale = Vector2(1, -1) * viewport_initial_size.y / get_viewport().size.y
|
||||
|
||||
@@ -1,24 +1,43 @@
|
||||
[gd_scene load_steps=4 format=2]
|
||||
[gd_scene load_steps=11 format=2]
|
||||
|
||||
[ext_resource path="res://3d_in_2d.gd" type="Script" id=1]
|
||||
[ext_resource path="res://robot_demo.png" type="Texture" id=2]
|
||||
[ext_resource path="res://robot_3d.tscn" type="PackedScene" id=3]
|
||||
|
||||
[sub_resource type="ViewportTexture" id=1]
|
||||
viewport_path = NodePath("Viewport")
|
||||
|
||||
[sub_resource type="AtlasTexture" id=2]
|
||||
atlas = ExtResource( 2 )
|
||||
region = Rect2( 0, 0, 64, 64 )
|
||||
|
||||
[sub_resource type="AtlasTexture" id=3]
|
||||
atlas = ExtResource( 2 )
|
||||
region = Rect2( 64, 0, 64, 64 )
|
||||
|
||||
[sub_resource type="AtlasTexture" id=4]
|
||||
atlas = ExtResource( 2 )
|
||||
region = Rect2( 128, 0, 64, 64 )
|
||||
|
||||
[sub_resource type="AtlasTexture" id=5]
|
||||
atlas = ExtResource( 2 )
|
||||
region = Rect2( 192, 0, 64, 64 )
|
||||
|
||||
[sub_resource type="AtlasTexture" id=6]
|
||||
atlas = ExtResource( 2 )
|
||||
region = Rect2( 256, 0, 64, 64 )
|
||||
|
||||
[sub_resource type="SpriteFrames" id=7]
|
||||
animations = [ {
|
||||
"frames": [ SubResource( 2 ), SubResource( 3 ), SubResource( 4 ), SubResource( 5 ), SubResource( 6 ) ],
|
||||
"loop": true,
|
||||
"name": "default",
|
||||
"speed": 5.0
|
||||
} ]
|
||||
|
||||
[node name="3Din2D" type="Node2D"]
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="Sprite2D" type="Sprite" parent="."]
|
||||
position = Vector2( 339.942, 311.204 )
|
||||
scale = Vector2( 3, 3 )
|
||||
texture = ExtResource( 2 )
|
||||
vframes = 2
|
||||
hframes = 16
|
||||
frame = 4
|
||||
|
||||
[node name="ViewportSprite" type="Sprite" parent="."]
|
||||
position = Vector2( 600, 320 )
|
||||
rotation = 3.14159
|
||||
|
||||
[node name="Viewport" type="Viewport" parent="."]
|
||||
size = Vector2( 300, 300 )
|
||||
own_world = true
|
||||
@@ -29,6 +48,16 @@ usage = 3
|
||||
|
||||
[node name="Robot3D" parent="Viewport" instance=ExtResource( 3 )]
|
||||
|
||||
[node name="ViewportSprite" type="Sprite" parent="."]
|
||||
position = Vector2( 650, 300 )
|
||||
scale = Vector2( 1, -1 )
|
||||
texture = SubResource( 1 )
|
||||
|
||||
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
|
||||
position = Vector2( 350, 300 )
|
||||
scale = Vector2( 3, 3 )
|
||||
frames = SubResource( 7 )
|
||||
|
||||
[node name="Camera2D" type="Camera2D" parent="."]
|
||||
offset = Vector2( 512, 300 )
|
||||
current = true
|
||||
|
||||
@@ -225,7 +225,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 2.75 )
|
||||
fov = 65.0
|
||||
near = 0.1
|
||||
|
||||
[node name="OmniLight" type="OmniLight" parent="."]
|
||||
[node name="OmniLight1" type="OmniLight" parent="."]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -2.25091, 1.43155, 2.15467 )
|
||||
light_color = Color( 1, 0.915375, 0.816406, 1 )
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 5.5 KiB |
Reference in New Issue
Block a user