JSW
JSW

Reputation: 13

Swift SceneView increase the thickness of a wireframe

In Swift using a SceneView how would i go about increasing the line thickness of a wireframe / line. Here's some example code of my SceneView etc

            // Create an array of SCNVector3 representing the points
            var points = [SCNVector3]()
            
            for vert in mesh.verticies
            {
                if let vert = vert as? Point3D
                {
                    points.append(SCNVector3(vert.x, vert.y, vert.z))
                }
            }

            // Create an array of UInt32 representing the face indices
            var faceIndices = [UInt32]()
            
            for face in mesh.faces
            {
                if let face = face as? Face3D
                {
                    faceIndices.append(UInt32(face.a))
                    faceIndices.append(UInt32(face.b))
                    faceIndices.append(UInt32(face.c))
                }
            }

            // Create a geometry source for the points
            let pointsSource = SCNGeometrySource(vertices: points)

            // Create a geometry element for the faces
            let faceElement = SCNGeometryElement(indices: faceIndices, primitiveType: .triangles)
            
            let geometry = SCNGeometry(sources: [pointsSource], elements: [faceElement])

            let node = SCNNode(geometry: geometry)
            
            node.name = "DebugNode"
            let material = SCNMaterial()
            material.isDoubleSided = true // Disable backface culling
            
            // Set the material on your mesh
            node.geometry?.materials = [material]
            node.geometry?.firstMaterial?.diffuse.contents = mesh3D.colour;
            node.geometry?.firstMaterial?.fillMode = .fill

I've tried using a MetalMaterial and creating a MetalShader to increase the LineThickness but nothing has worked so far. I've dug through the Apple Docs and cant seem to find anything related to lineWidth or thickness for wireframes or lines.

Here's my example of a MetalMaterial, but throws numerous errors

metalMaterial.shaderModifiers = [.geometry: """
    #pragma arguments
    float lineThickness;

    #pragma body
    _geometry.position = _geometry.position;
    _geometry.lineWidth = lineThickness;
"""]

And like i said previously i cant find any obvious properties to set on the SCNNode, SCNGeometry or SCNGeometryElement.

Upvotes: 1

Views: 145

Answers (1)

Jeshua Lacock
Jeshua Lacock

Reputation: 6658

As mentioned, you'll have to render the lines yourself. I found a Metal Shader that does just that.

let sm =
"float u = _surface.diffuseTexcoord.x; \n" +
"float v = _surface.diffuseTexcoord.y; \n" +
"int u100 = int(u * 100.0); \n" +
"int v100 = int(v * 100.0); \n" +
"if (u100 % 10 == 1 || v100 % 10 == 1) { \n" +
"    // do nothing \n" +
"} else { \n" +
"    discard_fragment(); \n" +
"} \n"

let sphere = SCNSphere(radius: 1)
sphere.firstMaterial?.shaderModifiers = [SCNShaderModifierEntryPointSurface:sm]
sphere.firstMaterial?.doubleSided = true

Code from how-can-i-create-a-wireframe-texture-for-a-sphere-in-scenekit

Upvotes: 0

Related Questions