Blitz3D+ Command Reference

PickedSurface ( )

Parameters

None.

Description

Returns a handle to the surface hit by the most recent pick.

After a CameraPick, EntityPick or LinePick strikes a mesh, this tells you which of the mesh's surfaces the ray went through. The handle works with the usual surface commands - count its triangles with CountTriangles, dig out the exact triangle with PickedTriangle and TriangleVertex. Since each surface has one brush, knowing the surface can tell you what kind of material was hit.

Surface information only exists when the pick tested actual polygons - the picked entity's EntityPickMode must be 2 (polygon). Picks resolved against sphere or box pick modes return 0 here.

See also: PickedTriangle, PickedEntity, EntityPickMode, CountTriangles, GetSurface.

Example

; PickedSurface Example
; ---------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,3,-8

light=CreateLight()
RotateEntity light,45,45,0

; Shooting-gallery floor - EntityPickMode 2 makes it pickable by its polygons
floor=CreatePlane()
EntityColor floor,70,110,70
EntityPickMode floor,2

; Three targets, all pickable by their polygons
cube=CreateCube()
PositionEntity cube,-3,1,4
EntityPickMode cube,2

ball=CreateSphere()
PositionEntity ball,0,1.4,4
EntityPickMode ball,2

drum=CreateCylinder()
PositionEntity drum,3,1,4
EntityPickMode drum,2

; Small marker sphere shows the exact pick point
marker=CreateSphere()
ScaleEntity marker,0.15,0.15,0.15
EntityColor marker,255,255,0
HideEntity marker

bob#=0

While Not KeyDown(1)

    ; Repaint the targets; the picked one is flashed white below
    EntityColor cube,200,180,60
    EntityColor ball,60,120,220
    EntityColor drum,220,80,60

    ; The ball bobs gently so the gallery feels alive
    bob=bob+2
    PositionEntity ball,0,1.4+0.4*Sin(bob),4

    ; Click to shoot - CameraPick casts a pick ray through the mouse cursor
    If MouseHit(1) Then CameraPick(camera,MouseX(),MouseY())

    ; Highlight the last picked entity and park the marker on the pick point
    hit=PickedEntity()
    If hit<>0
        If hit<>floor Then EntityColor hit,255,255,255
        ShowEntity marker
        PositionEntity marker,PickedX(),PickedY(),PickedZ()
    Else
        HideEntity marker
    EndIf

    ; PickedSurface returns the surface hit - compare handles to name its owner
    surf=PickedSurface()
    owner$="none - nothing picked yet"
    If surf=GetSurface(cube,1) Then owner$="the cube's surface"
    If surf=GetSurface(ball,1) Then owner$="the ball's surface"
    If surf=GetSurface(drum,1) Then owner$="the drum's surface"

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    ; Crosshair at the mouse position
    Rect MouseX()-6,MouseY(),13,1
    Rect MouseX(),MouseY()-6,1,13

    Text 0,0,"Click a target to pick it   Arrow keys: move camera   Esc: exit"
    Text 0,20,"PickedSurface: "+surf+" ("+owner$+")"
    Text 0,40,"The handle can be fed to surface commands such as CountTriangles"

    Flip

Wend

End

Index