Skip to content

Instantly share code, notes, and snippets.

@bennlich
Last active May 10, 2019 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennlich/ac7962b8fda70ae65f8a4d031384eaa1 to your computer and use it in GitHub Desktop.
Save bennlich/ac7962b8fda70ae65f8a4d031384eaa1 to your computer and use it in GitHub Desktop.
test case showing threebox rendering a tube
mapboxgl.accessToken = 'pk.eyJ1IjoiYmVubmxpY2giLCJhIjoieUxHOHQyNCJ9.VLDDBTTdzeHKJvR5ABYaLA'
let tubeMesh;
let tubeMaterial;
let origin = [-122.2639955060099, 37.7994199370347, 1000];
let map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: origin,
zoom: 12,
pitch: 50,
bearing: 350,
});
map.on('style.load', function() {
console.log("adding layer");
map.addLayer({
id: 'tube_layer',
type: 'custom',
onAdd: async function(map, mbxContext){
window.threebox = new Threebox(map, mbxContext);
const VERT_SHADER=`
varying vec2 texcoord;
void main() {
texcoord = vec2(uv);
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}`;
const FRAG_SHADER=`
precision highp float;
varying vec2 texcoord;
uniform float time;
void main() {
float timeOffset = time * 0.1 / 200.0;
float repeatVal = mod(texcoord.x + timeOffset, 0.1) * 10.0;
gl_FragColor = vec4(0.0, repeatVal, 0.5, 1);
}`;
const curve = new THREE.CurvePath();
let coords = [
[[0, 0, 0], [2000, 2000, 0]],
[[2000, 2000, 0], [2000, 3000, 1000]]
];
for (let coordPair of coords) {
let [start, end] = coordPair;
curve.add(new THREE.LineCurve3(
new THREE.Vector3(start[0], start[1], start[2]),
new THREE.Vector3(end[0], end[1], end[2])
));
}
let geom = new THREE.TubeBufferGeometry(curve, 512, 500, 8, false);
tubeMaterial = new THREE.ShaderMaterial({
uniforms: {
time: { value: 0 },
},
vertexShader: VERT_SHADER,
fragmentShader: FRAG_SHADER,
side: THREE.DoubleSide,
});
tubeMesh = new THREE.Mesh(geom, tubeMaterial);
threebox.addAtCoordinate(tubeMesh, origin);
},
render: function(gl, matrix){
threebox.update(true);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment