Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Zak's Ocean shader improved: Transparency map added (source code)  (Read 2224 times)
Lenn
Customers
Community Member
*****
Posts: 724

+/-


« on: February 14, 2009, 05:57:49 AM »


I have modified the beautiful ocean shader by Zaknafien to accept an alpha map which can be used to map transparency and allow for soft transitions into land, so the beaches have no hard edges as the ocean slowly disappears as the water gets more shallow. I guess some of you might find it useful. You can see this here:





You can download the VB.Net solution from:
http://www.johnmell.com/tv3d/TheOcean.zip (14 MB)



This is also an almost exact conversion to VB.Net. I've converted it in little time though, so not all coding formats have been kept, but the methods and variables are all exactly the same and have the same names. I've also removed the InfoGetter window.

There are now 5 meshes (ie floor / water plane). One is central around the island (near) and has the shader with alpha mapping applied to it, and the other 4 surround it and tile perfectly to increase how far the oceans stretch. Wink

I have modified the height map to better suit beachy looking transitions. The alpha map defining the shoreline has been made by increasing contrast of the heightmap to certain values (Curves in Photoshop) and blurring it a little. The values for doing this you will need to figure out yourself in order to get accurate transitions. Wink You can also for example have separate meshes (squares) that go along the shoreline with their alpha maps, connecting to non-transparent water further out to sea. There are many ways to optimize the use of this shader, so figuring that out will be up to you. This is just a small sample showing the code and the techniques, not really the most efficient way of doing things (especially for large terrains).

Let me know if you find it useful! Smiley
Logged

Mietze
Community Member
*
Posts: 412

Pleeease, don't let it crash!


WWW
« Reply #1 on: February 14, 2009, 07:07:19 AM »

Hey, you finally made it! Looking good!
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Zaknafein
Customers
Community Member
*****
Posts: 2929


WWW
« Reply #2 on: February 14, 2009, 10:23:23 AM »

Fantastic! This makes it look so much better.
Logged

Lenn
Customers
Community Member
*****
Posts: 724

+/-


« Reply #3 on: February 14, 2009, 02:10:29 PM »

Yep Mietze, thanks for your help too.

Fantastic! This makes it look so much better.

