Rendering models with MeshBasicMaterial works successfully, however, utilizing MeshPhongMaterial does not yield

I am currently developing a webpage using three.js. Here is the code I have implemented successfully:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 7;

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

var pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.x = 20;
pointLight.position.y = 20;
pointLight.position.z = 20;

scene.add(pointLight);
var material = new THREE.MeshBasicMaterial( { color: 0xFFFFFF } );

var object3d, loader = new THREE.OBJLoader();
loader.load("https://dl.dropbox.com/s/kasjdw78lx3bkk0/weirdshape.obj?dl=0", function (obj) {
  obj.material = material;
  obj.traverse(function(child) { child.material = material; });
  scene.add(obj);
  object3d = obj;
  render();
});

function render() {
  object3d.rotation.y += 0.01;
  object3d.rotation.x += 0.01;
  object3d.rotation.z += 0.01;
  requestAnimationFrame( render );
    renderer.render( scene, camera );
}

Everything functions correctly with the current code. However, when attempting to switch from MeshBasicMaterial to MeshPhongMaterial, the screen remains black despite proper lighting setup. How can this issue be resolved?

Answer №1

Check the source file with the extension .obj. It appears that your model is missing vertex normals, which are required by MeshPhongMaterial.

To address this issue, you can have three.js calculate the vertex normals for you. In your loader callback function, include the following code snippet:

if ( child.geometry ) child.geometry.computeVertexNormals();

Keep in mind: Since the loader is returning a BufferGeometry, using this workaround is appropriate. However, if your loader were to return a Geometry instead, you may need to first call geometry.computeFaceNormals() if those are also missing.

Using three.js version r.73

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

The Phong material in ThreeJS is taking on a deep, mysterious hue

When I load a BM3 file, it contains definitions of geometry and material information in the following format: { type: "Phong", opacity: 1, name: "", diffuseMap: 0, color: [ 0.254902, 0.2, 0.129412, ], transpare ...

Mapping distinct JSON keys to button components using Javascript/React

I am dealing with the JSON data below: [ {"location":"2034","type":"Residential","price":400000,"address":"123 Fake Street","suburb":"Maroubra","historical_DAs&q ...

Can someone help me figure out where I'm going wrong with the JS ternary conditional operator

Looking to improve my JavaScript logic skills as a beginner. Appreciate any tips or feedback on the function and not just this specific question. I'm curious why the code outputs --> [3,5] function divisors(integer) { let divisors = [] for (le ...

What is the proper method for setting initial values for scope upon loading the view using AngularJS and ngInit?

For the last few weeks, I've been immersing myself in AngularJS, studying large-scale applications to gain insights into real-world development practices. One common pattern I observed is the use of ng-init="init()" when loading a view - essentially c ...

creating a randomized location within an HTML element using JavaScript

Trying to figure out how to randomly generate a position within an HTML div. I've come up with a JavaScript function that looks like this: function randomInRange(min, max) { return(Math.floor((Math.random() * (max - min) + 1) + min)); } My ...

Retrieving data from notifications without having to interact with them (using Firebase Cloud Messaging and React Native)

Currently, I am able to handle messages whether the app is open, minimized, or closed. However, how can I process a message if a user logs into the application without receiving a notification? useEffect(() => { messaging().setBackgroundMessageHa ...

Unveiling the secret: Retrieving selected items from the Fluent UI DetailsList component

I am looking to utilize the DetailsList component https://developer.microsoft.com/en-us/fluentui#/controls/web/detailslist and need to select multiple items. However, it appears that the component does not have a built-in prop to retrieve all selected item ...

JQuery is having trouble with playing multiple sound files or causing delays with events

I've been working on a project that involves playing sounds for each letter in a list of text. However, I'm encountering an issue where only the last sound file is played instead of looping through every element. I've attempted to delay the ...

jquery surprise confirmation/prompt substitution

I've been working on creating a library code for alert, confirm, and prompt replacements using jQuery and Impromptu. My goal is to be able to call these functions in a similar way: For example: var userInput = jConfirm("Are you sure?"); alert(userIn ...

Learning how to dynamically update a value in Angular based on user input

My goal is to dynamically change the output value based on user input using Angular. I have successfully implemented the functionality to increment the value, but unfortunately, when the input changes, the outputed value remains static. Below is my curren ...

Create a custom autocomplete feature using AngularJS from scratch

I am currently working on creating my own search function with auto-complete functionality. Search MAC:<br/> <input type="text" ng-model="boxtext"> <tr ng-repeat="box in boxes | filter:boxtext"> <td>{{box.type}}</td> < ...

Managing both clicking and hovering events on a single element, ensuring that the popup modal remains open as long as it is being hovered over

After successfully implementing click and hover functionality on an element, I was able to position the popup relative to the mouse pointer based on a previous solution. However, I am now facing an issue where I want the popup modal to be fixed in a specif ...

Breaking down and analyzing XML information

I have data that I need to retrieve from an XML file, split the result, parse it, and display it in an HTML element. Here is a snippet of the XML file: <Root> <Foo> <Bar> <BarType>Green</BarType> &l ...

Attempting to extract the text within a table row cell by clicking on the cell directly following it

I am trying to extract the data from a table row when I click on a specific div within that row HTML: <td class="ms-cellstyle ms-vb2">10</td> <td class="ms-cellstyle ms-vb2"> <div class="ms-chkmark-container"> <div ...

Troubleshooting ASP.NET Ajax Error Code 0

Starting from scratch with asp.net and hoping to incorporate infinite scrolling using jQuery Ajax and ASP.NET MVC. Here's the progress so far: <div id="container"></div> <div id="progress" style="display:none"> <h4>Loading ...

Combining arrays to append to an array already in place

I have implemented the rss2json service to fetch an rss feed without pagination support. Instead of a page parameter, I can utilize the count parameter in my request. With this setup, I am successfully able to retrieve and display the feed using a service ...

endless update cycle in Vue

I'm currently working on developing a custom component. And I have an idea of how I want to use it: let app = new Vue({ el:'#app', template:` <tab> <tab-item name='1'> <h1> This is tab item 1& ...

The command 'npm install -g bigcommerce-stencil/stencil-cli' failed to run properly in the Windows command prompt

Having successfully completed all installations on my Windows 7 SP1 PC, I attempted to run the following command: npm install -g bigcommerce-stencil/stencil-cli Unfortunately, I encountered an error as shown in the screenshot below: error screen For mo ...

How to sort objects by keys in AngularJS

Currently, I am working on building a sorting list using AngularJS. The goal is to have the data updated in the DOM dynamically when a user clicks on the name of a value. I am attempting to order the values by categories such as Bracelets, Charms, Earrings ...

Concealing an element from a separate module

In the angularjs single page application project that I'm working on, there are two modules: module-'A' and module-'B'. Each module has its own view templates. The view template of module-'B' contains a div with id="sit ...