CreateLight ( [type][,parent] )
Parameters
|
type (optional) - type of light 1: directional (default) 2: point 3: spot parent (optional) - parent entity of light |
Description
|
Creates a light. Lights work by affecting the colour of all vertices within the light's range. You need to create at least one light if you wish to use 3D graphics, otherwise everything will appear flat. The optional type parameter allows you to specify the type of light you wish to create. A value of 1 creates a directional light. This works similar to a sun shining on a house: all walls facing a certain direction are lit the same, and how much they are lit depends on the angle of the light reaching them. Directional lights have infinite 'position' and infinite range. A value of 2 creates a point (or omni) light. This works a little bit like a light bulb in a house, starting from a central point and gradually fading outwards. A value of 3 creates a spot light. This is a cone of light, similar to shining a torch in a house. It starts with an inner angle of light and extends towards an outer angle of light. You can adjust the angles of a spot light with the LightConeAngles command. The optional parent parameter allows you to specify a parent entity for the light, so that when the parent is moved the child light will move with it. However, this relationship is one way; applying movement commands to the child will not affect the parent. Specifying a parent entity will still result in the light being created at position 0,0,0 rather than at the parent entity's position. Other notes: Up to 8 lights affect a scene at once. This was the classic DirectX-era guarantee (8 or 16 depending on the video card), and the modern DirectX 12 renderer keeps the same budget: it applies the first 8 visible lights and ignores any further ones for that render. Also, you should remember that each light added affects the rendering speed. Lights do not cast shadows by default. On the modern renderer you can enable real dynamic shadows per light with LightShadows (see the Shadows family). Many games also get around these issues by the use of a pre-calculated 'baked' lightmap texture for the static geometry in the scene. Other lighting techniques include: adjusting vertex colors, and/or dynamic lights (ie. moving the lights around in the scene as they are needed). See also: LightRange, LightColor, LightConeAngles, AmbientLight. |
Example
; CreateLight Example ; ------------------- Graphics3D 640,480,0,2 SetBuffer BackBuffer() camera=CreateCamera() PositionEntity camera,0,1,-6 ; Keep ambient light low so the created light does the work AmbientLight 30,30,30 ; A row of spheres to catch the light For i=-1 To 1 sphere=CreateSphere(16) PositionEntity sphere,i*2.5,0,2 Next ; Create a directional light to start with light_type=1 light=CreateLight(light_type) PositionEntity light,0,3,-3 RotateEntity light,45,0,0 While Not KeyDown(1) ; Keys 1-3 recreate the light: 1=directional 2=point 3=spot new_type=light_type If KeyHit(2) Then new_type=1 If KeyHit(3) Then new_type=2 If KeyHit(4) Then new_type=3 If new_type<>light_type light_type=new_type FreeEntity light light=CreateLight(light_type) PositionEntity light,0,3,-3 RotateEntity light,45,0,0 ; Point and spot lights fade with distance, so give them a short range If light_type>1 Then LightRange light,10 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 RenderWorld type_name$="directional" If light_type=2 Then type_name$="point" If light_type=3 Then type_name$="spot" Text 0,0,"1/2/3: light type Arrow keys: move camera Esc: exit" Text 0,20,"CreateLight("+light_type+") - "+type_name$ Flip Wend End
Index