Upstreaming improvements from three.js.