Example: /Mac64bit/SceneKit/SceneKit Spheres

Online Documentation   -   Statistics   -   FAQ   -   Plugin Parts (All, Dependencies)   -   Class hierarchy

New in Version 22.2 22.3 22.4 22.5 23.0 23.1 23.2 23.3 23.4 23.5 24.0 24.1

The list of the   topics,   classes,   interfaces,   controls,   modules,   global methods by category,   global methods by name,   screenshots,   licenses   and   examples.

Platforms to show: All Mac Windows Linux Cross-Platform

/Mac64bit/SceneKit/SceneKit Spheres


Required plugins for this example: MBS Mac64bit Plugin, MBS MacBase Plugin, MBS MacCG Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Mac64bit/SceneKit/SceneKit Spheres

This example is the version from Wed, 17th Mar 2020.

Project "SceneKit Spheres.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open() #if Target64Bit and TargetMacOS then // okay #else MsgBox "Please switch project to 64-bit and run on MacOS." #endif End EventHandler
End Class
Class MainWindow Inherits Window
Control MyControl Inherits SCNControlMBS
ControlInstance MyControl Inherits SCNControlMBS
EventHandler Function MouseDown(x as Integer, y as Integer, Modifiers as Integer) As Boolean Dim p As New CGPointMBS(x,y) Dim results() As SCNHitTestResultMBS = myview.hitTest(p) If results <> Nil And results.Ubound >= 0 Then // hit something Dim result As SCNHitTestResultMBS = results(0) info.Text = "Clicked on: "+result.node.Name+" @ "+_ Str(result.localCoordinates.x)+"/"+_ Str(result.localCoordinates.y)+"/"+_ Str(result.localCoordinates.z) 'Break End If End EventHandler
EventHandler Sub Open() MyView = me.View MyScene = new SCNSceneMBS MyView.scene = MyScene MyView.backgroundColor = NSColorMBS.blackColor MyView.autoenablesDefaultLighting = true Myview.allowsCameraControl = true End EventHandler
End Control
Control SegControl Inherits SegmentedControl
ControlInstance SegControl Inherits SegmentedControl
EventHandler Sub Action(itemIndex as integer) Select case itemIndex case 0 CreateSphere case 1 CreateRow case 2 CreateGrid case 3 CreateTriangle case 4 CreateCube end Select End EventHandler
End Control
Control Info Inherits Label
ControlInstance Info Inherits Label
End Control
Sub CreateCube() MyScene = New SCNSceneMBS dim y as double = 0.0 dim radius as double = 1.0 const yCount = 7 const zCount = 7 const xCount = 7 for row as integer = 0 to yCount-1 dim z as double = 0.0 for depth as integer = 0 to zCount-1 dim x as double = 0.0 for column as integer = 0 to xCount-1 dim sphereGeometry as new SCNSphereMBS(radius) if (row mod 2 = 0) then sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.orangeColor() else sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.purpleColor() end if Dim sphereNode As New SCNNodeMBS(sphereGeometry) sphereNode.position = SCNVector3MBS.Vector(x, y, z) sphereNode.name = "Sphere "+str(row)+" "+str(depth)+" "+str(column) MyScene.rootNode.addChildNode(sphereNode) x = x + 2.0 * (radius) next z = z + 2 * (radius) next y = y + 2 * (radius) next // show scene MyView.scene = MyScene End Sub
Sub CreateGrid() MyScene = New SCNSceneMBS dim y as Double = 0 dim radius as Double = 1.0 const yCount = 20 const xCount = 20 for row as integer = 0 to yCount-1 dim x as double = 0.0 for column as integer = 0 to xCount-1 dim sphereGeometry as new SCNSphereMBS(radius) if (row + column) mod 2 = 0 then sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.orangeColor() else sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.purpleColor() end if dim sphereNode as new SCNNodeMBS(sphereGeometry) sphereNode.position = SCNVector3MBS.Vector(x, y, 0.0) sphereNode.name = "Sphere "+Str(row)+" "+Str(column) MyScene.rootNode.addChildNode(sphereNode) x = x + 2 * (radius) next y = y + 2 * (radius) next // show scene MyView.scene = MyScene End Sub
Sub CreateRow() MyScene = New SCNSceneMBS dim x as Double dim radius as Double = 1 dim numberOfSpheres as integer = 20 for i as integer = 1 to numberOfSpheres dim sphereGeometry as new SCNSphereMBS(radius) if i mod 2 = 0 then sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.orangeColor else sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.purpleColor end if dim sphereNode as new SCNNodeMBS(sphereGeometry) sphereNode.position = SCNVector3MBS.Vector(x, 0.0, 0.0) sphereNode.name = "Sphere "+Str(i) MyScene.rootNode.addChildNode(sphereNode) x = x + 3 * radius next // show scene MyView.scene = MyScene End Sub
Sub CreateSphere() MyScene = New SCNSceneMBS dim sphereGeometry as new SCNSphereMBS(1.0) dim sphereNode as new SCNNodeMBS(sphereGeometry) sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.redColor MyScene.rootNode.addChildNode(sphereNode) sphereNode.name = "Red Sphere" // second sphere dim secondSphereGeometry as new SCNSphereMBS(0.5) dim secondSphereNode as new SCNNodeMBS(secondSphereGeometry) secondSphereNode.position = New SCNVector3MBS(3.0, 0.0, 0.0) secondSphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.greenColor MyScene.rootNode.addChildNode(secondSphereNode) secondSphereNode.name = "Green Sphere" // show scene MyView.scene = MyScene End Sub
Sub CreateTriangle() MyScene = New SCNSceneMBS dim y as double = 0.0 const radius = 1.0 const yCount = 20 const xCount = 20 for row as integer = 0 to yCount-1 dim x as double = (radius) * (row) for column as integer = 0 to xCount-row-1 dim sphereGeometry as new SCNSphereMBS(radius) if ((row + column) mod 2 = 0) then sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.orangeColor() else sphereGeometry.firstMaterial.diffuse.contents = NSColorMBS.purpleColor() end if dim sphereNode as new SCNNodeMBS(sphereGeometry) sphereNode.position = New SCNVector3MBS(x, y, 0.0) sphereNode.name = "Sphere "+str(row)+" "+str(column) MyScene.rootNode.addChildNode(sphereNode) x = x + 2 * (radius) next y = y + sqrt(3.0) * (radius) next // show scene MyView.scene = MyScene End Sub
Note "Note"
Based on code found in the tutorial Introduction To SceneKit – Part 1 https://www.weheartswift.com/introduction-scenekit-part-1/
Property MyScene As SCNSceneMBS
Property MyView As SCNViewMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

See also:

The items on this page are in the following plugins: MBS Mac64bit Plugin.


💬 Ask a question or report a problem