2015-12-27 7 views
10

में मुझे आईओएस सिम्युलेटर पर चलाने के लिए एक बनावट मानचित्र के साथ एक सरल ट्रैक्टर प्राप्त करने का प्रयास करने में परेशानी हो रही है। मैंने यहां अन्य सभी प्रश्नों को पढ़ा है जो समान चीजों से निपटते हैं और मैं अटक गया हूं। उत्पादन इस तरह दिखता है:आईएसओ कस्टम ज्यामिति SceneKit

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

[संपादित करें] मैं कोशिश की है कई अलग अलग बातें:

enter image description here

मेरे कोड यह है:

enter image description here

बनावट यह है इस कोड के साथ और कुछ भी काम नहीं लगता है। मुझे अभी तक उद्देश्य-सी में एक कामकाजी उदाहरण देखने को मिला है जो आईओएस पर काम करता है। Material.diffuse.wrapS में कोई भी बदलाव कोई प्रभाव नहीं पड़ता है।

मैंने बिना किसी मुद्दे के ओपनजीएल में इस तरह की चीज की है, लेकिन मैं इस कोड पर दिनों के लिए देख रहा हूं और मेरी गलती नहीं देख सकता। किसी भी तरह की सहायता की सच में प्रशंसा की जाएगी।

+3

एक CGFloat एक * डबल 64 बिट्स iOS पर * नहीं है? आप textureSource के 'byesPerComponent' के लिए * sizeof (float) * निर्दिष्ट करते हैं। –

+0

दुर्भाग्यवश कोई अंतर नहीं, बस कोशिश की। यद्यपि बहुत तेज़ उत्तर के लिए धन्यवाद। – Brett

+0

@ पॉल-जान नीचे दिए गए मेरे उत्तर को देखें, जब मैंने आपके सुझाव की कोशिश की, तो मैंने इसे केवल बनावट स्रोत में बदल दिया, न कि बनावट डेटा में। इसे फिर से देखा और दोनों जगहों में बदल गया और यह काम किया। यदि आप इसे एक उत्तर के रूप में लिखना चाहते हैं तो मैं इसे स्वीकार करूंगा, अन्यथा मैं अपना उत्तर स्वीकार करूंगा। – Brett

उत्तर

11

@ पॉल-जन मुझे सही रास्ते, जो मुझे इस उत्तर के लिए नेतृत्व पर डाल दिया:

मेरी बनावट बदलने फिक्स यह फ्लोट करने के लिए 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]; 

परिणाम:

enter image description here

+1

क्या आप अपना उत्तर स्वीकार कर सकते हैं? –

+1

पॉल-जन को पहली बार जवाब के रूप में अपनी टिप्पणी लिखने का मौका मिलेगा। – Brett

+1

कृपया अपना खुद का जवाब स्वीकार करें, आपने सभी कड़ी मेहनत की है, और अब आपका क्यू एंड ए ज्ञान के स्टैक ओवरफ्लो ढेर के लिए एक बढ़िया जोड़ा बनाता है :) –