Invoking a shader once per mesh
Asked Answered
V

3

0

I want to use a vertex shader to add some static noise to a mesh for procedurally generated terrain. It seems wasteful for this function to be called every time the mesh is rendered, though, since it doesn't change over time. Is there a way to create a vertex shader and call it once when I create the mesh? I could do it in the CPU, but I want to take advantage of parallel processing on the GPU.

Total beginner. Thanks in advance for the help.

Varner answered 31/8, 2021 at 1:23 Comment(0)
P
0

Well, shaders are - in a sense - descriptors for the renderer of how a surface should look and interact with the scene and it's lights. Unless you have a game that renders a single frame only I don't see how you could really run the shader just once.

However using a vertex shader for terrain mesh displacement is probably not the right way to go anyways since your terrain likely requires collisions too. Anything you do in the vertex shader wouldn't be reflected in the generated collision meshes anyways.

You'll likely want to look into array meshes and modifying mesh instances via the SurfaceTool and MeshDataTool instead. etc. Yes this would run on the CPU but it should be somewhat parallelizable via multi-threading and designing your terrain generation to be chunk based. This would allow for a mode more dynamic chunk generation and world shifting for bigger open world design too should it ever become necessary.

Prayer answered 31/8, 2021 at 6:6 Comment(0)
V
0

I hadn't thought about collisions. That makes sense. Thanks!

Varner answered 31/8, 2021 at 14:5 Comment(0)
A
0

You can use shader-based vertex displacement for rendering and use the CPU to configure a HeightMapShape within a StaticBody node for collision. Last time I checked, this is what Zylann's heightmap plugin does.

Anemochore answered 31/8, 2021 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.