"Enhance your Three.js experience with the MeshPhongMaterial and

I'm having trouble adding a texture to a MeshPhongMaterial in Three.js. When I try to do so, all I get is a black box with some lighting effects. I've been stuck on this issue for quite some time and can't seem to find a solution.

//Setting up the cube and lighting
function initializeCube() {
    var loader = new THREE.TextureLoader();
    var texture1 = loader.load("brick.jpg");
    cubeTexture = loader.load

    cube = new THREE.Mesh(new THREE.BoxGeometry(3, 3, 3), new 
    THREE.MeshPhongMaterial({color: 0xffffff, map: texture1}));
    scene.add(cube);

    cube.scale.set(0.5, 0.5, 0.5); 
    cube.position.x =  0;

    //Adding the first light
    cubeLight = new THREE.PointLight(0xFF0000 , 30, 1);
    cube.add(cubeLight);
    cubeLight.position.set(-0.5, -1, 0); 
    scene.add(cubeLight);
    cubeLight.power = 100;

    map.cubeLight = {
        light: cubeLight,
        added: true
    }

    //Adding the second light
    cubeLight2 = new THREE.PointLight(0x6600ff , 1, 1);
    cube.add(cubeLight2);
    cubeLight2.position.set(0, 1, 0); 
    scene.add(cubeLight2);
    cubeLight2.power = 100;

    map.cubeLight2 = {
        light: cubeLight2,
        added: true
    }
}

Answer №1

It appears that the lights are situated within the box. To move the lights outside of the box, consider adjusting the values to be greater. Additionally, if you place them outside the box, ensure that you also increase the distance value of your point lights. Failure to do so may result in the lights not illuminating anything beyond 1 unit.

You may also want to include ambient lighting in the scene to verify if the texture has been loaded successfully.

One more thing to note: it seems that the light has been added twice to the scene. Initially with scene.add(cubeLight) and then with cube.add(cubeLight), even though cube is already included in the scene.

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

What is the proper method for setting indices in Three.js BufferGeometry?

My current challenge involves setting per-face UV indices in a BufferGeometry. To achieve this, I am working with a Geometry where each face has a face.materialIndex that corresponds to a UV index. The goal is to convert this geometry into a BufferGeometr ...

Capturing HTML form values and storing them in my JavaScript data object

I have a JS object with preset data as shown below in the variable var json. I am trying to create a simple HTML web form where I want the user inputs to be added as a new data set within my initial JS object. Here is the initial JS object data. The submi ...

Is it necessary to create a Node endpoint for each item in the array?

My current tech stack includes ExpressJs, NodeJs, and AngularJs. Imagine I have an array containing various objects representing grocery store accounts and the amounts owed to them by the bank. [{ account: 1, amount: 2.33 }, { account: 2, amount: 5.99 ...

What is the method in svg.js for accessing the child nodes of an element?

Trying to work with an existing DOM SVG element in my HTML, I utilize the following code svgEle = SVG.adopt(svgDocument.getElementById('svg')); Is there a way to access child elements of svgEle without individually adopting them? elementById ...

Is it possible to send both props and a function within a single onClick event in React?

After spending hours searching for the right solution, I have yet to find it. Let me explain my situation clearly. I have an Image Carousel feature on my website that should open when a user clicks on an image. I have 5 images on the website, and when a us ...

Using variables in a Post request for a JSON object with JavaScript

I am attempting to retrieve an Xmlhttp.response from a website by using the following code snippet: var apiUrl = "http://somesite/someapicall/correctapiKey"; var xmlHttp = new XMLHttpRequest(); xmlHttp.open("POST", apiUrl, false); xmlHttp.setRequestHeader ...

Easily choose multiple items at once by searching with react-select

I have implemented react-select to showcase a searchable drop-down list of items where users can select multiple items. The list is lengthy, and users often find it tedious to multi-select many items that match the same filter string. This is because each ...

Tips for sorting through a multi-dimensional array of objects

I'm looking to filter this array based on the attributevalue. For example, if I search for blue color, I want all shirts in blue color to be displayed. And then if I further search for cotton fabric in blue color, I want all cotton products with blue ...

Is there a way to simulate a middle mouse click using Selenium and JavaScript?

I am looking to simulate a middle button mouse click on a text URL within a webpage using Selenium code in JavaScript so that it opens in a new tab. Here's an example: await driver.get("https://google.com"); // This is the xpath for the &a ...

Unit tests may not properly update the Angular Async pipe

Struggling with updating Observables using the async pipe in HTML during unit tests. I want to test not only the component itself but also ensure that child components are rendered correctly with the right Inputs. Here is a simple example where the issue ...

Trouble with using $.ajax to call a PHP function

Hey everyone, I hate to ask for help but I'm really stuck on this issue. I've tried everything and followed all the scenarios on this site, but I just can't seem to get it working. I even added alerts and echoes, but the function doesn' ...

Is it possible to link a Lua client with a Socket.io NodeJS server?

Currently, I have set up a Node JS server running socket.io that can successfully communicate with other clients using the socket.io-client package. My next step is to develop a Lua client that connects to my socket.io server and enables the sending and ...

display object issue with object display

I'm encountering a strange issue with my constructor objects in JS - when I try to view them, all I see is [object Object]. Can anyone help me figure out why this is happening? Check out the code snippet on MyCodeFiddle. ...

Encountering an unexpected token in the JSON file at the very start is causing an

An unexpected error has occurred, showing the following message:- Uncaught SyntaxError: Unexpected token u in JSON at position 0 The code causing the error is as follows:- import { useEffect, useState } from "react"; import { useNavigate, usePar ...

Tips for incorporating broadcasting into my Angular application?

console.log('js'); var myApp = angular.module('myApp', ["ngRoute"]); myApp.config(function($routeProvider) { $routeProvider.when('/', { templateUrlnp: 'views/partials/logIn.html', controller: 'LoginC ...

Converting all HTML classes into a PDF document for export

I am attempting to export an HTML element with the class .content into merged PDF pages. I am utilizing the pdfMake library, which can be found at this link: pdfMake Within the body of the document, there are two elements with the class .content, styled a ...

Using jQuery UI to dynamically add a widget based on a specific class, rather than relying on

Exploring the world of Apache Cordova and jQueryUI is a new adventure for me. I am currently experimenting with formatting the layout of my index.html using jQueryUI. As mentioned in this section of the jQueryUI tutorial, a widget can be added using the f ...

Identifying the Device Type within a Web-Based Program

In our Java application, we are looking to determine the type of device (mobile or desktop) that is making a request. Is there a way to achieve this? ...

Arrange information according to MLA names

Greetings! I would like to discuss the following Json Data: Is there a way to display only the MLA names list on the UI and determine which MLA has received the most votes? I need assistance in achieving this. Your help is greatly appreciated. Thank you ...

The table in Firefox is not displaying and hiding correctly. It only shows the header when the button is pressed, but not the entire row

There seems to be an issue with Firefox when it comes to displaying and hiding my table. When the button is pressed, only the header appears but not the row. What I'm trying to achieve is that when the user clicks the button, both the column header w ...