Change Material to new one and back

Everything related to 3D programming
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Change Material to new one and back

Post by Bananenfreak »

Heyho,

I´m programming a worldeditor (Some People call this mapeditor, too). If you want to place Entities or select them after placing them, there appear Meshlines around them (12 Lines per Entity; I use the Boundingbox for it). The only Problem is: This method is still too slow.
12 Lines per Entity and framerate Drops to 14 with 45 Entities selected (5 trees á 9 Entities) -> (540 Lines). Without the Meshlines it´s still on 60.

Ok, 540 Lines are not less. But 1 tree got 9 Entities (There is a reason for it, catchword: woodcutting) and every Entity gets 12 Lines.

I could try and give Objects with more than 1 Entity only 12 Meshlines (I think it´s easy to do) or I use a new idea, I could change the materials of whole tree (multitexture and multimaterial) to a new one or "transform" the given materials.
In my mind, I see the normal treetextures (bark, woodrings, Leaves) with a little transparency and a shade of green/red or a other Color.

There is only one Problem: My knowledge in materialscience ( :wink: ) is not that kind of fantastic (I´m a newbie with it) and so I don´t have a clue how to solve this.
What I can do is changing the material in a new one (green and transparent), but there´s not the original texture on selected entities. Additional I don´t have an idea on getting the old, original texture back.

So, could someone help me please? This would be great and a latern in the dark materialroom.

Greets,

Bananenfreak
Image
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Change Material to new one and back

Post by Samuel »

Hello Bananenfreak,
Bananenfreak wrote: What I can do is changing the material in a new one (green and transparent), but there´s not the original texture on selected entities. Additional I don´t have an idea on getting the old, original texture back.
In your map editor do you have a way of keeping track (like an array or structure) of each entities position, handle, etc
If so then you can just add another spot for the original material handle. Then you can easily grab it whenever needed.

If it's just temporary I guess you could also try using AddMaterialLayer(#Material, TextureID [, Mode]) to add a layer onto the entities.
Then you could use RemoveMaterialLayer(#Material) when you want to go back to your original.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Change Material to new one and back

Post by Bananenfreak »

Hey Samuel, thank you for your post.

Yes, Position,... is safed in an Array.
If so then you can just add another spot for the original material handle. Then you can easily grab it whenever needed.
I only add a entry for the original material and that´s all? But on one entity, there are more then one material.

AddMaterialLayer(#Material, TextureID [, Mode]) could be the thing I need, but is theren´t also a Problem with multimaterial on 1 Entity?
Image
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Change Material to new one and back

Post by Samuel »

You'll get into a problem with AddMaterialLayer(#Material, TextureID [, Mode]) if you have multiple entities using the same material.
If you were to select a rock and you had multiple rocks with that same material. All of them would end up being highlighted.

Here's an idea. Maybe you could still use lines, but don't use CreateLine3D(). Instead use CreateMesh(#Mesh , #PB_Mesh_LineList, #PB_Mesh_Static)
to create a mesh composed of lines. This will be much faster than having hundreds of 3D lines and still give you the original look you wanted.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Change Material to new one and back

Post by Bananenfreak »

This would be a way. Thank you Samuel, I will try it and inform you about the result.

I will also test for a possible result for "Change Material", because I think this is important.
Image
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Change Material to new one and back

Post by Bananenfreak »

Here´s my result for it. Standard Cube got 2 subentities. Original materials will be saved in an list so they can be restored later.

Press 1 and Material changes from green/red to yellow.

Dropbox:
https://www.dropbox.com/s/c5xf6s1vdof2s ... test_0.zip

Here is the code:

Code: Select all

EnableExplicit


Structure entthing
  entNb.i
  nbSbEnt.i
  List materials.i()
EndStructure


Define.f KeyX, KeyY, MouseX, MouseY
Define.i quit
Define.entthing cube
Define.b toggle
#CameraSpeed = 1


Enumeration Fenster
  #Window_0
EndEnumeration


Enumeration Mesh
  #wuerfel
EndEnumeration


Enumeration Material
  #mat_1
  #mat_2
  #mat_3
EndEnumeration


Procedure MakeYellow(*ent.entthing)
  Protected.i counter
  
  
  For counter = 0 To *ent\nbSbEnt - 1
    SetEntityMaterial(*ent\entNb, MaterialID(#mat_3), counter)       ;SubEntity isn´t marked in the Documentation. Why?
  Next counter
EndProcedure


Procedure MakeNormal(*ent.entthing)
  Protected.i counter
  
  
  For counter = 0 To *ent\nbSbEnt - 1
    SelectElement(*ent\materials(), counter)
    SetEntityMaterial(*ent\entNb, MaterialID(*ent\materials()), counter)       ;SubEntity isn´t marked in the Documentation. Why?
  Next counter
EndProcedure


Procedure WriteMaterials(*ent.entthing)
  Protected.i counter
  
  
  For counter = 0 To *ent\nbSbEnt - 1
    AddElement(*ent\materials())
    *ent\materials() = FetchEntityMaterial(*ent\entNb, #PB_Any, counter)        ;Function isn´t correct in the Bottom Line -- Why we need a matnum for a matnum?!
  Next counter
EndProcedure


If InitEngine3D()
  
  Add3DArchive("/", #PB_3DArchive_FileSystem)
  Parse3DScripts()        ;Don´t forget this line. Important ;)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindow(#Window_0, 0, 0, 1000, 1000, "Materialtest_0", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(#Window_0), 0, 0, 1000, 1000)
    
    AmbientColor(RGB(255,255,255))
    
    LoadMesh(#wuerfel, "Testwuerfel.mesh")
    GetScriptMaterial(#mat_1, "Mat_1")
    GetScriptMaterial(#mat_2, "Mat_2")
    GetScriptMaterial(#mat_3, "Mat_3")
    cube\entNb = CreateEntity(#PB_Any, MeshID(#wuerfel), #PB_Material_None)
    cube\nbSbEnt = GetEntityAttribute(cube\entNb, #PB_Entity_NbSubEntities)
    WriteMaterials(@cube)
    ForEach cube\materials()
      Debug cube\materials()
    Next
    
    
    ;Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 10, 10, 10, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
      
    Repeat
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_1)
          If toggle
            MakeNormal(@cube)
            toggle = #False
          Else
            MakeYellow(@cube)
            toggle = #True
          EndIf
        EndIf
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
                  
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf

      EndIf
          
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
  
End
Image
Post Reply