Multi-stop Gradient in THREE.js for Lines

点点圈 提交于 2019-12-13 19:18:04

问题


This shows an example of how to create a two-color gradient along a THREE.js line:

Color Gradient for Three.js line

How do you implement a multi-stop color gradient along a line? It looks like attributes will only interpolate across two values (I tried passing in three, it only worked with the first two values).


回答1:


This is the do-it-yourself color gradient approach:

Create a line geometry and add some vertices:

var lineGeometry = new THREE.Geometry();
lineGeometry.vertices.push( 
    new THREE.Vector3( -10, 0, 0 ), 
    new THREE.Vector3( -10, 10, 0 ) 
);

Use some helper functions for convenience:

var steps = 0.2;
var phase = 1.5;
var coloredLine = getColoredBufferLine( steps, phase, lineGeometry );
scene.add( coloredLine );

jsfiddle: http://jsfiddle.net/jfd58hbm/


Explaination:

getColoredBufferLine creates a new buffer geometry from the geometry, which is just for convenience. It then iterates the vertices, assigning each vertex a color. The color is calculated using another helper: color.set ( makeColorGradient( i, frequency, phase ) );.

Where basically frequency defines how many color changes you want the line to receive.

And phase is a shift of the color spectrum (= what color does the line start with).

I have added a dat.gui so you can play around with the parameters. If you want to change the color repetition or type, you can alter the makeColorGradient function to your needs. This page offers some good explaination how gradients are generated and where my example is based upon: http://krazydad.com/tutorials/makecolors.php.



来源:https://stackoverflow.com/questions/36830779/multi-stop-gradient-in-three-js-for-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!