I have SCNBoxin SCNScene. After the scene animates the changes SCNBox, this is the orientation that can be seen by checking it presentationNode.orientation. This value is returned in SCNVector4. How to determine which side the SCNBoxreverse side is from the values returned in SCNVector4?
I tried to normalize the data and came up with the following data
Side 1 = (-x, 0, 0, +x)
Side 2 = (0, -x, 0 +y)
Side 3 = (-x, +x, -y, y)
Side 4 = (x, x, y, y)
Side 5 = (-x, 0, +y, 0)
Side 6 = (-x, -y, +y, -x)
Unfortunately, this is not always the case, and checking for it sometimes sometimes returns invalid sides.
Is there a reliable way to determine the front side SCNBoxwith its geometric orientation?
Edit: Based on Toios' answer, I came up with the following code that doesn't work. Hope this helps get closer to the ultimate goal. I also used the code found in Extracting vertices from stagekit to get the vertices of my SCNBoxNode.
- (NSNumber *)valueForRotation:(SCNVector4)rotation andGeometry:(SCNGeometry*)geometry {
SCNVector4 inverse = SCNVector4Make(rotation.x, rotation.y, rotation.z, -rotation.w);
CATransform3D transform = CATransform3DMakeRotation(inverse.w, inverse.x, inverse.y, inverse.z);
GLKMatrix4 matrix = GLKMatrix4Make(transform.m11, transform.m12, transform.m13, transform.m14, transform.m21, transform.m22, transform.m23, transform.m24, transform.m31, transform.m32, transform.m33, transform.m34, transform.m41, transform.m42, transform.m43, transform.m44);
GLKVector4 vector = GLKVector4Make(rotation.x, rotation.y, rotation.z, rotation.w);
GLKVector4 finalVector = GLKMatrix4MultiplyVector4(matrix, vector);
NSArray *vertexSources = [geometry geometrySourcesForSemantic:SCNGeometrySourceSemanticVertex];
SCNGeometrySource *vertexSource = vertexSources[0];
NSInteger stride = vertexSource.dataStride;
NSInteger offset = vertexSource.dataOffset;
NSInteger componentsPerVector = vertexSource.componentsPerVector;
NSInteger bytesPerVector = componentsPerVector * vertexSource.bytesPerComponent;
NSInteger vectorCount = vertexSource.vectorCount;
SCNVector3 vertices[vectorCount];
NSLog(@"vetor count %i",vectorCount);
float highestProduct = 0;
int highestVector = -1;
NSMutableArray *highVectors;
for (NSInteger i=0; i<vectorCount; i++) {
float vectorData[componentsPerVector];
NSRange byteRange = NSMakeRange(i*stride + offset,
bytesPerVector);
[vertexSource.data getBytes:&vectorData range:byteRange];
float x = vectorData[0];
float y = vectorData[1];
float z = vectorData[2];
vertices[i] = SCNVector3Make(x, y, z);
NSLog(@"x:%f, y:%f, z:%f", x, y, z);
float product = (x * finalVector.x) + (y * finalVector.y) + (z * finalVector.z);
if (product > highestProduct) {
highestProduct = product;
highestVector = i;
}
}
NSLog(@"highestProduct = %f",highestProduct);
NSLog(@"highestVector = %i",highestVector);
NSLog(@"top verticy = %f, %f, %f",vertices[highestVector].x,vertices[highestVector].y,vertices[highestVector].z);
return [NSNumber numberWithInt:highestVector];
}