CameraEnvironmentWeight# ( camera,asset )

Parameters

camera - a camera that has rendered

asset - an environment asset, from FindWorldAsset

Description

Returns how much of an environment a camera drew with at its last render, from 0 to 1.

A world's look can be a mix of environments: its own, a time-of-day cycle, zones the camera is in, and fades started with FadeWorldEnvironment. This tells your game how much of one of them a camera saw, so gameplay can follow the look: shiver when the snow environment is above half, close the shop when the night environment takes over, or play a warning while a storm is rolling in.

The answer is from the camera's last render, so call it after RenderWorld (or RenderWorldToCanvas). Before a camera has rendered, it is the world's look without zones. For a world that is not blending, it is 1 for the world's own environment and 0 for any other. The weights of all the environments a camera saw add up to 1.

See also: FadeWorldEnvironment, ApplyWorldEnvironment, FindWorldAsset.

Example

; CameraEnvironmentWeight Example
; -------------------------------

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

camera=CreateCamera()
PositionEntity camera,0,2,-6

; A field with a scarecrow
ground=CreatePlane()
EntityColor ground,90,140,70
post=CreateCylinder(8)
ScaleEntity post,0.1,1.5,0.1
PositionEntity post,0,1.5,2
arms=CreateCube()
ScaleEntity arms,1.2,0.1,0.1
PositionEntity arms,0,2.2,2

; Fine weather, and a storm that rolls in and out every few seconds
fine=MakeEnvironment("{'ambient':[80,90,100],'sky':{'top':[60,120,205],'horizon':[185,210,235],'ground':[95,90,82],'clouds':0.3},'sun':{'color':[255,244,225],'elevation':50,'azimuth':135}}")
storm=MakeEnvironment("{'ambient':[50,55,65],'sky':{'top':[95,100,110],'horizon':[150,155,162],'ground':[70,72,75],'clouds':0.95,'cloudColor':[125,130,140]},'fog':{'color':[110,115,125],'near':5,'far':60}}")
ApplyWorldEnvironment GetWorld(),FindWorldAsset(fine,"resource")
storm_asset=FindWorldAsset(storm,"resource")
next_change=MilliSecs()+4000
coming=False

While Not KeyDown(1)

    ; Every four seconds the storm starts to arrive or leave, over three seconds
    If MilliSecs()>next_change Then
        coming=Not coming
        FadeWorldEnvironment GetWorld(),storm_asset,coming,3
        next_change=MilliSecs()+4000
    EndIf

    ; 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

    TurnEntity arms,0,0.5,0
    UpdateWorld
    RenderWorld

    ; How much storm the camera drew with: gameplay can react to it
    weight#=CameraEnvironmentWeight(camera,storm_asset)
    Text 0,0,"Arrow keys: move camera   Esc: exit"
    Text 0,20,"CameraEnvironmentWeight(camera,storm) = "+weight
    If weight>0.5 Then Text 0,40,"The storm is here: take shelter!"

    Flip

Wend

End

; Builds an environment from its settings (' for ") and waits for it
Function MakeEnvironment(definition$)
    document=CreateResourceDocument("environment")
    source$="{'format':'blitz3d.environment','version':1,'id':'"+CreateUuid()+"','assets':[],'definition':"+definition+"}"
    If Not ReplaceResourceDocumentSource(document,Replace(source,"'",Chr(34)),ResourceDocumentRevision(document)) Then RuntimeError ResourceDocumentError()
    preset=LoadResourceDocumentAsync(document)
    FreeResourceDocument document
    While Not WorldReady(preset)
        If WorldLoadState(preset)=WORLD_LOAD_FAILED Then RuntimeError WorldLoadError(preset)
        UpdateAsyncLoads 2
    Wend
    Return preset
End Function

Index