MaterialBlendingMode and Shadows

Everything related to 3D programming
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

MaterialBlendingMode and Shadows

Post by Samuel »

When using MaterialBlendingMode (while using #PB_Material_AlphaBlend as the mode)

the World Shadows will not appear on that material.

So I'm wondering if there is a workaround or if someone can point out how to make this work.



Code: Select all

;**********PRESS ESC TO EXIT*************
#CameraSpeed = 10
#CameraMove = 1


If InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops()
DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)

  OpenWindow(0, 0, 0, DeskWid, DeskHei, "Test", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)

Define.f KeyX, KeyY, MouseX, MouseY

    CreateTexture(0, 32, 32)
    StartDrawing(TextureOutput(0))
       DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
       Box(0, 0, 32, 32 ,RGBA(0,150,150,100))
     StopDrawing()
       
      CreateTexture(1, 256, 256)
      StartDrawing(TextureOutput(1))
      DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
      Box(0, 0, 256, 256, RGBA(0, 250, 0, 255))
      Box(4, 4, 248, 248, RGBA(255, 0, 0, 100))
      Circle(127, 127, 50, RGBA(0, 180, 0, 155))
      StopDrawing()


     CreateMaterial(0, TextureID(0))
     CreateMaterial(1, TextureID(1))

        MaterialBlendingMode(0, #PB_Material_AlphaBlend)
        MaterialBlendingMode(1, #PB_Material_AlphaBlend)
       
        ;MaterialSpecularColor(0,RGB(255, 255, 255))
        SetMaterialColor(0, #PB_Material_SpecularColor, RGB(255,255,255))
        MaterialShininess(0,10)

       
   CreateLight(0, RGB(50, 50, 50), 500, 200 , 1000)
   ;LightColor(0, RGB(250, 200, 200), #PB_Light_SpecularColor)
   SetLightColor(0, #PB_Light_SpecularColor, RGB(250,200,200))
   
     CreatePlane(2, 5000, 5000, 1, 1, 1, 1)
     CreateEntity(2,MeshID(2),MaterialID(1), 0, -190, 0)
     CreateCube(1,200)
     CreateEntity(1, MeshID(1), MaterialID(0),0,0,0)
     CreateCube(3,200)
     CreateEntity(3, MeshID(3), MaterialID(0),-200,-500,-200)
     
     
     
     WorldShadows(#PB_Shadow_Modulative, 1000, RGB(50,50,50))
     
    CreateCamera(0, 0, 0, 100, 100)
    ;CameraLocate(0, -200, 200, 850)
    MoveCamera(0, -200, 200, 850 , #PB_Absolute)
    CameraLookAt(0,0,0,0)

    Repeat

     
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraMove * 0.05
        MouseY = -MouseDeltaY() * #CameraMove * 0.05
      EndIf
     
      If ExamineKeyboard()
       
        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)

 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Last edited by Samuel on Thu Feb 14, 2013 12:03 am, edited 5 times in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: MaterialBlendingMode and Shadows

Post by applePi »

in MaterialBlendingMode(1, #PB_Material_AlphaBlend), you assign the AlphaBlend to the material wich are assigned to the plane and not the box, change 1 to 0 and the box will be complete transparent and with shadow..
or you can use pictures which have a tranparency (the tranparency can be made by photoshop as an example ie changing opacity by double clicking on Layer 0 and moving the slider to change opacity). every time you add this trasparent material the object will be more trasparent
AddMaterialLayer(#MAT, TextureID(#TEX) , #PB_Material_AlphaBlend), add two or three lines and the object will be a ghost.
click on this picture and copy it to the same folder as the your modified code below, you should get a semi transparent cube with a shadow (change the light position to cast more shadow)
Image

Code: Select all

;**********PRESS ESC TO EXIT*************
#CameraSpeed = 10
#CameraMove = 1
#TEX = 30
#MAT = 40

If InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops()
DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)

  OpenWindow(0, 0, 0, DeskWid, DeskHei, "Test", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)

Define.f KeyX, KeyY, MouseX, MouseY
Add3DArchive("\", #PB_3DArchive_FileSystem)

    ;WorldShadows(#PB_Shadow_Modulative, 1000, RGB(100,100,100))
    CreateTexture(0, 32, 32)
     StartDrawing(TextureOutput(0))
       Box(0, 0, 32, 32 ,RGB(0,150,150))
     StopDrawing()
       
      CreateTexture(1, 256, 256)
      StartDrawing(TextureOutput(1))
      DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
      Box(0, 0, 256, 256, RGBA(0, 250, 0, 255))
      Box(4, 4, 248, 248, RGBA(255, 0, 0, 100)) 
      Circle(127, 127, 50, RGBA(0, 180, 0, 255))
      StopDrawing()


     CreateMaterial(0, TextureID(0))
     CreateMaterial(1, TextureID(1))

    ;**************************COMMENT OUT MATERIALBLENDINGMODE TO SEE SHADOWS******************
        MaterialBlendingMode(0, #PB_Material_AlphaBlend)
    
        ;MaterialSpecularColor(0,RGB(255, 255, 255))
        SetMaterialColor(0, #PB_Material_SpecularColor, RGB(255,255,255))
        MaterialShininess(0,10)

        
   CreateLight(0, RGB(50, 50, 50), 500, 200 , 1000)
   ;LightColor(0, RGB(250, 200, 200), #PB_Light_SpecularColor)
   SetLightColor(0, #PB_Light_SpecularColor, RGB(250,200,200))

   LoadTexture(#TEX, "mosaic2.png")
   CreateMaterial(#MAT, TextureID(#TEX))
   MaterialBlendingMode(#MAT, #PB_Material_AlphaBlend)
   AddMaterialLayer(#MAT, TextureID(#TEX) , #PB_Material_AlphaBlend)
   AddMaterialLayer(#MAT, TextureID(#TEX) , #PB_Material_AlphaBlend)
   ;AddMaterialLayer(#MAT, TextureID(#TEX) , #PB_Material_AlphaBlend)
   ;AddMaterialLayer(#MAT, TextureID(#TEX) , #PB_Material_AlphaBlend)
   ;AddMaterialLayer(#MAT, TextureID(#TEX) , #PB_Material_AlphaBlend)
   
     CreatePlane(2, 5000, 5000, 1, 1, 1, 1)
     CreateEntity(2,MeshID(2),MaterialID(1), 0, -190, 0)
     CreateCube(1,200)
     CreateEntity(1, MeshID(1), MaterialID(#MAT),0,0,0)

     WorldShadows(#PB_Shadow_Modulative, 1000, RGB(100,100,100))
     
    CreateCamera(0, 0, 0, 100, 100)
    ;CameraLocate(0, -200, 200, 850)
    MoveCamera(0, -200, 200, 850 , #PB_Absolute)
    CameraLookAt(0,0,0,0)

    Repeat

      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraMove * 0.05
        MouseY = -MouseDeltaY() * #CameraMove * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        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)

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

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

Re: MaterialBlendingMode and Shadows

Post by Samuel »

First off I'd like to say thank you for taking the time to reply. That is a very nice example and explanation. It works perfect so
now I can continue with my experiments. Thank you again.



EDIT:
While on the subject I was wondering if you or someone could answer one more question.
I was wondering if its possible to give the box and the floor alpha and at the same time have both receive the shadows.
So then you would have shadows from the transparent box on the transparent floor.
I hope that makes sense. I can try to come up with an example of what I'm trying to say.

EDIT2:
I managed to figure it out. Thanks though for anyone who was trying to solve it.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: MaterialBlendingMode and Shadows

Post by applePi »

Hi Samuel
i can't make the cube with alpha to cast shadow on the floor with alpha.
how you have done it if possible. i like to cast a glass shadow on a glass plate.
thanks
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: MaterialBlendingMode and Shadows

Post by Samuel »

I too have been trying to get that glass shadow look. This example isn't perfect, but it gets the job done for me.
I'm still experimenting because the shadows don't always display when using certain combinations of textures.
Just create a texture using Photoshop (or whatever you like) and make sure it has a good chunk of opacity.
Then just pop it in and it should show a transparent box with a shadow on a transparent floor. Also I added a red
box underneath the floor to show that its transparent.
I hope this helps and maybe allows you to create a more solid routine.
Its almost 1:00am so I need to get to bed. So I'll explain more tomorrow if need be.

EDIT:
I tested it a bit more and it seems like it only works if you use pre made images. If you make the alpha textures in code then it seems
to mess up and not display them in alpha.


Code: Select all

;Press ESC to Exit
#CameraSpeed = 3

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  ExamineDesktops()
  
  ;Dont forget to change the path to your alpha picture!
  Add3DArchive("/Picture", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)
    If LoadFont(0, "Arial", 30,#PB_Font_Bold)
      SetGadgetFont(#PB_Default, FontID(0))
    EndIf
    
  OpenWindow(0, 0, 0, DeskWid, DeskHei, "New Graphics Testing", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

  OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)
  
  CreateTexture(0,32,32)
     StartDrawing(TextureOutput(0))
       Box(0, 0, 32, 32 ,RGB(150,0,0))
       StopDrawing()
       
     CreateMaterial(0,TextureID(0))  
     ;MaterialSpecularColor(0,RGB(255,255,255))
     SetMaterialColor(0, #PB_Material_SpecularColor, RGB(255,255,255))
     MaterialShininess(0,10)
     
     ;##############################################
     LoadTexture(1,"INSERT FILE NAME HERE.png")
     ;LoadTexture(1,"Tile.png")
     ;##############################################
    CreateMaterial(1,TextureID(1))
    MaterialBlendingMode(1,#PB_Material_AlphaBlend)
    MaterialDepthWrite(1, 1)

  
    CreateCube(0,50)
    CreateEntity(0,MeshID(0), MaterialID(1),0,-24,0)
    CreateEntity(2,MeshID(0), MaterialID(0),100,-100,-100)
    EntityRenderMode(2,0)
    
    CreatePlane(1,1000,1000,1,1,1,1)
    CreateEntity(1,MeshID(1),MaterialID(1),0,-50,0)
     EntityRenderMode(1,0)
    
    ; Camera 
    CreateCamera(0, 0, 0, 100, 100)
    CameraBackColor(0, RGB(70,100,100))
    MoveCamera(0,0, 0, 300,#PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    
    ; Light
    CreateLight(0, RGB(230,230,230), 500, 900, 500)
    CreateLight(1, RGB(200,200,200), -500, 900, 500)
    ;LightColor(0,RGB(255,255,255),#PB_Light_SpecularColor)
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    ;LightColor(1,RGB(200,200,200),#PB_Light_SpecularColor)
    SetLightColor(0, #PB_Light_SpecularColor, RGB(200,200,200))
    DisableLightShadows(0, #True)
    AmbientColor(RGB(0,0,0))
    
    WorldShadows(#PB_Shadow_Modulative, 6000, RGB(200,200,200))

    CreateSprite(0, 50, 50)
    StartDrawing(SpriteOutput(0))
    Box(0,0,50,50,RGB(0,0,0))
    DrawingFont(FontID(0)) 
    DrawText(3,3,"0")
    StopDrawing()
    DisplaySprite(0, 10, 10)
    
    Repeat
     Event=WaitWindowEvent()
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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    
        
        
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateEntity(0, 0, 1, 0, #PB_Relative)
      
      FPS=Engine3DFrameRate(#PB_Engine3D_Current)
      RenderWorld()

    StartDrawing(SpriteOutput(0))
      Box(0,0,50,50,RGB(0,0,0))
      DrawingFont(FontID(0)) 
      DrawText(3,3,Str(FPS))
    StopDrawing()
    DisplaySprite(0, 10, 10)
    
      FlipBuffers()

      
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: MaterialBlendingMode and Shadows

Post by applePi »

Thank you Samuel, so it is mainly the MaterialDepthWrite(#Material, State), then EntityRenderMode(...)
it is realy a very big subject. i have tested your code using this texture wich have some transparency
pure.tga

and have used it to texture the floating cube and the floor.
one finding is that using "MaterialCullingMode(1, #PB_Material_NoCulling ) will make the transparency more real so you can see the other parts of the transparent object. since the texture will appear from front and behind.
2) if we use "MaterialDepthWrite(1, 1)" the picture like this
Image
but if we use MaterialDepthWrite(1, 0) the picture like this
Image
note it is more resemble the glass so a fine tunning can achieve the goal. note also that as if the light have penetrated the floor then casting shadow on the underground cube. a more testing needs , since i have'nt tried separate textures for the cube and the floor.
3) a bizzare thing, i may be dreaming, is that if i change the pure.tga name to purebasic.tga in the code and on the file name. and we keep line 44 like this MaterialDepthWrite(1, 1), then we will not see the botton red cube. this works with names suche as purebasi , purebasic, textur, texture. etc. it could be my system have something wrong so i need confirmation, i have windows xp/sp3
Good Night.
copy the picture pure.tga to the same folder :

Code: Select all

;Press ESC to Exit
#CameraSpeed = 3

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  ExamineDesktops()
  
  ;Dont forget to change the path to your alpha picture!
  Add3DArchive("/", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)
    If LoadFont(0, "Arial", 30,#PB_Font_Bold)
      SetGadgetFont(#PB_Default, FontID(0))
    EndIf
    
  OpenWindow(0, 0, 0, DeskWid, DeskHei, "New Graphics Testing", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

  OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)
  
  CreateTexture(0,32,32)
     StartDrawing(TextureOutput(0))
       Box(0, 0, 32, 32 ,RGB(150,0,0))
       StopDrawing()
       
     CreateMaterial(0,TextureID(0))  
     ;MaterialSpecularColor(0,RGB(255,255,255))
     SetMaterialColor(0, #PB_Material_SpecularColor, RGB(255,255,255))
     MaterialShininess(0,10)
     
     ;##############################################
     
     LoadTexture(1,"pure.tga")
     
     ;##############################################
    CreateMaterial(1,TextureID(1))
    MaterialBlendingMode(1,#PB_Material_AlphaBlend)
    SetMaterialColor(1, #PB_Material_SpecularColor, RGB(255,0,0))
    MaterialDepthWrite(1, 1)
    MaterialCullingMode(1, #PB_Material_NoCulling )
    

  
    CreateCube(0,60)
    CreateEntity(0,MeshID(0), MaterialID(1),0,0,0)
    CreateEntity(2,MeshID(0), MaterialID(0),100,-120,-100)
    EntityRenderMode(2,0)
    
    CreatePlane(1,1000,1000,1,1,1,1)
    CreateEntity(1,MeshID(1),MaterialID(1),0,-50,0)
    EntityRenderMode(1,#PB_Entity_CastShadow )
    
    ; Camera 
    CreateCamera(0, 0, 0, 100, 100)
    CameraBackColor(0, RGB(70,100,100))
    MoveCamera(0,0, 0, 300,#PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    
    ; Light
    CreateLight(0, RGB(230,230,230), 500, 900, 500)
    CreateLight(1, RGB(200,200,200), -400, 600, 500)
    ;LightColor(0,RGB(255,255,255),#PB_Light_SpecularColor)
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255,255,255))
    ;LightColor(1,RGB(200,200,200),#PB_Light_SpecularColor)
    SetLightColor(0, #PB_Light_SpecularColor, RGB(200,200,200))
    DisableLightShadows(0, #True)
    AmbientColor(RGB(0,0,0))
    
    WorldShadows(#PB_Shadow_Modulative, 6000, RGB(200,200,200))

    CreateSprite(0, 50, 50)
    StartDrawing(SpriteOutput(0))
    Box(0,0,50,50,RGB(0,0,0))
    DrawingFont(FontID(0)) 
    DrawText(3,3,"0")
    StopDrawing()
    DisplaySprite(0, 10, 10)
    
    Repeat
     Event=WaitWindowEvent()
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
     
      ExamineKeyboard()
        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    
        
        
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateEntity(0, 0, 1, 0, #PB_Relative)
      
      FPS=Engine3DFrameRate(#PB_Engine3D_Current)
      RenderWorld()

    StartDrawing(SpriteOutput(0))
      Box(0,0,50,50,RGB(0,0,0))
      DrawingFont(FontID(0)) 
      DrawText(3,3,Str(FPS))
    StopDrawing()
    DisplaySprite(0, 10, 10)
    
      FlipBuffers()

      
    Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
Post Reply