Opposite of EntityAutoFade()

Blitz3D Forums/Blitz3D Programming/Opposite of EntityAutoFade()

I was thinking about making an LOD system by using the opposite of EntityAutoFade(entity, near, far)

So, the entity will fade out when the camera gets closer to the mesh.

cam = doh
entity = the entity to manipulate
near = the distance needed for the entity to be alpha 0
far = the distance needed for the entity to be alpha 1

Function EntityNAutoFade(cam, entity, near, far)
r# = entitydistance(cam,entity)
if r# >= near# and r# <= far then
alphaval# = I have no idea how to do this.
entityalpha entity, alphaval#
endif
End Function

I think this works.
Graphics3D 640,480, 0, 2
AppTitle "Negative AutoFade"


Global CAM=CreateCamera()
MoveEntity CAM, 0, 0, -10


c = CreateCube()
EntityFX c, 1
MoveEntity c, -5, 0, 0
c2 = CopyEntity(c)
MoveEntity c, 5, 0, 0
c3 = CopyEntity(c)
MoveEntity c3, 5, 0, 0

Const GameUPS=60 ; Updates per second
Local Period, FrameTime

Local Tween#,Ticks,i,Remaining,StartTime,Elapsed
Period=1000/GameUPS 
FrameTime=MilliSecs()-Period

MoveMouse GraphicsWidth()/2, GraphicsHeight()/2
While Not KeyHit(1)
	
	StartTime = MilliSecs()
	
	
	Repeat
		Elapsed=MilliSecs()-FrameTime
	Until Elapsed
	
	Ticks=Elapsed / Period
	Tween=Float(Elapsed Mod Period)/Float(Period)
	
	For i=1 To Ticks
		FrameTime=FrameTime+Period
		If i=Ticks Then
			CaptureWorld
		End If
		;TurnEntity GAME_TESTMESH, 0, 1, 0
		EntityNAutoFade(cam, c, 5, 20)
		Update_Game()
		UpdateWorld
	Next
	RenderWorld Tween
	
	Remaining = Period - (MilliSecs() - StartTime)
	If Remaining > 1 Then 
		Delay (Remaining-1) ; Free some CPU time
	End If
	

	Text 0, 0, EntityDistance(cam,c)
	Flip 0
	
Wend

End_Game()


Function Update_Game()
	
	FreeLook(CAM, .1)
	
End Function

Function End_Game()
	End
End Function

