<p>a quick working wrapper for OpenGL from R:</p>
<pre><code>require(rgl)
tr.points &lt;- matrix(c(1,0,0, cos(60/180*pi),sin(60/180*pi),0, 0,0,0),
                    ncol=3, byrow=TRUE)
triangles3d(tr.points, col=c('red', 'green', 'blue'),
            lit=FALSE, smooth=TRUE, textype='rgb',
            lwd=1.0,
            depth_mask=TRUE, depth_test='always')
rgl.viewpoint(theta=0, phi=0,
              fov=0, zoom=1,
              interactive=FALSE)
</code></pre>

CC BY-SA 4.0
2020-10-21T22:12:09.440Z
2020-10-21T22:12:09.440Z
ivan866
<p>Most 2D graphics programs are able to do linear gradients with arbitrary orientations. If you don't mind a little work, it is possible to set this up to imitate the 2D linear interpolation across a triangle.</p>
<p>Set up two layers:</p>
<ul>
<li>Pick two of the colors, say red and green, and set up a linear gradient between those two colors along the red-green edge.</li>
<li>In a second layer, set up a linear gradient between blue and transparent-blue (i.e. the same color with alpha = 0). Orient it perpendicular to the red-green edge.</li>
</ul>

CC BY-SA 4.0
2020-10-20T20:10:42.080Z
you've forgot to mention that i also need to account for the length of the edges in cases when the triangle is not regular
ivan866
CC BY-SA 4.0
2020-10-20T20:15:40.747Z
Not sure what you mean. If you put the linear gradient vertices at the right spots everything should be taken care of
Nathan Reed
CC BY-SA 4.0
2020-10-20T17:56:50.360Z
2020-10-20T17:56:50.360Z
Nathan Reed
<p>I need to draw some shapes filled with linear gradients starting from every point and blending smoothly between points, like an OpenGL standard shading (I guess it is called <code>GL_LINEAR</code> or <code>VERTEX_COLOR</code>), i.e. GIMP / Illustrator / Krita / Paint3D, etc. Something fast and simple.<br />
required result:<br />
<a href="https://i.sstatic.net/O6X3T.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/O6X3T.png" alt="enter image description here" /></a></p>

<opengl><texture><geometry><pixel-shader><gradient>
Is there a tool capable of drawing a triangular linear gradient fill?
ivan866
# Is there a tool capable of drawing a triangular linear gradient fill?

(Asked by: username_0 on 2020-10-20)

I need to draw some shapes filled with linear gradients starting from every point and blending smoothly between points, like an OpenGL standard shading (I guess it is called `GL_LINEAR` or `VERTEX_COLOR`), i.e. GIMP / Illustrator / Krita / Paint3D, etc. Something fast and simple.  
required result:  
[![enter image description here](https://i.sstatic.net/O6X3T.png)](https://i.sstatic.net/O6X3T.png)

**Tags:** opengl, texture, geometry, pixel-shader, gradient


---

## 2 answers

### Answer by username_0 on 2020-10-21. Score: 0 | Accepted answer

a quick working wrapper for OpenGL from R:

```
require(rgl)
tr.points <- matrix(c(1,0,0, cos(60/180*pi),sin(60/180*pi),0, 0,0,0),
                    ncol=3, byrow=TRUE)
triangles3d(tr.points, col=c('red', 'green', 'blue'),
            lit=FALSE, smooth=TRUE, textype='rgb',
            lwd=1.0,
            depth_mask=TRUE, depth_test='always')
rgl.viewpoint(theta=0, phi=0,
              fov=0, zoom=1,
              interactive=FALSE)

```


---

### Answer by username_1 on 2020-10-20. Score: 1

Most 2D graphics programs are able to do linear gradients with arbitrary orientations. If you don't mind a little work, it is possible to set this up to imitate the 2D linear interpolation across a triangle.

Set up two layers:

* Pick two of the colors, say red and green, and set up a linear gradient between those two colors along the red-green edge.
* In a second layer, set up a linear gradient between blue and transparent-blue (i.e. the same color with alpha = 0). Orient it perpendicular to the red-green edge.

> **Comments:**
> **username_0**: you've forgot to mention that i also need to account for the length of the edges in cases when the triangle is not regular  
> **username_1**: Not sure what you mean. If you put the linear gradient vertices at the right spots everything should be taken care of  

---

