I'm getting wrong texture mapping with following code:
size_t numberOfFaces = mesh->mIndices.size() / 3;
size_t numberOfVertices = mesh->mVertices.size();
NSLog(@"number of faces: %d, vertices: %d", (int)numberOfFaces, (int)numberOfVertices);
for (size_t i = 0; i < numberOfVertices; i++)
{
Vertex v;
v.x = mesh->mVertices[i].x;
v.y = mesh->mVertices[i].y;
v.z = mesh->mVertices[i].z;
v.nx = mesh->mNormals[i].x;
v.ny = mesh->mNormals[i].y;
v.nz = mesh->mNormals[i].z;
v.u = mesh->mTexCoords[i].u;
v.v = mesh->mTexCoords[i].v;
vertices.push_back(v);
}
NSData *data = [NSData dataWithBytes:vertices.data() length:sizeof(Vertex) * vertices.size()];
SCNGeometrySource *verticesSource = [SCNGeometrySource geometrySourceWithData:data
semantic:SCNGeometrySourceSemanticVertex
vectorCount:numberOfVertices
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:offsetof(Vertex, x)
dataStride:sizeof(Vertex)];
SCNGeometrySource *normalsSource = [SCNGeometrySource geometrySourceWithData:data
semantic:SCNGeometrySourceSemanticNormal
vectorCount:numberOfVertices
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:offsetof(Vertex, nx)
dataStride:sizeof(Vertex)];
SCNGeometrySource *coordsSource = [SCNGeometrySource geometrySourceWithData:data
semantic:SCNGeometrySourceSemanticTexcoord
vectorCount:numberOfVertices
floatComponents:YES
componentsPerVector:2
bytesPerComponent:sizeof(float)
dataOffset:offsetof(Vertex, u)
dataStride:sizeof(Vertex)];
NSData *elements = [NSData dataWithBytes:mesh->mIndices.data() length:sizeof(int) * mesh->mIndices.size()];
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:elements
primitiveType:SCNGeometryPrimitiveTypeTriangles
primitiveCount:numberOfFaces
bytesPerIndex:sizeof(int)];
SCNGeometry *geometry = [SCNGeometry geometryWithSources:@[verticesSource, normalsSource, coordsSource] elements:@[element]];
SCNMaterial *material = [SCNMaterial new];
NSString *name = [[file.lastPathComponent stringByDeletingPathExtension] stringByAppendingPathExtension:@"jpg"];
NSString *textureFile = [[file stringByDeletingLastPathComponent] stringByAppendingPathComponent:name];
material.diffuse.contents = [UIImage imageWithContentsOfFile:textureFile];
material.specular.contents = [UIColor whiteColor];
geometry.firstMaterial = material;
Texture has wrong position on model. Original viewer works fine with file so something wrong with SceneKit code.
Aucun commentaire:
Enregistrer un commentaire