簡體   English   中英

SceneKit中的iOS自定義幾何體

[英]iOS Custom Geometry in SceneKit

我無法嘗試使用紋理貼圖在iOS模擬器上運行一個簡單的四邊形。 我已經閱讀了這里涉及類似事情的所有其他問題而且我被困住了。 輸出如下所示:

在此輸入圖像描述

紋理是這樣的:

在此輸入圖像描述

我的代碼是這樣的:

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

[編輯]我用這段代碼嘗試了很多不同的東西,似乎沒什么用。 我還沒有在Objective-C中看到適用於iOS的工作示例。 對material.diffuse.wrapS的任何更改都無效。

我之前在OpenGL中做過這種事情沒有任何問題,但我已經盯着這段代碼好幾天了,看不出我的錯誤。 任何幫助將非常感激。

@ Paul-Jan讓我走上了正確的軌道,這引出了我的答案:

將我的紋理坐標從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];

結果:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM