Vertex Colors derived from Vector's z-coordinate in Three.js

Fernando Sanchez

Sample here: http://jsfiddle.net/c3shonu7/1/

As demonstrated in the code a BufferGeometry object is created by cloning an IcosahedronBufferGeometry's vertices. The intention is to color the subdivided icosahedron lighter at the poles and darker at the equator i.e. the lightness value of the vertex color goes from 1 to 0 based on the vertice's z-coordinate.

color.setHSL(0.1, 0.3, Math.abs(vertices[i + 2]) / geometry.parameters.radius);

However, each face is ending up "randomly" colored, what am I missing?

Berthur

If I understood your intention correctly, the only problem is that you are adding colours three times for each vertex:

for(var i = 0; i < vertices.length; i+= 1) {
  if(i % 3 == 0) {
    color.setHSL(0.1, 0.3, Math.abs(vertices[i + 2]) / geometry.parameters.radius);
  }
  colors.push(color.r,color.g,color.b);
}

Since the for loop is not iterating through vertices but through vertex coordinates, the colour should be pushed only when the if-statement is true. In short:

for(var i = 0; i < vertices.length; i+= 1) {
  if(i % 3 == 0) {
    color.setHSL(0.1, 0.3, Math.abs(vertices[i + 2]) / geometry.parameters.radius);
    colors.push(color.r,color.g,color.b);
  }
}

Did this fix your problem?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

THREE.js converting from 3D vector to 2D pixel position, interpreting z coordinate

How to dynamically change vertex colors in Three JS

three.js - passing vertex colors to fragment shader

How to prevent interpolation of vertex colors in THREE.js shader?

OpenGL: Must any vertex Z coordinate be negative or not?

How to give Three.js objects a z-index without setting their z-coordinate?

THREE.js rotate object vertex by vertex

Changing the z-coordinate of one of vertex in Polyline3d

How can I set Z up coordinate system in three.js?

How to convert x, y, z coordiate data to geometry (vertex, face) in three.js?

three.js Vector3 to 2D screen coordinate with rotated scene

Translate a vector from global space to local vector in three.js

colors for lines in three.js

How to convert coordinate from NDC to camera space in vertex shader?

Get mouse clicked point's 3D coordinate in three.js

Coordinate system issue in three.js

THREE.js generate UV coordinate

Three.js Coordinate system confusing

Three.js Calculating Vertex Normals

Read vertex positions as pixels in Three.js

Three.JS -- porting a custom vertex shader

Trying to assign vector of Base* from vector of Derived*

How can I make waves from the center of a Plane Geometry in Three.JS using the vertex shader?

Coordinate transformations from a randomly generated normal vector

How to calculate coordinate system from one vector?

Three.js How to control position of Vector3 value z using DAT.Gui

Three.js How to find World orientation vector of object's local Up vector?

How to change AxesHelper colors in three js?

Three Js : Different colors to different face of geometry