https://www.dropbox.com/s/xre5veks7e4qa ... 2.zip?dl=0
Create a folder called sound and place the rock2.wav (or any other humming sound) inside it.
Save the code below in parent to 'sound' folder.
If it won't run - try setting OpenGL as subsystem.
Code has been fixed and works perfectly on x86 too.
Thank you applePi.
I completely forgot about the cone parameter problem.
Code: Select all
;Deep Space Explorer - Tranquility (Flyby and follow)
;Create a folder called sound and
;Add an engine humming sound and load it. (rock2.wav)
;Have fun with this sloppy code :-)
InitEngine3D()
InitSprite()
InitKeyboard()
InitSound()
Structure Vector3
x.f
y.f
z.f
EndStructure
Structure _par
id.i
tx.i
ma.i
EndStructure
Structure _mes
id.i
ms.i
tx.i
ma.i
EndStructure
Structure _object
block._mes
box._mes
connect._mes[7]
eng._mes[2]
sections._mes[3]
par._par[3]
main.i
rot.i[2]
snod.i
EndStructure
Declare.i CreateShip()
Declare.i ReturnHabitatTexture(Win.i = #True)
Declare.i ReturnConnectionTexture()
Declare.i FakeSkyTexture()
Declare Normalize(*V.Vector3)
Declare CreateTube(meshID.i, outerRadius.f, innerRadius.f, height.f, numSegBase.i=16, numSegHeight.i=1)
Declare.i ReturnFlareTexture(Color = $FF00FFF3)
Global shp._object, sn.i, ret.i
OpenWindow(0, 0, 0, 1024, 768, "Deep Space Explorer - Tranquility (DK_PETER)", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)
Add3DArchive("sound", #PB_3DArchive_FileSystem)
CreateCamera(0, 0, 0, 100, 100)
CameraRange(0, 0, 505)
CameraFOV(0, 46)
;Home made Skybox
shp\box\ms = CreateCube(#PB_Any, 250)
shp\box\tx = FakeSkyTexture()
shp\box\ma = CreateMaterial(#PB_Any, TextureID(shp\box\tx))
MaterialCullingMode(shp\box\ma, #PB_Material_NoCulling)
shp\box\id = CreateEntity(#PB_Any, MeshID(shp\box\ms), MaterialID(shp\box\ma), 0, 0, 0)
Debug "Skybox created... MaterialID: " + Str(MaterialID(shp\box\ma))
ret = CreateShip()
;Check if there is sound and attach it...
sn = LoadSound3D(#PB_Any, "rock2.wav")
If IsSound3D(sn)
Debug "3D sound loaded - SoundID" + Str(SoundID3D(sn))
SoundVolume3D(sn, 100)
SoundRange3D(sn, 0, 1)
PlaySound3D(sn)
AttachNodeObject(shp\snod, SoundID3D(sn))
AttachNodeObject(shp\main, NodeID(shp\snod))
Debug "3D sound attached.."
Else
Debug "no sound"
EndIf
RotateNode(shp\main, 0, -90, 0)
MoveNode(shp\main, 0.2, 0.3, 3)
Repeat
Repeat
ev = WindowEvent()
If ev = #PB_Event_CloseWindow:Quit = 1:EndIf
Until ev = 0
RotateNode(shp\rot[0], 0.1, 0, 0,#PB_Relative)
RotateNode(shp\rot[1], 0.1, 0, 0,#PB_Relative)
MoveNode(shp\main, 0, 0, -0.002, #PB_Relative)
SoundListenerLocate(NodeX(shp\snod), NodeY(shp\snod), NodeZ(shp\snod))
RenderWorld()
ExamineKeyboard()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Procedure.i ReturnHabitatTexture(Win.i = #True)
Protected im.i, tx.i, x.i, y.i, ma
im = CreateImage(#PB_Any, 2048, 2048)
StartDrawing(ImageOutput(im))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(0,0,2048,2048, $FF111111)
For x = 0 To 2048 Step 30
For y = 0 To 2048 Step 50
Box(x, y, 15, 5, RGBA(255, 255, 255, Random(255,20)))
Next y
Next x
If Win = #True : Box(1024, 1010, 1024, 30, $FFEB4A00) : EndIf
StopDrawing()
ResizeImage(im, 1024, 1024)
tx = CreateTexture(#PB_Any, 1024, 1024)
StartDrawing(TextureOutput(tx))
DrawImage(ImageID(im), 0, 0)
StopDrawing()
FreeImage(im)
Debug "ReturnHabitatTexture() : TextureID: " + Str(TextureID(tx))
ProcedureReturn tx
EndProcedure
Procedure.i ReturnConnectionTexture()
Protected im.i, ma.i, tx.i, x.i, y.i, w.i, h.i, c.i, co.i
im = CreateImage(#PB_Any, 2048, 2048)
StartDrawing(ImageOutput(im))
For c = 0 To 200
x = -50 + Random(2300,0) : y = -50 + Random(2300,0)
w = Random(200, 10) : h = Random(200, 10)
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Gradient)
co = Random(200, 100)
FrontColor(RGBA(co, co, co, Random(255,210)))
BackColor(RGBA(co, co, co, 50))
LinearGradient(x, y, w, h)
Box(x, y, w, h)
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Outlined)
Box(x, y, w, h, RGBA(20, 20, 20, Random(255,70)))
Next c
Box(1024, 1010, 256, 30, $FFEB4A00)
StopDrawing()
ResizeImage(im, 1024, 1024)
tx = CreateTexture(#PB_Any, 1024, 1024)
StartDrawing(TextureOutput(tx))
DrawImage(ImageID(im), 0, 0)
StopDrawing()
FreeImage(im)
Debug "ReturnConnectionTexture() : TextureID: " + Str(TextureID(tx))
ProcedureReturn tx
EndProcedure
Procedure.i FakeSkyTexture()
Protected im.i, ma.i, tx.i, x.i, y.i, w.i, h.i, c.i, co.i
im = CreateImage(#PB_Any, 1024, 1024)
StartDrawing(ImageOutput(im))
For c = 0 To 20000
x = Random(1024,0) : y = Random(1024,0)
w = Random(200, 10) : h = Random(200, 10)
DrawingMode(#PB_2DDrawing_AlphaBlend)
co = Random(200, 100)
Plot(Random(1023,1), Random(1023,1), RGBA(co, co, co, Random(255,10)))
Next c
Box(1024, 1010, 256, 30, $FFEB4A00)
StopDrawing()
ResizeImage(im, 1024, 1024)
tx = CreateTexture(#PB_Any, 1024, 1024)
StartDrawing(TextureOutput(tx))
DrawImage(ImageID(im), 0, 0)
StopDrawing()
FreeImage(im)
Debug "FakeSkyTexture() : TextureID: " + Str(TextureID(tx))
ProcedureReturn tx
EndProcedure
Procedure.i ReturnFlareTexture(Color = $FF00FFF3)
Protected enmat.i, entex.i
Entex = CreateTexture(#PB_Any, 20, 20)
StartDrawing(TextureOutput(entex))
DrawingMode(#PB_2DDrawing_Gradient)
FrontColor(Color)
BackColor($FFFFFFFF)
CircularGradient(10, 10, 7)
Circle(10, 10, 8)
StopDrawing()
Debug "ReturnFlareTexture() : TextureID: " + Str(TextureID(entex))
ProcedureReturn entex
EndProcedure
Procedure.i CreateShip()
Protected before.s, After.s
before = Str((MemoryStatus(#PB_System_FreePhysical) / 1024) / 1024) + " mb."
Debug "Memory Before creation : " + before
shp\main = CreateNode(#PB_Any, 0, 0, 0) ;attaching everything below to nodes
shp\snod = CreateNode(#PB_Any, 1.825, 0, 0)
shp\rot[0] = CreateNode(#PB_Any, -0.7, 0, 0)
shp\rot[1] = CreateNode(#PB_Any, 0.7, 0, 0)
shp\sections[0]\ms = CreateSphere(#PB_Any, 0.2)
shp\sections[0]\tx = ReturnHabitatTexture()
shp\sections[0]\ma = CreateMaterial(#PB_Any, TextureID(shp\sections[0]\tx))
MaterialCullingMode(shp\sections[0]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\sections[0]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\sections[0]\ma, #PB_Material_DepthWrite, #True)
shp\sections[0]\id = CreateEntity(#PB_Any,MeshID(shp\sections[0]\ms), MaterialID(shp\sections[0]\ma), -1.5, 0, 0)
Debug "1 - Section 0 - MaterialID: " + Str(MaterialID(shp\sections[0]\ma))
shp\sections[1]\tx = ReturnHabitatTexture(#False)
shp\sections[1]\ma = CreateMaterial(#PB_Any, TextureID(shp\sections[1]\tx))
MaterialCullingMode(shp\sections[1]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\sections[1]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\sections[1]\ma, #PB_Material_DepthWrite, #True)
shp\sections[1]\ms = CreateSphere(#PB_Any, 0.2)
shp\sections[1]\id = CreateEntity(#PB_Any,MeshID(shp\sections[1]\ms), MaterialID(shp\sections[1]\ma), 0, 0, 0)
Debug "2 - Section 1 - MaterialID: " + Str(MaterialID(shp\sections[1]\ma))
shp\sections[2]\tx = ReturnHabitatTexture(#False)
shp\sections[2]\ma = CreateMaterial(#PB_Any, TextureID(shp\sections[2]\tx))
MaterialCullingMode(shp\sections[2]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\sections[2]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\sections[2]\ma, #PB_Material_DepthWrite, #True)
shp\sections[2]\ms = CreateSphere(#PB_Any, 0.2)
shp\sections[2]\id = CreateEntity(#PB_Any,MeshID(shp\sections[2]\ms), MaterialID(shp\sections[2]\ma), 1.2, 0, 0)
Debug "3 - Section 2 - MaterialID: " + Str(MaterialID(shp\sections[2]\ma))
shp\connect[0]\tx = ReturnConnectionTexture()
shp\connect[0]\ma = CreateMaterial(#PB_Any, TextureID(shp\connect[0]\tx))
MaterialCullingMode(shp\connect[0]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\connect[0]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\connect[0]\ma, #PB_Material_DepthWrite, #True)
shp\connect[0]\ms = CreateCylinder(#PB_Any, 0.025, 3.2)
shp\connect[0]\id = CreateEntity(#PB_Any, MeshID(shp\connect[0]\ms), MaterialID(shp\connect[0]\ma), 0, 0, 0)
RotateEntity(shp\connect[0]\id, 0, 0, 90)
Debug "4 - Connect 0 - MaterialID: " + Str(MaterialID(shp\connect[0]\ma))
shp\block\tx = ReturnConnectionTexture()
shp\block\ma = CreateMaterial(#PB_Any, TextureID(shp\block\tx))
MaterialCullingMode(shp\block\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\block\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\block\ma, #PB_Material_DepthWrite, #True)
ScaleMaterial(shp\block\ma, 2, 2)
shp\block\ms = CreateCube(#PB_Any, 0.2)
shp\block\id = CreateEntity(#PB_Any, MeshID(shp\block\ms), MaterialID(shp\block\ma), 1.7, 0, 0)
Debug "5 - Blok - MaterialID: " + Str(MaterialID(shp\block\ma))
shp\eng[0]\tx = ReturnConnectionTexture()
shp\eng[0]\ma = CreateMaterial(#PB_Any, TextureID(shp\eng[0]\tx))
MaterialCullingMode(shp\eng[0]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\eng[0]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\eng[0]\ma, #PB_Material_DepthWrite, #True)
shp\eng[0]\ms = CreateCone(#PB_Any, 0.03, 0.04, 16, 16)
shp\eng[0]\id = CreateEntity(#PB_Any, MeshID(shp\eng[0]\ms),MaterialID(shp\eng[0]\ma), 1.8, -0.05, 0)
RotateEntity(shp\eng[0]\id, 0, 0, 90)
Debug "6 - Engine 0 - MaterialID: " + Str(MaterialID(shp\eng[0]\ma))
shp\eng[1]\tx = ReturnConnectionTexture()
shp\eng[1]\ma = CreateMaterial(#PB_Any, TextureID(shp\eng[1]\tx))
MaterialCullingMode(shp\eng[1]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\eng[1]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\eng[1]\ma, #PB_Material_DepthWrite, #True)
shp\eng[1]\ms = CreateCone(#PB_Any, 0.03, 0.04, 16, 16)
shp\eng[1]\id = CreateEntity(#PB_Any, MeshID(shp\eng[1]\ms),MaterialID(shp\eng[1]\ma), -1.8, -0.05, 0)
RotateEntity(shp\eng[1]\id, 0, 0, 90)
Debug "7 - Engine 1 - MaterialID: " + Str(MaterialID(shp\eng[1]\ma))
shp\connect[1]\tx = ReturnConnectionTexture()
shp\connect[1]\ma = CreateMaterial(#PB_Any, TextureID(shp\connect[1]\tx))
MaterialCullingMode(shp\connect[1]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\connect[1]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\connect[1]\ma, #PB_Material_DepthWrite, #True)
shp\connect[1]\ms = CreateCylinder(#PB_Any, 0.01, 1)
shp\connect[1]\id = CreateEntity(#PB_Any, MeshID(shp\connect[1]\ms), MaterialID(shp\connect[1]\ma), 0, 0, 0)
RotateEntity(shp\connect[1]\id, 90, 0, 90)
Debug "8 - Connect 1 - MaterialID: " + Str(MaterialID(shp\connect[1]\ma))
shp\connect[2]\tx = ReturnConnectionTexture()
shp\connect[2]\ma = CreateMaterial(#PB_Any, TextureID(shp\connect[2]\tx))
MaterialCullingMode(shp\connect[2]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\connect[2]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\connect[2]\ma, #PB_Material_DepthWrite, #True)
shp\connect[2]\ms = CreateCylinder(#PB_Any, 0.01, 1)
shp\connect[2]\id = CreateEntity(#PB_Any, MeshID(shp\connect[2]\ms), MaterialID(shp\connect[2]\ma), 0, 0, 0)
RotateEntity(shp\connect[2]\id, 0, 90, 0)
Debug "9 - Connect 2 - MaterialID: " + Str(MaterialID(shp\connect[2]\ma))
shp\connect[5]\tx = ReturnConnectionTexture()
shp\connect[5]\ma = CreateMaterial(#PB_Any, TextureID(shp\connect[5]\tx))
MaterialCullingMode(shp\connect[5]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\connect[5]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\connect[5]\ma, #PB_Material_DepthWrite, #True)
ScaleMaterial(shp\connect[5]\ma, 1,20)
shp\connect[5]\ms = CreateTube(#PB_Any, 0.55, 0.50, 0.07, 40, 1)
shp\connect[5]\id = CreateEntity(#PB_Any, MeshID(shp\connect[5]\ms), MaterialID(shp\connect[5]\ma), 0, 0, 0)
RotateEntity(shp\connect[5]\id, 0, 90, 90)
Debug "10 - Connect 5 - MaterialID: " + Str(MaterialID(shp\connect[5]\ma))
shp\connect[3]\tx = ReturnConnectionTexture()
shp\connect[3]\ma = CreateMaterial(#PB_Any, TextureID(shp\connect[3]\tx))
MaterialCullingMode(shp\connect[3]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\connect[3]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\connect[3]\ma, #PB_Material_DepthWrite, #True)
shp\connect[3]\ms = CreateCylinder(#PB_Any, 0.01, 1)
shp\connect[3]\id = CreateEntity(#PB_Any, MeshID(shp\connect[3]\ms), MaterialID(shp\connect[3]\ma), 0, 0, 0)
RotateEntity(shp\connect[3]\id, 90, 0, 90)
Debug "11 - Connect 3 - MaterialID: " + Str(MaterialID(shp\connect[3]\ma))
shp\connect[4]\tx = ReturnConnectionTexture()
shp\connect[4]\ma = CreateMaterial(#PB_Any, TextureID(shp\connect[4]\tx))
MaterialCullingMode(shp\connect[4]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\connect[4]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\connect[4]\ma, #PB_Material_DepthWrite, #True)
shp\connect[4]\ms = CreateCylinder(#PB_Any, 0.01, 1)
shp\connect[4]\id = CreateEntity(#PB_Any, MeshID(shp\connect[4]\ms), MaterialID(shp\connect[4]\ma), 0, 0, 0)
RotateEntity(shp\connect[4]\id, 0, 90, 0)
Debug "12 - Connect 4 - MaterialID: " + Str(MaterialID(shp\connect[4]\ma))
shp\connect[6]\tx = ReturnConnectionTexture()
shp\connect[6]\ma = CreateMaterial(#PB_Any, TextureID(shp\connect[6]\tx))
MaterialCullingMode(shp\connect[6]\ma, #PB_Material_NoCulling)
MaterialFilteringMode(shp\connect[6]\ma, #PB_Material_Anisotropic, 8)
SetMaterialAttribute(shp\connect[6]\ma, #PB_Material_DepthWrite, #True)
ScaleMaterial(shp\connect[6]\ma, 1, 20)
shp\connect[6]\ms = CreateTube(#PB_Any, 0.55, 0.50, 0.07, 40, 1)
shp\connect[6]\id = CreateEntity(#PB_Any, MeshID(shp\connect[6]\ms), MaterialID(shp\connect[6]\ma), 0, 0, 0)
RotateEntity(shp\connect[6]\id, 0, 90, 90)
Debug "13 - Connect 6 - MaterialID: " + Str(MaterialID(shp\connect[6]\ma))
shp\par[0]\id = CreateParticleEmitter(#PB_Any, 0.001, 0.001, 0.001, #PB_Particle_Point, 0, 0.046, 0)
shp\par[0]\tx = ReturnFlareTExture()
shp\par[0]\ma = CreateMaterial(#PB_Any, TextureID(shp\par[0]\tx))
MaterialBlendingMode(shp\par[0]\ma, #PB_Material_Add)
AddMaterialLayer(shp\par[0]\ma, TextureID(shp\par[0]\tx), #PB_Material_Add)
ParticleMaterial(shp\par[0]\id, MaterialID(shp\par[0]\ma))
ParticleEmissionRate(shp\par[0]\id, 50)
ParticleVelocity(shp\par[0]\id, 0.001, 0.001)
ParticleSpeedFactor(shp\par[0]\id, 0.4)
ParticleSize(shp\par[0]\id, 0.055, 0.055)
ParticleTimeToLive(shp\par[0]\id, 0.01, 0.01)
Debug "14 - Engine par 0 - MaterialID: " + Str(MaterialID(shp\par[0]\ma))
shp\par[1]\id = CreateParticleEmitter(#PB_Any, 0.001, 0.001, 0.001, #PB_Particle_Point, 0, -0.046, 0)
shp\par[1]\tx = ReturnFlareTexture()
shp\par[1]\ma = CreateMaterial(#PB_Any, TextureID(shp\par[0]\tx))
MaterialBlendingMode(shp\par[1]\ma, #PB_Material_Add)
AddMaterialLayer(shp\par[1]\ma, TextureID(shp\par[1]\tx), #PB_Material_Add)
ParticleMaterial(shp\par[1]\id, MaterialID(shp\par[1]\ma))
ParticleEmissionRate(shp\par[1]\id, 50)
ParticleVelocity(shp\par[1]\id, 0.001, 0.001)
ParticleSpeedFactor(shp\par[1]\id, 0.4)
ParticleSize(shp\par[1]\id, 0.055, 0.055)
ParticleTimeToLive(shp\par[1]\id, 0.01, 0.01)
Debug "15 - Engine par 1 - MaterialID: " + Str(MaterialID(shp\par[1]\ma))
shp\par[2]\id = CreateParticleEmitter(#PB_Any, 150, 150, 20, #PB_Particle_Point, 0, 0, -400)
shp\par[2]\tx = ReturnFlareTexture($FFFFFFFF)
shp\par[2]\ma = CreateMaterial(#PB_Any, TextureID(shp\par[2]\tx))
MaterialBlendingMode(shp\par[2]\ma, #PB_Material_Add)
ParticleMaterial(shp\par[2]\id, MaterialID(shp\par[2]\ma))
ParticleSize(shp\par[2]\id, 0.2, 0.2)
ParticleEmissionRate(shp\par[2]\id, 200)
ParticleTimeToLive(shp\par[2]\id, 2, 12)
ParticleVelocity(shp\par[2]\id, 3, 20)
ParticleEmitterDirection(shp\par[2]\id, 0, 0, 1)
Debug "16 - star par 2 - MaterialID: " + Str(MaterialID(shp\par[2]\ma))
Debug "--------------------------"
Debug "Attaching objects to nodes"
Debug "Node and ids:"
Debug "Attaching Emitter 0 (ID): " + Str(ParticleEmitterID(shp\par[0]\id)) + " to Node (ID): " + Str(NodeID(shp\snod))
AttachNodeObject(shp\snod, ParticleEmitterID(shp\par[0]\id))
Debug "Attaching Emitter 1 (ID): " + Str(ParticleEmitterID(shp\par[1]\id)) + " to Node (ID): " + Str(NodeID(shp\snod))
AttachNodeObject(shp\snod, ParticleEmitterID(shp\par[1]\id))
Debug "Attaching Connect Entity 1 (ID): " + Str(EntityID(shp\connect[1]\id)) + " to Node (ID): " + Str(NodeID(shp\rot[0]))
AttachNodeObject(shp\rot[0], EntityID(shp\connect[1]\id))
Debug "Attaching Connect Entity 2 (ID): " + Str(EntityID(shp\connect[2]\id)) + " to Node (ID): " + Str(NodeID(shp\rot[0]))
AttachNodeObject(shp\rot[0], EntityID(shp\connect[2]\id))
Debug "Attaching Connect Entity 5 (ID): " + Str(EntityID(shp\connect[5]\id)) + " to Node (ID): " + Str(NodeID(shp\rot[0]))
AttachNodeObject(shp\rot[0], EntityID(shp\connect[5]\id))
Debug "Attaching Connect Entity 3 (ID): " + Str(EntityID(shp\connect[3]\id)) + " to Node (ID): " + Str(NodeID(shp\rot[1]))
AttachNodeObject(shp\rot[1], EntityID(shp\connect[3]\id))
Debug "Attaching Connect Entity 4 (ID): " + Str(EntityID(shp\connect[4]\id)) + " to Node (ID): " + Str(NodeID(shp\rot[1]))
AttachNodeObject(shp\rot[1], EntityID(shp\connect[4]\id))
Debug "Attaching Connect Entity 6 (ID): " + Str(EntityID(shp\connect[6]\id)) + " to Node (ID): " + Str(NodeID(shp\rot[1]))
AttachNodeObject(shp\rot[1], EntityID(shp\connect[6]\id))
;Put it all together
AttachNodeObject(shp\main, EntityID(shp\block\id))
AttachNodeObject(shp\main, EntityID(shp\eng[0]\id))
AttachNodeObject(shp\main, EntityID(shp\eng[1]\id))
AttachNodeObject(shp\main, EntityID(shp\sections[0]\id))
AttachNodeObject(shp\main, EntityID(shp\sections[1]\id))
AttachNodeObject(shp\main, EntityID(shp\sections[2]\id))
AttachNodeObject(shp\main, EntityID(shp\connect[0]\id))
AttachNodeObject(shp\main, NodeID(shp\rot[0]))
AttachNodeObject(shp\main, NodeID(shp\rot[1]))
ProcedureReturn #True
EndProcedure
Procedure Normalize(*V.Vector3)
Define.f magSq, oneOverMag
magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z
If magsq > 0
oneOverMag = 1.0 / Sqr(magSq)
*V\x * oneOverMag
*V\y * oneOverMag
*V\z * oneOverMag
EndIf
EndProcedure
Procedure CreateTube(meshID.i, outerRadius.f, innerRadius.f, height.f, numSegBase.i=16, numSegHeight.i=1)
Protected Normal.Vector3, returnMesh.i
If meshID = -1
returnMesh = CreateMesh(#PB_Any)
Else
returnMesh = meshID
CreateMesh(returnMesh)
EndIf
If numSegBase < 1
numSegBase = 1
EndIf
If numSegHeight < 1
numSegHeight = 1
EndIf
deltaAngle.f = #PI*2 / numSegBase
deltaHeight.f = height / numSegHeight
height2.f = height / 2.0
offset = 0
For i = 0 To numSegHeight
For j = 0 To numSegBase
x0.f = outerRadius * Cos(j*deltaAngle)
z0.f = outerRadius * Sin(j*deltaAngle)
Normal\x = x0
Normal\y = 0
Normal\z = z0
Normalize(@Normal)
MeshVertexPosition(x0, i*deltaHeight-height2, z0)
MeshVertexNormal(Normal\x, Normal\y, Normal\z)
MeshVertexTextureCoordinate(j / numSegBase, i / numSegHeight)
If i <> numSegHeight
MeshIndex(offset + numSegBase + 1)
MeshIndex(offset)
MeshIndex(offset + numSegBase)
MeshIndex(offset + numSegBase + 1)
MeshIndex(offset + 1)
MeshIndex(offset)
EndIf
offset + 1
Next
Next
For i = 0 To numSegHeight
For j = 0 To numSegBase
x0.f = innerRadius * Cos(j*deltaAngle)
z0.f = innerRadius * Sin(j*deltaAngle)
Normal\x = x0
Normal\y = 0
Normal\z = z0
Normalize(@Normal)
MeshVertexPosition(x0, i*deltaHeight-height2, z0)
MeshVertexNormal(Normal\x, Normal\y, Normal\z)
MeshVertexTextureCoordinate(j / numSegBase, i / numSegHeight)
If i <> numSegHeight
MeshIndex(offset + numSegBase + 1)
MeshIndex(offset + numSegBase)
MeshIndex(offset)
MeshIndex(offset + numSegBase + 1)
MeshIndex(offset)
MeshIndex(offset + 1)
EndIf
offset + 1
Next
Next
;low cap
For j = 0 To numSegBase
x0.f = innerRadius * Cos(j*deltaAngle)
z0.f = innerRadius * Sin(j*deltaAngle)
MeshVertexPosition(x0, -height2, z0)
MeshVertexNormal(0, -1, 0)
MeshVertexTextureCoordinate(j / numSegBase, 1)
x0 = outerRadius * Cos(j*deltaAngle)
z0 = outerRadius * Sin(j*deltaAngle)
MeshVertexPosition(x0, -height2, z0)
MeshVertexNormal(0, -1, 0)
MeshVertexTextureCoordinate(j / numSegBase, 0)
If j <> numSegBase
MeshIndex(offset)
MeshIndex(offset + 1)
MeshIndex(offset + 3)
MeshIndex(offset + 2)
MeshIndex(offset)
MeshIndex(offset + 3)
EndIf
offset + 2
Next
;high cap
For j = 0 To numSegBase
x0.f = innerRadius * Cos(j*deltaAngle)
z0.f = innerRadius * Sin(j*deltaAngle)
MeshVertexPosition(x0, height2, z0)
MeshVertexNormal(0, 1, 0)
MeshVertexTextureCoordinate(j / numSegBase, 0)
x0 = outerRadius * Cos(j*deltaAngle)
z0 = outerRadius * Sin(j*deltaAngle)
MeshVertexPosition(x0, height2, z0)
MeshVertexNormal(0, 1, 0)
MeshVertexTextureCoordinate(j / numSegBase, 1)
If j <> numSegBase
MeshIndex(offset + 1)
MeshIndex(offset)
MeshIndex(offset + 3)
MeshIndex(offset)
MeshIndex(offset + 2)
MeshIndex(offset + 3)
EndIf
offset + 2
Next
FinishMesh(#True)
ProcedureReturn returnMesh
EndProcedure
https://www.dropbox.com/s/p3hi39iv5r8kb ... t.jpg?dl=0

