Groverburger's 3D Engine (g3d) v1.5.2 Release

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by Jasoco »

veethree wrote: Tue Sep 28, 2021 9:33 am There is. Conveniently i was just in the process of figuring this out for my game :)

fog.gif

Before i continue i want to get across that i'm not a huge shader guy, So there might be many things wrong with this. Maybe someone can check my work here.

You need to write a custom shader to achieve this. Here's what i came up with:

Code: Select all

    varying vec4 worldPosition;
    extern vec3 player;
    extern number fog_min;
    extern number fog_max;
    extern vec4 fog_color;
    vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc) {
        vec4 pixel = Texel(tex, tc);
        number d = distance(player, vec3(worldPosition[0], worldPosition[1], worldPosition[2]));
        number amount = smoothstep(fog_min, fog_max, d);
        return mix(pixel, fog_color, amount);
    }
`player` is the position of the player (or camera), `fog_min` & `fog_max` is the minumum & maximum distance of the fog and `fog_color` is the fogs color.

Then you use this, along with g3d's standard vertex shader to render your models. I found that for whatever reason, If i just passed my shader to the draw function of models it wasn't working, So i ended up just replacing g3d.shader in init.lua with my own shader.

EDIT: Changed the shader to have a min & max amount for the fog.
Sorry to double-post but can you help me figure out how to implement this into my game? I'm not sure where to put the code and how to load it so it works with g3d. This is exactly what I need right now as one of the many people now trying to make 3d games in Löve.
User avatar
veethree
Inner party member
Posts: 874
Joined: Sat Dec 10, 2011 7:18 pm

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by veethree »

Jasoco wrote: Fri Oct 22, 2021 11:52 pm
veethree wrote: Tue Sep 28, 2021 9:33 am There is. Conveniently i was just in the process of figuring this out for my game :)

fog.gif

Before i continue i want to get across that i'm not a huge shader guy, So there might be many things wrong with this. Maybe someone can check my work here.

You need to write a custom shader to achieve this. Here's what i came up with:

Code: Select all

    varying vec4 worldPosition;
    extern vec3 player;
    extern number fog_min;
    extern number fog_max;
    extern vec4 fog_color;
    vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc) {
        vec4 pixel = Texel(tex, tc);
        number d = distance(player, vec3(worldPosition[0], worldPosition[1], worldPosition[2]));
        number amount = smoothstep(fog_min, fog_max, d);
        return mix(pixel, fog_color, amount);
    }
`player` is the position of the player (or camera), `fog_min` & `fog_max` is the minumum & maximum distance of the fog and `fog_color` is the fogs color.

Then you use this, along with g3d's standard vertex shader to render your models. I found that for whatever reason, If i just passed my shader to the draw function of models it wasn't working, So i ended up just replacing g3d.shader in init.lua with my own shader.

EDIT: Changed the shader to have a min & max amount for the fog.
Sorry to double-post but can you help me figure out how to implement this into my game? I'm not sure where to put the code and how to load it so it works with g3d. This is exactly what I need right now as one of the many people now trying to make 3d games in Löve.
Sure, Keep in mind, I'm still not a shader guy :)

First, Define that shader somewhere, I'm sure you don't need help with that part.

The model:draw() function in g3d is supposed to be able to take a shader as an argument, But i didn't have much success with that. So i slightly altered the shader definition in init.lua in g3d.

Code: Select all

g3d.shader = love.graphics.newShader(g3d.shaderpath, your_shader_here)
After that just send it the appropriate externs and it should work. I believe the externs to be quite self explanatory, Except maybe the "player" vec3 which i should have definitely named "camera".
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by Jasoco »

veethree wrote: Sat Oct 23, 2021 7:59 am Sure, Keep in mind, I'm still not a shader guy :)

First, Define that shader somewhere, I'm sure you don't need help with that part.

The model:draw() function in g3d is supposed to be able to take a shader as an argument, But i didn't have much success with that. So i slightly altered the shader definition in init.lua in g3d.

Code: Select all

g3d.shader = love.graphics.newShader(g3d.shaderpath, your_shader_here)
After that just send it the appropriate externs and it should work. I believe the externs to be quite self explanatory, Except maybe the "player" vec3 which i should have definitely named "camera".
Oh wow it works! Thanks for the reply. I'll have to play around and see what I can do with shaders and g3d.

I wonder if there's also a way to use the normals of the triangles to shade them depending on the direction they're facing so I don't have to create a texture atlas with separate normal and darker versions of every texture.
User avatar
veethree
Inner party member
Posts: 874
Joined: Sat Dec 10, 2011 7:18 pm

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by veethree »

I wonder if there's also a way to use the normals of the triangles to shade them depending on the direction they're facing so I don't have to create a texture atlas with separate normal and darker versions of every texture.
For sure, g3ds vertex shader gives you access to everything you need. I managed to implement some basic lighting based on this tutorial
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by Jasoco »

veethree wrote: Sat Oct 23, 2021 4:07 pm For sure, g3ds vertex shader gives you access to everything you need. I managed to implement some basic lighting based on this tutorial
I saw Groverburger's Flamerunner does lighting too. I have a lot to learn and I'm really excited. I was getting tired of fumbling around with a slow raycaster. As neat as it looks now.
User avatar
groverburger
Prole
Posts: 49
Joined: Tue Oct 30, 2018 9:27 pm

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by groverburger »

