iOS SceneKit 中的自定义几何体
iOS Custom Geometry in SceneKit
我在尝试在 iOS 模拟器上获得一个带有纹理映射到 运行 的简单四边形时遇到了问题。我已经阅读了这里处理类似事情的所有其他问题,但我被卡住了。输出如下所示:
质地是这样的:
我的代码是这样的:
// v1 +----+ v0
// | |
// v2 +----+ v3
SCNVector3 vertices[] = { SCNVector3Make( 5.0, 5.0, 0.0),
SCNVector3Make( -5.0, 5.0, 0.0),
SCNVector3Make( -5.0, -5.0, 0.0),
SCNVector3Make( 5.0, -5.0, 0.0)
};
SCNVector3 normals[] = { SCNVector3Make( 0.0f, 0.0f, 1.0),
SCNVector3Make( 0.0f, 0.0f, 1.0),
SCNVector3Make( 0.0f, 0.0f, 1.0),
SCNVector3Make( 0.0f, 0.0f, 1.0)
};
CGPoint textureCoordinates[] = { CGPointMake( 1.0, 1.0),
CGPointMake( 0.0, 1.0),
CGPointMake( 0.0, 0.0),
CGPointMake( 1.0, 0.0)
};
NSUInteger vertexCount = 4;
NSMutableData *indicesData = [NSMutableData data];
UInt8 indices[] = { 0, 1, 2, 0, 2, 3};
[indicesData appendBytes:indices length:sizeof(UInt8)*6];
SCNGeometryElement *indicesElement = [SCNGeometryElement geometryElementWithData:indicesData
primitiveType:SCNGeometryPrimitiveTypeTriangles
primitiveCount:2
bytesPerIndex:sizeof(UInt8)];
NSMutableData *vertexData = [NSMutableData dataWithBytes:vertices length:vertexCount * sizeof(SCNVector3)];
SCNGeometrySource *verticesSource = [SCNGeometrySource geometrySourceWithData:vertexData
semantic:SCNGeometrySourceSemanticVertex
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(SCNVector3)];
NSMutableData *normalData = [NSMutableData dataWithBytes:normals length:vertexCount * sizeof(SCNVector3)];
SCNGeometrySource *normalsSource = [SCNGeometrySource geometrySourceWithData:normalData
semantic:SCNGeometrySourceSemanticNormal
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(SCNVector3)];
NSMutableData *textureData = [NSMutableData dataWithBytes:textureCoordinates length:vertexCount * sizeof(CGPoint)];
SCNGeometrySource *textureSource = [SCNGeometrySource geometrySourceWithData:textureData
semantic:SCNGeometrySourceSemanticTexcoord
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:2
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(CGPoint)];
SCNGeometry *geometry = [SCNGeometry geometryWithSources:@[verticesSource, normalsSource, textureSource]
elements:@[indicesElement]];
SCNMaterial *material = [SCNMaterial material];
//material.diffuse.contents = [UIColor redColor];
material.diffuse.contents = [UIImage imageNamed:@"diffuse.jpg"];
material.diffuse.wrapS = SCNWrapModeRepeat;
material.diffuse.wrapT = SCNWrapModeRepeat;
material.diffuse.contentsTransform = SCNMatrix4MakeScale( 1.0f, 1.0f, 1.0f);
material.doubleSided = YES;
material.normal.wrapS = SCNWrapModeRepeat;
material.normal.wrapT = SCNWrapModeRepeat;
// material.litPerPixel = YES;
geometry.materials = @[material];
[编辑] 我用这段代码尝试了很多不同的东西,但似乎没有任何效果。我还没有在 Objective-C 中看到适用于 iOS 的工作示例。对 material.diffuse.wrapS 的任何更改均无效。
我以前在 OpenGL 中做过这种事情,没有任何问题,但我一直盯着这段代码看了好几天,看不出我的错误。任何帮助将不胜感激。
@Paul-Jan 让我走上了正确的轨道,这让我得到了这个答案:
将我的纹理坐标从 CGPoint 更改为浮动修复它。
float textureCoordinates[] = { 1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
NSMutableData *textureData = [NSMutableData dataWithBytes:textureCoordinates length:vertexCount * sizeof(float) * 2];
SCNGeometrySource *textureSource = [SCNGeometrySource geometrySourceWithData:textureData
semantic:SCNGeometrySourceSemanticTexcoord
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:2
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(float) * 2];
结果:
我在尝试在 iOS 模拟器上获得一个带有纹理映射到 运行 的简单四边形时遇到了问题。我已经阅读了这里处理类似事情的所有其他问题,但我被卡住了。输出如下所示:
质地是这样的:
我的代码是这样的:
// v1 +----+ v0
// | |
// v2 +----+ v3
SCNVector3 vertices[] = { SCNVector3Make( 5.0, 5.0, 0.0),
SCNVector3Make( -5.0, 5.0, 0.0),
SCNVector3Make( -5.0, -5.0, 0.0),
SCNVector3Make( 5.0, -5.0, 0.0)
};
SCNVector3 normals[] = { SCNVector3Make( 0.0f, 0.0f, 1.0),
SCNVector3Make( 0.0f, 0.0f, 1.0),
SCNVector3Make( 0.0f, 0.0f, 1.0),
SCNVector3Make( 0.0f, 0.0f, 1.0)
};
CGPoint textureCoordinates[] = { CGPointMake( 1.0, 1.0),
CGPointMake( 0.0, 1.0),
CGPointMake( 0.0, 0.0),
CGPointMake( 1.0, 0.0)
};
NSUInteger vertexCount = 4;
NSMutableData *indicesData = [NSMutableData data];
UInt8 indices[] = { 0, 1, 2, 0, 2, 3};
[indicesData appendBytes:indices length:sizeof(UInt8)*6];
SCNGeometryElement *indicesElement = [SCNGeometryElement geometryElementWithData:indicesData
primitiveType:SCNGeometryPrimitiveTypeTriangles
primitiveCount:2
bytesPerIndex:sizeof(UInt8)];
NSMutableData *vertexData = [NSMutableData dataWithBytes:vertices length:vertexCount * sizeof(SCNVector3)];
SCNGeometrySource *verticesSource = [SCNGeometrySource geometrySourceWithData:vertexData
semantic:SCNGeometrySourceSemanticVertex
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(SCNVector3)];
NSMutableData *normalData = [NSMutableData dataWithBytes:normals length:vertexCount * sizeof(SCNVector3)];
SCNGeometrySource *normalsSource = [SCNGeometrySource geometrySourceWithData:normalData
semantic:SCNGeometrySourceSemanticNormal
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(SCNVector3)];
NSMutableData *textureData = [NSMutableData dataWithBytes:textureCoordinates length:vertexCount * sizeof(CGPoint)];
SCNGeometrySource *textureSource = [SCNGeometrySource geometrySourceWithData:textureData
semantic:SCNGeometrySourceSemanticTexcoord
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:2
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(CGPoint)];
SCNGeometry *geometry = [SCNGeometry geometryWithSources:@[verticesSource, normalsSource, textureSource]
elements:@[indicesElement]];
SCNMaterial *material = [SCNMaterial material];
//material.diffuse.contents = [UIColor redColor];
material.diffuse.contents = [UIImage imageNamed:@"diffuse.jpg"];
material.diffuse.wrapS = SCNWrapModeRepeat;
material.diffuse.wrapT = SCNWrapModeRepeat;
material.diffuse.contentsTransform = SCNMatrix4MakeScale( 1.0f, 1.0f, 1.0f);
material.doubleSided = YES;
material.normal.wrapS = SCNWrapModeRepeat;
material.normal.wrapT = SCNWrapModeRepeat;
// material.litPerPixel = YES;
geometry.materials = @[material];
[编辑] 我用这段代码尝试了很多不同的东西,但似乎没有任何效果。我还没有在 Objective-C 中看到适用于 iOS 的工作示例。对 material.diffuse.wrapS 的任何更改均无效。
我以前在 OpenGL 中做过这种事情,没有任何问题,但我一直盯着这段代码看了好几天,看不出我的错误。任何帮助将不胜感激。
@Paul-Jan 让我走上了正确的轨道,这让我得到了这个答案:
将我的纹理坐标从 CGPoint 更改为浮动修复它。
float textureCoordinates[] = { 1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
NSMutableData *textureData = [NSMutableData dataWithBytes:textureCoordinates length:vertexCount * sizeof(float) * 2];
SCNGeometrySource *textureSource = [SCNGeometrySource geometrySourceWithData:textureData
semantic:SCNGeometrySourceSemanticTexcoord
vectorCount:vertexCount
floatComponents:YES
componentsPerVector:2
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(float) * 2];
结果: