Adding a line to a 3D texture presents an interesting challenge. Are you looking to create a decal on your model or actually paint onto the texture? There are alternative methods for applying decals that may be more suitable, but let's explore painting onto the texture. While I have experience with Unity in this aspect, WebGL presents its own set of limitations which I am not as familiar with. The process involves using a program similar to Substance Painter or ZBrush that allows you to paint directly onto 3D models. It is advisable to approach this non-destructively at runtime by working on a separate render texture and then combining it with the final model texture for rendering.
To achieve this, you must first render your model into texture space by outputting the UV coordinates of the model in your vertex shader.
Having recently focused on hlsl and Cg coding, my knowledge of glsl is somewhat outdated. Consider the following code snippet as pseudocode:
//Vertex Shader
in vec4 position;
in vec2 uv;
out vec4 fworldPos;
out vec2 fuv;
uniform mat4 transform;
void main()
{
gl_Position = vec4(uv.x*2.0-1.0, uv.y*2.0-1.0, 0.0, 1.0);
fuv = uv;
fworldPos = transform * position;
}
In the pixel shader, implement collision detection with your brush to determine if the world position falls within the brush area. In a recent project, we utilized raycasts for the brush to follow the model surface seamlessly. This method results in an initial rough brush stroke, requiring fine-tuning parameters such as falloff strength and radius to achieve desired effects - similar to settings found in traditional 3D painting software.
You can use various shapes like planes, boxes, spheres, and lines for painting. The key is to check if the world position aligns with the brush shape. Different shaders will be necessary depending on the type of brush being used.