Updating materials is usually very fast. The start/stop drawing is where your slowdown is coming from and I'm not sure how you can speed that up.
Just to show you the swapping speed I created a little example. This code has 100 cubes all randomly swapping materials and I'm getting a steady 60 fps.
Code: Select all
;### WARNING!!
;### There are a lot of flashing cubes. Don't run this if you have epilepsy.
If InitEngine3D(#PB_Engine3D_DebugLog) = 0
End
EndIf
If InitKeyboard() = 0
End
EndIf
If InitSprite() = 0
End
EndIf
Enumeration
#Window
#Camera
#Light
#Node
#Texture1
#Texture2
#Texture3
#Material1
#Material2
#Material3
#PlaneMesh
#Plane
#CubeMesh
EndEnumeration
Global Dim EntityData.i(100, 2)
Global EntityTotal.i = 100
Declare TS_CreateCubes()
Declare TS_UpdateCubes()
If OpenWindow(#Window, 0, 0, 1024, 768, "Material Swapping", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(#Window),0, 0, 1024, 768)
Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
LoadTexture(#Texture1, "grass.jpg")
LoadTexture(#Texture2, "clouds.jpg")
LoadTexture(#Texture3, "dirt.jpg")
CreateMaterial(#Material1, TextureID(#Texture1))
CreateMaterial(#Material2, TextureID(#Texture2))
CreateMaterial(#Material3, TextureID(#Texture3))
CreatePlane(#PlaneMesh, 10, 10, 10, 10, 10, 10)
CreateEntity(#Plane, MeshID(#PlaneMesh), MaterialID(#Material1), 0, 0, 0)
CreateCube(#CubeMesh, 0.5)
CreateLight(#Light, RGB(150, 150, 150), 0, 10, 12)
CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera, 0, 5, 15, #PB_Absolute)
CameraLookAt(#Camera, 0, 1, 0)
CameraBackColor(#Camera, RGB(30, 30, 30))
CreateNode(#Node, 0, 0, 0)
AttachNodeObject(#Node, CameraID(#Camera))
TS_CreateCubes()
Repeat
Event = WindowEvent()
ExamineKeyboard()
TS_UpdateCubes()
RotateNode(#Node, 0, 1, 0, #PB_Relative)
RenderWorld()
FlipBuffers()
Until Event = #PB_Event_CloseWindow Or KeyboardReleased(#PB_Key_Escape)
EndIf
EndIf
End
Procedure TS_CreateCubes()
For CTR = 1 To EntityTotal
x = Random(8, 0) - 4
y = Random(5, 0)
z = Random(8, 0) - 4
m = Random(2, 1)
If m = 1
Material = #Material2
Else
Material = #Material3
EndIf
Handle = CreateEntity(#PB_Any, MeshID(#CubeMesh), MaterialID(Material), x, y, z)
EntityData(CTR, 1) = Handle
EntityData(CTR, 2) = Material
Next
EndProcedure
Procedure TS_UpdateCubes()
For CTR = 1 To EntityTotal
Handle = EntityData(CTR, 1)
m = Random(2, 1)
If m = 1
Material = #Material2
Else
Material = #Material3
EndIf
SetEntityMaterial(Handle, MaterialID(Material))
EntityData(CTR, 2) = Material
Next
EndProcedure