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

unity pcd 二进制版 简单显示文件对象(单色)

unity Point Cloud Viewer and Tool 那个插件不支持pcd二进制,而且网上到处都是AI

我恨这种AI滥用,提供不了一点价值

好了,言归正传

可以在Point Cloud Viewer and Tool这个插件报错地方转用这个代码,具体咋结合请自行研究。

部分参考:

Unity PointCloud开发:Mesh渲染点云_unity 点云特效-CSDN博客

步骤大致为

按行读取文件

发现是二进制文件

解码,计算出是三维坐标点的位置

然后直接实例化对象

为了保证性能,用了上面链接的方案,这样不用第三方库,也能兼容统信和多种平台

在一个空对象上直接挂载代码,作者设置如下

全代码+注释如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;public class PCDReader :UnitySingleton<PCDReader> 
{public string filePath = "文件路径"; // PCD文件路径,需放置在Unity工程中public GameObject _obj;void Start(){LoadPCD(filePath);}public void LoadPCD(string path)//FileStream{/*if (!File.Exists(path)){Debug.LogError("文件不存在:" + path);return;}*/// 读取文件流using (FileStream stream=new FileStream(path, FileMode.Open))using (BinaryReader reader = new BinaryReader(stream)){// Step 1: 解析头部(ASCII格式)Dictionary<string, string> header = new Dictionary<string, string>();string line;while (true){line = ReadLine(reader); // 自定义方法读取一行ASCII文本if (line == null || line.StartsWith("DATA binary"))break;string[] parts = line.Split(' ');if (parts.Length >= 2){header[parts[0]] = parts[1];}}// 验证头部关键字段if (!header.ContainsKey("FIELDS") || !header.ContainsKey("POINTS") || !header.ContainsKey("SIZE")){Debug.LogError("头部信息不完整");}// 提取元数据string[] fields = header["FIELDS"].Split(' ');int pointCount = int.Parse(header["POINTS"]);string[] sizes = header["SIZE"].Split(' ');List<Vector3> points = new List<Vector3>(); // 存储点坐标List<Color> colors = new List<Color>(); // 存储点颜色(如果存在rgb字段)// 计算每个点的大小(基于SIZE和COUNT)int pointSize = 0;foreach (string size in sizes){pointSize += int.Parse(size); // SIZE每个值通常是4(字节)}// Step 2: 读取二进制数据部分for (int i = 0; i < pointCount; i++){// 读取坐标(假设前三个字段是x,y,z)float x = reader.ReadSingle(); // 4字节浮点数float y = reader.ReadSingle();float z = reader.ReadSingle();points.Add(new Vector3(x, y, z));// 如果存在rgb字段,读取并转换为Unity颜色/*if (fields.Length >= 4 &amp;&amp; fields[3] == "rgb"){uint rgb = reader.ReadUInt32(); // rgb通常以UInt32存储float r = ((rgb >> 16) &amp; 0xFF) / 255.0f;float g = ((rgb >> 8) &amp; 0xFF) / 255.0f;float b = (rgb &amp; 0xFF) / 255.0f;colors.Add(new Color(r, g, b));}*/// 跳过其他字段(如有)int remainingSize = pointSize;//- 12 - 4; // 减去xyz(12字节)和rgb(4字节)reader.ReadBytes(remainingSize);}// Step 3: 在Unity中创建点云对象CreatePointCloud(points, colors);//return points;}}// 辅助方法:从二进制流读取一行ASCII文本private string ReadLine(BinaryReader reader){List<byte> bytes = new List<byte>();byte nextByte;while (reader.BaseStream.Position < reader.BaseStream.Length){nextByte = reader.ReadByte();if (nextByte == '\n') // 换行符结束行break;bytes.Add(nextByte);}return System.Text.Encoding.ASCII.GetString(bytes.ToArray());}// 辅助方法:创建点云GameObject(使用Mesh)private void CreatePointCloud(List<Vector3> points, List<Color> colors){GameObject pointCloud = _obj;/*new GameObject("PointCloud");Mesh mesh = new Mesh();// mesh.vertices = points.ToArray();// 设置颜色(如果存在)/*if (colors.Count == points.Count){mesh.colors = colors.ToArray();}#1#// 创建MeshRenderer和MeshFilterMeshFilter meshFilter = pointCloud.AddComponent<MeshFilter>();MeshRenderer meshRenderer = pointCloud.AddComponent<MeshRenderer>();meshFilter.mesh = mesh;// 使用默认材质(可通过Unity材质自定义)meshRenderer.material = new Material(Shader.Find("Standard"));*/CreateMesh(points.Count,points);/*for (int i = 0; i < points.Count; i++){GameObject _obj=GameObject.Instantiate(pointCloud);_obj.transform.position = points[i];}*/Debug.Log("点云加载完成,点数: " + points.Count);}public  Mesh meshNeed;void CreateMesh(int num,List<Vector3> point){int _PointNum = num;GameObject pointObj = new GameObject();pointObj.name = "PointCloud";// 绑定Mesh组件pointObj.AddComponent<MeshFilter>();pointObj.AddComponent<MeshRenderer>();// 为点云创建新的MeshMaterial mat = new Material(Shader.Find("Custom/VertexColor"));pointObj.GetComponent<MeshFilter>().mesh = meshNeed;pointObj.GetComponent<MeshRenderer>().material = mat;Vector3[] points = new Vector3[num];Color[] colors = new Color[num];int[] indecies = new int[num];for (int i = 0; i < num; ++i){points[i] = point[i];indecies[i] = i;colors[i] = Color.white;}meshNeed.vertices = points;meshNeed.colors = colors;meshNeed.SetIndices(indecies, MeshTopology.Points, 0);// 设置Mesh的渲染类型为PointmeshNeed.SetIndexBufferParams(num, UnityEngine.Rendering.IndexFormat.UInt32);}}


文章转载自:

http://Vgx2t13y.sskkf.cn
http://T6aQead5.sskkf.cn
http://OttNoIA5.sskkf.cn
http://xd838HG4.sskkf.cn
http://FTGUaAvL.sskkf.cn
http://cB9qKZmH.sskkf.cn
http://z3lzwcgx.sskkf.cn
http://FqB1G8Bp.sskkf.cn
http://cHKaL629.sskkf.cn
http://P4rGfKZ0.sskkf.cn
http://YiAV6IFJ.sskkf.cn
http://UMW5RS3R.sskkf.cn
http://NP94ISjG.sskkf.cn
http://sjcjiCbF.sskkf.cn
http://wxrvpTaC.sskkf.cn
http://dUgPnpBK.sskkf.cn
http://Nkr9xFiZ.sskkf.cn
http://PaJPTFFH.sskkf.cn
http://tIgZseZY.sskkf.cn
http://x9INwRoI.sskkf.cn
http://AOjxLqhv.sskkf.cn
http://Rz8E3L4b.sskkf.cn
http://v9NeIb5H.sskkf.cn
http://LIASyRNz.sskkf.cn
http://ffa9n35z.sskkf.cn
http://NR6IH7Je.sskkf.cn
http://3TfSCK9S.sskkf.cn
http://dSkecVui.sskkf.cn
http://fimpYW8C.sskkf.cn
http://ivhzh2kh.sskkf.cn
http://www.dtcms.com/a/380669.html

相关文章:

  • 面试题:Redis要点总结(复制、哨兵、集群)
  • Leetcode 18 java
  • Redis集群为何采用16384个槽的设计?
  • 《树与二叉树详解:概念、结构及应用》
  • Certimate SSL证书自动申请部署
  • 《Spring事务的失效》
  • Maya绑定:小球挤压拉伸变形详细绑定(晶格、簇、测量工具、节点编辑器)
  • 【比亚迪璇玑架构深度解析:重新定义智能电动汽车的“整车智能”】
  • jdbc DAO封装及BaseDAO工具类
  • jajajajajajajava
  • 自动生成链接
  • LeetCode 3258.统计满足K约束的子字符串数量 I
  • “量子能量泵”:一种基于并联电池与电容阵的动态直接升压架构
  • fastapi 使用本地资源自定义swagger文档
  • Vue FullPage.js 完整使用指南:Vue 3 官方全屏滚动解决方案
  • ARM IRQ中断
  • Ruoyi-vue-plus-5.x第八篇文件管理与存储: 8.2 OSS云存储集成
  • 解决:NVIDIA-SMI couldn‘t find libnvidia-ml.so library in your system.
  • 【LLM】VLLM:容器运行 ModelScope 模型
  • HarmonyOS 应用开发深度解析:基于 Stage 模型与 ArkUI 的跨组件状态共享最佳实践
  • TOGAF——战术性调整,战略性变更
  • 【计算机 UTF-8 转换为本地编码的含义】
  • 当人工智能遇上知识检索:RAG技术的深度解析与实践探索
  • 在线商城管理系统功能清单的系统设计
  • SLAM 系统设计是如何保证前端(tracking/VO)和后端(优化/BA/图优化)如何同步实时性思路汇总思考
  • 代码随想录二刷之“动态规划”~GO
  • zynq arm全局计时器和私有定时器
  • TCP套接字的使用
  • 红日靶场(三)——个人笔记
  • Linux 进程和线程基础知识解析