Aye Tongue
I think you left it out on purpose? Because it was fairly easy to implement (now that i've learned HLSL a little more at least). You just wanted someone to show dedication... heheh Wink

The hard part is, anyway, to generate accurate alpha maps around the shoreline depending on terrain height. If I add that functionality, and if it turns out to be pluggable, I will release it too.
Logged

Zaknafein
Customers
Community Member
*****
Posts: 2929


WWW
« Reply #4 on: February 14, 2009, 03:25:39 PM »

Yep... It's simple on the shader side, harder on the application side. The real difficulty is to have an integrated editor tool to generate those alphamaps.
I was planning to make such a tool, ended up being lazy and dropping the project.



Also a fun take on this "problem" of shorelines would be to use something like I did in my Volume/Area Fog demo, but in the water; do a depth difference between the bottom of the ocean (the landscape) and its surface, and use this as the alpha. No pre-processing, but surely much slower.
Logged

aksoysoft
Community Member
*
Posts: 46


WWW
« Reply #5 on: February 14, 2009, 03:28:17 PM »

Performance as very successful Wink

Thanks Zak's, Lenn.
« Last Edit: February 14, 2009, 04:14:10 PM by aksoysoft » Logged

Lenn
Customers
Community Member
*****
Posts: 724

+/-


« Reply #6 on: February 14, 2009, 04:04:17 PM »

@Zaknafien

I assumed you dropped it so i didn't even ask. It isn't that easy to do, plus there are so many ways people could map their meshes for use with the shader that you cannot predict all of the situations. Such as tiling planes only along the shoreline with irregular shapes, etc. So it has to be specific.

The only problem I see right now though, is that the size of the waves (volume map texture) have to follow the size of the alpha map because they use the same UV space. I think I will further modify the shader to send the alpha map to the 2nd UV channel while allowing the diffuse and other textures to tile. That would be much more efficient instead of having heaps of small alpha maps if there is lots of tiling.

That IS, the only problem if I managed to work around this huge bug which probably makes your shader fairly useless to me. Read it, you might have a clue:

http://www.truevision3d.com/forums/bugs/using_tvlandscapesetsplattingmode_breaks_the_tex3d_lookup_in_external_shader-t18820.0.html

@aksoysoft,
glad you like it. Smiley
« Last Edit: February 14, 2009, 04:16:29 PM by Lenn » Logged

rootsage
Customers
Community Member
*****
Posts: 446

Gamer Enthusiast


WWW
« Reply #7 on: February 15, 2009, 02:16:14 AM »

This is awesome! Thank you Lenn. Cheesy
Logged

while( !( succeed = try_again()) );
------
10 print "Is this recursive?"
20 goto 10
pabloescobar
Customers
Community Member
*****
Posts: 147


WWW
« Reply #8 on: February 22, 2009, 06:02:08 PM »

I created an function to generate such alpha map... maybe can help you.

Code:
Sub GenerateWaterMask(waterstart As TV_2DVECTOR, waterend As TV_2DVECTOR, masksize As Long, altitude As Single, FileName As String)
Dim SizeX As Long
Dim SizeY As Long

Dim i As Long
Dim cur As Long

Dim X As Long
Dim y As Long

Dim transws As TV_2DVECTOR
Dim transwe As TV_2DVECTOR

Dim CurAlt As Single
Dim Color As Long
Dim Stage As Single
Dim MaxDeep As Single

Dim LandData() As Single
Dim TexData() As Long

Dim MinStage As Single

Dim TX As Long, TY As Long

MinStage = 0.4
MaxDeep = 10
'10 = 255
'0 = 0

cur = 0

transws.X = 0
transws.y = 0
transwe.X = waterend.X - waterstart.X
transwe.y = waterend.y - waterstart.y

SizeX = waterend.X - waterstart.X
SizeY = waterend.y - waterstart.y

If masksize = 0 Then masksize = CLng(SizeX * 0.25)
If masksize > 1024 Then masksize = 1024

For X = waterstart.X To waterend.X
    For y = waterstart.y To waterend.y
        cur = cur + 1
        CurAlt = TVL.GetHeight(X, y)
        If CurAlt < altitude Then
            If (altitude - CurAlt) > MaxDeep Then
                MaxDeep = (MaxDeep + (altitude - CurAlt)) / 2
            End If
        End If
    Next
Next

cur = 0
i = TVT.CreateTexture(masksize, masksize)
TVT.LockTexture i
For X = waterstart.X To waterend.X
    For y = waterstart.y To waterend.y
        cur = cur + 1
        CurAlt = TVL.GetHeight(X, y)
       
        If X >= 0 And y >= 0 Then
            If X <= WORLD.WorldSize And y <= WORLD.WorldSize Then
                If CurAlt > altitude Then
                    Color = RGBA(MinStage, MinStage, MinStage, 1)
                Else
                    If (altitude - CurAlt) > MaxDeep Then
                        Color = RGBA(1, 1, 1, 1)
                    Else
                        Stage = (((altitude - CurAlt) / MaxDeep) * (1 - MinStage)) + MinStage
                        If Stage < MinStage Then Stage = MinStage
                        Color = RGBA(Stage, Stage, Stage, 1)
                    End If
                End If
            Else
                Color = RGBA(1, 1, 1, 1)
            End If
        Else
            Color = RGBA(1, 1, 1, 1)
        End If
        TX = ((X - waterstart.X) / (transwe.X - transws.X)) * masksize
        TY = ((y - waterstart.y) / (transwe.y - transws.y)) * masksize
       
        If TX < masksize And TY < masksize Then TVT.SetPixel i, TX, TY, Color
    Next
Next
TVT.UnlockTexture i
TVT.SaveTexture i, FileName, TV_IMAGE_DXT1
TVT.DeleteTexture i

End Sub


TVT is the object of TVTextureFactory.
TVL is the object of TVLandscape.

The parameters:
waterstart = X and Y where your water plane start
waterend = X and Y where your water plane ends
masksize = The texture size (max size = 1024 x 1024)
altitude = The altitude of your water plane
FileName = The texture filename (DDS)
Logged

-----------------------------
http://www.cristianocouto.com.br
roto23
Customers
Community Member
*****
Posts: 171


« Reply #9 on: April 12, 2009, 03:37:05 PM »

I have been studying this tutorial and I notice it uses a camera. I don't use a camera in my projects.  I always use the camera that comes with the scene object.  I tried to get this to work in my project and I see the render surfaces but nothing happens on it. In your code, I replaced "camera" with tvScene.Getcamera().  Do you think this could be a problem?

Also, I just copied the .fx files and loaded them, I don't know much about shaders yet and was curious if thats all I need to do with it? Like, I don't have to add the shader code to my project, only load the .fx file, right?
Logged

-Roto-
beyonder
Customers
Community Member
*****
Posts: 356


« Reply #10 on: April 14, 2009, 04:04:30 AM »

Hey Lenn... thanks for the shader. Zak too! Do you have any idea why every minute or so the ocean texture jumps? Its like the texture moves to a different uv or something... then after another minute it moves again.

I figure it has to do with the wavespeed/length of the texture/object. Do you have this problem too? Any ideas how to resolve?

Logged
roto23
Customers
Community Member
*****
Posts: 171


« Reply #11 on: April 14, 2009, 07:24:08 AM »

regarding the .fx files...
When I try to puts parts of this tutorial into my project, can I just copy the .fx files or do I actually have to create a reference to them or something like that?
Logged

-Roto-
beyonder
Customers
Community Member
*****
Posts: 356


« Reply #12 on: April 14, 2009, 11:01:40 AM »

roto, you can put them anywhere... just change your path when you load the shader in your code. check the code... check the code...  Wink
Logged
Lenn
Customers
Community Member
*****
Posts: 724

+/-


« Reply #13 on: April 15, 2009, 12:11:26 PM »

Hey Lenn... thanks for the shader. Zak too! Do you have any idea why every minute or so the ocean texture jumps? Its like the texture moves to a different uv or something... then after another minute it moves again.

I figure it has to do with the wavespeed/length of the texture/object. Do you have this problem too? Any ideas how to resolve?



Oh, that! Well, I don't have the project set up here right now to try it out, but I'm pretty sure that you need to use the

Code:

Shader.SetTimePeriod(fTimePeriodInSeconds)


to set the number of seconds when the values reset. Normally you would set it to some very high value for it to not happen frequently, but I'm not sure what it's for (other than overflow protection) and what happens if you set it to 0 (it probably freezes the value?). So try that out.
Logged

sybixsus
Customers
Community Member
*****
Posts: 1333


WWW
« Reply #14 on: April 18, 2009, 06:32:56 PM »

I don't know the shader concerned, but if it's using sin and cos to distort things then setting the shader's time period to 2*pi or something like that ought to work. I remember having to do this for a couple of other shaders.
Logged
beyonder
Customers
Community Member
*****
Posts: 356


« Reply #15 on: April 21, 2009, 09:35:17 AM »

Hey Lenn. Hey there Sybixus.

Well I changed the shader.settimeperiod and it hasnt happened since. I'm not sure tho cuz I'm not going to wait there and stare are it. But I think that's done it.

Thx!

Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks