gpt4 book ai didi

ios - 使用 SceneKit 和 SCNGeometryPrimitiveTypePoint 制作自定义几何体时如何调用 glPointSize() (或 SceneKit 等效项)

转载 作者:行者123 更新时间:2023-12-02 07:51:29 27 4
gpt4 key购买 nike

我正在编写一个 iOS 应用程序,它使用自定义几何体在 SceneKit 中渲染点云。 This post对我实现这一目标非常有帮助(尽管我将其翻译为 Objective-C),David Rönnqvist 的书《3D Graphics with SceneKit》也是如此(请参阅有关自定义几何图形的章节)。代码工作正常,但我想让点以更大的点大小渲染 - 目前点非常小。

根据 OpenGL 文档,您可以通过调用 glPointSize() 来完成此操作。据我了解,SceneKit 是构建在 OpenGL 之上的,因此我希望有一种方法可以访问此函数或使用 SceneKit 执行等效操作。任何建议将不胜感激!

我的代码如下。我还在 bitbucket 可访问 here 上发布了一个小示例应用程序.

// set the number of points
NSUInteger numPoints = 10000;

// set the max distance points
int randomPosUL = 2;
int scaleFactor = 10000; // because I want decimal points
// but am getting random values using arc4random_uniform

PointcloudVertex pointcloudVertices[numPoints];

for (NSUInteger i = 0; i < numPoints; i++) {

PointcloudVertex vertex;

float x = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));
float y = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));
float z = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));

vertex.x = (x - randomPosUL * scaleFactor) / scaleFactor;
vertex.y = (y - randomPosUL * scaleFactor) / scaleFactor;
vertex.z = (z - randomPosUL * scaleFactor) / scaleFactor;

vertex.r = arc4random_uniform(255) / 255.0;
vertex.g = arc4random_uniform(255) / 255.0;
vertex.b = arc4random_uniform(255) / 255.0;

pointcloudVertices[i] = vertex;

// NSLog(@"adding vertex #%lu with position - x: %.3f y: %.3f z: %.3f | color - r:%.3f g: %.3f b: %.3f",
// (long unsigned)i,
// vertex.x,
// vertex.y,
// vertex.z,
// vertex.r,
// vertex.g,
// vertex.b);
}

// convert array to point cloud data (position and color)
NSData *pointcloudData = [NSData dataWithBytes:&pointcloudVertices length:sizeof(pointcloudVertices)];

// create vertex source
SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithData:pointcloudData
semantic:SCNGeometrySourceSemanticVertex
vectorCount:numPoints
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:0
dataStride:sizeof(PointcloudVertex)];

// create color source
SCNGeometrySource *colorSource = [SCNGeometrySource geometrySourceWithData:pointcloudData
semantic:SCNGeometrySourceSemanticColor
vectorCount:numPoints
floatComponents:YES
componentsPerVector:3
bytesPerComponent:sizeof(float)
dataOffset:sizeof(float) * 3
dataStride:sizeof(PointcloudVertex)];

// create element
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:nil
primitiveType:SCNGeometryPrimitiveTypePoint
primitiveCount:numPoints
bytesPerIndex:sizeof(int)];

// create geometry
SCNGeometry *pointcloudGeometry = [SCNGeometry geometryWithSources:@[ vertexSource, colorSource ] elements:@[ element]];

// add pointcloud to scene
SCNNode *pointcloudNode = [SCNNode nodeWithGeometry:pointcloudGeometry];
[self.myView.scene.rootNode addChildNode:pointcloudNode];

最佳答案

我自己在 ios 中研究渲染点云,并在 twitter 上找到了一个解决方案,作者是“vade”,我想我将其发布在这里供其他人使用:

专业提示:SceneKit 着色器修改器很有用:

mat.shaderModifiers = @{SCNShaderModifierEntryPointGeometry : @"gl_PointSize = 16.0;"};

关于ios - 使用 SceneKit 和 SCNGeometryPrimitiveTypePoint 制作自定义几何体时如何调用 glPointSize() (或 SceneKit 等效项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38257339/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com