Persevering with my investigation into SceneKit
Some time again, I wrote a piece concerning the Swift Algorithms library. A set of code primitives developed by Apple that — effectively — sooner or later might make it into the core Swift language. Implementations which might be in all probability higher items of code than you or I might have time to craft. Information buildings like stacks and double-ended arrays — code you must use relatively than reinvent.
In the identical vein, I need to proceed an exploration of customized geometric meshes and shapes, that’s, reusing code that Apple has already written, though maybe a little bit otherwise from the Swift Algorithms library.
Now, in SceneKit, you’ve gotten half a dozen predefined shapes you’ll be able to construct, like
SCNBox. They’re all good, however generally you need/want extra.
Extra, you may get maybe a little bit simpler by making use of these predefined shapes in your customized geometry — be part of me to take a look at how you should utilize SceneKit to construct the naked bones of your mannequin after which add to it later. Be warned, I plan to make use of the createGeometry routine I printed in this paper earlier within the month.
As earlier than, I need to begin this piece with some code with an extension to extract a template of vertices to construct a given form. Apologies prematurely for this code, which in frequent with a few of the different SceneKit snip-lets seems to be prefer it belongs in a pc science class.
Now just a few notes; as I stated, this can be a template, so within the case of ordinary objects, I get again one thing that’s 1x1x1, it doesn’t matter what the dimensions I put in.
To make the consequence a little bit extra sensible, I added the code to probably multiply the template’s dimension. Right here is a picture of the information I re-imported and redrawn as a customized object in SceneKit.
To be clear, I didn’t calculate the vertices for this sphere. SceneKit did all the maths. I simply extracted them after which used them to construct the customized sphere you see right here. The code to place all of it collectively seems to be like this.
One thing odd it’s possible you’ll discover in the event you run this code and look fastidiously is that you just get again greater than 600 vertices; it has 625 — the vertex past 600 types this form.
Bon — Now it’s possible you’ll ask at this level the place we’re going with this. I’ve in spite of everything completed little greater than merely made a job I may have completed utilizing a strains materials so much more durable. So be part of me to have some enjoyable. Given you’ve gotten the co-ordinates, modify just a few of them by including the next line code BEFORE you rebuild the geometry—
if vectors![i].y > 1.5 || vectors![i].y < -1.5 || vectors![i].x > 1.5 || vectors![i].x < -1.5 let newVector = SCNVector3(vectors![i].x * 1.1, vectors![i].y * 1.1, vectors![i].z * 1.1)vectors![i] = newVector
After which you’ll get this — cosmic rock form.
After all, you’ll be able to have hours of enjoyable with this; attempt enjoying round — add this code to double the variety of vertices and check out the magic mountain code I confirmed on this article.
After all, 600 vertex isn’t that many, double them with this quick piece of code.
Code that produces this — and no I’m not fairly positive what’s going on with the core there — I go away it to you to analyze.
Certainly you are able to do the identical for the longitude strains too, however sufficient stated — the actual enjoyable begins while you don’t use strains, however determine how one can use triangles.
for i in stride(from: 0, to: 3600, by: 1)
indices.append(Int32(i + 0))
indices.append(Int32(i + 1))
indices.append(Int32(i + 25)) indices.append(Int32(i + 1))
indices.append(Int32(i + 26))
indices.append(Int32(i + 25))
Now we will create a planet that is stuffed with magic mountains.
With the “fait au complet” altering our geometry coloring technique to alter the colour relying on the land’s top. I did so with the picture main this text.
After all, this isn’t fairly excellent — to get there you must work more durable, more durable as in matching the sides, a activity described on this paper.
All of which brings me to the top of this quick piece. I hope you discover it helpful. Strive another shapes, attempt playing around with the colours and the vertex returned… it is an actual rabbit gap.