Skip to content

Instantly share code, notes, and snippets.

@anselm
Created May 6, 2016 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anselm/751cdcfd7c9ea2def21878d27bffbb07 to your computer and use it in GitHub Desktop.
Save anselm/751cdcfd7c9ea2def21878d27bffbb07 to your computer and use it in GitHub Desktop.
//
// a snippet for for iOS scenekit
// generate an open half cylinder in such a way that the texture stretches from 0->1 over the surface
// the idea is that this could be used for a 180' pano
//
- (SCNGeometry*) createCurve:(int)resolution width:(float)radius {
if(resolution<1)return nil;
int nquadsaxis = resolution;
int nvertsaxis = resolution+1;
int nvertices = nvertsaxis*nvertsaxis;
int ntriangles = nquadsaxis*nquadsaxis*2;
SCNVector3 normals[nvertices];
SCNVector3 vertices[nvertices];
CGPoint textures[nvertices];
int indexes[ntriangles*3];
for(int i = 0; i < nvertsaxis; i++) {
float x = cos(((float)i)*M_PI/((float)resolution));
float z = sin(((float)i)*M_PI/((float)resolution));
for(int j = 0; j < nvertsaxis; j++) {
normals[i+j*nvertsaxis] = SCNVector3Make(x,0,z);
}
}
for(int i = 0; i < nvertsaxis; i++) {
float x = -cos(((float)i)*M_PI/((float)resolution))*radius;
float z = sin(((float)i)*M_PI/((float)resolution))*radius;
for(int j = 0; j < nvertsaxis; j++) {
float y = (((float)j)/((float)resolution))*radius*2-radius;
vertices[i+j*nvertsaxis] = SCNVector3Make(x,y,z);
}
}
for(int i = 0; i < nvertsaxis; i++) {
float x = ((float)i)/((float)resolution);
for(int j = 0; j < nvertsaxis; j++) {
float y = ((float)j)/((float)resolution);
textures[i+j*nvertsaxis] = CGPointMake(x,y);
}
}
for(int i = 0 ; i < nvertices; i++) {
NSLog(@"t %f %f", textures[i].x, textures[i].y);
}
for(int i = 0; i < nquadsaxis; i++) {
for(int j = 0; j < nquadsaxis; j++) {
indexes[(i+j*nquadsaxis)*2*3+0] = i+j*nvertsaxis;
indexes[(i+j*nquadsaxis)*2*3+1] = i+j*nvertsaxis+1;
indexes[(i+j*nquadsaxis)*2*3+2] = i+j*nvertsaxis+nvertsaxis;
indexes[(i+j*nquadsaxis)*2*3+3] = i+j*nvertsaxis+1;
indexes[(i+j*nquadsaxis)*2*3+4] = i+j*nvertsaxis+nvertsaxis+1;
indexes[(i+j*nquadsaxis)*2*3+5] = i+j*nvertsaxis+nvertsaxis;
}
}
SCNGeometrySource *normalSource = [SCNGeometrySource geometrySourceWithNormals:normals count:nvertices];
SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithVertices:vertices count:nvertices];
SCNGeometrySource *textureSource = [SCNGeometrySource geometrySourceWithTextureCoordinates:textures count:nvertices];
NSData *indexData = [NSData dataWithBytes:indexes length:ntriangles*3*sizeof(int)];
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:indexData primitiveType:SCNGeometryPrimitiveTypeTriangles
primitiveCount:ntriangles bytesPerIndex:sizeof(int)];
SCNGeometry *geometry = [SCNGeometry geometryWithSources:@[vertexSource, normalSource, textureSource] elements:@[element]];
return geometry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment