[英]iOS Custom Geometry in SceneKit
I am having trouble trying to get a simple quad with a texture map to run on the iOS Simulator. 我无法尝试使用纹理贴图在iOS模拟器上运行一个简单的四边形。 I have read all other questions on here which deal with similar things and I am stuck.
我已经阅读了这里涉及类似事情的所有其他问题而且我被困住了。 The output looks like this:
输出如下所示:
The texture is this: 纹理是这样的:
My code is this: 我的代码是这样的:
// 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];
[Edit] I have tried many different things with this code and nothing seems to work. [编辑]我用这段代码尝试了很多不同的东西,似乎没什么用。 I am yet to see a working example in Objective-C that works on iOS.
我还没有在Objective-C中看到适用于iOS的工作示例。 Any changes to material.diffuse.wrapS has no effect.
对material.diffuse.wrapS的任何更改都无效。
I have done this kind of thing in OpenGL before without any issue, but I have been staring at this code for days and can't see my mistake. 我之前在OpenGL中做过这种事情没有任何问题,但我已经盯着这段代码好几天了,看不出我的错误。 Any help would be really appreciated.
任何帮助将非常感激。
@Paul-Jan put me on the right track, which lead me to this answer: @ Paul-Jan让我走上了正确的轨道,这引出了我的答案:
Changing my texture coordinates from CGPoint to float fixes it. 将我的纹理坐标从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];
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.