[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)
相关推荐
sbjdhjd7 小时前
开源分享 | 超浪漫 3D 圣诞树立体动画(附零基础使用教程)
3d·青少年编程·开源·html·节日
lrh30259 小时前
Custom SRP - 16 Render Scale
3d·unity·srp·render pipeline·render scale
毕安格 - BimAngle14 小时前
Revit 模型一键输出 3D Tiles (for Cesium) 和 glTF/glb
3d·cesium·gltf·glb·3d tiles
map_vis_3d15 小时前
JSAPIThree 加载 3D Tiles 学习笔记:大规模三维场景渲染
笔记·学习·3d
da_vinci_x16 小时前
Substance 3D Painter 进阶:手绘“掉漆”太累?用 Anchor Point 让材质“活”过来
游戏·3d·aigc·材质·设计师·技术美术·游戏美术
攻城狮7号18 小时前
微软开源 TRELLIS.2:单图 3 秒变 3D?
人工智能·3d·trellis.2·o-voxel·sc-vae·微软开源模型
樱桃园园长19 小时前
【Three.js 实战】手势控制 3D 奢华圣诞树 —— 从粒子系统到交互实现
javascript·3d·交互
二狗哈19 小时前
Cesium快速入门30:CMZL动画
javascript·3d·webgl·cesium·地图可视化
二狗哈20 小时前
Cesium快速入门29:CMZL数据格式加载
3d·状态模式·webgl·cesium·着色器·地图可视化
map_vis_3d1 天前
JSAPIThree LODModel 性能优化学习笔记:细节层次模型加载
笔记·学习·3d