Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.Js draw linear gradient texture in webgl

Tags:

three.js

webgl

With canvas renderer, I am using a function that draws a linear gradient. I would like this to work with the webgl renderer as well, but it chokes on transparency. The code is below and here is a link to a fiddle which demonstrates what I mean.

function generateTexture() {

var size = 512;

// create canvas
canvas = document.createElement( 'canvas' );
canvas.width = size;
canvas.height = size;

// get context
var context = canvas.getContext( '2d' );

// draw gradient
context.rect( 0, 0, size, size );
var gradient = context.createLinearGradient( 0, 0, size, size );
gradient.addColorStop(0, '#99ddff'); // light blue 
gradient.addColorStop(1, 'transparent');
context.fillStyle = gradient;
context.fill();

return canvas;

}

like image 493
g3k0 Avatar asked Nov 15 '13 02:11

g3k0


1 Answers

For WebGLRenderer, you need to set material.transparent = true.

var material = new THREE.MeshBasicMaterial( { map: texture, transparent: true } );

Updated fiddle: http://jsfiddle.net/FtML5/3/

three.js r.62

like image 176
WestLangley Avatar answered Nov 16 '22 02:11

WestLangley