nicklockwood / Euclid

A Swift library for creating and manipulating 3D geometry
MIT License
644 stars 53 forks source link

Create custom 3d #49

Closed chellem closed 3 years ago

chellem commented 3 years ago

You can create a Mesh in Euclid by manually creating an array of Polygons, but that's pretty tedious

Can you please provide an example of this please ?

Am not seeing anything when trying to do this.

nicklockwood commented 3 years ago

@chellem can you post an example of what you've tried?

chellem commented 3 years ago

I have something like this. Am sure that polygons and scene are not empty

let mesh = Mesh(polygons)
let geometry = SCNGeometry(mesh)
let node = SCNNode(geometry: geometry)
scene.rootNode.addChildNode(node)
nicklockwood commented 3 years ago

@nicklockwood you've completed ignored my other issue. (Big thanks for the support )

I didn't realize you were the same user since you opened a new ticket. I'm not sure the sarcasm was called for.

chellem commented 3 years ago

@nicklockwood can you help ?

nicklockwood commented 3 years ago

@chellem Euclid has no code for loading models, so for that you'd need to use SceneKit or ModelIO, or something else. Here is some code to load the model using SceneKit, create a Euclid Mesh, and then convert back to SceneKit for display.

(I've used the .usdz version of the model since SceneKit didn't like the .obj).

// load scene
guard let url = Bundle.main.url(forResource: "T-shirt", withExtension: "usdz"),
      let importedScene = try? SCNScene(url: url, options: [
          .flattenScene: true,
          .createNormalsIfAbsent: true,
          .convertToYUp: true,
      ])
else {
    preconditionFailure("Failed to load model")
}

// create Mesh
var importedNode = importedScene.rootNode
var mesh: Mesh!
while true {
    if let geometry = importedNode.geometry {
        mesh = Mesh(geometry)
        break
    }
    guard let childNode = importedNode.childNodes.first else {
        preconditionFailure("Model contained no geometry")
    }
    importedNode = childNode
}

// create SCNNode
let geometry = SCNGeometry(mesh)
let node = SCNNode(geometry: geometry)
scene.rootNode.addChildNode(node)