3

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
Marc Hampson
  • 257
  • 1
  • 4
  • 17

1 Answers1

4

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.

Alan
  • 1,132
  • 7
  • 15
  • Many thanks Alan, I'll convert to Swift and see how it goes. – Marc Hampson Aug 14 '17 at 12:57
  • No problem Marc, I added in some code that just explains the variables a bit more. – Alan Aug 14 '17 at 13:00
  • I've updated my original question with the new code based on yours above. Can you see anything wrong? It's now squashing the design rather than showing it as an aspect fill. Should have mentioned, this is being generated in ARKit and the width is coming out as 0.06 and height 2.0 so it's a fairly short, tall wall. The wallpaper graphic is 1200x800 but I've tried others of varying sizes. Cheers – Marc Hampson Aug 14 '17 at 13:20
  • I'm sorry Marc, not too sure what the issue might be. I'm not too experienced with ARKit yet, but that's what worked for me and I thought it might for you as well. – Alan Aug 14 '17 at 14:43
  • No worries Alan, thanks for your help. It looks like it should work. It might be my texture coordinates, although it is mapping the image. Some of this is new to me too, getting there though. Cheers. – Marc Hampson Aug 14 '17 at 14:46
  • After playing some more with this, it looks like this is the way to go, but I can't simply use the values h/w of the geometry as they are. For a shorter width than height, I set the SCNMatrix4Scale values to (8,1,0) and the texture appears correctly. I'll dig around more to work out how best to automate the figures but will set this as the correct answer. Cheers – Marc Hampson Aug 15 '17 at 07:59
  • Ah fair enough Marc. Cheers. Much appreciated. – Alan Aug 16 '17 at 07:15