Platforms to show: All Mac Windows Linux Cross-Platform
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/Free form
This example is the version from Tue, 4th May 2020.
Project "Free form.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Mapp Inherits SCNControlMBS
ControlInstance Mapp Inherits SCNControlMBS
EventHandler Sub Open()
//Initializing my environment
MyView = Me.View
MyScene = New SCNSceneMBS
MyView.scene = MyScene
myView.autoenablesDefaultLighting = False
MyView.allowsCameraControl = True
myView.backgroundColor = NSColorMBS.whiteColor
//Initializing the camera
CameraNode = New SCNNodeMBS
Camera = New SCNCameraMBS
CameraNode.Camera = Camera
Camera.zFar = 300
Camera.zNear = 5
//Starting position of the camera
CameraNode.Position = New SCNVector3MBS(50, 70, -30)
CameraNode.orientation = New SCNVector4MBS(-0.38458, 0.767000, 0.211688, 0.467966)
MyView.pointOfView = CameraNode
//Add ambient light source
Dim AmbientLight As New SCNNodeMBS
AmbientLight.light = New SCNLightMBS
AmbientLight.Light.Type = SCNLightMBS.SCNLightTypeAmbient
AmbientLight.Light.Intensity = 400
AmbientLight.light.Color = NSColorMBS.colorWithWhite(0.75, 1)
MyScene.rootNode.addChildNode(AmbientLight)
//Add omni light source
Dim OmniLight1 As New SCNNodeMBS
OmniLight1.Position = New SCNVector3MBS(10, 10, -10)
OmniLight1.light = New SCNLightMBS
OmniLight1.Light.Type = SCNLightMBS.SCNLightTypeOmni
OmniLight1.Light.Intensity = 2000
OmniLight1.light.Color = NSColorMBS.colorWithWhite(0.75, 1)
MyScene.rootNode.addChildNode(OmniLight1)
//Add omni light source
Dim OmniLight2 As New SCNNodeMBS
OmniLight2.Position = New SCNVector3MBS(10, 10, 10)
OmniLight2.light = New SCNLightMBS
OmniLight2.Light.Type = SCNLightMBS.SCNLightTypeOmni
OmniLight2.Light.Intensity = 2000
OmniLight2.light.Color = NSColorMBS.colorWithWhite(0.75, 1)
MyScene.rootNode.addChildNode(OmniLight2)
//Create the cube
cube
End EventHandler
End Control
Sub cube()
// Following the tutorial: https://github.com/d-ronnqvist/blogpost-codesample-CustomGeometry/blob/master/CustomGeometry/CustomGeometryView.m
Dim cubeSide As Double = 15.0
Dim halfSide As Double = cubeSide / 2
//The 8 corner coordinates
//We repeat them because we want to use them in different ways
Dim positions() As SCNVector3MBS
positions.append New SCNVector3MBS(-halfSide, -halfSide, halfSide)
positions.append New SCNVector3MBS( halfSide, -halfSide, halfSide)
positions.append New SCNVector3MBS(-halfSide, -halfSide, -halfSide)
positions.append New SCNVector3MBS( halfSide, -halfSide, -halfSide)
positions.append New SCNVector3MBS(-halfSide, halfSide, halfSide)
positions.append New SCNVector3MBS( halfSide, halfSide, halfSide)
positions.append New SCNVector3MBS(-halfSide, halfSide, -halfSide)
positions.append New SCNVector3MBS(halfSide, halfSide, - halfSide)
// repeat exactly the same
positions.append New SCNVector3MBS(-halfSide, -halfSide, halfSide)
positions.append New SCNVector3MBS( halfSide, -halfSide, halfSide)
positions.append New SCNVector3MBS(-halfSide, -halfSide, -halfSide)
positions.append New SCNVector3MBS( halfSide, -halfSide, -halfSide)
positions.append New SCNVector3MBS(-halfSide, halfSide, halfSide)
positions.append New SCNVector3MBS( halfSide, halfSide, halfSide)
positions.append New SCNVector3MBS(-halfSide, halfSide, -halfSide)
positions.append New SCNVector3MBS( halfSide, halfSide, -halfSide)
// repeat exactly the same
positions.append New SCNVector3MBS(-halfSide, -halfSide, halfSide)
positions.append New SCNVector3MBS( halfSide, -halfSide, halfSide)
positions.append New SCNVector3MBS(-halfSide, -halfSide, -halfSide)
positions.append New SCNVector3MBS( halfSide, -halfSide, -halfSide)
positions.append New SCNVector3MBS(-halfSide, halfSide, halfSide)
positions.append New SCNVector3MBS(halfSide, halfSide, halfSide)
positions.append New SCNVector3MBS(-halfSide, halfSide, -halfSide)
positions.append New SCNVector3MBS( halfSide, halfSide, -halfSide)
//Description of the individual triangles that together construct the cube
Dim indices() As Integer
// bottom
indices.append 0
indices.append 2
indices.append 1
indices.append 1
indices.append 2
indices.append 3
// back
indices.append 10 //2
indices.append 14//6
indices.append 11//3
indices.append 11//3
indices.append 14//6
indices.append 15//7
// left
indices.append 16//0
indices.append 20//4
indices.append 18//2
indices.append 18//2
indices.append 20//4
indices.append 22//6
// right
indices.append 17//1
indices.append 19//3
indices.append 21//5
indices.append 19//3
indices.append 23//7
indices.append 21//5
// front
indices.append 8//0
indices.append 9//1
indices.append 12//4
indices.append 9//1
indices.append 13//5
indices.append 12//4
// top
indices.append 4
indices.append 5
indices.append 6
indices.append 5
indices.append 7
indices.append 6
Dim mem As New MemoryBlock(4 * 36)
Dim offset As Integer
For row As Integer = 0 To indices.Ubound
mem.Int32Value(offset) = indices(row)
offset = offset + 4
Next
//Determining the normal vectors
Dim normals(23) As SCNVector3MBS
normals(0) = New SCNVector3MBS(0, -1, 0)
normals(1) = New SCNVector3MBS(0, -1, 0)
normals(2) = New SCNVector3MBS(0, -1, 0)
normals(3) = New SCNVector3MBS(0, -1, 0)
normals(4) = New SCNVector3MBS(0, 1, 0)
normals(5) = New SCNVector3MBS(0, 1, 0)
normals(6) = New SCNVector3MBS(0, 1, 0)
normals(7) = New SCNVector3MBS(0, 1, 0)
normals(8) = New SCNVector3MBS(0, 0, 1)
normals(9) = New SCNVector3MBS(0, 0, 1)
normals(10) = New SCNVector3MBS(0, 0, -1)
normals(11) = New SCNVector3MBS(0, 0, -1)
normals(12) = New SCNVector3MBS(0, 0, 1)
normals(13) = New SCNVector3MBS(0, 0, 1)
normals(14) = New SCNVector3MBS(0, 0, -1)
normals(15) = New SCNVector3MBS(0, 0, -1)
normals(16) = New SCNVector3MBS(-1, 0, 0)
normals(17) = New SCNVector3MBS(1, 0, 0)
normals(18) = New SCNVector3MBS(-1, 0, 0)
normals(19) = New SCNVector3MBS(1, 0, 0)
normals(20) = New SCNVector3MBS(-1, 0, 0)
normals(21) = New SCNVector3MBS(1, 0, 0)
normals(22) = New SCNVector3MBS(-1, 0, 0)
normals(23) = New SCNVector3MBS(1, 0, 0)
//Construction of a geometry source from the corner coodinates
Dim vertexSource As SCNGeometrySourceMBS = SCNGeometrySourceMBS.geometrySourceWithVertices(positions)
//Construction of a geometry source from the normal vectors
Dim normalSource As SCNGeometrySourceMBS = SCNGeometrySourceMBS.geometrySourceWithNormals(normals)
Const SCNGeometryPrimitiveTypeTriangles = 0
//Construction of the geometry element with : the memory block of the coordinates of each triangle, the indication that the geometry is triangular, Specify how many triangles there are and the size of the blocks of information
Dim element As SCNGeometryElementMBS = SCNGeometryElementMBS.geometryElementWithData(mem, SCNGeometryPrimitiveTypeTriangles, 12, 4)
//The actual geometry then consists of the two geometry sources of the vertices and the normal vectors,
//as well as the geometry element from the triangles
Dim geometry As SCNGeometryMBS = SCNGeometryMBS.geometryWithSources(Array(vertexSource, normalSource), Array(element))
// Then we can use the geometry as usual
// Give the cube a red colored material
Dim redMaterial As New SCNMaterialMBS
redMaterial.diffuse.contents = NSColorMBS.redColor
geometry.setMaterials Array(redMaterial)
Dim NodeOne As New SCNNodeMBS(geometry)
MyScene.rootNode.addChildNode(NodeOne)
End Sub
Property Camera As SCNCameraMBS
Property CameraNode As SCNNodeMBS
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
The items on this page are in the following plugins: MBS Mac64bit Plugin.