Setting contents (images) on SCNCylinder is reversed (flipped)

邮差的信 提交于 2019-12-11 01:56:13

问题


I have trying to render an image on SCNCylinder object using following code:

    let coinGeometry = SCNCylinder(radius: 50, height: 55)
    coinNode = SCNNode(geometry: coinGeometry)
    coinNode.position = SCNVector3Make(0.0, 25.0, 25.0)
    coinScene.rootNode.addChildNode(coinNode)

   //Add image on cylinder shape
    let frontFacingImageMaterial = SCNMaterial()
    frontFacingImageMaterial.diffuse.contents = UIImage(named: "map")
    frontFacingImageMaterial.specular.contents = UIColor.blueColor()
    frontFacingImageMaterial.shininess = 5.0
    coinGeometry.firstMaterial = frontFacingImageMaterial

On cylinder, this image is being shown as reversed (flipped). How can I fix it?

Thanks


回答1:


Don't flip the original image data, just flip the way it gets mapped onto the geometry. Set frontFacingImageMaterial.diffuse.contentsTransform to a matrix that flips either X or Y.




回答2:


From How to flip UIImage horizontally?:

UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
                                        scale:sourceImage.scale
                           orientation:UIImageOrientationUpMirrored];

So in Swift, something like:

if let originalImage = UIImage(named: "map") {
    let materialImage = UIImage(CGImage: originalImage.CGImage!, 
                                scale: originalImage.scale, 
                                orientation: .UpMirrored)
}


来源:https://stackoverflow.com/questions/33638532/setting-contents-images-on-scncylinder-is-reversed-flipped

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!