Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[EO 3.0] Tile based simulated lighting system 1.0 (Deathbeam)


zerohero
 Share

Recommended Posts

**[EO 3.0] Tile based simulated lighting system 1.0**

This is the very first revision for a project which I intent to carry on improving and adjusting to become far more powerful than it currently is. In short this is a slightly revised version of **DeathBeams** CS:DE script and all credit goes to him for his hard work. This is currently tested and fully 3.0 (nightly compatible).

The final result will be simular to this

>! ![](http://i1012.photobucket.com/albums/af244/zeroohero/result.jpg)

So what are we waiting for? Lets begin!

**All modifications will be client side only**

Open up the '**ModGlobals**' module and at the very bottom add

```
' Light emitter
Public Const LIGHT_MAXSTEPS As Long = 8
Public Const LIGHT_BEGIN As Long = 255
Public Const LIGHT_END As Long = 50
Public Const AlphaThreshold As Long = 70

```
**Change** The **AlphaThreshold** variable to the alpha amount that you want the light source to kick in at (this is set via the map properties) . I am currently using a threshold of 70 in my project.

Ok now open up your '**ModGameLogic**' Module and at the very bottom add this function

```
Public Function CalcVertexBrightness(LightX As Long, LightY As Long, VertexX As Long, VertexY As Long)
Dim x1 As Long, x2 As Long, y1 As Long, y2 As Long, distance As Double, LightSteps As Long
    Dim lXDistance As Long
    Dim lYDistance As Long
    Dim lDistance As Long
    Dim lVertexDistance As Long
    Dim lDistanceNormal As Long
    Dim lLightSteps As Long

    x1 = ConvertMapX(LightX * 32) + 8
    y1 = ConvertMapY(LightY * 32) + 8
    x2 = VertexX
    y2 = VertexY

    lDistanceNormal = CLng(Sqr((PIC_X / 2) ^ 2 + (PIC_Y / 2) ^ 2))
    '// Calculate distance from vertex
    lXDistance = CLng(Abs(x1 - x2))
    lYDistance = CLng(Abs(y1 - y2))
    lVertexDistance = CLng(Sqr(lXDistance ^ 2 + lYDistance ^ 2) / lDistanceNormal)

    If lVertexDistance <= LIGHT_MAXSTEPS Then
        '// Calculate light steps
        lLightSteps = CLng(Abs((LIGHT_BEGIN - LIGHT_END)) / (LIGHT_MAXSTEPS))

        If lLightSteps = 0 Then
                lLightSteps = 1
        End If

        '// Calculate vertex brightness
        CalcVertexBrightness = CLng(LIGHT_BEGIN - (lVertexDistance * lLightSteps))
    Else
        '// Return lowest value
        CalcVertexBrightness = LIGHT_END
    End If
End Function
```
Open up your '**ModGraphics**' Module and search for

```
Public Sub RenderTexture
```
Above that sub add this entire sub, this is what will be drawing our simulated light -

```
Public Sub RenderPlayerLight(ByRef TextureRec As DX8TextureRec, ByVal dX As Long, ByVal dY As Long, ByVal sX As Single, ByVal sY As Single, ByVal dWidth As Single, ByVal dHeight As Single, ByVal sWidth As Single, ByVal sHeight As Single, Optional Color As Long = -1, Optional IsDark As Boolean)
    Dim textureWidth As Long, textureHeight As Long
    Dim Box(0 To 3) As TLVERTEX, x As Long
    Dim lVertexBrightness As Long
    Dim TextureNum As Long

    TextureNum = TextureRec.Texture

    ' Create color differs
    lVertexBrightness = CalcVertexBrightness(GetPlayerX(MyIndex), GetPlayerY(MyIndex), dX, dY)
    Color = D3DColorARGB(CurrentTintA + Abs(255 - CurrentTintA) / 2, lVertexBrightness, lVertexBrightness, lVertexBrightness)

    textureWidth = gTexture(TextureNum).TexWidth
    textureHeight = gTexture(TextureNum).TexHeight

    If sY + sHeight > textureHeight Then Exit Sub
    If sX + sWidth > textureWidth Then Exit Sub
    If sX < 0 Then Exit Sub
    If sY < 0 Then Exit Sub

    For x = 0 To 3
        Box(x).RHW = 1
        Box(x).Color = Color
    Next

    Box(0).x = dX
    Box(0).y = dY
    Box(0).tu = (sX / textureWidth)
    Box(0).tv = (sY / textureHeight)
    Box(1).x = dX + dWidth
    Box(1).tu = (sX + sWidth + 1) / textureWidth
    Box(2).x = Box(0).x
    Box(3).x = Box(1).x

    Box(2).y = dY + dHeight
    Box(2).tv = (sY + sHeight + 1) / textureHeight

    Box(1).y = Box(0).y
    Box(1).tv = Box(0).tv
    Box(2).tu = Box(0).tu
    Box(3).y = Box(2).y
    Box(3).tu = Box(1).tu
    Box(3).tv = Box(2).tv

    Direct3D_Device.SetTexture 0, gTexture(TextureNum).Texture
    Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Box(0), FVF_Size
End Sub

```
Ok now it is time to start drawing the player light!

Open up your '**ModGraphics**' module once more and search for

```
Public Sub DrawMapTile(ByVal x As Long, ByVal y As Long)
```
Within this sub, directly underneath -

```
' Draw normally
                RenderTexture Tex_Tileset(.Layer(i).Tileset), ConvertMapX(x * PIC_X), ConvertMapY(y * PIC_Y), .Layer(i).x * 32, .Layer(i).y * 32, 32, 32, 32, 32, -1

```
**Add** The following code -

```
If CurrentTintA > AlphaThreshold Then
                RenderPlayerLight Tex_Tileset(.Layer(i).Tileset), ConvertMapX(x * PIC_X), ConvertMapY(y * PIC_Y), .Layer(i).x * 32, .Layer(i).y * 32, 32, 32, 32, 32, -1 

End If
```
What this does is tells our game engine to render the light between the ground and mask layer, meaning it will not render on top of the fringe layers, you can change this if you wish.

Believe it or not but you are now done! Open up your admin panel, map editor and then hit properties. Set the RGB values of the map all to 1 and increase the alpha amount above your threshold and the lighting should kick it!

The final result should be something like this, note how the light does not overlap the fringe elements, user interface or anything else it should not-

![](http://i1012.photobucket.com/albums/af244/zeroohero/result.jpg)

***some notes***

The line

```
CurrentTintA + Abs(255 - CurrentTintA) / 2
```
Is completely subjective to my game environment, meaning it may not have a good effect in yours. Try playing with these numbers to find what is best for you.

I would also recommend that anyone wishing to use this extensively should think bout added map moral checks, so that the lighting responds differently for example to indoor v outdoor environments.

I will try and update this with any progress I make on the subject. Please remember this is Deathbeams concept that I have just modified for better uses.
Link to comment
Share on other sites

@Deathbeam:

> I think more light than one cant be done via this metod i tried it and i chached them like sound tiles but it just dont work, rendering will overlay

That is exactly right, this method of rendering is not really suited for many light sources, however I believe it can be adapted and change to do so.. which is what I am looking into. The easiest and most effective form of lighting would defiantly be the dx8 lighting protocalls themselves, but I am still unsure how to use them in the game environment yet.
Link to comment
Share on other sites

@zerohero:

> That is exactly right, this method of rendering is not really suited for many light sources, however I believe it can be adapted and change to do so.. which is what I am looking into. The easiest and most effective form of lighting would defiantly be the dx8 lighting protocalls themselves, but I am still unsure how to use them in the game environment yet.

Here is base for DX8 lighting (real metod) what i made: http://www.touchofdeathforums.com/smf2/index.php/topic,81705.0.html
i hope you can get it to work
Link to comment
Share on other sites

@or3o:

> I did the tutorial and i have to say it works great. i was just wondering if there is a reletivly simple way to adjust my light radius? like i could make it so items add to my light radius?

Heya or3o, try ajusting

```
Public Const LIGHT_MAXSTEPS As Long =
```
The higher the number the greater the tile light radius and vice versa. Hope this helps.
Link to comment
Share on other sites

@or3o:

> thank you very much and thanks agian for the tutorial  hey is it just me or does the lighting act wierd when used on a map with autotiles?

Hmm that is because the light is drawn between ground and mask2 layers in this tutorial, we are not telling it to draw over auto tiles. Lets fix that -

Use this code again -

```
If CurrentTintA > AlphaThreshold Then
                RenderPlayerLight Tex_Tileset(.Layer(i).Tileset), ConvertMapX(x * PIC_X), ConvertMapY(y * PIC_Y), .Layer(i).x * 32, .Layer(i).y * 32, 32, 32, 32, 32, -1 

End If
```
And now place that underneath the auto-tile drawing -

```
ElseIf Autotile(x, y).Layer(i).renderState = RENDER_STATE_AUTOTILE Then
                ' Draw autotiles
```
So your entire '**DrawMapTile**' sub within **ModGtaphics** looks like this

```
Public Sub DrawMapTile(ByVal x As Long, ByVal y As Long)
Dim rec As RECT
Dim i As Long

    ' If debug mode, handle error then exit out
    If Options.Debug = 1 Then On Error GoTo errorhandler

    With Map.Tile(x, y)
        For i = MapLayer.Ground To MapLayer.Mask2
            If Autotile(x, y).Layer(i).renderState = RENDER_STATE_NORMAL Then
                ' Draw normally
                RenderTexture Tex_Tileset(.Layer(i).Tileset), ConvertMapX(x * PIC_X), ConvertMapY(y * PIC_Y), .Layer(i).x * 32, .Layer(i).y * 32, 32, 32, 32, 32, -1
                If CurrentTintA > AlphaThreshold Then
                RenderPlayerLight Tex_Tileset(.Layer(i).Tileset), ConvertMapX(x * PIC_X), ConvertMapY(y * PIC_Y), .Layer(i).x * 32, .Layer(i).y * 32, 32, 32, 32, 32, -1 

End If
            ElseIf Autotile(x, y).Layer(i).renderState = RENDER_STATE_AUTOTILE Then
                ' Draw autotiles
                DrawAutoTile i, ConvertMapX(x * PIC_X), ConvertMapY(y * PIC_Y), 1, x, y
                DrawAutoTile i, ConvertMapX((x * PIC_X) + 16), ConvertMapY(y * PIC_Y), 2, x, y
                DrawAutoTile i, ConvertMapX(x * PIC_X), ConvertMapY((y * PIC_Y) + 16), 3, x, y
                DrawAutoTile i, ConvertMapX((x * PIC_X) + 16), ConvertMapY((y * PIC_Y) + 16), 4, x, y
If CurrentTintA > AlphaThreshold Then
                RenderPlayerLight Tex_Tileset(.Layer(i).Tileset), ConvertMapX(x * PIC_X), ConvertMapY(y * PIC_Y), .Layer(i).x * 32, .Layer(i).y * 32, 32, 32, 32, 32, -1 

End If
            End If
        Next
    End With

    ' Error handler
    Exit Sub

errorhandler:
    HandleError "DrawMapTile", "modGraphics", Err.Number, Err.Description, Err.Source, Err.HelpContext
    Err.Clear
    Exit Sub
End Sub

```
Hope this helps
Link to comment
Share on other sites

@Deathbeam:

> But you drawed it into new texture that overlays entire screen or?

Screen coloring xD

I used to use that method…

Of course, I'm still working on the light, currently i load a texture. But the darkness is blended away from the player through map coloring.
Link to comment
Share on other sites

  • 5 months later...
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...