Obtaining a vibrant solid color surface rather than a textured one in Three.js

When attempting to add texture to my object, I am encountering an issue where only the color of the texture is being applied, rather than the actual texture itself. For example, if I try to apply a wood texture image, the object just appears brown instead of showing the grain of the wood.

The basic code I am using is as follows:

var loader = new THREE.STLLoader();
var newMaterial = new THREE.MeshPhongMaterial( { color: colorValue, specular: 0xffffff, shininess: 100  } );

loader.load( './models/stl/binary/'+top, function ( newGeometry ) {

newMesh = new THREE.Mesh( newGeometry, newMaterial );

newMesh.position.set(  0,0.6, 0  );
newMesh.rotation.set(0,0.8,0);
newMesh.scale.set( 0.04, 0.04, 0.04 );

scene.add( newMesh );

I have attempted the following methods to apply texture:

var newMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 100, map: THREE.ImageUtils.loadTexture( "textures/table/lighttexture.jpg" ),
    side: THREE.DoubleSide } );
    newMaterial.map.minFilter = THREE.LinearFilter;

However, the result remains the same with only the color of the texture being displayed.

var texture = new THREE.TextureLoader().load( "textures/water.jpg" );
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 4, 4 );
                var newMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 100, map: texture } );

An error occurs when trying the above method:

Uncaught TypeError: Cannot set property 'wrapS' of undefined

If anyone can provide guidance on how to successfully apply the original texture to my object, it would be greatly appreciated.

EDIT

It has been noted that the texture works correctly when applied to a sphere geometry, as shown in the code snippet below.

            var sphereGeometry = new THREE.SphereGeometry(1, 10, 10);
            var mat = new THREE.MeshPhongMaterial();
            mat.map = new THREE.ImageUtils.loadTexture(
            "textures/table/lighttexture.jpg");
            mat.transparent = true;
            mat.side = THREE.DoubleSide;
            mat.depthWrite = false;
            mat.color = new THREE.Color(0xff0000);
            var sphere = new THREE.Mesh(sphereGeometry, mat);
            scene.add(sphere);

Answer №1

It seems that the issue lies with the UVs in your geometry. Start by attempting to apply a texture to a geometry created using Three.js:

geometry = new THREE.PlaneGeometry(100, 100);

If the texture displays correctly on this geometry, it indicates that the STL geometry you imported may not have UVs set or they could be incorrect (e.g., all zeros).

To access the UVs, use:

geometry.faceVertexUvs

for Geometry,

and

geometry.getAttribute("uv")

if you are working with BufferGeometry.

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

Calculating the Mean of Numbers in JavaScript/AngularJS

Could you help me calculate the average throughput_kbps for TCP protocol in each result array from a JSON response? I am using JavaScript/AngularJS. You can refer to this JSON for more information. Thank you in advance! ...

Challenges with loading content on the initial page load using the HTML5

Upon page load, I wanted to save the initial page information so that I could access it when navigating back from subsequent pages. (Initial Page -> Page2 -> Initial Page) After some trial and error, I ended up storing a global variable named first ...

Capturing link titles for control navigation in Nivo Slider

Essentially, in the nivoslider plugin, the controlnav section displays numbers based on the "rel" attribute in the code. I am wondering if there is a way to modify this so that it retrieves the "title" or "alt" from the link added to the slideshow. This wo ...

transmit an array variable using an ajax post call

I am currently facing an issue while attempting to process a form using an ajax request. The problem arises when dealing with an array variable within the form. Upon trying to serialize it for the ajax request, the result returned is as follows: email_id% ...

What is the best way to trigger a JavaScript function to execute as soon as the innerHtml has been modified via Ajax

Utilizing Ajax to dynamically update the content of a div upon clicking a link is my current project. I am seeking guidance on how to call a JavaScript function once the file has been retrieved from the server and the div's content has been replaced. ...

Activate Gulp watch to run task just one time

I have integrated gulp-livereload to automatically refresh pages after modifying .js files. Below is the code snippet: const gulp = require('gulp'); const livereload = require('gulp-livereload'); gulp.task('jsLiveReload', ...

Step-by-step guide on setting up a click counter that securely stores data in a text file, even after the

Can anyone help me make this link actually function as intended? Right now it only runs the JavaScript code, but I would like it to run the code and redirect to a webpage. Additionally, I need the data to be saved to a text file. Please provide assistanc ...

Modifying the data of an HTML form using Javascript, encrypting the information, and transferring it to PHP

I am new to PHP and have a simple code management question. I would like to create an input box in HTML where someone can enter their name, and with the use of Javascript, generate a link with an encoded version of the name preceded by a website string. Wh ...

Sending specific object model upon button press/activation of event

I currently have a button that passes the selected value to the removeContract() function. <td class="col-md-1" colspan="1" style="text-align: center; vertical-align: middle;"> <button class="btn btn-primary" data-ng-click="removeContract(ctr ...

Exploring Angular Scope within a Typescript-based javascript function

My current setup involves Typescript with Angular combined with Breezejs. class CounterController { count: number = 0; static $inject = ['$scope']; constructor($scope) { $scope.vm = this; } setCount14(): void { ...

Implementing JWT Authentication upon page load

Currently, I am facing a challenge with JWT authentication on my node website using passport. I find myself struggling to fully grasp the concepts involved. Let's imagine I am an authenticated user with my token stored in local storage. Now, suppose ...

Anchor tag in AngularJS not responding to ng-disabled directive

I have been successfully using ng-disabled for input and buttons, but I have run into an issue with anchor tags. How can I make it work for anchor tags as well? HTML code <a ng-disabled="addInviteesDisabled()">Add</a> JS code $scope.addIn ...

What is the significance of :: in AngularJS?

When reviewing certain Angular JS files, I often come across the following syntax. What is the significance of a double colon "::" in this context? <span>{{::x}}</span> <div>{{::y.z()}}</div> ...

Steps for configuring mode as 'open' when generating a shadow element with vue-custom-element

Here is the method I used to generate a shadow component Vue.customElement('my-element', MyElement, { shadow: true, shadowCss: mystyles, }); ...

Employ the react() function from the Direct Messaging tool

I am trying to implement the .react() method in a DM. However, the post's reaction is being sent by a bot. I need to enable users to send reactions instead. var server = bot.servers.get(SERVER_ID); if(server && server.channels.get(channel_ID)){ ...

Child component in Angular fails to detect input changes

Hey there! I'm currently utilizing parent-child communication in my Angular project. In the parent component, I have an array that represents graph data. If you'd like to check out a demo of what I'm working on, feel free to click here. The ...

Failed to load TypeScript file or similar issue

Whenever I attempt to generate a TypeScript file from a partial view (MVC .NET) that is loaded through a rest call and then appended to a div element, I encounter an error in my console. The error message reads: Uncaught ReferenceError: xyz is not defined ...

Using jQuery to deactivate buttons upon submission or clicking within forms and hyperlinks

Within my Rails application, there are buttons that send data to the server. Some of these buttons are part of a form while others are standalone. I am seeking a solution to implement my jQuery code, which disables the buttons upon click, for both scenario ...

What is the method of accessing a function external to an extended function?

I came across this code snippet (function ($) { $.fn.CustomFunction = function (containerId) { let container = document.getElementById(containerId); let allFields = container.querySelectorAll("[is-required]"); ...

Encountering difficulty accessing the router during testing in Next.js

Hello, I'm currently attempting to test a scenario where when a button is pressed, it should redirect to '/'. Normally this works fine, but during testing it fails and shows the following error: Cannot read properties of null (reading ' ...