zerohero Posted July 23, 2012 Author Share Posted July 23, 2012 **[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 emitterPublic Const LIGHT_MAXSTEPS As Long = 8Public Const LIGHT_BEGIN As Long = 255Public Const LIGHT_END As Long = 50Public 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 IfEnd 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_SizeEnd 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 More sharing options...
tslusny Posted July 23, 2012 Share Posted July 23, 2012 Gj bro :P But as i see this affect only tiles and players, resources etc it wont affect so it will be still 100 percent visible but under map tint so they visibility wont change depending on light Link to comment Share on other sites More sharing options...
GodWar Posted July 23, 2012 Share Posted July 23, 2012 Do you try it with more lights than one? How THIW looks like? Link to comment Share on other sites More sharing options...
tslusny Posted July 23, 2012 Share Posted July 23, 2012 @GodWar:> Do you try it with more lights than one? How THIW looks like?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 Link to comment Share on other sites More sharing options...
zerohero Posted July 23, 2012 Author Share Posted July 23, 2012 @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 overlayThat 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 More sharing options...
tslusny Posted July 23, 2012 Share Posted July 23, 2012 @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.htmli hope you can get it to work Link to comment Share on other sites More sharing options...
or3o Posted July 23, 2012 Share Posted July 23, 2012 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? Link to comment Share on other sites More sharing options...
zerohero Posted July 24, 2012 Author Share Posted July 24, 2012 @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 More sharing options...
or3o Posted July 24, 2012 Share Posted July 24, 2012 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? Link to comment Share on other sites More sharing options...
zerohero Posted July 24, 2012 Author Share Posted July 24, 2012 @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 RECTDim 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, yIf 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 Suberrorhandler:  HandleError "DrawMapTile", "modGraphics", Err.Number, Err.Description, Err.Source, Err.HelpContext  Err.Clear  Exit SubEnd Sub```Hope this helps Link to comment Share on other sites More sharing options...
hisherwin Posted July 24, 2012 Share Posted July 24, 2012 How this work? Link to comment Share on other sites More sharing options...
or3o Posted July 24, 2012 Share Posted July 24, 2012 now the lightings right but it does some wierd stuff with the autotiles lol here have a look >! ![](http://onlineorigins.zapto.org/wth.png) Link to comment Share on other sites More sharing options...
Vus Posted July 24, 2012 Share Posted July 24, 2012 Looks awesome, but I will wait till all the bugs are fixed :) Link to comment Share on other sites More sharing options...
Zopto Posted July 24, 2012 Share Posted July 24, 2012 wery immpresiv nice work Link to comment Share on other sites More sharing options...
JohnPony Posted July 24, 2012 Share Posted July 24, 2012 Mine is pixel based :) Link to comment Share on other sites More sharing options...
hisherwin Posted July 24, 2012 Share Posted July 24, 2012 It works , But how can i make the light even more lighter? Link to comment Share on other sites More sharing options...
tslusny Posted July 25, 2012 Share Posted July 25, 2012 @General:> Mine is pixel based :)But you drawed it into new texture that overlays entire screen or? Link to comment Share on other sites More sharing options...
JohnPony Posted July 25, 2012 Share Posted July 25, 2012 @Deathbeam:> But you drawed it into new texture that overlays entire screen or?Screen coloring xDI 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 More sharing options...
Ryo Posted January 24, 2013 Share Posted January 24, 2013 just use lightmaps, easier lighter and the result is way much better :/ Link to comment Share on other sites More sharing options...
zerohero Posted January 24, 2013 Author Share Posted January 24, 2013 > just use lightmaps, easier lighter and the result is way much better :/Make a tutorial on how to do that then go ahead and say how much better your method is, until then. Link to comment Share on other sites More sharing options...
Tic Tac 2 Posted May 16, 2015 Share Posted May 16, 2015 Anyone know how to make other light sources with this? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now