Implementing Vector Addition Via `blendFunc` In WebGL?

31 views Asked by At

I want to implement the N-body problem in WebGL. My plan is to use two textures, one holding the position and velocity data of the particles, and at a given point in time, I want to draw a square for each particle to the second texture, representing the particle's force exerted on its surroundings. After drawing all the squares, this texture represents the combined force field and I run a second shader on the data texture where each particle queries the force field for the force exerted on its exact position by the other particles and then computes its new position and velocity.

So I'd use two channels for X and Y since this texture is basically representing a vector field. In the beginning every point is set to [0.5, 0.5], which corresponds to zero force. But then my problem is, can I use any blendFunc which would help me to perform a simple vector addition when drawing a square? E.g. when the existing value is [0.2, 0.6], which corresponds to the force [-0.3, 0.1] and the corresponding fragment value in the square is [0.6, 0.4], I want the resulting value in the texture to be [0.3, 0.5]. I don't think this is directly possible with blendFunc, but maybe there is a smarter way of mapping the force values to color values so they play nicer with blend arithmetic?

0

There are 0 answers