Why aren't these dynamic shadows UV-mapping right?

Blitz3D Forums/Blitz3D Programming/Why aren't these dynamic shadows UV-mapping right?

Here is the same routine, with more convenient commands. I am going to implement this version into the engine right now, and hand it over to you to develop further:

Graphics3D 800,600,16,2 
SetBuffer BackBuffer() 

l=CreateLight()

Global cam=CreateCamera()
MoveEntity cam,0,80,-80
TurnEntity cam,45,0,0

Dim mario(5)

For n=0 To 5
	mario(n)=LoadAnimMesh("C:\Program Files\Blitz3D\Samples\Blitz 3D Samples\mak\castle\markio\mariorun.x")
	PositionEntity mario(n),Rand(-50,50),0,Rand(100)
	enableshadows mario(n),20,64,20
	TurnEntity mario(n),0,Rnd(360),0
	EntityRadius mario(n),10
	EntityType mario(n),1
	Animate mario(n),1,.5
	Next

Collisions 1,1,1,2
	
b=CreateCube()
ScaleMesh b,200,.5,200
PositionEntity b,0,-1,0
t=LoadTexture("C:\Program Files\Blitz3D\Samples\Blitz 3D Samples\mak\castle\castle\gothic3.jpg")
ScaleTexture t,0.1,0.1
EntityTexture b,t

Type ShadowLight
Field camera
Field texture
Field shadowmesh
Field height#
Field range#
End Type

Type ShadowCaster
Field entity
Field shadowmesh
Field shadowsize#
Field texture
Field size
Field magnification#
End Type

l=createshadowlight()
PositionEntity l,30,30,0

period#=1000.0/60.0
time#=Float(MilliSecs())-period

While Not KeyHit(1)
	Repeat
  		elapsed#=Float(MilliSecs())-time
  		Until elapsed
 	ticks#=elapsed/period
 	tween#=Float(elapsed Mod period)/Float(period)
 	For k=1 To ticks
  		time=time+period

		For n=0 To 5
			TurnEntity mario(n),0,2,0
			MoveEntity mario(n),0,0,1
			Next
		UpdateWorld
		Next

	CameraProjMode cam,0
	UpdateShadows
	CameraProjMode cam,1
	RenderWorld
	Text 0,0,"FPS: "+fps()
	Flip False
Wend 
End

Function EnableShadows(entity,shadowsize=10,size=256,magnification#=12.0)
s.shadowcaster=New shadowcaster
s\entity=entity
s\size=size
s\texture=CreateTexture(size,size)
s\shadowmesh=CreateMesh()
s\shadowsize=shadowsize
surf=CreateSurface(s\shadowmesh)
AddVertex surf,-1,0,-1,0,1
AddVertex surf,-1,0,1,0,0
AddVertex surf,1,0,1,1,0
AddVertex surf,1,0,-1,1,1
AddTriangle surf,0,1,2
AddTriangle surf,2,3,0
EntityFX s\shadowmesh,9
EntityBlend s\shadowmesh,2
s\magnification=magnification
EntityTexture s\shadowmesh,s\texture
End Function

Function CreateShadowLight(parent=0,range=200,height=0)
s.shadowlight=New shadowlight
s\camera=CreateCamera(parent)
s\height=height
s\range=range
CameraClsColor s\camera,255,255,255
CameraFogMode s\camera,1
CameraFogRange s\camera,0,0
CameraRange s\camera,1,range
CameraProjMode s\camera,0
Return s\camera
End Function

Function UpdateShadows()
For s.shadowlight=Each shadowlight
	CameraProjMode s\camera,1
	TranslateEntity s\camera,0,-20000,0,1
	For c.shadowcaster=Each shadowcaster
		CameraViewport s\camera,0,0,c\size,c\size
		TranslateEntity c\entity,0,-20000,0,1
		zoom#=EntityDistance(s\camera,c\entity)/c\magnification
		CameraZoom s\camera,zoom
		PointEntity s\camera,c\entity
		fog=200.0*Sqr(EntityDistance(s\camera,c\entity)/s\range)+55
		CameraFogColor s\camera,fog,fog,fog
		RenderWorld
		TranslateEntity c\entity,0,20000,0,1
		CopyRect 0,0,c\size,c\size,0,0,BackBuffer(),TextureBuffer(c\texture) 
		surf=GetSurface(c\shadowmesh,1)
		nx#=EntityX(c\entity,1)-EntityX(s\camera,1)
		ny#=EntityY(c\entity,1)-EntityY(s\camera,1)	
		nz#=EntityZ(c\entity,1)-EntityZ(s\camera,1)
		PositionEntity c\shadowmesh,EntityX(c\entity,1),s\height,EntityZ(c\entity,1)
		RotateEntity c\shadowmesh,0,0,0
		AlignToVector c\shadowmesh,nx,0,nz,3
		AlignToVector c\shadowmesh,0,1,0,2
		CameraProject s\camera,EntityX(c\entity,1),s\height-20000,EntityZ(c\entity,1)
		y=ProjectedY()
		stretch#=PointDistance(EntityX(c\entity,1),0,EntityZ(c\entity,1),EntityX(s\camera,1),0,EntityZ(s\camera,1))
		stretch=stretch/5.0
		ScaleEntity c\shadowmesh,1,1,1
		MoveEntity c\shadowmesh,0,0,(Float(y)/Float(c\size)-.5)*c\shadowsize*2.0+stretch
		ScaleEntity c\shadowmesh,c\shadowsize,1,c\shadowsize+stretch
		Next
	TranslateEntity s\camera,0,20000,0,1
	CameraProjMode s\camera,0
	Next

End Function

Global fpstime
Global lastupdate
Global oldfps

Function fps()
oldtime=fpstime
fpstime=MilliSecs()
elapsed=fpstime-oldtime
If Not elapsed elapsed=1
time=Int(MilliSecs()/1000)
If time>lastupdate
	lastupdate=time
	oldfps=1000/elapsed
	EndIf
Return oldfps	
End Function

;Function CreateQuad()
;mesh=CreateMesh()
;surf=CreateSurface(mesh)
;AddVertex surf,-1,0,-1,0,1
;AddVertex surf,-1,0,1,0,0
;AddVertex surf,1,0,1,1,0
;AddVertex surf,1,0,-1,1,1
;AddTriangle surf,0,1,2
;AddTriangle surf,2,3,0
;Return mesh
;End Function