[Godot] 3D拾取

CollisionObject3D文档
Camera3D文档

CollisionObject3D有个信号_input_event,可以用于处理3D拾取。

Camera3D也有project_position用于将屏幕空间坐标投影到3D空间。

gdscript 复制代码
extends Node3D

#是否处于选中状态
var selected : bool = false
#摄像机的前向量
var front : Vector3 = Vector3(0.0, 0.0, -1.0)
#待拾取的对象
@onready var a : CollisionObject3D = $a
#标记,表示在3D物体上的位置
@onready var mark : MeshInstance3D = $mark

func _on_a_mouse_entered() -> void:
	print("entered")
	mark.visible = true

func _on_a_mouse_exited() -> void:
	print("exited")
	mark.visible = false

func _on_a_input_event(camera: Node, event: InputEvent, position: Vector3, normal: Vector3, shape_idx: int) -> void:
	var str = \
	"camera: " + str(camera) + "\n" + \
	"event: " + str(event) + "\n" + \
	"position: " + str(position) + "\n" + \
	"normal: " + str(normal) + "\n" + \
	"shape_idx: " + str(shape_idx) + "\n" + \
	"a.position: " + str(a.position)
	$mark.position = position
	$label.text = str
	
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.pressed:
				selected = true
			else:
				selected = false
	
	elif event is InputEventMouseMotion:
		if selected:
			var c : Camera3D = camera as Camera3D
			#由相机指向物体的向量
			var dir : Vector3 = a.position - c.position
			#dir投影到front上所得的长度
			var dis : float = front.dot(dir)
			#将位置投影到3D空间
			a.position = c.project_position(event.position, dis)
相关推荐
Zwarwolf3 小时前
Godot零散知识点项目汇总
游戏引擎·godot
趋之7 小时前
Mars3D 三维可视化开发入门实战教程
3d
探物 AI7 小时前
零基础入门3D点云深度学习:从PointNet开始,理解3D数据处理
人工智能·深度学习·3d
码来的小朋友8 小时前
[Python] 制作小游戏创意之3D魔方
python·3d·pygame
pythonpioneer19 小时前
PyTorch3D:基于 PyTorch 的高效 3D 深度学习工具库
pytorch·深度学习·其他·3d
大江东去浪淘尽千古风流人物19 小时前
【PromptStereo】零样本立体匹配新范式:用结构与运动Prompt驱动迭代优化(CVPR 2026)
深度学习·3d·slam·视觉定位·dust3r·3d重建·mast3r
智海深蓝1 天前
海上平行战场:态势模拟三维可视化平台
3d·ue5
苏州邦恩精密1 天前
2026江苏GOM三维扫描仪定制厂家找哪家?企业数字化转型视角
人工智能·机器学习·3d·自动化·制造
Zldaisy3d1 天前
面向LPBF过程监测的物理引导多源数据融合模型:从数据生成到熔化状态识别
3d
智海深蓝1 天前
水下怎么使用3D高斯?布里斯托大学&北航提出R-Splatting:水下3DGS迎来新突破
3d