2015-12-27 9 views
10

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

enter image description here

Tekstura jest taka:

enter image description here

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.

+3

nie jest CGFloat * a * podwójne na 64 bitach z iOS? Określasz * sizeof (float) * dla 'byesPerComponent' of textureSource. –

+0

Nie ma różnicy, niestety, wypróbowałem to. Dziękuję za bardzo szybką odpowiedź. – Brett

+0

@ 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

Odpowiedz

11

@ Paul-Jan położył mnie na właściwy trop, który doprowadzi mnie do tej odpowiedzi:

zmianie mojego współrzędne tekstury z CGPoint unosić przytwierdza to.

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]; 

Wynik:

enter image description here

+1

czy możesz zaakceptować swoją odpowiedź? –

+1

Daje Paul-Janowi szansę na napisanie swojego komentarza jako odpowiedzi w pierwszej kolejności. – Brett

+1

Zaakceptuj własną odpowiedź, wykonałeś całą ciężką pracę, a teraz twoje pytania i odpowiedzi stanowią doskonałe uzupełnienie stosu wiedzy StackOverflow :) –

Powiązane problemy