0

I've been trying to figure out some things re .lerpColors() - specifically if it's possible to transition between more than just 2 colors.

I quickly put together a little app - the code is below and it works 100%, creating a nice smooth transition between WHITE and BLUE.
I also put this exact same code in a fiddle - but for some reason it's NOT working there, even though it's the same code: https://jsfiddle.net/gilomer88/6cw8az72/16/

Either way, what I'd like to know is if it's possible to transition from say Blue to White to Red. Or even transition between 4 colors - as opposed to only two colors, the way I have it right now.

Here's my code:

function makeCube() {
  
   var cubeMaterial = new THREE.MeshPhongMaterial({ 
        flatShading: false,
        shininess: 80,
        vertexColors: true
      });

   var cubeGeometry = new THREE.BoxBufferGeometry(10, 10, 10, 30, 30);
   var cubeVertexPositionsArray = cubeGeometry.attributes.position;
   console.log("cubeVertexPositionsArray.count = ", cubeVertexPositionsArray.count);
                
   const vertex = new THREE.Vector3();

   cubeGeometry = cubeGeometry.toNonIndexed(); 

   // "BoundingBox" business:
   cubeGeometry.computeBoundingBox();
   const aabb = cubeGeometry.boundingBox;
   const f = aabb.max.z - aabb.min.z;
                
   cubeVertexPositionsArray = cubeGeometry.attributes.position;

   let c1 = new THREE.Color("white");
   let c2 = new THREE.Color("red");
  
   // Make the Colors Array:
   var cubeColorsArray = [];
   let finalColor = new THREE.Color();


   for(let i = 0; i < cubeVertexPositionsArray.count; i++) {
      vertex.fromBufferAttribute( cubeVertexPositionsArray, i );
      let alphaValue = ( vertex.z - aabb.min.z) / f ;
      finalColor.lerpColors( c1, c2, alphaValue );
      /* finalColor = whiteColor.lerpHSL( redColor, alphaValue ) */;
      // cubeMaterial.color.copy(whiteColor).lerpHSL(redColor, alphaValue);
      cubeColorsArray.push(finalColor.r, finalColor.g, finalColor.b);  
   }

   cubeGeometry.setAttribute("color", new THREE.Float32BufferAttribute(cubeColorsArray, 3));     
   cubeMaterial.vertexColors = true;
   cubeMaterial.flatShading = false;

   let cubeMesh = new THREE.Mesh(cubeGeometry, cubeMaterial);
  
   scene.add(cubeMesh);
   cubeMesh.position.set(0, 0, 0);

}
Sirab33
  • 1,247
  • 3
  • 12
  • 27
  • Have a look at this SO answer (3rd example): https://stackoverflow.com/a/52615186/4045502 – prisoner849 Feb 17 '21 at 15:27
  • BTW: You don't see an effect in your fiddle since the camera is positioned at the wrong place. Try it with: https://jsfiddle.net/ug7zh493/ – Mugen87 Feb 17 '21 at 15:52
  • @Sirab33 your JSFiddle link seems to be broken. I'm getting "That page doesn't exist." – M - Feb 18 '21 at 00:26
  • @prisoner849 I think the 3rd. example on the page you linked me to will work nicely. I haven't tried it yet, but I think I get what going on there, how it's approaching the solution. Did you wanna re-post your comment as an answer so I can officially give you credit for the right answer? – Sirab33 Feb 18 '21 at 01:47

0 Answers0