Ooops
October 28, 2020, 06:18:03 AM

Author Topic: [bb] 3D Elite Style Scanner by Krischan [ 1+ years ago ]  (Read 1520 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 3D Elite Style Scanner by Krischan [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : 3D Elite Style Scanner
Author : Krischan
Posted : 1+ years ago

Description : This demo shows the use of a 3D space scanner like in the game "Elite". It shows a type field within a given distance with "blips" and "poles" attached to the blips - they represent the relative height to the centered spaceship. The demo creates two procedural textures blended together to get a nice glowing radar screen. Arrows/Mouse to move, LMB=increase speed, RMB=halves scanner range. Have fun!

Edit: shortened code and added color schemes / more constants to declare.



Code :
Code: BlitzBasic
  1. AppTitle "3D Elite Style Scanner"
  2.  
  3. Graphics3D 800,600,32,2
  4.  
  5. Const TurnSpeed#    = 4.00      ; cam turn speed
  6. Const RollSpeed#    = 0.50      ; cam roll speed
  7. Const CameraSpeed#  = 1.00      ; cam move speed
  8. Const Objects%      = 2000      ; number of space objects
  9. Const RadarSize%    = 256       ; radar size in pixels
  10.  
  11. Const PoleWidth#    = 1.0       ; width of poles
  12. Const BlipSize#     = 3.0       ; size of radar blip
  13. Const VertScale#    = 1.0       ; height multiplicator of pole/blip
  14. Const RadarBorder#  = 0.9       ; range within 1.0 to display
  15.  
  16. ; color scheme: blue
  17. Const R1%=  0,G1%=128,B1%=192   ; grid lines color
  18. Const R2%=  0,G2%= 16,B2%= 32   ; scanner background color
  19. Const R3%=  0,G3%=128,B3%=255   ; scanner border glow color
  20.  
  21. ; color scheme: green
  22. ;Const R1%=  0,G1%=192,B1%=  0   ; grid lines color
  23. ;Const R2%=  0,G2%= 32,B2%=  0   ; scanner background color
  24. ;Const R3%=  0,G3%=255,B3%=  0   ; scanner border glow color
  25.  
  26. Type blip
  27.    
  28.     Field entity%
  29.     Field blip%
  30.     Field pole%
  31.     Field x#,y#,z#
  32.     Field vx#,vy#,vz#
  33.     Field r%,g%,b%
  34.     Field size#
  35.    
  36. End Type
  37.  
  38. Global WIDTH%=GraphicsWidth()
  39. Global HEIGHT%=GraphicsHeight()
  40. Local TIMER%=CreateTimer(60)
  41.  
  42. ; camera
  43. Local CAM%=CreateCamera()
  44. CameraRange CAM,1,WIDTH+RadarSize
  45.  
  46. ; setup radar
  47. Local RADAR=InitRadar(RadarSize,512,10,CAM)
  48.  
  49. ; setup objects
  50. InitObjects(RADAR,Objects,1000)
  51.  
  52. MoveMouse WIDTH/2,HEIGHT/2
  53.  
  54. While Not KeyHit(1)
  55.    
  56.     Local range%=256,visible%
  57.    
  58.     ; move player
  59.     Movement(CAM)
  60.    
  61.     ; RMB = short range radar
  62.     If MouseDown(2) Then range=128
  63.    
  64.     ; update radar
  65.     visible%=UpdateRadar(CAM,RADAR,RadarSize,range,90)
  66.    
  67.     RenderWorld
  68.    
  69.     WaitTimer TIMER
  70.    
  71.     Text 0, 0,"Scanner range....: "+range
  72.     Text 0,15,"Objects / visible: "+Objects+" / "+visible
  73.    
  74.     Flip
  75.    
  76. Wend
  77.  
  78. End
  79.  
  80. ; populate scene with dummy objects
  81. Function InitObjects(radar%,number%=100,range#=500.0)
  82.    
  83.     Local i%,b.blip
  84.    
  85.     For i=1 To number
  86.        
  87.         b.blip = New blip
  88.         bentity=CreateCone(16)
  89.         blip=CreateSphere(6,radar)
  90.         bpole=CreateCube(radar)
  91.         bx=Rnd(-range,range)
  92.         by=Rnd(-range,range)
  93.         bz=Rnd(-range,range)
  94.         bvx=Rnd(-0.1,0.1)
  95.         bvy=Rnd(-0.1,0.1)
  96.         bvz=Rnd(-0.1,0.1)
  97.         b
  98. =Rand(255)
  99.         bg=Rand(255)
  100.         b=Rand(255)
  101.         bsize=2
  102.        
  103.         ; align "ship" to its flight vector
  104.         AlignToVector bentity,bvx,bvy,bvz,2,1
  105.        
  106.         ; create the entity itself
  107.         PositionEntity bentity,bx,by,bz
  108.         EntityFX bentity,1
  109.         EntityColor bentity,b
  110. ,bg,b
  111.         ScaleEntity bentity,bsize,bsize*2,bsize
  112.         EntityAutoFade bentity,1,range
  113.        
  114.         ; creates a radar blip
  115.         EntityFX blip,1
  116.         EntityOrder blip,-100
  117.         EntityColor blip,b
  118. ,bg,b
  119.        
  120.         ; creates a blip pole
  121.         EntityFX bpole,1
  122.         EntityOrder bpole,-100
  123.         EntityColor bpole,b
  124. ,bg,b
  125.        
  126.     Next
  127.    
  128. End Function
  129.  
  130. ; simple spaceship movement
  131. Function Movement(cam%,sensitivity#=1.0)
  132.    
  133.     Local roll#,cz#,tx#,ty#,multi%=1
  134.    
  135.     ; arrows = move / SHIFT or LMB = Turbo
  136.     cz=(KeyDown(200)-KeyDown(208))*CameraSpeed
  137.     roll=(KeyDown(203)-KeyDown(205))*RollSpeed
  138.     If KeyDown(42) Or KeyDown(54) Or MouseDown(1) Then multi=10
  139.    
  140.     tx=Normalize(MouseX(),0,WIDTH , 1,-1)
  141.     ty=Normalize(MouseY(),0,HEIGHT,-1, 1)
  142.    
  143.     If ty<0 Then ty=(Abs(ty)^sensitivity)*-1 Else ty=ty^sensitivity
  144.     If tx<0 Then tx=(Abs(tx)^sensitivity)*-1 Else tx=tx^sensitivity
  145.    
  146.     TurnEntity cam,ty*TurnSpeed,tx*TurnSpeed,roll*TurnSpeed
  147.     MoveEntity cam,0,0,cz*multi
  148.        
  149. End Function
  150.  
  151. ; creates a nice elite stype radar sprite at screen bottom
  152. Function InitRadar(scale#,size%=512,quads%=8,parent%=False,sharp#=1.0,blur#=50.0)
  153.    
  154.     Local RADAR%=CreateQuad()
  155.     EntityParent RADAR,parent
  156.     Local tex1%=CreateGridTexture(size,quads)
  157.     Local tex2%=CreateGlowTexture(size,sharp,blur)
  158.    
  159.     ; apply textures to radar quad
  160.     EntityFX RADAR,1
  161.     ScaleEntity RADAR,scale,scale,scale
  162.     PositionEntity RADAR,0,-HEIGHT+(RadarSize*0.75),WIDTH
  163.     TextureBlend tex1,2
  164.     TextureBlend tex2,3
  165.     EntityTexture RADAR,tex1,0,1
  166.     EntityTexture RADAR,tex2,0,2
  167.     EntityBlend RADAR,1
  168.    
  169.     ; create centered ship marker (you!)
  170.     Local CENTER%=CreateCone(3,1,RADAR)
  171.     RotateMesh CENTER,30,0,0
  172.     PositionEntity CENTER,0,0,0
  173.     ScaleEntity CENTER,1.0/32,1.0/32,1.0/32
  174.     EntityFX CENTER,1
  175.     EntityColor CENTER,255,255,0
  176.    
  177.     EntityOrder CENTER,-20
  178.     EntityOrder RADAR,-10
  179.    
  180.     Return RADAR
  181.    
  182. End Function
  183.  
  184. ; updates the radar scene
  185. Function UpdateRadar(cam%,radar%,scale#=128.0,range#=256.0,angle#=60.0)
  186.    
  187.     Local b.blip
  188.     Local d#
  189.     Local rx#,ry#,rz#
  190.    
  191.     Local halfscale#=scale/2.0
  192.     Local doublescale#=scale*2.0
  193.     Local ps#=PoleWidth/scale
  194.     Local bs#=BlipSize/halfscale
  195.     Local div#=((scale/2.0)/range)*RadarBorder
  196.    
  197.     Local visible%=0
  198.    
  199.     For b.blip = Each blip
  200.        
  201.         ; dumb object movement
  202.         bx=bx+bvx
  203.         by=by+bvy
  204.         bz=bz+bvz
  205.         PositionEntity bentity,bx,by,bz
  206.        
  207.         ; get distance to object
  208.         d=EntityDistance(bentity,cam)
  209.        
  210.         ; within scanner range?
  211.         If d<range Then
  212.            
  213.             visible=visible+1
  214.            
  215.             ; show blip and its pole
  216.             ShowEntity bpole
  217.             ShowEntity blip
  218.            
  219.             ; transform position
  220.             TFormPoint (bx,by,bz,0,cam)
  221.             rx=-TFormedX()/halfscale*div
  222.             rz=TFormedZ()/halfscale*div
  223.             ry=TFormedY()*div
  224.            
  225.             ; reposition/scale blip
  226.             PositionEntity blip,0,0,0
  227.             TranslateEntity blip,rx,rz,ry/scale*VertScale
  228.             ScaleEntity blip,bs,bs,bs
  229.            
  230.             ; reposition/scale pole
  231.             PositionEntity bpole,0,0,0
  232.             ScaleEntity bpole,ps,ps,ry/doublescale*VertScale
  233.             TranslateEntity bpole,rx,rz,ry/doublescale*VertScale
  234.            
  235.         Else
  236.            
  237.             ; hide blip and its pole
  238.             HideEntity bpole
  239.             HideEntity blip
  240.            
  241.         EndIf
  242.        
  243.     Next
  244.    
  245.     ; radar always points to cam
  246.     RotateEntity radar,-angle,180,0
  247.    
  248.     Return visible
  249.    
  250. End Function
  251.  
  252. ; simple quad creation
  253. Function CreateQuad(r%=255,g%=255,b%=255,a#=1.0)
  254.    
  255.     Local mesh%,surf%,v1%,v2%,v3%,v4%
  256.    
  257.     mesh=CreateMesh()
  258.     surf=CreateSurface(mesh)
  259.    
  260.     v1=AddVertex(surf,-1,1,0,1,0)
  261.     v2=AddVertex(surf,1,1,0,0,0)
  262.     v3=AddVertex(surf,-1,-1,0,1,1)
  263.     v4=AddVertex(surf,1,-1,0,0,1)
  264.    
  265.     VertexColor surf,v1,r,g,b,a
  266.     VertexColor surf,v3,r,g,b,a
  267.     VertexColor surf,v2,r,g,b,a
  268.     VertexColor surf,v4,r,g,b,a
  269.    
  270.     AddTriangle(surf,0,1,2)
  271.     AddTriangle(surf,3,2,1)
  272.    
  273.     FlipMesh mesh
  274.    
  275.     Return mesh
  276.    
  277. End Function
  278.  
  279. ; create procedural glowing border texture
  280. Function CreateGlowTexture(size%=512,sharpness#=1.0,blur#=50.0)
  281.    
  282.     Local strength#=512*1.0/size*sharpness
  283.     Local tex%=CreateTexture(size,size,3)
  284.     Local tb%=TextureBuffer(tex)
  285.    
  286.     Local x%,y%,i#,j%,col%,rgb%,rc%,gc%,bc%
  287.    
  288.     LockBuffer tb
  289.    
  290.     For x=0 To size-1
  291.        
  292.         For y=0 To size-1
  293.            
  294.             ; clear colors
  295.             rgb=0*$1000000+0*$10000+0*$100+0
  296.             WritePixelFast x,y,rgb,tb
  297.            
  298.         Next
  299.        
  300.     Next
  301.    
  302.     For j=1 To (size/2)-2
  303.        
  304.         ; exponential falloff
  305.         col=j*strength/Exp((((size/2)-2)-j)*(strength/blur))
  306.        
  307.         If col>224 Then col=Normalize(col,224,255,224,0)
  308.         If col<0 Then col=0
  309.        
  310.         ; multiply RGB with brightness
  311.         rc=R3*col Shr 8
  312.         gc=G3*col Shr 8
  313.         bc=B3*col Shr 8
  314.        
  315.         rgb=255*$1000000+rc*$10000+gc*$100+bc
  316.        
  317.         For i=0 To 360 Step 0.05
  318.            
  319.             WritePixelFast ((size-2)/2.0)+(Sin(i)*j),((size-2)/2.0)+(Cos(i)*j),rgb,tb
  320.            
  321.         Next
  322.        
  323.     Next
  324.    
  325.     UnlockBuffer tb
  326.    
  327.     Return tex
  328.    
  329. End Function
  330.  
  331. ; normalize a value
  332. Function Normalize#(value#=128.0,vmin#=0.0,vmax#=255.0,nmin#=0.0,nmax#=1.0)
  333.    
  334.     Return ((value#-vmin#)/(vmax#-vmin#))*(nmax#-nmin#)+nmin#
  335.    
  336. End Function
  337.  
  338. ; create radar grid texture
  339. Function CreateGridTexture(size%=512,quads%=16)
  340.    
  341.     Local x%,y%,j%,i#,rgb%,col%,r%,g%,b%
  342.     Local steps%=size/quads
  343.    
  344.     Local tex%=CreateTexture(size,size,3+8)
  345.     Local buffer%=TextureBuffer(tex)
  346.    
  347.     SetBuffer buffer
  348.    
  349.     ; background color
  350.     Color R2,G2,B2
  351.     Rect 0,0,size,size,1
  352.    
  353.     ; create grid
  354.     Color R1,G1,B1
  355.     For x=0 To quads-1
  356.         For y=0 To quads-1
  357.             Rect x*steps,y*steps,steps+1,steps+1,0
  358.         Next
  359.     Next
  360.    
  361.     Color 255,255,255
  362.    
  363.     LockBuffer buffer
  364.    
  365.     ; create glowing border and delete pixels outside the circle
  366.     For j=(size/2)-6 To size
  367.        
  368.         For i=0 To 360 Step 0.05
  369.            
  370.             x=((size-2)/2.0)+(Sin(i)*j)
  371.             y=((size-2)/2.0)+(Cos(i)*j)
  372.            
  373.             col=0
  374.             If j<=(size/2) Then col=Normalize(j,(size/2)-6,(size/2),255,0)
  375.            
  376.             ; multiply RGB with brightness
  377.             r=R3*col Shr 8
  378.             g=G3*col Shr 8
  379.             b=B3*col Shr 8
  380.            
  381.             rgb=0*$1000000+r*$10000+g*$100+b
  382.            
  383.             If x>=0 And x<size And y>=0 And y<size Then
  384.                                
  385.                 WritePixelFast x,y,rgb,buffer
  386.                
  387.             EndIf
  388.            
  389.         Next
  390.        
  391.     Next
  392.        
  393.     UnlockBuffer buffer
  394.    
  395.     SetBuffer BackBuffer()
  396.    
  397.     Return tex
  398.    
  399. End Function


Comments :


puki(Posted 1+ years ago)

 Cool.


BlitzSupport(Posted 1+ years ago)

 Yeah, very nicely done. The demo itself has a really good feel to it too.


_PJ_(Posted 1+ years ago)

 Nice! I love the effect and how it's been done as a textured mesh rather than direct 2D drawing :)Really useful!


Krischan(Posted 1+ years ago)

 For a clean 3D only variant take a look at this. Here I'm using quads as blips and poles to create the radar echoes instead of cubes and spheres. They are textured, too! Looks even more cool now.
Code: [Select]
AppTitle "3D Elite Style Scanner Example"

SeedRnd 7

Graphics3D 800,600,32,2

Const TurnSpeed#    = 4.00        ; cam turn speed
Const RollSpeed#    = 0.50        ; cam roll speed
Const CameraSpeed#  = 0.25        ; cam move speed
Const Objects%      = 1000        ; number of space objects
Const RadarSize%    = 300         ; radar size in pixels
Const Factor#       = 1.0         ; scale factor
Const UniverseRange#= 250.0       ; "universe" range
Const Maxrange#     = 64.0        ; Scanner Range in Lightyears

Const PoleWidth#    =  1.0        ; width of poles
Const BlipSize#     =  8.0        ; size of radar blip
Const VertScale#    =  1.0        ; height multiplicator of pole/blip
Const RadarBorder#  =  0.8        ; range within 1.0 to display
Const Distribution$ = "spherical" ; star distribution (cubic, spherical)

Const R1%=  0,G1%=128,B1%=192   ; grid lines color
Const R2%=  0,G2%= 16,B2%= 32   ; scanner background color
Const R3%=  0,G3%=128,B3%=255   ; scanner border glow color

Type blip
   
    Field entity%
    Field blip%
    Field pole%
    Field x#,y#,z#
    Field r%,g%,b%
    Field scale#
   
End Type

Global WIDTH%=GraphicsWidth()
Global HEIGHT%=GraphicsHeight()
Global SUNTEX_RED%=CreateSunTexture(256,255,128,128) : TextureBlend SUNTEX_RED,3
Global SUNTEX_YEL%=CreateSunTexture(256,255,224,192) : TextureBlend SUNTEX_YEL,3
Global SUNTEX_BLU%=CreateSunTexture(256,128,128,255) : TextureBlend SUNTEX_BLU,3
Global SUNTEX_WHI%=CreateSunTexture(256,255,255,255) : TextureBlend SUNTEX_WHI,3
Local TIMER%=CreateTimer(60)

; camera
Local CAM%=CreateCamera()
CameraRange CAM,1.0/Factor,(WIDTH*Factor)+RadarSize

; setup radar
Local RADAR=InitRadar(RadarSize,512,10,CAM)

; setup objects
InitObjects(CAM,RADAR,Objects,UniverseRange,Distribution)

MoveMouse WIDTH/2,HEIGHT/2

MoveEntity CAM,0,0,-0.1*Factor

While Not KeyHit(1)
   
    Local range%=Maxrange
   
    ; move player
    Movement(CAM)
   
    ; RMB = short range radar
    If MouseDown(2) Then range=Maxrange/2.0
   
    ; update objects
    UpdateObjects(CAM)
   
    ; update radar
    UpdateRadar(CAM,RADAR,RadarSize,range*Factor,100)
   
    RenderWorld
   
    WaitTimer TIMER
   
    Flip 0
   
Wend

End

; populate scene with dummy objects
Function InitObjects(cam%,radar%,number%=100,range#=500.0,method$="cubic")
   
    Local i%,b.blip,col#,tex%
   
    AmbientLight 255,255,255
   
    Local temp%=CreatePivot()
   
    For i=1 To number
       
        b.blip = New blip
       
        bscale=Rnd(1,Rnd(2,Rnd(3,4)))
       
        Select method
               
            ; random cubic placement
            Case "cubic":
               
                bx=Rnd(-range,range)*Factor
                by=Rnd(-range,range)*Factor
                bz=Rnd(-range,range)*Factor
               
            ; random spherical placement
            Case "spherical":
               
                PositionEntity temp,0,0,0
                TurnEntity temp,Rnd(-Rnd(0,360),Rnd(0,360)),Rnd(0,Rnd(0,360)),0
                MoveEntity temp,0,0,Rnd(Rnd(Rnd(range),range),range)*Factor
                bx=EntityX(temp)
                by=EntityY(temp)
                bz=EntityZ(temp)
               
        End Select
       
        ; color
        col=Rnd(1)
        If col>0.50 And col<=1.00 Then b=255 : bg=  0 : b=  0 : tex=SUNTEX_RED : bscale=bscale/2.00
        If col>0.20 And col<=0.50 Then b=255 : bg=255 : b=  0 : tex=SUNTEX_YEL : bscale=bscale/1.00
        If col>0.10 And col<=0.20 Then b=  0 : bg=  0 : b=255 : tex=SUNTEX_BLU : bscale=bscale*1.25
        If col>0.00 And col<=0.05 Then b=255 : bg=255 : b=255 : tex=SUNTEX_WHI : bscale=bscale*1.50
       
        ; create the entity itself
        bentity=CreateQuad(False,1.0,tex,3,2+32,b,bg,b,1.00)
        PositionEntity bentity,bx,by,bz
        PointEntity bentity,cam
        EntityBlend bentity,3
       
        ; creates a radar blip
        blip=CreateQuad(radar,1.0,tex,3,2+32,b,bg,b,1.00)
        EntityOrder blip,-100
        RotateMesh blip,90,0,0
       
        ; creates a blip pole
        bpole=CreateQuad(radar,1.0,False,3,2+32,b,bg,b,0.50)
        EntityOrder bpole,-50
        RotateMesh bpole,90,0,0
       
    Next
   
End Function

; simple spaceship movement
Function Movement(cam%,sensitivity#=1.0)
   
    Local roll#,cx#,cz#,tx#,ty#,multi%=1
   
    ; arrows = move / SHIFT or LMB = Turbo
    cz=(KeyDown(200)-KeyDown(208))*CameraSpeed
    ;cx=(KeyDown(205)-KeyDown(203))*CameraSpeed
    roll=(KeyDown(203)-KeyDown(205))*RollSpeed
    If KeyDown(42) Or KeyDown(54) Or MouseDown(1) Then multi=10
   
    tx=Normalize(MouseX(),0,WIDTH , 1,-1)
    ty=Normalize(MouseY(),0,HEIGHT,-1, 1)
   
    If ty<0 Then ty=(Abs(ty)^sensitivity)*-1 Else ty=ty^sensitivity
    If tx<0 Then tx=(Abs(tx)^sensitivity)*-1 Else tx=tx^sensitivity
   
    TurnEntity cam,ty*TurnSpeed,tx*TurnSpeed,roll*TurnSpeed
    MoveEntity cam,cx*multi,0,cz*multi
   
End Function

; creates a nice elite stype radar sprite at screen bottom
Function InitRadar(scale#,size%=512,quads%=8,parent%=False,sharp#=1.0,blur#=50.0)
   
    Local RADAR%=CreateQuad()
    EntityParent RADAR,parent
   
    Local tex1%=CreateGridTexture(size,quads)
    Local tex2%=CreateGlowTexture(size,sharp,blur)
   
    ; apply textures to radar quad
    EntityFX RADAR,1+16
    ScaleEntity RADAR,scale,scale,scale
    PositionEntity RADAR,0,-HEIGHT+(RadarSize*0.6),WIDTH
    TextureBlend tex1,2
    TextureBlend tex2,3
    EntityTexture RADAR,tex1,0,1
    EntityTexture RADAR,tex2,0,2
    EntityBlend RADAR,1
   
    ; create centered ship marker (you!)
    Local CENTER%=CreateCone(4,1,RADAR)
    RotateMesh CENTER,-100,0,0
    PositionEntity CENTER,0,0,0
    ScaleEntity CENTER,1.0/32,1.0/32,1.0/32
    EntityFX CENTER,1
    EntityColor CENTER,255,255,0
   
    EntityOrder RADAR,-50
    EntityOrder CENTER,-100
   
    Return RADAR
   
End Function

Function UpdateObjects(cam%)
   
    Local b.blip,d#,s#
   
    For b.blip = Each blip
       
        If EntityInView(bentity,cam) Then
           
            d=EntityDistance(bentity,cam)
            s=Exp(1.0/d*(Factor*10))*bscale
            If s<d*0.005 Then s=d*0.005
            ScaleEntity bentity,s,s,s
            ShowEntity bentity
            PointEntity bentity,cam
           
        Else
           
            HideEntity bentity
           
        EndIf
       
    Next
   
End Function

; update quad color / alpha
Function UpdateQuad(mesh%,r%=255,g%=255,b%=255,a#=1.0)
   
    Local v%,surf=GetSurface(mesh,1)
   
    For v=0 To 3
       
        VertexColor surf,v,r,g,b,a
       
    Next
   
End Function

; updates the radar scene
Function UpdateRadar(cam%,radar%,scale#=128.0,range#=256.0,angle#=60.0)
   
    Local b.blip
    Local d#,a#
    Local rx#,ry#,rz#
   
    Local halfscale#=scale/2.0
    Local doublescale#=scale*2.0
    Local ps#=PoleWidth/scale
    Local bs#=BlipSize/halfscale
    Local div#=((scale/2.0)/range)*RadarBorder
   
    Local visible%=0
   
    For b.blip = Each blip
       
        ; get distance to object
        d=EntityDistance(bentity,cam)
       
        ; hide radar blip/pole
        HideEntity bpole
        HideEntity blip
       
        ; object within scanner range?
        If d<range*2 Then
           
            ; within scanner range?
            If d<range Then
               
                ; soft fade out objects reaching radar border
                a=Normalize(d,range*RadarBorder,range,1,0)
                If a>1 Then a=1 Else If a<0 Then a=0
                UpdateQuad(blip,b,bg,b,a)
                UpdateQuad(bpole,b,bg,b,a*0.5)
               
                ; show blip and its pole
                ShowEntity bpole
                ShowEntity blip
               
                ; transform position
                TFormPoint (bx,by,bz,0,cam)
                rx=TFormedX()/halfscale*div
                rz=TFormedZ()/halfscale*div
                ry=-TFormedY()*div
           
                ; reposition/scale blip
                PositionEntity blip,0,0,0
                TranslateEntity blip,rx,rz,ry/scale*VertScale
                ScaleEntity blip,bs,bs,bs
           
                ; reposition/scale pole
                PositionEntity bpole,0,0,0
                ScaleEntity bpole,ps,ps,Abs(ry/doublescale*VertScale)
                TranslateEntity bpole,rx,rz,ry/doublescale*VertScale
               
            EndIf
           
        EndIf
       
    Next
   
    ; radar always points to cam
    RotateEntity radar,angle,0,0
   
    Return visible
   
End Function

; advanced quad creation
Function CreateQuad(parent%=False,scale#=1.0,tex%=False,blend%=False,fx%=False,r%=255,g%=255,b%=255,a#=1.0)
   
    Local mesh%=CreateMesh()
    Local surf%=CreateSurface(mesh)
   
    Local v0%=AddVertex(surf, 1, 1,0,0,0)
    Local v1%=AddVertex(surf,-1, 1,0,1,0)
    Local v2%=AddVertex(surf,-1,-1,0,1,1)
    Local v3%=AddVertex(surf, 1,-1,0,0,1)
   
    AddTriangle surf,v0,v1,v2
    AddTriangle surf,v0,v2,v3
   
    If parent Then EntityParent mesh,parent
    If fx Then EntityFX mesh,fx
    If tex Then EntityTexture mesh,tex
    If blend Then EntityBlend mesh,blend
   
    EntityColor mesh,r,g,b
    EntityAlpha mesh,a
   
    VertexColor surf,v0,r,g,b,a
    VertexColor surf,v1,r,g,b,a
    VertexColor surf,v2,r,g,b,a
    VertexColor surf,v3,r,g,b,a
   
    ScaleEntity mesh,scale,scale,scale
   
    Return mesh
   
End Function

; create procedural glowing border texture
Function CreateGlowTexture(size%=512,sharpness#=1.0,blur#=50.0)
   
    Local strength#=512*1.0/size*sharpness
    Local tex%=CreateTexture(size,size,3)
    Local tb%=TextureBuffer(tex)
   
    Local x%,y%,i#,j%,col%,rgb%,rc%,gc%,bc%
   
    LockBuffer tb
   
    For x=0 To size-1
       
        For y=0 To size-1
           
            ; clear colors
            rgb=0*$1000000+0*$10000+0*$100+0
            WritePixelFast x,y,rgb,tb
           
        Next
       
    Next
   
    For j=1 To (size/2)-2
       
        ; exponential falloff
        col=j*strength/Exp((((size/2)-2)-j)*(strength/blur))
       
        If col>224 Then col=Normalize(col,224,255,224,0)
        If col<0 Then col=0
       
        ; multiply RGB with brightness
        rc=R3*col Shr 8
        gc=G3*col Shr 8
        bc=B3*col Shr 8
       
        rgb=255*$1000000+rc*$10000+gc*$100+bc
       
        For i=0 To 360 Step 0.05
           
            WritePixelFast ((size-2)/2.0)+(Sin(i)*j),((size-2)/2.0)+(Cos(i)*j),rgb,tb
           
        Next
       
    Next
   
    UnlockBuffer tb
   
    Return tex
   
End Function

; normalize a value
Function Normalize#(value#=128.0,vmin#=0.0,vmax#=255.0,nmin#=0.0,nmax#=1.0)
   
    Return ((value#-vmin#)/(vmax#-vmin#))*(nmax#-nmin#)+nmin#
   
End Function

; create radar grid texture
Function CreateGridTexture(size%=512,quads%=16)
   
    Local x%,y%,j%,i#,rgb%,col%,r%,g%,b%
    Local steps%=size/quads
   
    Local tex%=CreateTexture(size,size,3+8)
    Local buffer%=TextureBuffer(tex)
   
    SetBuffer buffer
   
    ; background color
    Color R2,G2,B2
    Rect 0,0,size,size,1
   
    ; create grid
    Color R1,G1,B1
    For x=0 To quads-1
        For y=0 To quads-1
            Rect x*steps,y*steps,steps+1,steps+1,0
        Next
    Next
   
    Color 255,255,255
   
    LockBuffer buffer
   
    ; create glowing border and delete pixels outside the circle
    For j=(size/2)-6 To size
       
        For i=0 To 360 Step 0.05
           
            x=((size-2)/2.0)+(Sin(i)*j)
            y=((size-2)/2.0)+(Cos(i)*j)
           
            col=0
            If j<=(size/2) Then col=Normalize(j,(size/2)-6,(size/2),255,0)
           
            ; multiply RGB with brightness
            r=R3*col Shr 8
            g=G3*col Shr 8
            b=B3*col Shr 8
           
            rgb=0*$1000000+r*$10000+g*$100+b
           
            If x>=0 And x<size And y>=0 And y<size Then
               
                WritePixelFast x,y,rgb,buffer
               
            EndIf
           
        Next
       
    Next
   
    UnlockBuffer buffer
   
    SetBuffer BackBuffer()
   
    Return tex
   
End Function

; create a stunning sun texture
Function CreateSunTexture(size%=512,r%=255,g%=255,b%=255)
   
    Local tex%=CreateTexture(size,size,3)
    Local tb%=TextureBuffer(tex)
   
    Local i#,j%,col%,rgb%
    Local x%,y%,xx%,yy%
    Local a%
   
    LockBuffer tb
   
    For j=0 To (size/2)-1
       
        col=255-Normalize(j,0,(size/2)-1,0,255)
        If col>255 Then col=255
        rgb=col*$1000000+col*$10000+col*$100+col
       
        For i=0 To 360 Step 0.05
           
            WritePixelFast (size/2)+(Sin(i)*j),(size/2)+(Cos(i)*j),rgb,tb
           
        Next
       
    Next
   
    UnlockBuffer tb
   
    Local tempcam%=CreateCamera()
    Local tempsun%=CreatePivot()
   
    CreateQuad(tempsun,size/4.0,tex,3,1+8+16,r*1.00,g*1.00,b*1.00,1.00)
    CreateQuad(tempsun,size/1.5,tex,3,1+8+16,r*1.00,g*1.00,b*1.00,1.00)
    CreateQuad(tempsun,size/1.2,tex,3,1+8+16,r*0.75,g*0.75,b*0.50,0.75)
    CreateQuad(tempsun,size/1.0,tex,3,1+8+16,r*0.50,g*0.50,b*0.50,0.50)
   
    PositionEntity tempsun,0,0,WIDTH
   
    RenderWorld
   
    FreeEntity tempsun
   
    LockBuffer BackBuffer()
    LockBuffer tb
   
    For x=0 To size-1
       
        For y=0 To size-1
           
            xx=(WIDTH/2)-(size/2)+x
            yy=(HEIGHT/2)-(size/2)+y
           
            rgb=ReadPixelFast(xx,yy,BackBuffer())
           
            r=(rgb And $ff0000)/$10000
            g=(rgb And $ff00)/$100
            b=(rgb And $ff)
            a=255
           
            If (r+g+b)/3 < 32 Then a=Normalize((r+g+b)/3,0,32,0,255)
           
            rgb=(r+g+b)/3*$1000000+r*$10000+g*$100+b
           
            WritePixelFast x,y,rgb,tb
           
        Next
       
    Next
   
    UnlockBuffer tb
    UnlockBuffer BackBuffer()
   
    RenderWorld
   
    FreeEntity tempcam
   
    Return tex
   
End Function



puki(Posted 1+ years ago)

 Here are the two images together, it makes it easier to cross-reference:


Blitzplotter(Posted 1+ years ago)

 Indeed, very cool & inspiring - lovely piece of coding. In fact it inpsired me to look at the relative distance of the planets & their paths through our solar system:-<a href="http://www.solarviews.com/eng/solarsys.htm" target="_blank">http://www.solarviews.com/eng/solarsys.htm[/url] [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal