scenekitscenescnnodescnscenemtl-file

How to scale the material image based on user value?


I have a T-Shirt 3D Object model, and I have loaded that using SCNScene Kit. I need to change that model submesh material image size based on the user input value.

func AddNode(scene:SCNScene, viewerSetup: Insert3DViewer, modelSetup: Insert3DModel) -> SCNNode  {
    
guard let url = Bundle.main.url(forAuxiliaryExecutable: modelSetup.mesh) else {
    fatalError("Failed to find model file.")
}

let asset = MDLAsset(url: url)
guard let object = asset.object(at: 0) as? MDLMesh else {
    fatalError("Failed to get mesh from asset.")
}

// Create a material from the texture
let scatteringFunction = MDLScatteringFunction()
let material = MDLMaterial(name: "baseMaterial", scatteringFunction: scatteringFunction)

let meshUrl = Bundle.main.url(forResource: modelSetup.material, withExtension: "")
material.setProperty(MDLMaterialProperty(name: modelSetup.material, semantic: .baseColor, url: meshUrl))

for  submesh in object.submeshes!  {
    if let submesh = submesh as? MDLSubmesh {
        submesh.material = material
    }
}

// Wrap the ModelIO object in a SceneKit object
return SCNNode(mdlObject: object) }

I have used one striped image as material, I need to increase/Decrease the Strip width based on user Scaling input.

enter image description here


Solution

  • You can set wrapS and wrapT to Repeat and set the transform scale.

    node.geometry!.firstMaterial!.diffuse.contents = "image.jpg"
    node.geometry!.firstMaterial!.diffuse.contentsTransform = SCNMatrix4Mult(SCNMatrix4MakeScale(4, 4, 1), SCNMatrix4MakeTranslation(0, 0, 0))
    node.geometry!.firstMaterial!.diffuse.wrapS = .repeat
    node.geometry!.firstMaterial!.diffuse.wrapT = .repeat