title: How to fit a texture to a surface? ARKit SceneKit Swift
body: <p>I am very new to scenekit and 3d development in general and I'm playing around with ARKit and trying to fit a texture to a plane (well really a scnbox but only the top surface) but I'm seriously failing and also failing to find anything helpful on the web.</p>
  
  <p>I have a texture of a road that is a very long rectangular png image. width:height ratio is about 20:1</p>
  
  <p>I want to apply this texture to the surface of a table, once arkit has found the plane for me. I do not know the dimensions of the table before the app starts.</p>
  
  <p>I can currently apply a texture to this plane, and also rotate the texture as desired.</p>
  
  <p>What I would like to accomplish is to stretch the texture (keeping original ratio) so that the short sides of the plane and texture line up and then the texture continues until the end of the plane, cutting off or repeating depending on the length or ratio of the plane.</p>
  
  <p>Here is the function that gets the ScnMaterial Object</p>
  
  <pre><code>class func getRunwayMaterial() -&gt; SCNMaterial {
      let name = "runway"
      var mat = materials[name]
      if let mat = mat {
          return mat
      }
      mat = SCNMaterial()
      mat!.lightingModel = SCNMaterial.LightingModel.physicallyBased
      mat!.diffuse.contents = UIImage(named: "./Assets.scnassets/Materials/runway/runway.png")
      mat!.diffuse.wrapS = SCNWrapMode.repeat
      mat!.diffuse.wrapT = SCNWrapMode.repeat
      materials[name] = mat
      return mat!
  }
  </code></pre>
  
  <p>This is the function that should be doing the scaling and rotating of the texture on the plane.</p>
  
  <pre><code>func setRunwayTextureScale(rotation: Float? = nil, material: SCNMaterial? = nil) {
      let texture = material != nil ? material! : planeGeometry.materials[4]
      var m: SCNMatrix4 = SCNMatrix4MakeScale(1, 1, 1)
      if(rotation != nil){
          textureRotation = rotation! + textureRotation
      }
      m = SCNMatrix4Rotate(m, textureRotation, 0, 1, 0)
      texture.diffuse.contentsTransform = m
  }
  </code></pre>
  
  <p>Please help me fill in the blanks here, and if anyone has any links or articles on how to do this kind of manipulation please link me!</p>
  
  <p>Thanks!
  Ethan</p>
  
  <p>edit: btw I'm using xcode 9</p>