[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)
相关推荐
Struart_R9 小时前
LLaVA-3D,Video-3D LLM,VG-LLM,SPAR论文解读
人工智能·深度学习·计算机视觉·3d·大语言模型·多模态
杀生丸学AI17 小时前
【无标题】GAP: 用文本指导对任何点云进行高斯化
3d·aigc·三维重建·视觉大模型·动态重建
audyxiao0011 天前
为了更强大的空间智能,如何将2D图像转换成完整、具有真实尺度和外观的3D场景?
人工智能·计算机视觉·3d·iccv·空间智能
范男1 天前
基于Pytochvideo训练自己的的视频分类模型
人工智能·pytorch·python·深度学习·计算机视觉·3d·视频
点云SLAM2 天前
SLAM文献之-Globally Consistent and Tightly Coupled 3D LiDAR Inertial Mapping
3d·机器人·slam·vgicp算法·gpu 加速·lidar-imu 建图方法·全局匹配代价最小化
LetsonH2 天前
⭐CVPR2025 给3D高斯穿 “UV 衣” 框架[特殊字符]
3d·uv
新启航-光学3D测量2 天前
从 48 小时到 4 小时:三维逆向工程中自动化工具链如何重构扫描建模效率
科技·3d·制造
彩旗工作室2 天前
腾讯混元3D系列开源模型:从工业级到移动端的本地部署
3d·开源·腾讯混元
CG_MAGIC2 天前
主流 3D 模型格式(FBX/OBJ/DAE/GLTF)材质支持与转换操作指南
3d·渲染·动画·材质·贴图·3d 模型格式·材质支持与转换操作指南
scoone3 天前
开源游戏引擎Bevy 和 Godot
游戏引擎·godot