Mam problem z uzyskaniem prostego kwadratu z mapą tekstur do uruchomienia na symulatorze systemu iOS. Przeczytałem wszystkie inne pytania dotyczące podobnych rzeczy i utknąłem. Wyjście wygląda następująco:Niestandardowa geometria iOS w programie SceneKit
Tekstura jest taka:
Mój kod to:
// 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];
[Edytuj] Próbowałem wiele różnych rzeczy z tym kodem i nic nie działa. Jeszcze nie widzę działającego przykładu w Objective-C, który działa na iOS. Wszelkie zmiany w pliku material.diffuse.wrapS nie przynoszą żadnego efektu.
Zrobiłem tego rodzaju rzeczy w OpenGL wcześniej bez żadnego problemu, ale patrzyłem na ten kod przez kilka dni i nie widzę mojego błędu. Każda pomoc będzie naprawdę doceniona.
nie jest CGFloat * a * podwójne na 64 bitach z iOS? Określasz * sizeof (float) * dla 'byesPerComponent' of textureSource. –
Nie ma różnicy, niestety, wypróbowałem to. Dziękuję za bardzo szybką odpowiedź. – Brett
@ Paul-Jan Zapoznaj się z moją odpowiedzią poniżej, kiedy spróbowałem twojej sugestii, zmieniłem ją tylko w textureSource, a nie w textureData. Patrzyłem na to jeszcze raz i zmieniłem się w obu miejscach i zadziałało. Jeśli chcesz napisać to jako odpowiedź, zaakceptuję to, w przeciwnym razie przyjmuję moją własną odpowiedź. – Brett