Godot Engine:用Sprite3D派生了一个Canvas3D类用于实现3D中的UI显示

Godot Engine中在3D世界显示2D的UI,需要用到Viewport,整个操作步骤还是有些麻烦,所以我用Sprite3D派生了一个Canvas3D类用于实现3D中的UI显示

代码如下
extends Sprite3D

class_name Canvas3D

export var viewport_path:NodePath = "viewport"

func _ready():
	var viewport  = get_node_or_null(viewport_path)
	if is_instance_valid(viewport) and viewport is Viewport:
		self.billboard = SpatialMaterial.BILLBOARD_ENABLED
		self.texture = viewport.get_texture()
		viewport.usage = Viewport.USAGE_2D
		viewport.transparent_bg = true
		viewport.render_target_v_flip = true
	else:
		push_error("a valid viewport should be assigned!")

你可能感兴趣的:(Godot笔记,#,Godot,实践,Godot,GDScript,Viewport,游戏开发)