Color Gradient using vertex colors on planeBufferGeometry?

goal : would like to avoid textures/canvas and directly use vertex colors to get a simple gradient effect on a plane

the code i use is

 const width = 1
const height =0.1
const widthSeg = 10
const heightSeg = 1

 let planeGeo = new THREE.PlaneBufferGeometry(width, height, widthSeg, heightSeg)


        const color = new THREE.Color()
        const colors = []
        const count = planeGeo.attributes.position.count
       

for (let index = 0; index < count; index++) {
            const t = index / count

            color.setHSL(t, 1.0, 0.5);

            colors.push(color.r, color.g,  color.b);

        }



planeGeo.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));

const planeMat = new THREE.MeshBasicMaterial({ color: 0xffffff, vertexColors: true })
const gradientPlane= new THREE.Mesh(planeGeo, planeMat)
scene.add(gradientPlane)

the result from this seems incorrect as the planes are using three points to form a face and the transition is messed up.

what is the correct way to parse through the vertices to get a good horizontal gradient ?

fiddle

here

2 Likes

using the geometry position of the vert to drive the hue , nice

in case of ring geometry what would the difference be since the position goes in a circle ?

try attributes.uv

2 Likes

Or bend the plane, depends on your goal: https://codepen.io/prisoner849/pen/oNpmOLp?editors=0010

2 Likes