What is the process for accessing the inherent color of an STL file with the help of three.js?

I have a binary STL file that is colored. I can easily view the colors using an online mesh viewer, which you can find at .

Despite successfully loading and visualizing the STL file using my standard approach below, the intrinsic colors do not display correctly and seem to be very sensitive to changes in lighting.

Detector.addGetWebGLMessage();
scene = new THREE.Scene();
var aspect = window.innerWidth / window.innerHeight;
var d = 100;
camera = new THREE.OrthographicCamera( - d * aspect, d, d * aspect,- d, 1, 1000 );
camera.position.set( 0, 0, 200 );
camera.lookAt( scene.position ); 
scene.add( camera );
var light = new THREE.AmbientLight( 0x404040 ); // soft white light
scene.add( light );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.x = 0; 
directionalLight.position.y = 0; 
directionalLight.position.z = 1; 
directionalLight.position.normalize();
scene.add( directionalLight );
var loader = new THREE.STLLoader();
var material = new THREE.MeshPhongMaterial( { color: 0xAAAAAA, specular: 0x111111, shininess: 0 } );
// Colored binary STL

var stlfile = "myBinarySTLColoredFile.stl"
loader.load( stlfile, function ( geometry ) {
    var meshMaterial = material;
    if (geometry.hasColors) {
        meshMaterial = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors });
    }
mesh = new THREE.Mesh( geometry, meshMaterial );
scene.add( 
mesh.position.set(-100, -100, 0);
});

renderer = new THREE.WebGLRenderer({ alpha: true,  antialias: true } ); 
renderer.setSize(....);

var container = document.getElementById('`enter code here`flex2');

Question: How can I properly visualize the colors embedded within the STL file?

Thank you!

Answer №1

Choosing a color can be subjective as it depends on the lighting conditions. For more insights, check out this informative discussion on achieving realistic sunlight with Three.js.

Answer №2

After many attempts, I have finally achieved proper rendering of the colorful object.

Instead of using STLLoader, I switched to PLYLoader.

It seems that the three.js STL API does not handle colors effectively.

Answer №3

This question may be outdated, but for those who stumble upon it in the future, I disagree with the suggested solution of using PLYLoader over STLLoader. The STLLoader seems to handle embedded color in binary STL files just fine, as shown in this example.

It is possible that the issue the original poster faced was due to incorrectly using THREE.VertexColors in the material constructor:

THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors })
, instead of setting it to true following the guidance provided in the documentation/example.

Answer №4

To achieve the original color scheme without directional lighting, simply remove the directional light and increase the ambient light to its maximum brightness:

...
scene.add( camera );

var light = new THREE.AmbientLight( 0xFFFFFF ); // Full-bright white light
scene.add( light );

var loader = new THREE.STLLoader();
...

If this adjustment does not produce the desired outcome, kindly include screenshots showcasing both the current visual appearance and the intended look for further assistance.

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

Activate the next tab by clicking a button in ReactJS

I currently have a web page with 4 tabs, each containing different sets of data. Within the first tab, I have a button that should activate the next tab's content when clicked by the user. render(){ return ( <MuiThemeProvider> ...

Can a variable be passed to 'require' in NodeJS?

NOTE: To clarify, the code provided below is a simplified version focusing on the specific issue. I am aware that in Node.js, the global object `window` does not exist. However, in my case, this code is used with the `jsdom` library for offline rendering, ...

Is there a way to search for a specific item within a nested array?

