Somerussian
Somerussian

Reputation: 391

Three.js - how to set borders to objects?

I have polygon/polyhedron created with three.js? Is it possible to set border width and color for it? Here is my example code, you may see a figure in it. How to set borders? May be there are other ways to create polygons?

<html> 
<head> 
	<title>Моё</title> 
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<style>
		body { margin: 0; }
		canvas { width: 100%; height: 100% }
	</style> 
</head> 
<body>
	<script src="../build/three.min.js"></script>
	<script src="http://stemkoski.github.io/Three.js/js/OrbitControls.js"></script>
	<script>
	var phi = 0;
	var scene    = new THREE.Scene();
	var camera   = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 3000 );
	//var camera = new THREE.OrthographicCamera( window.innerWidth / - 200, window.innerWidth / 200, window.innerHeight / 200, window.innerHeight / - 200, 0.1, 1000 );
	var renderer = new THREE.WebGLRenderer();
	renderer.setSize( window.innerWidth, window.innerHeight );
	document.body.appendChild( renderer.domElement );
	
	
	var polyhedronShape, polyhedronPts = [], cube, mesh;
	var container, scene, camera, renderer, controls, stats;
	// CONTROLS
	controls = new THREE.OrbitControls( camera, renderer.domElement );
	
	polyhedronPts.push( new THREE.Vector2 ( 0, 60) );
	polyhedronPts.push( new THREE.Vector2 ( 60, 60) );
	polyhedronPts.push( new THREE.Vector2 ( 60, 0) );
	
	polyhedronShape = new THREE.Shape( polyhedronPts );

	var extrudeSettings = {amount: 20}; // bevelSegments: 2, steps: 2 , bevelSegments: 5, bevelSize: 8, bevelThickness:5
	
	var geometry = new THREE.ExtrudeGeometry( polyhedronShape, extrudeSettings );

	mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x00cc00 } ) );
	//mesh.position.set( 0, 0, 0 );
	//mesh.rotation.set( 0, 0, 100 );
	//mesh.scale.set( 1, 1, 1 );
	scene.add( mesh );
	

	camera.position.z = 111;
	//cube.position.x += 2;
	var cam_z = 0.1, cam_max = 9, cam_min = 2;
	function render() {
		requestAnimationFrame( render );
		controls.update();
		if (Math.random() > 0.2) {
		renderer.render( scene, camera );
			//camera.position.z += cam_z;
		}
	}
	render();
	</script>
</body>
</html>

Upvotes: 0

Views: 7080

Answers (1)

Wilt
Wilt

Reputation: 44383

I am not sure if this outline effect is what you are looking for, but it is a nice way to put a border around your objects:

Here a similar question where you find more examples and a working fiddle to fool around with.

It uses some WebGL native shader effect if I am not mistaken it is this part:

vertex_shader: [
    "uniform float offset;",
    "void main() {",
        "vec4 pos = modelViewMatrix * vec4( position + normal * offset, 1.0 );",
        "gl_Position = projectionMatrix * pos;",
    "}"
].join("\n"),

Upvotes: 5

Related Questions