How can I leverage the new linewidth feature in Three.js r91 to make lines appear thicker or wider?

As I was working on my project, I encountered a situation where I needed to create wireframe-style lines with transparency inside to give the appearance of outlining shapes in a comic or cartoon style without actually creating solid objects.

These outlines required a thickness greater than the default linewidth provided by LineBasicMaterial. Since linewidth did not function properly with LineBasicMaterial, I was unsure how to proceed.

Prior to this issue, I had been using r90. Fortunately, the release of version r91 of three.js introduced "fat lines," which seemed like a perfect solution for increasing the line thickness. However, after upgrading to r91 and attempting to implement fat lines based on an example code, I faced difficulties getting the linewidth to work.

This is a snippet of my code before the upgrade to r91:

var Pavement = function() {
    this.mesh = new THREE.Object3D();
    this.mesh.name = "pavement";

    geomPavement = new THREE.BoxBufferGeometry(100, 25, 20000);
    var edgesPavement = new THREE.EdgesGeometry(geomPavement);
    var linePavement = new THREE.LineSegments(edgesPavement, new THREE.LineBasicMaterial({ color: Colors.black, linewidth: 10 }));
    matPavement = new THREE.MeshPhongMaterial({ color: Colors.black });
    PavementObject = new THREE.Mesh(geomPavement, matPavement);

    PavementObject.receiveShadow = true;
    PavementObject.castShadow = true;

    this.mesh.add(linePavement);
}

Although my code may seem cluttered, it basically involved creating objects without lines initially, then attempting to add line features later by extracting edges from BoxGeometry and utilizing them to make LineSegments with a LineBasicMaterial. The Mesh creation remained to allow potential non-transparency options and act as a color fill between the lines.

Post-upgrade to r91, I attempted the following simplification:

var Pavement = function() {
    this.mesh = new THREE.Object3D();
    this.mesh.name = "pavement";

    geomPavement = new THREE.BoxBufferGeometry(100, 25, 20000);
    var edgesPavement = new THREE.EdgesGeometry(geomPavement);
    var linePavement = new THREE.LineSegments2(edgesPavement, new THREE.LineMaterial({ color: Colors.black, linewidth: 10 }));
    matPavement = new THREE.MeshPhongMaterial({ color: Colors.black });
    PavementObject = new THREE.Mesh(geomPavement, matPavement);
    PavementObject.receiveShadow = true;
    PavementObject.castShadow = true;
    this.mesh.add(linePavement);
}

The change simply involved swapping LineBasicMaterial for LineMaterial and LineSegments for LineSegments2.

Seeking Assistance:

I am looking for guidance on how to increase the linewidth of my LineSegments, preferably using the new "fat lines" feature in r91. It appears that my struggle stems from a lack of understanding and documentation for the latest version, and any help would be greatly appreciated!

Additional Query:

In the example's code that I referenced, each new file linked individually via script tags. I followed suit in my project, but I wonder if this step is necessary. Does the functionality not reside in the main build file, or must it be included through script tags similar to OrbitControls, for instance?

Thank you.

Answer №1

At the moment, full integration of support for fat lines into the core library is still pending; however, there is an example available.

If you wish to render EdgesGeometry with fat lines, you can follow this pattern:

var geomPavement = new THREE.BoxBufferGeometry( 10, 2, 20 );

var edgesPavement = new THREE.EdgesGeometry( geomPavement );

var lineGeometry = new THREE.LineSegmentsGeometry().setPositions( edgesPavement.attributes.position.array );

var lineMaterial = new THREE.LineMaterial( { color: 0xffffff, linewidth: 10 } );

lineMaterial.resolution.set( window.innerWidth, window.innerHeight ); // it's crucial at this point

var linePavement = new THREE.LineSegments2( lineGeometry, lineMaterial );

scene.add( linePavement );

This code snippet is based on three.js r.91 version.

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

JavaScript libraries are not required when using the .append function to add HTML elements

Currently, I am attempting to utilize $.ajax in order to retrieve an html string from a php file and append it to the current html div. Oddly enough, when I use php echo, everything functions properly. However, when I attempt to load dynamically using $.lo ...

Tips for displaying my libraries' function arguments while hovering in VSCode

As the creator of a JavaScript library, I am intrigued by how some libraries display function parameters and definitions when hovering over them in VSCode. Despite my efforts to add jsdoc style comments around the class definitions, I have not been able to ...

VueJS: Preloading data prior to and following component initialization

VueJS is a new technology for me, and I'm currently working on a component that needs to retrieve data from an API before loading the corresponding route. The component should only load once the data is fetched. Additionally, after the component is cr ...

