2015-12-27 14 views
10

iOS Simulator'da çalışacak bir doku haritasına sahip basit bir dörtlü görüntülemeye çalışırken sorun yaşıyorum. Burada benzer şeylerle uğraşan diğer soruları okudum ve sıkışıp kaldım. Çıktı aşağıdaki gibidir:iOS Özel Geometri SceneKit'te

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

[Düzenle] denedim çok farklı şeyler:

enter image description here

Benim kod şudur:

enter image description here

doku şudur Bu kodla ve hiçbir şey işe yaramıyor gibi görünüyor. Henüz iOS'ta çalışan Objective-C'de çalışan bir örnek göremiyorum. Material.diffuse.wrapS için herhangi bir değişiklik yoktur.

OpenGL'de bu tür bir şeyi daha önce hiç sorun olmadan yaptım, ancak bu koda günlerce bakıyordum ve hatamı göremiyorum. Herhangi bir yardım gerçekten takdir edilecektir. Paul-Jan Bu yanıt bana yol doğru yolda, beni koymak @

+3

64 bit iOS üzerinde * a * çift bir CGFloat değil midir? TextureSource öğesinin "byesPerComponent" değeri için * sizeof (float) * değerini belirtin. –

+0

Maalesef hiç farketmedik. Ancak çok hızlı cevap için teşekkürler. – Brett

+0

@ Paul-Jan Tavsiyemi denediğimde cevabımı aşağıya bakın, sadece textureSource içinde değiştirdim ve textureData içinde değil. Tekrar baktı ve her iki yerde de değişti ve işe yaradı. Bunu bir cevap olarak yazmak isterseniz, bunu kabul edeceğim, aksi takdirde kendi cevabımı kabul edeceğim. – Brett

cevap

11

:

benim dokusunu değiştirme düzeltmeleri float CGPoint dan koordine eder.

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

Sonuç:

enter image description here

+1

Cevabınızı kabul edebilir misiniz? –

+1

Paul-Jan'a ilk olarak yorumunu bir cevap olarak yazma şansı verecekti. – Brett

+1

Lütfen kendi cevabınızı kabul edin, tüm zor işleri yaptınız, ve şimdi Soru-Cevaplarınız Bilgi StackOverflow Kazıklarına iyi bir ekleme yapıyor :) –