Page 1 of 1

Alpha2Shadow again

Posted: Sun Feb 16, 2014 4:21 pm
by applePi
in addition to the MaterialDepthWrite function i think there is a need to a function to reflect this: alpha_rejection greater xxx
this is to be able to cast the shadow of a material with alpha correctly without using the *.Material file to ease things for the user.
here is again Samuel material file and code http://www.purebasic.fr/english/viewtop ... 77#p421777 to show the correct situation to display the grass shadow on the plane:

AlphaToShadow.MATERIAL

Code: Select all

material Transparent
{
   receive_shadows off
   transparency_casts_shadows off
   technique Default
   {
      pass Main
      {
         ambient 0.8 0.8 0.8 0.8
         specular 0 0 0 0 0
         emissive  0.8 0.8 0.8 0.8

         cull_hardware none
         cull_software none
         scene_blend alpha_blend 
         
         alpha_rejection greater 128 
         depth_write on 

         
         texture_unit
         {
             //CHANGE Spark1.png To a texture that has some alpha.
            texture grass2.png -1
         }         
      }
   }
}
code: originally by Samuel

Code: Select all

;PRESS Escape to exit

#CameraSpeed = 1

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()

  InitSprite()
  InitKeyboard()
  InitMouse()
  ExamineDesktops()
  
DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

 If OpenWindow(0, 0, 0, DeskWid, DeskHei, "Alpha to Shadow", #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)
    
    Parse3DScripts()
    
    GetScriptMaterial(BoxMaterial, "Transparent")
    ;AddMaterialLayer(BoxMaterial, TextureID(5) ,#PB_Material_Replace     )
    ;DisableMaterialLighting(BoxMaterial, 0)

    
    BoxMesh=CreateCube(#PB_Any,40)
    BoxEntity=CreateEntity(#PB_Any,MeshID(BoxMesh),MaterialID(BoxMaterial),0,0,0)
    
    
    PlaneTexture=CreateTexture(#PB_Any,32,32)
    StartDrawing(TextureOutput(PlaneTexture))
      Box(0,0,32,32,RGB(255,255,255))
    StopDrawing()
    
    PlaneMaterial=CreateMaterial(#PB_Any, TextureID(PlaneTexture))
    
    PlaneMesh=CreatePlane(#PB_Any,10000,10000,10,10,10,10)
    PlaneEntity=CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(PlaneMaterial),0,-40,0)
    EntityRenderMode(PlaneEntity,0)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, -50, 0, 120, #PB_Absolute)
    CameraBackColor(0, RGB(255,255,0))

    CameraLookAt(0,0,-10,0)

    CreateLight(0,RGB(100,100,100),0,100,0)
    
    WorldShadows(#PB_Shadow_TextureAdditive, 0, RGB(127, 127, 127), 4096)
    
    Repeat
            
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 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
            
      RotateEntity(BoxEntity,0,0.5,0,#PB_Relative)
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(0, KeyX, 0, KeyY)

    
      RenderWorld()

      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) 
  EndIf
 EndIf
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
  
End
but here the partially working without the material file , just using MaterialDepthWrite(Mat, 1) we need a function to call alpha_rejection greater xxx

Code: Select all

;PRESS Escape to exit

#CameraSpeed = 1

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()

  InitSprite()
  InitKeyboard()
  InitMouse()
  ExamineDesktops()
  
DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

 If OpenWindow(0, 0, 0, DeskWid, DeskHei, "Alpha to Shadow", #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)
   WorldShadows(#PB_Shadow_TextureAdditive, 0, RGB(127, 127, 127), 4096)

    
Tex  = LoadTexture(#PB_Any, "grass2.png")
Mat = CreateMaterial(#PB_Any, TextureID(Tex))

MaterialBlendingMode(Mat, #PB_Material_AlphaBlend)
MaterialDepthWrite(Mat, 1)
MaterialCullingMode(Mat, #PB_Material_NoCulling  )
    
    BoxMesh=CreateCube(#PB_Any,40)
    BoxEntity=CreateEntity(#PB_Any,MeshID(BoxMesh),MaterialID(Mat),0,0,0)
    
    
    PlaneTexture=CreateTexture(#PB_Any,32,32)
    StartDrawing(TextureOutput(PlaneTexture))
      Box(0,0,32,32,RGB(255,255,255))
    StopDrawing()
    
    PlaneMaterial=CreateMaterial(#PB_Any, TextureID(PlaneTexture))
    
    PlaneMesh=CreatePlane(#PB_Any,10000,10000,10,10,10,10)
    PlaneEntity=CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(PlaneMaterial),0,-40,0)
    EntityRenderMode(PlaneEntity,0)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, -50, 0, 120, #PB_Absolute)
    CameraBackColor(0, RGB(255,255,0))

    CameraLookAt(0,0,-10,0)

    CreateLight(0,RGB(100,100,100),40,100,0)
    
    WorldShadows(#PB_Shadow_TextureAdditive, 0, RGB(127, 127, 127), 4096)
    
    Repeat
            
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 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
            
      RotateEntity(BoxEntity,0,0.5,0,#PB_Relative)
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(0, KeyX, 0, KeyY)

    
      RenderWorld()

      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) 
  EndIf
 EndIf
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
  
End