I have 2 arrays within an array, each containing objects. How can I locate the object with the name "Sneijder"? const players = [ [ { id: 1, name: "Hagi", }, { id: 2, name: "Carlos", }, ], [ { id: 3 ...

Issue with toggleClass being triggered twice while resizing browser window

I am currently in the process of creating a responsive navigation menu. I have completed the initial setup but encountered an issue while resizing the browser window. It appears that the toggleClass function is being triggered multiple times when I resize ...

Error encountered when generating bower.json due to absence of version number

After generating the bower.json file, I noticed that the version number was not included in the information collected. The current content is as follows: { "Name": "conFusion", "Authors": [ "Aurora" ], ... } Although the version n ...

Reinitializing the database with Cypress (postgresql)

When populating the database, I intentionally do it partially for certain tests. However, after completing all tests, I would like to reset the database in order to avoid any complications that may arise during the next populate process. How can I efficien ...

Make an AJAX request to a PHP page without having to refresh the page every time the

I am looking to utilize AJAX to call a PHP page without having to reload PHP classes each time. For example, upon the first running of AJAX, the PHP page loads and sets data. However, on subsequent runs of AJAX, I want the PHP page to retrieve the same dat ...

The ng-bootstrap modal fails to appear when incorporating [formGroup]="FormName" or formControlName="elementName"

Utilizing ng-bootstrap to create a popup modal has been a challenge for me. When I import FormsModule and ReactiveFormsModule in src/app/modal-basic.module.ts, the code inside it looks like this: import { NgModule } from '@angular/core'; import { ...

Running res.render in an asynchronous manner within an express application

My goal is to make this router respond asynchronously: var express = require('express'), router = express.Router(); router.get('/', function(req, res, next) { res.render('contact', { titleShown: true, title: & ...

Update the second dropdown menu depending on the selection made in the first dropdown menu

While I know this question has been posed previously, I'm struggling to apply it to my current situation. In my MySQL database, I have a table named "subject" that includes columns for subject name and subject level. Here's an example: Subject ...

The functionality of the delete button in Datatables is only operational on the initial page, failing to

My datatable is giving me trouble. The delete button in the action column only works on the first page, but not on the other pages. Here is the code for my delete button: <table id="example" class="table table-striped table-bordered" cellspacing="0" wi ...

What is the best way to incorporate and organize the templateHyper folder within an established web project directory?

Can anyone offer advice on how to properly integrate and organize the 'templateHyper' (Bootstrap template) folder within my web project? I've been developing a project using C#, JavaScript, HTML, and Bootstrap's templateHyper. I'm ...

What is the best way to efficiently execute a function multiple times and ensure each call is completed before the next

Currently utilizing node.js I have a function that requires multiple parameters to be called inside a loop. It is crucial for the function to be called with the loop iterator as one of its parameters and waiting for it to finish processing before calling ...

Looping through data in AngularJS with duplicated entries

I'm having an issue with my AngularJS ng-repeat when trying to display JSON data. The records are duplicating, but only after reloading the page. Initially, everything works fine. Take a look at the JSON data below: [{ "EmployeeName": "Jishn ...

Is there a method for eliminating divs that have scrolled out of view on a never-ending webpage in order to prevent lag?

Looking for a solution to remove divs that have been scrolled past in Chrome in order to improve speed on an endlessly scrolling page. The sluggishness is making the page unusable. ...

Why is it beneficial to define a function as an immediately invoked function expression that returns another function?

The pattern of using IIFE (Immediately Invoked Function Expression) in three.js source code is quite common. Interestingly, removing the IIFE doesn't seem to have any impact on the functionality. It appears that using a named function instead of an an ...

Assigning a distinct identifier to every item in a dropdown list in MVC

I'm struggling to assign a unique ID to each element in a SelectList (using @Html.DropDownListFor). The current structure of the div in the view is as follows: <div id="Courses"> @Html.DropDownListFor(c => c.Courses, new SelectList(Model ...

The element is being implicitly assigned the 'any' type due to the inability to use a 'string' type expression to index the style type

I have encountered an issue that I am unsure how to solve: "Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ prop_first: string; prop_second: string; }'. No index si ...

What steps should I follow to utilize a JavaScript dependency following an NPM installation?

After successfully installing Fuse.js using npm, I am having trouble using the dependency in my JavaScript code. The website instructions suggest adding the following code to make it work: var books = [{ 'ISBN': 'A', 'title&ap ...

Tips for effectively storing and accessing a user's data in MongoDB

I’m a newcomer to Express and currently working on developing an application that can store and retrieve user notes in MongoDB based on their username. My current method involves creating a user object and saving their notes as an array. Below is the co ...