iosswift3scenekitscnmaterial

SCNMaterial prevent stretch


I have a custom geometry quadrangle and my texture image is displaying on it, but I want it to display as an Aspect Fill, rather than stretching or compressing to fit the space.

I'm applying the same texture to multiple walls in a room so if the image is wallpaper, it has to look correct.

Is there a way to use the following and also determine how it fills?

quadNode.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "wallpaper3.jpg")

Thanks

[UPDATE]

let quadNode = SCNNode(geometry: quad)

let (min, max) = quadNode.boundingBox

let width = CGFloat(max.x - min.x)
let height = CGFloat(max.y - min.y)

let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "wallpaper3.jpg")
material.diffuse.contentsTransform = SCNMatrix4MakeScale(Float(width), Float(height), 1)
material.diffuse.wrapS = SCNWrapMode.repeat
material.diffuse.wrapT = SCNWrapMode.repeat

quadNode.geometry?.firstMaterial = material

Solution

  • I think this might help you, It is in objective c but it should be understandable:

        CGFloat width = self.planeGeometry.width;
        CGFloat height = self.planeGeometry.length;
    
        SCNMaterial *material = self.planeGeometry.materials[4];
        material.diffuse.contentsTransform = SCNMatrix4MakeScale(width, height, 1); 
        material.diffuse.wrapS = SCNWrapModeRepeat;
        material.diffuse.wrapT = SCNWrapModeRepeat;
    

    Plane Geometry is defined as follows:

    self.planeGeometry = [SCNBox boxWithWidth:width height:planeHeight length:length chamferRadius:0]; 
    //planeHeight = 0.01;
    

    I'm using this to show horozontal planes, and the material it's made of doesn't get stretched out, but merely extends. Hoping that's what you need.

    The dimensions of the plane are defined as: (incase it is needed)

    float width = anchor.extent.x;
    float length = anchor.extent.z;
    

    This is being done in initWithAnchor method which uses the ARPlaneAnchor found on a plane.