I do know how to increase performance considerably, but I always appreciate your input.
The above example was simply meant to show that there is an 'overhead' somewhere at some
point, when using Static geometry. Not the best example, but I made my point, even if I
scaled the entities up and as PMV stated: Loaded each entity seperately.
(Which is possible and usable if you wish to add many different entities to the static geo).
I've made some changes to the next example.
There are two meshes, several images and sound.
Too much to put here. This is my last static geo example.
Code: Select all
; Example3: Static Geo town by DK_PETER
;----------------------------------------------------------------------------
; A MORE OPTIMIZED VERSION
;-----------------------------------------------------------------------------
; In the previous example, I didn't keep everything small, but entities were
; scaled up considerably. This was to show, that there is something going on,
; which I can't figure out. Loading time was increased considerably, while
; omitting the Static Geo decreased the loading time considerably.
;-----------------------------------------------------------------------------
; In a 'real world' example, the proper way is to keep everthing VERY small.
; Simply scale everything down and you'll see that the speed is drastically improved.
;-----------------------------------------------------------------------------
; In this example I'll do just that.
; Note: Only the leaf texture size has been decreased
; and we're adding two entities to the static geometry.
; Remember: Physics + STATIC GEO won't work.
; But that's not the purpose of the static geometry anyway. :-)
;-----------------------------------------------------------------------------
; I've added a church/courthouse besides the tree and attached some bells sounds to the courthouses/churches.
; This time the performance is good. Less that 4 seconds and YES..It can be done even better.
; Without Static geo - it's blazingly fast (Instant creation 0 seconds).
;-----------------------------------------------------------------------------
; Tested with: GFX: Gigabyte 770 GTX | Window 7 64 bit | 3 GHz. Quad Core AMD.
;-----------------------------------------------------------------------------
EnableExplicit
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
If InitEngine3D() = 0
MessageRequester("Error", "Can't initialize Engine")
End
EndIf
If InitSprite() = 0
MessageRequester("Error", "Can't initialize Screen")
End
EndIf
If InitKeyboard() = 0
MessageRequester("Error", "Can't initialize Keyboard")
End
EndIf
If InitMouse() = 0
MessageRequester("Error", "Can't initialize Mouse")
End
EndIf
If InitSound() = 0
MessageRequester("Error", "Can't initialize Sound")
End
EndIf
Structure _Object
id.i
ms.i
ma.i
tx.i
x.f
y.f
z.f
time.i
EndStructure
Structure _Bellsound
snd.i
nod.i
EndStructure
Structure _Objects
Cloud._Object
ground._Object
Light.i
Leafs._Object[3]
church._Object
tree._Object
EndStructure
Structure MainVars
Geo.i
cam.i
ret.i
Events.i
KeyX.f
KeyY.f
KeyZ.f
tx_extra.i
FinishedCreationTime.i
EndStructure
Declare.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
Declare.i BuildTown(WithGeo.i = #True)
Global ob._Objects, m.MainVars, x.i, y.i, NewList sn._Bellsound()
OpenWindow(0, 0, 0, 1024, 768, "ChurchTown", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)
Add3DArchive("mesh", #PB_3DArchive_FileSystem)
Add3DArchive("material", #PB_3DArchive_FileSystem)
Add3DArchive("texture", #PB_3DArchive_FileSystem)
Add3DArchive("sound", #PB_3DArchive_FileSystem)
Parse3DScripts()
m\cam = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(m\cam, 130, 1, 130)
ob\Light = CreateLight(#PB_Any, $CEFFFF41, 0, 5, 0,#PB_Light_Directional)
LightDirection(ob\Light, -0.2, -1, 0)
With ob\ground
\tx = LoadTexture(#PB_Any, "2.jpg")
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
ScaleMaterial(\ma, 0.01, 0.01)
\ms = CreatePlane(#PB_Any, 400, 400, 10, 10, 1, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
EndWith
With ob\Cloud
m\tx_extra = CreateTexture(#PB_Any, 1024, 1024)
StartDrawing(TextureOutput(m\tx_extra))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For x = 0 To 6000
Circle(Random(1020, 5), Random(1020,5) ,Random(2,1), RGBA(255, 255, 255, Random(255,30)))
Next x
StopDrawing()
\tx = LoadTexture(#PB_Any, "1.jpg")
\ma = CreateMaterial(#PB_Any, TextureID(m\tx_extra))
MaterialBlendingMode(\ma, #PB_Material_Color)
AddMaterialLayer(\ma, TextureID(\tx),#PB_Material_Add)
AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Modulate)
RotateMaterial(\ma, 90, #PB_Material_Fixed, 1)
ScaleMaterial(\ma, 0.005, 0.005, 0)
ScaleMaterial(\ma, 0.1, 0.1, 1)
ScaleMaterial(\ma, 0.1, 0.1, 2)
ScrollMaterial(\ma, 0.02, -0.01, #PB_Material_Animated, 1)
ScrollMaterial(\ma, -0.01, 0.01, #PB_Material_Animated, 2)
\ms = CreatePlane(#PB_Any, 400, 400, 10, 10, 1, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 100, 0)
RotateEntity(\id, -180,0, 0)
ScaleEntity(\id, 10, 1, 10)
EndWith
For x = 0 To 2
With ob\Leafs[x]
\id = CreateParticleEmitter(#PB_Any, 100, 1, 100, #PB_Particle_Box, 0, 1, 0)
\tx = LoadTexture(#PB_Any, "leaf.png")
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialBlendingMode(\ma, #PB_Material_AlphaBlend)
RotateMaterial(\ma, randomf(-3, 3), #PB_Material_Animated)
ParticleMaterial(\id, MaterialID(\ma))
ParticleSize(\id, 0.1 + RandomF(1,0.2) , 0.1 + RandomF(1,0.2))
ParticleEmitterDirection(\id, Randomf(-1,1), 0, Randomf(-1,1))
ParticleEmissionRate(\id, Random(100, 50))
ParticleVelocity(\id, 0.1, 5)
ParticleSpeedFactor(\id, 1.2)
EndWith
Next x
;******************************************************************************
m\ret = BuildTown(#False) ;With Geo or without...
;******************************************************************************
For x = 0 To 2
ob\Leafs[x]\time = ElapsedMilliseconds()
Next x
Repeat
Repeat
m\Events = WindowEvent()
Until m\Events = 0
ExamineMouse()
RotateCamera(m\cam, MouseDeltaY() * 0.05, -MouseDeltaX() * 0.05, 0, #PB_Relative)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
m\KeyX = -0.1
ElseIf KeyboardPushed(#PB_Key_Right)
m\KeyX = 0.1
Else
m\KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
m\KeyY = -0.1
ElseIf KeyboardPushed(#PB_Key_Down)
m\KeyY = 0.1
Else
m\KeyY = 0
EndIf
MoveParticleEmitter(ob\Leafs\id, CameraX(m\cam), CameraY(m\cam), CameraZ(m\cam), #PB_Absolute)
For x = 0 To 2
With ob\Leafs[x]
If ElapsedMilliseconds() - \time > Random(800, 200)
ParticleEmitterDirection(\id, randomf(-1,1), randomf(-0.1,0.1), Randomf(-1,1)) ;Edited the (y-dir) - made the leafs fly more 'realistic'
\time = ElapsedMilliseconds()
EndIf
EndWith
Next x
SoundListenerLocate(CameraX(m\cam), CameraY(m\cam), CameraZ(m\cam))
MoveCamera(m\cam, m\KeyX, 0, m\KeyY)
RenderWorld()
SetWindowTitle(0, Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) + " | " + Str(Engine3DStatus(#PB_Engine3D_NbRenderedTriangles)) + " | " + Str(Engine3DStatus(#PB_Engine3D_NbRenderedBatches)))
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Procedure.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
If SeedVal = #PB_Ignore : SeedVal = ElapsedMilliseconds() : EndIf
ProcedureReturn (Min + (Max - Min) * Random(SeedVal) / SeedVal)
EndProcedure
Procedure.i BuildTown(WithGeo.i = #True)
Protected newval.i = 0, newx.f, newy.f, BaseSound.i
Protected NewList newEnt.i()
BaseSound = LoadSound3D(#PB_Any, "churchbells.wav")
m\FinishedCreationTime = ElapsedMilliseconds()
With ob\tree
\ma = GetScriptMaterial(#PB_Any, "leafs")
\ms = LoadMesh(#PB_Any, "tree1.mesh")
\id = CreateEntity(#PB_Any, MeshID(\ms), #PB_Material_None)
EndWith
With ob\church
\ma = GetScriptMaterial(#PB_Any, "_52bac0e2.dds")
\ms = LoadMesh(#PB_Any, "courtbuilding.mesh")
\id = CreateEntity(#PB_Any, MeshID(\ms), #PB_Material_None)
EndWith
;CONCERNING BELLS
;This is waaay too much but here I attach a bell to each courthouse/church
If WithGeo = #True
m\Geo = CreateStaticGeometry(#PB_Any, 250, 50, 250, 0)
For y = 0 To 200 Step 20
For x = 0 To 200 Step 20
If newval = 0
newx = RandomF(8,0) : newy = randomf(8, 0)
AddStaticGeometryEntity(m\Geo, EntityID(ob\tree\id), x + newx, 0, y + newy, 0.2, 0.2, 0.2, 0, 0, 0)
newval = 1
Else
newx = RandomF(8,0) : newy = randomf(8, 0)
AddStaticGeometryEntity(m\Geo, EntityID(ob\church\id), x + newx, 0, y + newy, 0.1, 0.1, 0.1, 0, 0, 0)
AddElement(sn())
sn()\nod = CreateNode(#PB_Any, x + newx, 1, y + newy)
sn()\snd = LoadSound3D(#PB_Any, "churchbells.wav")
SoundRange3D(sn()\snd, 0, 12)
AttachNodeObject(sn()\nod, SoundID3D(sn()\snd))
PlaySound3D(sn()\snd, #PB_Sound3D_Loop)
newval = 0
EndIf
Next x
Next y
BuildStaticGeometry(m\Geo)
Else ;Without Static geo...And now you can do everything - physics - raycasting a.s.f...
For y = 0 To 200 Step 20
For x = 0 To 200 Step 20
If newval = 0
newx = RandomF(8,0) : newy = randomf(8, 0)
AddElement(newent())
newEnt() = CopyEntity(ob\tree\id, #PB_Any)
ScaleEntity(newEnt(), 0.2, 0.2, 0.2)
MoveEntity(newEnt(), x + newx, 0, y + newy)
newval = 1
Else
newx = RandomF(8,0) : newy = randomf(8, 0)
newEnt() = CopyEntity(ob\church\id, #PB_Any)
ScaleEntity(newEnt(), 0.1, 0.1, 0.1)
MoveEntity(newEnt(), x + newx, 0, y + newy)
AddElement(sn())
sn()\nod = CreateNode(#PB_Any, x + newx, 1, y + newy)
sn()\snd = LoadSound3D(#PB_Any, "churchbells.wav")
SoundRange3D(sn()\snd, 0, 12)
AttachNodeObject(sn()\nod, SoundID3D(sn()\snd))
PlaySound3D(sn()\snd, #PB_Sound3D_Loop)
newval = 0
EndIf
Next x
Next y
EndIf
Debug (ElapsedMilliseconds() - m\FinishedCreationTime) / 1000
;Free basic objects - if one wants to in this example
FreeEntity(ob\church\id)
FreeEntity(ob\tree\id)
ProcedureReturn #True
EndProcedure