Platforms to show: All Mac Windows Linux Cross-Platform
/Mac64bit/SceneKit/Triangle Demo
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/Triangle Demo
This example is the version from Mon, 10th May 2020.
Project "Triangle Demo.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control MyControl Inherits SCNControlMBS
ControlInstance MyControl Inherits SCNControlMBS
EventHandler Sub Open()
MyView = Me.View
MyScene = New SCNSceneMBS
MyView.scene = MyScene
'MyScene.fogStartDistance=500
'MyScene.fogEndDistance=1000
'MyScene.fogColor = NSColorMBS.blackColor
myView.backgroundColor = NSColorMBS.blackColor
MyView.autoenablesDefaultLighting = True
BuildDefaultCamera
RenderScene
buildATriangle
End EventHandler
End Control
Control x Inherits TextField
ControlInstance x(0) Inherits TextField
ControlInstance x(1) Inherits TextField
ControlInstance x(2) Inherits TextField
EventHandler Sub TextChange(index as Integer)
buildATriangle
End EventHandler
End Control
Control y Inherits TextField
ControlInstance y(0) Inherits TextField
ControlInstance y(1) Inherits TextField
ControlInstance y(2) Inherits TextField
EventHandler Sub TextChange(index as Integer)
buildATriangle
End EventHandler
End Control
Control z Inherits TextField
ControlInstance z(0) Inherits TextField
ControlInstance z(1) Inherits TextField
ControlInstance z(2) Inherits TextField
EventHandler Sub TextChange(index as Integer)
buildATriangle
End EventHandler
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
buildATriangle
End EventHandler
End Control
Control Blended Inherits CheckBox
ControlInstance Blended Inherits CheckBox
EventHandler Sub Action()
buildATriangle
End EventHandler
EventHandler Sub Open()
me.Value = true
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control Label5 Inherits Label
ControlInstance Label5 Inherits Label
End Control
Control Label6 Inherits Label
ControlInstance Label6 Inherits Label
End Control
Private Sub BuildDefaultCamera()
Var MyCamera As New SCNCameraMBS
CameraNode = New SCNNodeMBS
CameraNode.Camera = MyCamera
CameraNode.Position = New SCNVector3MBS(0, 100, 200)
MyCamera.zNear = 10
MyCamera.zFar = 12000
myview.pointOfView = CameraNode
Myview.allowsCameraControl = True
myscene.rootNode.addChildNode CameraNode
End Sub
Private Sub RenderScene()
Var geometry As SCNGeometryMBS
Var node As SCNNodeMBS
Var col As Color
Var Color As NSColorMBS
AxisNode = New SCNNodeMBS
MyScene.rootNode.addChildNode AxisNode
// background light
var ambientLight As New SCNLightMBS
ambientLight.type = SCNLightMBS.SCNLightTypeAmbient
ambientLight.Color = NSColorMBS.colorWithRGB( .60, 0.6, 0.6, 1)
Var ambientnode As New SCNNodeMBS
ambientnode.Light = ambientLight
MyScene.rootNode.addChildNode ambientnode
// spot light
var light As New SCNLightMBS
light.type = SCNLightMBS.SCNLightTypeSpot
light.spotInnerAngle = 30
light.spotOuterAngle = 70
light.castsShadow = True
light.zFar = 400
var lightNode As New SCNNodeMBS
lightNode.light = light
lightNode.position = SCNVector3MBS.Vector(0, 380, 0)
MyScene.rootNode.addChildNode lightNode
Var axis As New SCNTubeMBS(0.25,0.5,256)
geometry = axis
geometry.firstMaterial.diffuse.contents = NSColorMBS.colorWithCalibratedRGB(0.5,0.5,0.5,0.8)
node = New SCNNodeMBS(geometry)
node.Position = SCNVector3MBS.Vector( 0,0,0)
AxisNode.addChildNode( node)
axis = New SCNTubeMBS(0.25,0.5,256)
geometry = axis
geometry.firstMaterial.diffuse.contents = NSColorMBS.colorWithCalibratedRGB(0.5,0.5,0.5,0.8)
node = New SCNNodeMBS(geometry)
node.Position = SCNVector3MBS.Vector( 0,0,0)
node.rotateBy New SCNVector4MBS(0, 0, 0.7071, 0.7071), node.Position
AxisNode.addChildNode( node)
axis = New SCNTubeMBS(0.25,0.5,128)
geometry = axis
geometry.firstMaterial.diffuse.contents = NSColorMBS.colorWithCalibratedRGB(0.5,0.5,0.5,0.8)
node = New SCNNodeMBS(geometry)
node.Position = SCNVector3MBS.Vector( 0,0,0)
node.rotateBy New SCNVector4MBS( 0.7071, 0, 0, 0.7071), node.Position
AxisNode.addChildNode( node)
// limit moving
var constraint As New SCNLookAtConstraintMBS(AxisNode)
constraint.gimbalLockEnabled = True
cameraNode.setConstraints Array(constraint)
lightNode.setConstraints Array(constraint)
End Sub
Private Sub buildATriangle()
' get rid of the last triangle to keep the display clean.
If triangle <>Nil Then triangle.removeFromParentNode
Var p() As VectorC
' add the three vertex in counter clockwise order
' this informs SceneKit which way the triangle is facing
For i As Integer = 2 DownTo 0
Var xx, yy, zz As Double
xx = Val(x(i).Text)
yy = Val(y(i).Text)
zz =Val(z(i).Text)
' random values for show here.
' replace with approriate colors for each vertex
Var c As Color = RGB(Rnd*255, Rnd*255, Rnd*255)
p.AddRow New VectorC( xx, yy, zz, c)
Next
' checking blended makes the triangle set the colour to blend from each of the three vertex
' turning off blending averages the three vertex colours
Var t As New Triangle3D( p, blended.value)
triangle = t.node
MyScene.rootNode.addChildNode t.node
End Sub
Property Private AxisNode As SCNNodeMBS
Property Private CameraNode As SCNNodeMBS
Property Private MyScene As SCNSceneMBS
Property Private MyView As SCNViewMBS
Property Private triangle As SCNNodeMBS
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
Class Triangle3D
Sub Constructor(pts() as vectorC, MultiColour as boolean)
node = New SCNNodeMBS
Var positions(2) As SCNVector3MBS
For i As Integer = 0 To 2
positions(i) = pts(i).VectorValue
Next
' describe the triangle
var indices() As Integer = Array(0,1,2)
'for setting up the vertex specific colour
' modify these to set
Var channelSize As Integer = 4 '
Var Channels As Integer = 3 '
Var coloursize As Integer = channelSize*channels
Var memColour As MemoryBlock
If MultiColour Then memColour = New memoryBlock(ColourSize*indices.Count)
var mem As New MemoryBlock(4*indices.count) ' for referencing the vertex
var offset As Integer
For row As Integer = 0 To 2
If MultiColour Then
' **** i have tried setting this to single and double and using and not using an alpha.
memColour.SingleValue( row*coloursize )= pts(row).red
memColour.SingleValue( row*coloursize+channelsize )= pts(row).Green
memColour.SingleValue( row*coloursize+channelsize*2 )= pts(row).blue
End If
mem.Int32Value(offset) = indices(row)
offset = offset + 4
Next
Var normals(0) As SCNVector3MBS
normals(0)=GetSurfaceNormal( positions(0), positions(1), positions(2))
//Construction of a geometry source from the corner coodinates
var vertexSource As SCNGeometrySourceMBS = SCNGeometrySourceMBS.geometrySourceWithVertices(positions)
//Construction of a geometry source from the normal vectors
var 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
var 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
var geometry As SCNGeometryMBS
If MultiColour Then
Var colourSource As SCNGeometrySourceMBS =SCNGeometrySourceMBS.geometrySourceWithData( memColour, SCNGeometrySourceMBS.SCNGeometrySourceSemanticColor,3, True, channels,channelsize,0,coloursize)
geometry = SCNGeometryMBS.geometryWithSources(Array(vertexSource, normalSource, colourSource), Array(element))
Else
Geometry = SCNGeometryMBS.geometryWithSources(Array(vertexSource, normalSource), Array(element))
End If
If MultiColour=False Then
' render the triangle from an average of it's three associated colours
Var rr, gg, bb As Double
rr = (pts(0).red+pts(1).red+pts(2).red)/3
gg = (pts(0).green+pts(1).Green+pts(2).green)/3
bb = (pts(0).blue+pts(1).blue+pts(2).blue)/3
var theMaterial As New SCNMaterialMBS
theMaterial.diffuse.contents = NSColorMBS.colorWithRGB(rr,gg,bb,1)
geometry.setMaterials Array(theMaterial)
' option, the material can be set to double sided if you want it to be visible from both sides.
End If
var TriangleNode As New SCNNodeMBS(geometry)
node.addChildNode TriangleNode
End Sub
Private Function CrossProduct(V1 as SCNVector3MBS, V2 as SCNVector3MBS) As SCNVector3MBS
Var cross As New SCNVector3MBS
cross.x=v1.y*v2.z-v1.z*v2.y
cross.y = v1.z*v2.x-v1.x*v2.z
cross.z = v1.x*v2.y-v1.y*v2.x
return cross
End Function
Private Function GetSurfaceNormal(v1 as SCNVector3MBS, v2 as SCNVector3MBS, v3 as SCNVector3MBS) As SCNVector3MBS
Var p1 As New SCNVector3MBS( v2.x-v1.x, v2.y-v1.y, v2.z-v1.z)
Var p2 As New SCNVector3MBS( v3.x-v1.x, v3.y-v1.y ,v3.z-v1.z)
Var cross As SCNVector3MBS= CrossProduct(p1, p2)
normalize(cross)
return cross
End Function
Private Sub Normalize(v as SCNVector3MBS)
Var l As Double = Sqrt((v.x*v.x)+(v.y*v.y)+(v.z*v.z))
v.x=v.x/l
v.y=v.y/l
v.z=v.z/l
End Sub
Property node As SCNNodeMBS
End Class
Class VectorC
Function Blue() As double
return Simulation.blue/255
End Function
Sub Constructor(xx as double, yy as double, zz as double, col as color)
// Calling the overridden superclass constructor.
x=xx
y=yy
z= zz
Simulation=col
End Sub
Function Green() As double
return Simulation.Green/255
End Function
Function Red() As double
return Simulation.red/255
End Function
Function VectorValue() As SCNVector3MBS
return new SCNVector3MBS( x, y, z)
End Function
Property Simulation As Color
Property x As Double
Property y As double
Property z As double
End Class
End Project
The items on this page are in the following plugins: MBS Mac64bit Plugin.