How we can shows 3D animations in 2D scene in Godot 4.2.1?

Godot 4.2.1

I have spinning_top.glb aniamation which designed in blender, and iy is 3D animation but I want to shows it in 2D scene, my code and scene tree like below:

extends Node2D

# Load the GLB file as a PackedScene
var glb_scene:PackedScene = preload("res://spinning_top.glb")

# Create an instance of the scene
var glb_model:Node3D

# Create a new SubViewport
var sub_viewport = SubViewport.new()

func _ready():
	# Add the SubViewport to your scene
	add_child(sub_viewport)

	# Instantiate the 3D model
	glb_model = glb_scene.instantiate()

	# Add the model to the SubViewport
	sub_viewport.add_child(glb_model)

	# Translate the model
	glb_model.translate(Vector3(0.0, 0.0, -10.0))

	# Play the animation
	var animation_player = glb_model.get_node("AnimationPlayer")
	if animation_player != null:
		animation_player.play("ConeAction")

scene tree:
27
I missed what in there, project is work but I am not see spinning_top on 2D scene, any idea?

Here’s a demo about rendering 3D in a 2D scene godot-demo-projects/viewport/3d_in_2d at master · godotengine/godot-demo-projects · GitHub

By default the content of SubViewports is not displayed. If you put the SubViewport as child of a SubViewportContainer, then the SubViewportContainer displays the SubViewport content.

I try it but it is not shows my animation, just shows black shapes of object

I am not get it I will adding SubViewportContainer as child of SubViewport and adding animation in SubViewportContainer?

adding SubViewportContainer as child of SubViewport

No, the other way round. The scene tree should look like this:

  • Node2D
    • SubViewportContainer
      • SubViewport
        • glb_model

still not see I need adding OmniLight3D or DirectionalLight3D in for GLB?

Yes, but make an inherited scene from the glb (in Godot) and add lights in there. Then add your inherited scene under SubViewport.

ETA - Also needs an Environment.

I dont know how OmniLight3D or DirectionalLight3D related to animation, I adding them but sometimes object not shows sometimes shwos animation with shadow dark, by the way what is mean ETA, nad why I need an Environment?

ETA==Edited To Add

An Environment node will help with lighting. You can experiment.

1 Like

scene tree and object with OmnliLight3D location like that, I dont know what I missed, I need to adding Camera also?

Yes, camera.

1 Like