当前位置: 首页 > news >正文

Unity实现在3D模型标记

Canvas 模式是UI与3D混合模式(Render model=Screen space-Camera)

实现在3D模型标记,旋转跟随是UI不在3D物体下

在这里插入图片描述

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClickHandler : MonoBehaviour
{public Transform object3D; // 总体模型public GameObject imgUIPrefab;public Canvas canvas;public float fadeSpeed = 2.0f; // 淡入淡出的速度private bool isRotating = false;private GameObject clickedObject;private Vector3 lastMousePosition;private Vector3 delta;private Dictionary<GameObject, GameObject> generatedUIs = new Dictionary<GameObject, GameObject>();private void Update(){if (Input.GetMouseButtonDown(0)){lastMousePosition = Input.mousePosition;isRotating = true;Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if (Physics.Raycast(ray, out hit)){clickedObject = hit.collider.gameObject;// 检查是否为小模型if (clickedObject.CompareTag("Model")){isRotating = false;if (!HasGeneratedUI(clickedObject.name)){// 创建UIGameObject imgUI = CreateUIForModel(clickedObject);if (imgUI != null){// 添加到字典中generatedUIs.Add(imgUI, clickedObject);}}}}UpdateImgUIPosition();}if (Input.GetMouseButtonUp(0)){isRotating = false;}if (isRotating){delta = Input.mousePosition - lastMousePosition;float rotationSpeed = 0.5f;object3D.Rotate(Vector3.up, delta.x * -rotationSpeed, Space.World);UpdateImgUIPosition();}lastMousePosition = Input.mousePosition;}private GameObject CreateUIForModel(GameObject model){// 获取被点击物体的中心点位置Vector3 modelCenter = model.transform.position;// 将模型的世界坐标转换为屏幕坐标Vector3 screenPoint = Camera.main.WorldToScreenPoint(modelCenter);// 将屏幕坐标转换为Canvas内的局部坐标RectTransform canvasRect = canvas.GetComponent<RectTransform>();Vector2 canvasLocalPoint;RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, Camera.main, out canvasLocalPoint);// 设置预制体的位置为Canvas内的局部坐标,将Z轴位置设置为-150Vector3 prefabPosition = new Vector3(canvasLocalPoint.x, canvasLocalPoint.y,-150f);// 实例化Img UI预制件,并设置其位置为转换后的局部坐标GameObject imgUI = Instantiate(imgUIPrefab, prefabPosition, Quaternion.identity);imgUI.name = model.name; // 生成UI名字为3D模型名字imgUI.transform.SetParent(canvas.transform, false); // 设置Img UI的父对象为Canvas,确保其显示在屏幕上return imgUI;}private void UpdateImgUIPosition(){foreach (var uiEntry in generatedUIs){GameObject ui = uiEntry.Key;GameObject matchedModel = uiEntry.Value;bool isOccluded = IsObjectOccluded(matchedModel);// ui.SetActive(!isOccluded);  // 如果模型被遮挡,则隐藏UI;否则显示UI// 如果模型被遮挡,则UI淡出 否则淡入Image uiImage = ui.GetComponent<Image>();Color color = uiImage.color;float targetAlpha = isOccluded ? 0.0f : 1.0f;color.a = Mathf.MoveTowards(color.a, targetAlpha, Time.deltaTime * fadeSpeed);uiImage.color = color;if (!isOccluded){// 获取匹配模型的中心点位置Vector3 modelCenter = matchedModel.transform.position;// 将模型的世界坐标转换为屏幕坐标Vector3 screenPoint = Camera.main.WorldToScreenPoint(modelCenter);// 将屏幕坐标转换为Canvas内的局部坐标RectTransform canvasRect = canvas.GetComponent<RectTransform>();Vector2 canvasLocalPoint;RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, Camera.main, out canvasLocalPoint);Vector3 prefabPosition = new Vector3(canvasLocalPoint.x, canvasLocalPoint.y, -150f);// 更新UI的位置ui.GetComponent<RectTransform>().anchoredPosition = prefabPosition;}}}private bool HasGeneratedUI(string name){foreach (var ui in generatedUIs.Keys){if (ui.name == name){return true;}}return false;}private bool IsObjectOccluded(GameObject obj){// 获取摄像机到物体的方向Vector3 directionToTarget = obj.transform.position - Camera.main.transform.position;// 发射射线Ray ray = new Ray(Camera.main.transform.position, directionToTarget);RaycastHit hit;// 射线检测是否有其他碰撞器位于射线路径上if (Physics.Raycast(ray, out hit, directionToTarget.magnitude)){// 如果射线击中的物体不是目标物体,则表示目标物体被遮挡if (hit.collider.gameObject != obj){return true;}}return false;}
}
http://www.lryc.cn/news/104913.html

相关文章:

  • iOS开发-NotificationServiceExtension实现实时音视频呼叫通知响铃与震动
  • 性能调试【学习笔记】
  • 【taro react】---- 获取元素的位置和宽高等信息
  • Java【Spring】项目创建、存储和获取 Bean 的基本方式
  • docker minio安装
  • 设计模式-命令模式在Java中的使用示例-桌面程序自定义功能键
  • 分冶算法 剑指 07 重建二叉树 排序算法:剑指45 把数组排成最小的数 10-I 斐波那契数列
  • Postgresql取消正在执行的任务或强制终止正在执行的任务
  • 【Linux】Centos7 的 Systemctl 与 创建系统服务 (shell脚本)
  • Redis集群Cluster搭建
  • swing组件应用
  • Spring学习记录----十五、面向切面编程AOP+十六、Spring对事务的支持
  • Color Correction (颜色校正)
  • Unity-缓存池
  • ubuntu samba 配置常见问题
  • vue3.3-TinyMCE:TinyMCE富文本编辑器基础使用
  • 基于以太坊+IPFS的去中心化数据交易方法及平台
  • NestJS 的 拦截器 学习
  • Spring AOP 中的代理对象是怎么创建出来的?
  • 解决@Scope(“prototype“)不生效的问题
  • Mybatis 知识点
  • PHP中关于is,between,in等运算符的用法是什么?
  • 2023-07-29:华清远见嵌入式2017年线下班:文件IO笔记
  • 2023年第四届“华数杯”数学建模思路 - 复盘:光照强度计算的优化模型
  • Typescript第七章 处理错误(返回null,抛出异常,返回异常,Option类型)
  • Qt库xcb问题
  • C++ | 哈希表的实现与unordered_set/unordered_map的封装
  • 【漏洞挖掘】Xray+rad自动化批量漏洞挖掘
  • Swagger UI教程 API 文档和Node的使用
  • P5691 [NOI2001] 方程的解数