shadows

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Hi
Is there an example for shadows please?
I think i remeber an example of primitives casting shadows, but cant find it in the examples folder
thankyou
Rich
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
thank you... guess what im playing with tonight ;-)
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
i was playing with the renderdemo last night with success... I'm not sure it was the simplest of examples especially when canvas has built in methods like SetLightPosition() etc, so i decided to play with the canvas methods this morning with mixed results.

Im trying to use canvas.SetShadowMap(Image) and not getting any shadows..
I have the below block of code.
canvas shadows:
Strict

Import mojo2

Class myClass Extends App
    Field canvas:Canvas

    Field fore_img:Image
    Field fore_cnvs:Canvas
    
    Field back_img:Image
    Field back_cnvs:Canvas
    
    Field block_img:Image
    
    Method OnCreate:Int()
        ' load in default demo tile
        block_img = Image.Load("t3.png")

        ' Set up random for ground       
        fore_img = New Image(400,400,0,0)
        fore_cnvs = New Canvas(fore_img)
        fore_cnvs.Clear(0,0,0,0)
        fore_cnvs.SetColor 0,0,0,1
        Local i:Int=0
        While i<20
        fore_cnvs.DrawRect(Rnd(400),Rnd(400),32,32)
        i=i+1
        Wend
        fore_cnvs.Flush()
        
        ' set up background
        back_img = New Image(400,400,0,0)
        back_cnvs = New Canvas(back_img)
        back_cnvs.SetAmbientLight(0.5,0.5,0.5,0.5)
        back_cnvs.SetLightPosition(0,100,100,-100)
        back_cnvs.SetLightColor(0,1,.5,1)
        back_cnvs.SetLightRange(0,400)
        back_cnvs.SetLightType(0,1)
'        back_cnvs.SetShadowMap(fore_img)
        back_cnvs.Flush()
        
        SetUpdateRate(60)               
        canvas = New Canvas
        Return 0
    End
    
    Method DrawBack:Void()
         back_cnvs.Clear(0,0,0,0)
        
        Local y:Int=0
        While y<400
        Local x:Int=0
        While x<400
        back_cnvs.DrawImage block_img,x,y
        x=x+128
        Wend
        y=y+128
        Wend
    End

    Method OnUpdate:Int()
        Return 0
    End
    
    Method OnRender:Int()
        ' Clear the canvas with a black color.
        canvas.Clear (0,0.5,0)   

        ' position light       
        back_cnvs.SetLightPosition(0,MouseX(),MouseY(),-100)
        
        ' draw background
        DrawBack()
        back_cnvs.Flush()

        ' draw back and fore to cnvs
        canvas.DrawImage back_img,0,0
        canvas.DrawImage fore_img,0,0
                
        ' draw cnvs
        canvas.Flush

        Return 0
    End
End

Function Main:Int()
    New myClass       
    Return 0
End
im expecting the fore_img to cast shadows onto the back_img. It uses the default demo t3.png for its normals etc
Line 40 sets the shadow map

With line 40...
545


without line 40..
546


Is the shadow map a different coord system? Where are my shadows?
Im assuming Im issing something very obvious

Has anyone got a demo of canvas.SetShadowMap please?

Rich
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Ive played around with it a bit more.
This time i drew black sqaures onto a white background

Cerberus X:
        ' Set up random for ground      
        fore_img = New Image(400,400,0,0)
        fore_cnvs = New Canvas(fore_img)
        fore_cnvs.Clear(1,1,1)
        fore_cnvs.SetColor 0,0,0
        Local i:Int=0
        While i<20
            fore_cnvs.DrawRect(Rnd(400),Rnd(400),32,32)
            i=i+1
        Wend
        fore_cnvs.Flush()
and then dont draw it on top of back_img

Now i get this
547

It looks like the ShadowMap is just a static shadow image that I have to build and doesnt change based on light sources.
Not what i was expecting.
Looks like i need to work with shadowcasters still...
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Thanks for looking.

canvas.AddShadowCaster(shadowCaster,0,0) doesnt do anything to the output when I flush the canvas to the image.
Ive also tried canvas.Render() and have the same output (just textured floor with a single light)

Lights seem to work, but not shadows.
Do Shadows work with Canvas's or just Renderers?
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
I recall having to do some head scratching when re-writing the demo. If shadows generally don't work as they should it would be worth knowing for sure.
 
Last edited:

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
The shadowcaster approach is working fine. The shadowmap feature of images and drawlists are a myth at the moment.
 
Top Bottom