Is it possible to create a dynamic input form that appears within a textarea using jquery?

I've designed a unique form using a textarea to display messages from users, rather than the traditional ul list. Additionally, I have included an input form where users can type their messages. My goal is to make this input form visible on the textar ...

Step-by-step guide to generating the .bin and .js model files for the Lamborghini Gallardo (with detailed examples)

I'm interested in learning how to generate the model files for a Lamborghini Gallardo (in this case). This model is quite intricate and is divided into multiple files, such as: .htaccess GallardoNoUv_bin.bin GallardoNoUv_bin.js and w ...

Can a JavaScript function be used with images to operate across multiple elements?

How can I make a function work on multiple elements? let image = document.getElementById("opacityLink"); image.onmouseover = function() { image.style = "opacity:0.9"; }; image.onmouseout = function() { image.style = "opacity:1"; }; <div class=" ...

AngularJS property sorting: organize your list by name

I have a complicated structure that resembles: { 'street35':[ {'address154': 'name14'}, {'address244': 'name2'} ], 'street2':[ {'address15& ...

The camera steadily advances in WebVR Cardboard using three.js technology, never stopping its forward movement

I've been experimenting with trying to create a smooth, continuous forward movement for the camera in a three.js/WebVR project, specifically with only a cardboard viewer needed. After much trial and error, I've discovered that the usual camera m ...

Implementing a function to specify size limits

As a newcomer to the world of JavaScript, I am facing a particular challenge. My goal is to add a conditional statement in my code that will only execute on screen sizes larger than 768px. $(window).on('scroll', function () { if ($(window ...

New Trainee - Error: document has not been defined

Encountering an Error message while attempting to run Intern tests from the test files directory. The structure of the directory is as follows: test resources rest pickup.js cashManagement.js gitignore intern.js packages.js ...

What is the reason for memory allocation only occurring when geometry.elementsNeedUpdate is used?

When using a basic animate function like this: function animate() { geometry.elementsNeedUpdate = true; requestAnimationFrame( animate ); renderer.render( scene, camera ); } A memory allocation occurs even if no elements are changed. To address this, I ...

Vue-Router failing to redirect in a Single Page Application using Vue and Laravel

I have been attempting to implement vue-router in a Vue.js/Laravel project. My project consists of two simple pages: home page: https://i.stack.imgur.com/GmHOR.png about page (single-page scrolling): https://i.stack.imgur.com/CZDnZ.png The files used ...

Issue with JavaScript causing circles to form around a parent div

I am struggling to position a set of circles around a parent div in my code. I want 6 circles to form a circle around the parent div, but they are not lining up correctly. Can someone help me identify what I'm doing wrong? var div = 360 / 6; var ra ...

Using angularJS and Regular Expressions to eliminate the "+" symbols in an email address

I have been given the task of disallowing email addresses with a "+" sign from registering. As someone new to regex, my understanding is that it is better to focus on defining what constitutes a valid input rather than listing what should be excluded. I re ...

Create a square grid around a 3D object using THREE.js

I have been searching extensively but have yet to find a solution that fits my needs. With THREE.js, I am working with a 3D object, such as a human skull. My goal is to envelop its surface with a square grid that maintains uniform spacing between lines. Th ...

Turn off all pointer events on the overlaying div but keep scrolling enabled

Issue: I am facing a problem with two containers that have overflowing text content like this: https://i.sstatic.net/wsasV.png In this setup, the blue <div>s have overflow:hidden applied. Now, I am trying to synchronize the scrolling behavior of th ...

Why does the <div> element still have an offset even though its width and height are set to 100px and padding, margin, and border are set to 0px?

After styling my div element with specific CSS rules, I'm encountering an issue. The div has a set width and height of 100px along with padding, border, and margin all set to 0px. However, the elements are not being offset from the edges of the browse ...

What is the best way to store JSON data in the state of a ReactJS application?

I need assistance with a data format related issue. The current format of the data is being set by someone else in the backend of the application. When the data is empty, it looks like this: "opening_time":{"Mon":[["0"],["0"]], "Tue":[["0"],["0"]], "Wed" ...

Issue with inconsistent functionality of Socket.io

I've encountered an issue while working with multiple modules - specifically, socket.io is not functioning consistently... We have successfully implemented several 'routes' in Socket.io that work flawlessly every time! However, we are now ...

What is the reason behind only receiving one-dimensional arrays?

var $arr = { field_hide : '', field_values: '', field_triggers: {} } function setTrigger ($trigger, $values, $show, $arr) { $arr.field_hide += $show + ','; $arr.field_values += $values + ...