veethree wrote: Sat Oct 23, 2021 7:59 am The model:draw() function in g3d is supposed to be able to take a shader as an argument, But i didn't have much success with that. So i slightly altered the shader definition in init.lua in g3d.
If you have any more details about what didn't work for you, I'd be interested to hear it. Here's how I intended shaders to work and how I use them in my projects. I'll demonstrate with a shader that renders vertex normals. Hopefully this will be useful to other people too.

I define a "normal.frag" file somewhere in my project which contains the following GLSL:

Code: Select all

varying vec3 vertexNormal;

vec4 effect(vec4 color, Image tex, vec2 texcoord, vec2 pixcoord) {
    return vec4(vertexNormal.xyz, 1);
}
Fragment shaders can use "varying" variables defined in the vertex shader, and I've defined a few common useful ones in g3d's vertex shader like "vertexNormal" here.

Then when I go to define a model that uses this shader, I do something like this:

Code: Select all

model = g3d.newModel("model.obj", "modelTexture.png")
modelShader = love.graphics.newShader(g3d.path .. "/g3d.vert", "normal.frag")
When we create the Love2D shader object for use with g3d, we have to give it both a vertex shader and a fragment shader argument. Here I'm using g3d's default vertex shader.

Now when I go to render the model in my love.draw function I do this:

Code: Select all

g3d.camera.updateProjectionMatrix(modelShader)
g3d.camera.updateViewMatrix(modelShader)
model:draw(modelShader)
The trick here is that this new shader won't know about g3d's camera, so we have to update its view and projection matrices every frame before it's drawn. That's what updateProjectionMatrix and updateViewMatrix are for, and why I pass in "modelShader" as an argument. This will be streamlined down to one function call in g3d 1.5.

I've made a few fragment and vertex shaders that people will probably find useful (like billboarding around arbitrary axes, etc.) over the time I've been working on g3d and my own personal projects. If people are interested I can bundle 'em up and share 'em.
User avatar
veethree
Inner party member
Posts: 874
Joined: Sat Dec 10, 2011 7:18 pm

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by veethree »

Oh. It’s been a while since I messed around with this, but I’m pretty sure I didn’t include the vertex shader. So I was just using it wrong. That’s my bad.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by Jasoco »

I definitely want to learn how to make and use shaders with this because I saw what you did with the lighting in that Flamerunner game and would love to have some sort of lighting in my game so I can have dark moody areas and not just have everything so perfectly lit throughout. Seems I'll have to do modifications to the shaders you made for that since it was written before g3d was made so it's more self-contained.

I'm also curious about the collision code that's included. What's capsule collision? I've been using Bump for years as it's simple to use but it doesn't do angled surfaces. Does the collision here do that?
User avatar
groverburger
Prole
Posts: 49
Joined: Tue Oct 30, 2018 9:27 pm

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by groverburger »

veethree wrote: Mon Oct 25, 2021 10:16 am Oh. It’s been a while since I messed around with this, but I’m pretty sure I didn’t include the vertex shader. So I was just using it wrong. That’s my bad.
No prob, happy to help.
Jasoco wrote: Mon Oct 25, 2021 11:24 am I definitely want to learn how to make and use shaders with this because I saw what you did with the lighting in that Flamerunner game and would love to have some sort of lighting in my game so I can have dark moody areas and not just have everything so perfectly lit throughout. Seems I'll have to do modifications to the shaders you made for that since it was written before g3d was made so it's more self-contained.

I'm also curious about the collision code that's included. What's capsule collision? I've been using Bump for years as it's simple to use but it doesn't do angled surfaces. Does the collision here do that?
Yep, the collision here works on any angled surfaces. The collision code tests basic geometric shapes like spheres, rays, capsules against the model's mesh, so anything in the model's mesh is fair game.

I'd recommend this article to learn about capsule collision, it's what I referenced when implementing it in g3d.

You can also take a look at this basic first person movement engine I whipped up back in January in this Github repo. It works pretty well but still has a little bit of jank to it (like going down slopes) that I've improved in my own personal projects since then. I'll get around to updating it soon.

Also to answer your question from a few days ago about whether models that are not in frame are still drawn and calculated -- yes, they are. However drawing models is super fast so unless you have a ton and start to feel your performance drop, it probably wouldn't be useful to try to implement any custom frustum culling. Something to note in g3d is that it's more efficient to have fewer complicated models than many simple models, so if you can combine them I would recommend doing so.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: Groverburger's 3D Engine (g3d) v1.4 Release

Post by 4aiman »

Can anyone explain why all tutorials on GLSL have this main() function but love2d works with effects?
Should I assume that the entire file will be included into main() ?
But main has no real return value, since it's void.

Seems like I'm googling it wrong, 'cause I can't find a simple example of ambient lighting + a point light source that I can plug in and get anything but pitch black screen.
Well, ok, the builtin one works just fine but I can't make things darker and illuminate a portion of a map with a light source.

I've looked at the suggested tutorial, but struggle to comprehend any of that, cause I can't plug it somewhere and toy with it till it'll make some sense.

Any help?
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests