How can I define boundaries for objects in Three.js?

How can I customize the border width and color of a polygon/polyhedron created with three.js? The example code below shows a figure, but I'm unsure how to set borders. Are there alternative methods for creating 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>

Answer №1

Perhaps the outline effect mentioned in this link may be of interest to you. It's a neat way to add borders around your objects:

For more examples and a chance to experiment, check out this related question. There's also a working fiddle for you to play around with.

This effect involves utilizing some WebGL native shader code. Here is what it looks like:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Animated jQuery carousel with a timer countdown feature

Currently, I am developing a jquery slider/carousel to display various promotions. I am seeking a method to indicate the time left until the next promotion appears. Similar to the flash promo on this website: Do you have any suggestions? ...

The attempt to install "expo-cli" with the command "npm install -g expo-cli" was unsuccessful

Encountered an issue while trying to install expo-cli for creating android applications using npm install -g expo-cli. NPM version: 7.19.1 Node version: v15.14.0 Upon running npm install -g expo-cli, the installation failed with the following error mess ...

Creating an Editor for Input Text Field in HTML: A Step-by-Step Guide

In the vast landscape of JS libraries that can achieve this function, like Trumbowyg and more. However, prior to my rails project displaying that slim version, I need to ensure JavaScript is properly escaped! Therefore, I need to create an editor using o ...

Ways to modify the color of the legend text in a HighChart graph

Click here https://i.sstatic.net/iuPs4.png I am attempting to modify the color of the series legend text from black to any color other than black: $(function () { $('#container').highcharts({ legend: { color: '#FF0000', ...

Tips for looping through a JSON object?

Similar Question: How to extract a specific value from a nested JSON data structure? I am looking to loop through a two-dimensional JSON object, whereas I already know how to do so for a one-dimensional JSON object. for (var key in data) { alert(data ...

Utilizing the WebSocket readyState to showcase the connection status on the application header

I am currently in the process of developing a chat widget with svelte. I aim to indicate whether the websocket is connected or not by utilizing the websocket.readyState property, which has the following values: 0- Connecting, 1- Open, 2- Closing, 3- Close ...

Gather information from users using Node.js and integrate it with Stripe

As I am new to Node.js, my goal is to utilize nodejs & Stripe to gather user data such as name, phone number, email, city, etc. This way, I can easily identify the user from the Stripe dashboard along with all their corresponding information. on the server ...

A custom Mongoose schema attribute configured with distinct values

I've been working on some code and here is what I have: var userSchema = new mongoose.Schema({ email: String, password: String, role: Something }); I'm aiming to set specific values ('admin', 'member', 'guest&apos ...

The ID of the jQuery droppable element cannot be found

Is there a way to retrieve the ID of a li element from a droppable function? Currently, when I try to do so, it returns undefined. Any suggestions on why this might be happening? The structure of each li element is as follows: <li class="ui-state-def ...

Using a function as an argument within an Angular directive

Looking for a solution to pass a promise-returning function into a directive? Here's what I'm currently doing: In the parent controller, I've created a callback: $scope.myCb = function(data) { console.log(data); } Directive Scope: sco ...

Using JS to switch between text and state in ToneJS

I am facing an issue with the text not displaying in the "play-stop-toggle" element, despite my limited experience with JS. My desired outcome includes: [SOLVED] The text in <div id="play-stop-toggle" onclick="togglePlay()">Play ...

Ways to determine if JavaScript array objects overlap

I am working with an array of objects that contain start and end range values. var ranges = [{ start: 1, end: 5 }] My goal is to add a new object to the array without any overlapping with the existing ranges, { start: 6, end: 10 } I need ...

What sets Java classes apart from JavaScript classes?

After working with C# and Java, I decided to dive into learning javascript/node.js. However, I am facing some challenges trying to understand what is wrong with this code. Here is the snippet from my main.js file: const MyClass = require("./MyClass"); le ...

What is causing my radio button event to activate when a separate radio button is selected?

I have implemented the code below to display "pers" and hide "bus," and vice versa. JQuery: $('input[name="perorbus"]').click(function() { if ($(this).attr('id') == 'bus') { $('#showbus&apo ...

Transform ISO-8859-1 encoding into UTF-8

Recently, I encountered an issue while sending a HTTP request using jQuery's ajax. The server, unfortunately, returns the response in ISO-8859-1 format while my page is set to UTF-8. This discrepancy causes some characters to become unreadable. How ...

Bring in d3 along with d3-force-attract

Recently, I have installed D3 along with d3-force-attract. npm install @types/d3 -S npm install -S d3-force-attract I am currently facing an issue with importing d3 force attract as it is not recognized as a typescript module, unlike d3 itself. The inco ...

Experiencing the issue of receiving `undefined` when trying to access res.query with a JSON query string using

Utilizing qs for constructing JSON-based query parameters for my REST API request: On the client side: import qs from "qs"; let query = { dateTime: { $gte: 1664557995000 } }; let q = qs.stringify(query); let url = "http://lo ...

How can I store items in a JavaScript array so that they are accessible on a different page?

I'm running into an issue with my PHP website. Each item has a "request a quote" button that, when clicked, is supposed to add the item name to an array using JavaScript. This array should then be added to the "message" field on the contact.php form. ...

Choosing various choices using AngularJS

My goal seems simple using vanilla JS, but with AngularJS, I'm looking for the best way to achieve it within the framework. I aim to update the selected options in a multiple select box without adding or removing any options. Below is a snippet of my ...

What is the best way to display a header element in front of an article element?

I'm struggling with making my header element sticky and appear in front of my article. Modifying the z-index hasn't given me the desired result so far. Is the z-index ineffective when dealing with floating elements? Or is there a workaround to m ...