Volumetric clouds in T3D

Volumetric clouds in T3D

MilkywayM16
Posts: 38
Joined: Sat Oct 03, 2015 7:26 am
Hey guys!
I've been watching T3D's development since release 3.5 and I finally decided to contribute.

This is kind of a continuation of the discussion on page 3 of this thread: http://forums.torque3d.org/viewtopic.php?f=12&t=378
I figured I'd give the topic its own thread.

So long story short, I created volumetric clouds in T3D! (not really)
I had the idea to stack cloudLayer objects in the world editor to achieve a "volumetric" cloud effect. It worked.

http://imgur.com/a/wAOdb
(These images were created with no new code, just in the level editor)

After testing my theory, I started coding a new sceneobject. The new sceneobject started with the cloudLayer code as a base, then heavily modified. I added new functions to stack layers of cloud textures, add space between them, modulate the exposure of the layers to create depth, and modulate the shape of the volume. The shader code remains unchanged so far.

I ran into a problem though. The volumes aren't being shaded correctly. This image shows what I mean:

Compared to the image above, this one looks very different even though it's largely the same thing.

This image shows a custom texture being used which I made to exaggerate the incorrect shading. The dark layers are supposed to be rendering before the lighter layers, but they aren't. I haven't modified the shader yet or any of the code that calculates normals, but it looks like a problem with the normals facing the wrong way. Even after trying to invert normals here and there, nothing seems to fix it.

I'd really like to get past this bug so that I can add more features, finish up the code, and possibly put it on github for the next release. If anyone wants to help out or has any idea on what could be causing the bug, let me know. In the mean time, I can answer any questions. Thanks!

Re: Volumetric clouds in T3D

Chelaru
Posts: 204
Joined: Wed Jul 01, 2015 10:33 am
I think that the top layers should be smaller then the lower layers. This will help with the 3d effect.

Maybe this will help: http://docs.garagegames.com/torque-3d/r ... erBin.html

Re: Volumetric clouds in T3D

MilkywayM16
Posts: 38
Joined: Sat Oct 03, 2015 7:26 am
I think that the top layers should be smaller then the lower layers. This will help with the 3d effect.

Maybe this will help: http://docs.garagegames.com/torque-3d/r ... erBin.html
Thanks for the suggestion and the link! I already have a function that controls the shape of the volume based on how many layers it has. I turned it off to exaggerate the bug that's happening. As for the render binning, I'll definitely look into it later. It sounds promising.

Re: Volumetric clouds in T3D

Chelaru
Posts: 204
Joined: Wed Jul 01, 2015 10:33 am
Happy to help. I know we talked about the possibility of adding shadow to the cloud layer and make it occlude the god ray shader. I did some digging up and found that in order to do that, one must render the cloud layer to a frame buffer and use that frame buffer for the shadow and god ray.

https://youtu.be/21UsMuFTN0k?t=1m17s

Re: Volumetric clouds in T3D

Nils
Posts: 159
Joined: Thu Feb 05, 2015 3:32 am

Good idea, I like it!

I think it would be cool to influence the material properties like colour and opacity. As you're using the same map for all layers it looks like "extruded shapes" if you know what I mean. Gradually changing colour and transparency could perhaps reduce that a bit.

Re: Volumetric clouds in T3D

MilkywayM16
Posts: 38
Joined: Sat Oct 03, 2015 7:26 am
@
Nils

Thanks! I'm definitely planning for those properties to be controlled through the level editor along with many others!

Also good news, I fixed the rendering bug! I'd be happy to explain what I was doing wrong if anyone cares to hear.

Re: Volumetric clouds in T3D

Chelaru
Posts: 204
Joined: Wed Jul 01, 2015 10:33 am
@Nils
Thanks! I'm definitely planning for those properties to be controlled through the level editor along with many others!

Also good news, I fixed the rendering bug! I'd be happy to explain what I was doing wrong if anyone cares to hear.

I would like to hear. You could add some code .

Re: Volumetric clouds in T3D

JeffR
Steering Committee
Posts: 878
Joined: Tue Feb 03, 2015 9:49 pm

I, too, would like to know what needed to be changed to fix it

Re: Volumetric clouds in T3D

MilkywayM16
Posts: 38
Joined: Sat Oct 03, 2015 7:26 am
So the fix was actually quite simple. The problem was caused by the rendering order of the layers in the cloud volumes, and by intersecting layers.

Code: Select all

for( U32 i = 0; i <= LAYER_COUNT; i++ ) { Point3F vertScale( 16.0f, 16.0f, mHeight += mSpacing ); F32 zOffset = -( mCos( mSqrt( 1.0f ) ) + 0.01f ); mVB[i].set( GFX, smVertCount, GFXBufferTypeStatic ); GFXVolumeCloudVertex *pVert = mVB[i].lock(); if(!pVert) return; for ( U32 y = 0; y < smVertStride; y++ ) { ... } 
I have this loop generating the geometry for the layers in the cloud volume (same general code that exists in the _initBuffers() function in the cloudLayer object). The LAYER_COUNT variable was set to 20, so I was creating a stack of 20 curved planes and then storing them in the vertexBufferHandle array mVB. Each layer had a different height based on the starting height defined in the world editor, which is represented by the mHeight variable. The spacing between each layer was defined by mSpacing, which was also set in the world editor. So the loop goes: create layer 1 > stretch layer vertically by mHeight+mSpacing amount > create layer 2 > stretch layer vertically by mHeight+mSpacing+mSpacing amount > create layer 3 > stretch layer vertically by mHeight+mSpacing+mSpacing+mSpacing amount > etc.

The problem was that I was starting from the bottom of the stack, and generating layers up to the top of the stack. This is a problem because of the way that the mHeight variable actually affects the geometry. It doesn't actually set a new height for the layer(ie, new coordinates), it just scales the layer upward. I made this image to represent what the stacked layers would look like with the above code:

The numbers represent the order in which the layers were being created. If the red layer was created first, then each subsequent layer was actually being created and scaled through all preceding layers. After applying textures to each layer, this gave the whole volume the same type of look that happens on a model with inverted normals. This is the effect that I mean:

The polys seem to render facing inwards instead of outwards.

After realizing that this was the problem, it only took me a couple seconds to fix the loop to start from the top instead of the bottom. The code now looks like this:

Code: Select all

for( U32 i = LAYER_COUNT; i > 0; i-- ) { Point3F vertScale( 16.0f, 16.0f, mHeight -= mSpacing ); F32 zOffset = -( mCos( mSqrt( 1.0f ) ) + 0.01f ); mVB[i].set( GFX, smVertCount, GFXBufferTypeStatic ); GFXVolumeCloudVertex *pVert = mVB[i].lock(); if(!pVert) return; for ( U32 y = 0; y < smVertStride; y++ ) { ... } 
And a visual representation of the layer generation order now:

(Once again, the numbers in the image only represent the order in which the layer is being generated.)

This way, I'm actually generating the 20th layer at the top, then working my way down the stack.

That pretty much sums it up! With that out of the way, I'm now focusing on how I want to give the clouds variable shape and the best way to allow the user to control the shape through the world editor. I'll post some screenshots or a video of that in a few days to get some input from anyone who wants throw ideas around.

Re: Volumetric clouds in T3D

Janders
Posts: 45
Joined: Wed Sep 02, 2015 12:41 am
Just an idea but it would be nice clouds can cast subtle shadows, I mean, take the alpha channel of the clouds and do a projection to everything below.

Who is online

Users browsing this forum: No registered users and 1 guest