So I was wondering if there is a small example or someone could possibly make one of how to add / delete / move vertices on a water plane so I can form water plane to exact shape of trench I carved in landscape to place the water? If I didn’t explain well enough, please do let me know & I will do my best to explain more in-depth. Here are 2 examples : https://jsfiddle.net/prisoner849/snyqgmke/ & https://youtu.be/ao_PHrIpoEo?t=161
I can’t make you an example now, but I can give you a few pointers in the right directions:
- Use pixel raycasting on the terrain to get world positions of points. Examples (with source), THREE.Raycaster
- Populate a THREE.BufferGeometry with the points as you work.
- Visualize the BufferGeometry using THREE.Points.
- Use pixel raycasting on the points to be able to pick them, and use this to create triangles that are represented as triples of point indices in
- Make a THREE.Mesh with your geometry and a suitable material. You may have to move the mesh slightly underground so that the edges go below the terrain surface. This offset can be set with
The procedure of drawing a river like this may be cumbersome. There are many other possible solutions that make the work faster, such as:
- Physical simulation (much harder to implement, and may require a lot of computing power).
- Very approximate physical simulation or analysis of the terrain, e.g. by dropping virtual particles (initially using raycasting from OrthographicCamera) that follow the landscape gradient and have some 2D momentum, and render them with additive blending and frame feedback to get a texture that holds information about the water “density” in one channel and water velocity in two other channels. Then analyse this texture to generate a geometry, or use it to blend in a water effect continuously in your terrain shader, after first using it to smooth out the terrain a bit where the water density is largest.
- Render a depth texture from above using an OrthographicCamera and use it to find depth positions of a fat line drawn from the same view. Then use a custom shader on the generated geometry.
I haven’t watched the video yet, though. So maybe I have missed something important… This is all original “research” from the top of my head.
Thank you @EliasHasle! I was actually thinking it would be better to modify the above fiddle example to work with a water plane instead of random generated vertices.
Yes, maybe you can adapt it, or use ideas from it. But rebuilding the entire geometry in every frame will not scale well. It should be possible to borrow code from the spline geometry class that is used, so that it reuses the same geometry and only updates the affected spline segments. And it should also be possible to extract the code for making only the end cap of the ExtrudedGeometry. And then project all vertices down on the terrain by querying and interpolating between the nearest terrain elevations (or, less efficient, cast one ray down from each vertex).
No, I don’t want to rebuild the landscape geometry, I want to reposition & add / delete vertices of the water plane itself. I apologize for not explaining well enough.
That video shows well what I mean.
Only one of my approaches above would involve altering the landscape.
Anyway, if you would scrap some of my suggestions above:
- Make a THREE.PlaneBufferGeometry with n length segments and 2 width segments (or the other way around, as I do not remember the order of dimensions), where n is relatively coarse.
- Make a way to select desired end points and standard width of the river, to get it positioned approximately in the scene.
- Render the geometry as THREE.Points.
- Implement drag&drop (using raycasting etc.) on the points.
- Use the vertex index modulo 3 to decide drag&drop behavior. Edge points are moved independently, and center points move also the corresponding edge points.
- Make a much denser (in river length direction) PlaneBufferGeometry for the water mesh.
- Calculate edge positions in the new geometry using cubic splines with the edge points of the first geometry as control points, and calculate center curve positions as averages of corresponding edge curve positions. Use cubic splines in 2D, and project down to terrain afterwards, using interpolation between nearest vertices or lookup in an orthographic depth texture.
- Make a mesh, offset it very slightly to underground and render!