How do you properly subclass SCNGeometry with your own sources and elements?

229 Views Asked by At

Trying to create my own custom SCNGeometry subclass similar to how the framework has SCNBox, SCNPyramid, SCNCapsule, etc. I've already built up my source and element arrays, but the following is a convenience initializer, not a designated one!

public convenience init(
    sources. : [SCNGeometrySource],
    elements : [SCNGeometryElement]?)

The only one I know of is super.init() but if you use that, the sources and elements are read-only.

Failed Attempt

I attempted to simply call the super.init(), then override the sources and elements properties with my own arrays but to no avail.

Note: These are the exact same two arrays that I pass to the convenience initializer above on SCNGeometry directly, along with setting the materials, and that works just fine so I know the arrays are all correct. This however doesn't work so something else is obviously going on in that convenience initializer but I can't see what.

It's really a shame Apple doesn't actually show you the source for things like this, nor have good documentation on it. I mean they mark SCNGeometry as open, but then don't give you any information on how to actually use it and so much of the API docs are completely blank!

class TestGeometry : SCNGeometry {

    override init(
        sources   : [SCNGeometrySource],
        elements  : [SCNGeometryElement],
        materials : [SCNMaterials]){

        self.mySources  = sources
        self.myElements = elements

        super.init()

        self.materials = materials
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private let mySources  : [SCNGeometrySource]
    private let myElements : [SCNGeometryElement]

    override var sources  : [SCNGeometrySource]  { mySources }
    override var elements : [SCNGeometryElement] { myElements }
}

Workaround

I have already completed all the information to successfully build up my sources and elements (again, the same ones I tried to pass into my above class), and have confirmed it works and renders correctly using the convenience initializer, then the materials property, so technically I could wrap all that up in a static factory function and be done with it, but I really want to follow Apple's API here where you instantiate a geometry via an initializer, not a factory function so I don't want to just let this go. It really stinks having all the steps 2 through 'n' completed successfully, only to trip up completely at step one... initialization!

So, what's the designated initializer for SCNGeometry that lets me specify my own vertices and materials, etc., and if there isn't one for that, how does one specify that information while using super.init()?

1

There are 1 best solutions below

0
On BEST ANSWER

Welp... thanks to the labs at DubDub this year, Apple's engineers said this isn't possible. You have to use the convenience initializer, which you can use via factory methods if you prefer. Shame.