ARKit学习之SCNGeometrySource加顶点、法线、纹理及索引时贴图不正确

Stella981
• 阅读 481

1、背景

  需求:通过ARKit,让用户拍摄房间时显示挑选的家具或其它模型。

  要求:需要感知房间的空间大小,让家具物体贴近现实。

2、功能实现

  由于公司不是用通用的3D模型obj、dae或者苹果官方的scn文件。

  之前对于3D建模知识完全不懂,所以只能摸索有没有更底层的方法。

  后面看例子,发现可以用SCNGeometrySource和SCNGeometryElement实现。

代码如下:

typedef struct {

float x, y, z; // position

float nx, ny, nz; // normal

float s, t;       // texture coordinates

} SourceVertex;

- (SCNGeometry *)geometryWithSourceVertex:(SourceVertex *)vertexcs faces:(int *)faces vertexNum:(int)vertexNum faceNum:(int)faceNum {

NSData *data = [NSData dataWithBytes:vertexcs length:sizeof(SourceVertex)*vertexNum];

SCNGeometrySource *vertexSource, *normalSource, *tcoordSource;

vertexSource = [SCNGeometrySource geometrySourceWithData:data

semantic:SCNGeometrySourceSemanticVertex

vectorCount:vertexNum

floatComponents:YES

componentsPerVector:3 // x, y, z

bytesPerComponent:sizeof(float)

dataOffset:offsetof(SourceVertex, x)

dataStride:sizeof(SourceVertex)];

normalSource = [SCNGeometrySource geometrySourceWithData:data

semantic:SCNGeometrySourceSemanticNormal

vectorCount:vertexNum

floatComponents:YES

componentsPerVector:3 // nx, ny, nz

bytesPerComponent:sizeof(float)

dataOffset:offsetof(SourceVertex, nx)

dataStride:sizeof(SourceVertex)];

tcoordSource = [SCNGeometrySource geometrySourceWithData:data

semantic:SCNGeometrySourceSemanticTexcoord

vectorCount:vertexNum

floatComponents:YES

componentsPerVector:2 // s, t

bytesPerComponent:sizeof(float)

dataOffset:offsetof(SourceVertex, s)

dataStride:sizeof(SourceVertex)];

NSData *eleData = [NSData dataWithBytes:faces length:faceNum*3*sizeof(int)];

SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:eleData primitiveType:SCNGeometryPrimitiveTypeTriangles primitiveCount:faceNum bytesPerIndex:sizeof(int)];

SCNGeometry * geometry = [SCNGeometry geometryWithSources:@[vertexSource,normalSource,tcoordSource]

elements:@[element]];

return geometry;

}

3. 问题

  加载完成后,纹理图片显示不正确,会有扭曲的情况。

  由于例子比较少,网上的相关问题也少,搞到我花了很多时间去研究。开始以为是API方法问题,或者数据取值导致,但对照网上的加载显示六方形的方法,检查不出有问题。

  后来只能尝试把模型转成obj文件格式,通过ModelIO去加载obj,测试后完全没问题,那只能从数据方面入手了。

  不过尝试过,直接读obj的顶点、纹理、法向量和索引,加载出来的图形也是有问题。但通过ModelIO加载出来的数据,再用SCNGeometrySource加载也没问题。

  这样比较清晰了,数据要加工处理过才行。

  网上搜索到一个从obj加载数据,然后进行合面操作,网址是:https://blog.csdn.net/qinyuanpei/article/details/49991607 。加载出来显示没问题,但有性能问题,因为算法要对面索引

  进行n*n的循环,当面索引数据大的时候,加载一个面要1分钟以上。

4. 解决方法

  通过搜索加载obj文件数据的方案发现,纹理显示不正确,是因为顶点、纹理数据和面索引对应不上,后面想到直接根据面索引,重排顶点和纹理数据就可以解决这个问题了。

代码如下:

for (int i = 0; i < facesNum; i++) {

// 第一个顶点

int index = faces[i*9] - 1;

vertexcs[i*3].x = point[index*3];

vertexcs[i*3].y = point[index*3+1];

vertexcs[i*3].z = point[index*3+2];

// 纹理

index = faces[i*9+1] - 1;

vertexcs[i*3].s = tex[index*2];

vertexcs[i*3].t = tex[index*2+1];

// 法向量

index = faces[i*9+2] - 1;

vertexcs[i*3].nx = normal[index*3];

vertexcs[i*3].ny = normal[index*3+1];

vertexcs[i*3].nz = normal[index*3+2];

// 第二个顶点

index = faces[i*9+3] - 1;

vertexcs[i*3+1].x = point[index*3];

vertexcs[i*3+1].y = point[index*3+1];

vertexcs[i*3+1].z = point[index*3+2];

// 纹理

index = faces[i*9+4] - 1;

vertexcs[i*3+1].s = tex[index*2];

vertexcs[i*3+1].t = tex[index*2+1];

// 法向量

index = faces[i*9+5] - 1;

vertexcs[i*3+1].nx = normal[index*3];

vertexcs[i*3+1].ny = normal[index*3+1];

vertexcs[i*3+1].nz = normal[index*3+2];

// 第三个顶点

index = faces[i*9+6] - 1;

vertexcs[i*3+2].x = point[index*3];

vertexcs[i*3+2].y = point[index*3+1];

vertexcs[i*3+2].z = point[index*3+2];

// 纹理

index = faces[i*9+7] - 1;

vertexcs[i*3+2].s = tex[index*2];

vertexcs[i*3+2].t = tex[index*2+1];

// 法向量

index = faces[i*9+8] - 1;

vertexcs[i*3+2].nx = normal[index*3];

vertexcs[i*3+2].ny = normal[index*3+1];

vertexcs[i*3+2].nz = normal[index*3+2];

triangleFace[i*3] = i*3;

triangleFace[i*3+1] = i*3+1;

triangleFace[i*3+2] = i*3+2;

}

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
2年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
ARKit入门
ARKit介绍ARKit是iOS11引入的一个全新的框架,使用VisualInertialOdometry(VIO,视觉惯性里程计)来精确跟踪现实世界中的真实场景。相比其它设备平台,ARKit中的VIO可以将传感器数据和CoreMotion的数据融合在一起,从而提供更为精确的信息。ARKit可以让iOS设备精确感知它如何在房间内移动,而无需外
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这