Global FreeLookXS#, FreeLookZS#, FreeLookRotXS#, FreeLookRotYS#
Function FreeLook(camera, sp# = .1)
If sp# > 0 Then
	FreeLookXS# = (FreeLookXS# + ((KeyDown(32) Or KeyDown(205)) - (KeyDown(30) Or KeyDown(203))) * sp#) * .75
	FreeLookZS# = (FreeLookZS# + ((KeyDown(17) Or KeyDown(200)) - (KeyDown(31) Or KeyDown(208))) * sp#) * .75
	MoveEntity camera, FreeLookXS#, 0, FreeLookZS#
EndIf
FreeLookRotXS# = ((MouseXSpeed() - FreeLookRotXS#) * .2 + FreeLookRotXS#) * .9
FreeLookRotYS# = ((MouseYSpeed() - FreeLookRotYS#) * .2 + FreeLookRotYS#) * .9
If EntityPitch(camera) + FreeLookRotYS# < -89 pitch# = -89 ElseIf EntityPitch(camera) + FreeLookRotYS# > 89 pitch# = 89 Else pitch# = EntityPitch(camera) + FreeLookRotYS#
yaw# = -FreeLookRotXS# + EntityYaw(camera)
RotateEntity camera, pitch#, yaw#, 0
MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
End Function



Function EntityNAutoFade(cam, entity, near#, far#)
	r# = EntityDistance(cam, entity)
	If r >= near# And r <= far# Then
		r2# = r#-near#
		r3# = far#-near#
		alphaval# = r2#/r3#
		
		If alphaval# <.05 Then alphaval# = 0
		If alphaval# > .99 Then alphaval# = 1
		DebugLog alphaval#
		EntityAlpha entity, alphaval#
	EndIf
End Function


Try this (tested):-

Const C_FADEOUT_START# = 20.0
Const C_FADEOUT_END# = 4.0
Global G_user_not_rendered

Function UpdateUserDistanceFade()
; Fades the user's character model (G_user_entity) if the camera target (G_camera_target) gets too close to the camera (G_camera).

	Local alpha#
	Local distance# = EntityDistance( G_camera, G_camera_target )
	If distance# < C_FADEOUT_START#
		G_user_not_rendered = True
		alpha# = ( distance# - C_FADEOUT_END# ) / ( C_FADEOUT_START# - C_FADEOUT_END# )
		If alpha# < 0.0 Then alpha# = 0.0
		EntityAlpha( G_user_entity, alpha# );SetRecursiveEntityAlpha( G_user_entity, alpha# )
	ElseIf G_user_not_rendered
		G_user_not_rendered = False
		EntityAlpha( G_user_entity, 1.0 );SetRecursiveEntityAlpha( G_user_entity, 1.0 )
	EndIf
End Function


Basically how it works:
Graphics3D 640,480, 0, 2
AppTitle "Negative AutoFade"

Global CAM=CreateCamera()
MoveEntity CAM, 0, 0, -10

c = CreateCube()
EntityFX c, 1
c2 = CopyEntity(c)
c3 = CopyEntity(c)

EntityColor c, 255, 0, 0
EntityColor c2, 0, 255, 0
EntityColor c3, 0, 0, 255

Const GameUPS=60 ; Updates per second
Local Period, FrameTime

Local Tween#,Ticks,i,Remaining,StartTime,Elapsed
Period=1000/GameUPS 
FrameTime=MilliSecs()-Period

MoveMouse GraphicsWidth()/2, GraphicsHeight()/2
While Not KeyHit(1)
	
	StartTime = MilliSecs()
	
	
	Repeat
		Elapsed=MilliSecs()-FrameTime
	Until Elapsed
	
	Ticks=Elapsed / Period
	Tween=Float(Elapsed Mod Period)/Float(Period)
	
	For i=1 To Ticks
		FrameTime=FrameTime+Period
		If i=Ticks Then
			CaptureWorld
		End If
		
		EntityAutoFade c, 0, 15
		EntityNAutoFade(cam, c2, 5, 10, 10, 20)
		EntityNAutoFade(cam, c3, 10, 20, 0, 0)
		Update_Game()
		UpdateWorld
	Next
	RenderWorld Tween
	
	Remaining = Period - (MilliSecs() - StartTime)
	If Remaining > 1 Then 
		Delay (Remaining-1) ; Free some CPU time
	End If
	

	Text 0, 0, EntityDistance(cam,c)
	Text 0, 20, "red is high quality - closest"
	Text 0, 40, "green is med quality - med"
	Text 0, 60, "blue is low quality - furthest"
	Flip 0
	
Wend

End_Game()


Function Update_Game()
	
	FreeLook(CAM, .1)
	
End Function

Function End_Game()
	End
End Function

Global FreeLookXS#, FreeLookZS#, FreeLookRotXS#, FreeLookRotYS#
Function FreeLook(camera, sp# = .1)
	If sp# > 0 Then
		FreeLookXS# = (FreeLookXS# + ((KeyDown(32) Or KeyDown(205)) - (KeyDown(30) Or KeyDown(203))) * sp#) * .75
		FreeLookZS# = (FreeLookZS# + ((KeyDown(17) Or KeyDown(200)) - (KeyDown(31) Or KeyDown(208))) * sp#) * .75
		MoveEntity camera, FreeLookXS#, 0, FreeLookZS#
	EndIf
	FreeLookRotXS# = ((MouseXSpeed() - FreeLookRotXS#) * .2 + FreeLookRotXS#) * .9
	FreeLookRotYS# = ((MouseYSpeed() - FreeLookRotYS#) * .2 + FreeLookRotYS#) * .9
	If EntityPitch(camera) + FreeLookRotYS# < -89 pitch# = -89 ElseIf EntityPitch(camera) + FreeLookRotYS# > 89 pitch# = 89 Else pitch# = EntityPitch(camera) + FreeLookRotYS#
	yaw# = -FreeLookRotXS# + EntityYaw(camera)
	RotateEntity camera, pitch#, yaw#, 0
	MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
End Function


Function EntityNAutoFade(cam, entity, near#, far#, nnear#=0, nfar#=0)
	Local r#, r2#, r3#, alphaval#
	r# = EntityDistance(cam, entity)
	If r >= near# And r <= far# Then
		r2# = r#-near#
		r3# = far#-near#
		alphaval# = r2#/r3#
		
		If alphaval# <.05 Then alphaval# = 0
		If alphaval# > .99 Then alphaval# = 1
		EntityAlpha entity, alphaval#
	Else
		If nnear <> 0 And nfar# <> 0 Then
			EntityAutoFade entity, nnear#, nfar#
		EndIf
	EndIf
End Function