It appears that Three.js MeshPhongMaterial is not influenced by any light sources other than ambient lighting

After experimenting with a function that graphs a pair of arrays as a double-sided surface in threejs, I realized I was facing some issues. Despite my experience using threejs for a few weeks, custom shapes seemed to complicate things. I scoured the internet for solutions but found none that worked for me. Hence, I am reaching out here.

The two relevant code snippets are below, but the entire code can be accessed on JsFiddle.

var ambient = new THREE.AmbientLight( 0x444444 );
scene.add( ambient );

var spotLight = new THREE.DirectionalLight( 0xffffff );
spotLight.position.set( 0, 1, 1 ).normalize();
scene.add(spotLight);
and
function graph(array1, array2)
{

    var particleCount = array1.length*array2.length,
        particles = new THREE.Geometry(),
        pMaterial = new THREE.MeshPhongMaterial({
            side: THREE.DoubleSide,
            shading: THREE.SmoothShading
        });

    // now create the individual particles
    for (var p = 0; p < particleCount; p++) {


    var row = (p%array1.length),
        col = Math.floor(p/array1.length),
        pX = 2*row - array1.length,
        pY = 2*col - array2.length,
        pZ = (array1[row])*(array2[col]),
        particle = new THREE.Vector3(pX, pZ, pY);

    // add it to the geometry
    particles.vertices.push(particle);
    if (row != 0 && col != 0)
    {
        particles.faces.push( new THREE.Face3( col * array1.length + row, col * array1.length + row - 1, (col - 1) * array1.length + row) );
        particles.faces.push( new THREE.Face3( (col - 1) * array1.length + row, col * array1.length + row - 1, (col - 1) * array1.length + row - 1) );
    }
}

// create the particle system
var particleSystem = new THREE.Mesh(
    particles,
    pMaterial);

// add it to the scene
scene.add(particleSystem);

onRenderFcts.push(function(){
    var angle   = Date.now()/10000 * Math.PI;
    particleSystem.rotation.y   = angle;        
})

}

https://jsfiddle.net/6Lp74xdn/1/

Despite thorough testing, I'm stumped as to why the directional light is not illuminating the surface correctly.

Answer №1

Once you have created all the vertices and faces, remember to execute computeFaceNormals().

Simply include particles.computeFaceNormals(); in your graph function at the very end.

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

Encountering an issue when trying to upload a file for the second time

I am currently working on a project where I need to upload an excel file and send it to an API using ReactJS. So far, I have been able to successfully send the file to the API. However, in my submit function, I want to reset the saved excel file from the s ...

Ensure your Vue components have type-checked props at compile time when using TypeScript

I have encountered an issue while using Vue.js with Typescript. The code I am working with is pretty straightforward, utilizing vue-class-component and vue-property-decorator. <script lang="ts"> import { Component, Prop, Vue } from 'vue-pro ...

Guide to iterating through a queue of promises (sequentially handling asynchronous messages)

Currently, I am working on processing a queue of promises (which are representations of messages) in a specific order and using AngularJS for the task. Just to give you an idea, let's say that I have a method called connect() which returns a promise ...

Tips for refreshing a webpage after switching screens

// I have implemented the code below to navigate from one screen to another, specifically the Home page. However, I am facing issues with the page refreshing or reloading when navigating to the home screen. None of the lifecycle methods are being called, e ...

What is the best way to include a date range together with other placeholder variables in a PostgreSQL and Express setup?

Forgive me for what may be a basic question, but as I dive headfirst into teaching myself SQL & PostgresSQL, I've stumbled upon an issue. I'm attempting to insert a large number of placeholders into an INSERT statement, but I'm struggling t ...

Implementing JavaScript to override inline CSS styles

Is it possible to override inline CSS using JavaScript with compatibility for IE6? I came across a pure CSS solution, but unfortunately it doesn't work in IE. http://www.sitepoint.com/blogs/2009/05/27/override-inline-css/ <div class="block"> ...

Creating PDF files for iPhone using Phonegap

While Phonegap does not have this feature in its API, iOS offers the capability through Quartz 2D. You can find more information about it here. How can I achieve similar functionality in Phonegap for iPhone? As a beginner, any guidance on setting up the n ...

Dynamic addition of HTML is not possible in AngularJS

I've encountered a challenging issue that I'm struggling to resolve. Despite my best efforts, I can't seem to find a suitable solution to achieve the desired outcome. Let me provide some context to explain my problem: I need to dynamically ...

What is the most efficient method for appending /.json to the conclusion of express routes?

I am currently transitioning a DJANGO API to Node.js and have been tasked with ensuring that routes support the .json extension at the end. For instance, sending a GET request to /users/:id/.json should return a JSON object representing the user. The cha ...

Discover the best way to connect your Chrome bookmarks to a server through an extension using an API

I have embarked on creating a unique Chrome extension that will enable users to not only view, update, create, and delete Chrome bookmarks but also save and retrieve bookmarks through our server without the need for Google account synchronization. One chal ...

Having trouble resolving an error while attempting to incorporate an npm module into a vanilla JavaScript application

I admit my lack of expertise in building modern JavaScript apps is becoming evident. Currently, we have a Capacitor app that uses plain JavaScript without any build tools, and it functions well. Our goal is to incorporate Microsoft Code Push support throu ...

The function setState() is not performing as expected within the useEffect() hook

After retrieving data from my Mongo database, it's returned as an object within the useEffect hook function, specifically in the response. I then initialize a state called myorders with the intention of setting its value to the data fetched from the A ...

What is the best way to implement rotation functionality for mobile devices when dragging on a 3D globe using D3.js and an HTML canvas?

I have been experimenting with the techniques demonstrated in Learning D3.js 5 mapping to create a 3D globe and incorporate zoom and rotation functionalities for map navigation. Here is the function that handles both zooming and dragging on devices equipp ...

Repetitive cycling through an array

My array consists of classes: const transferClasses = [ { id: "c5d91430-aaab-ed11-8daf-85953743f5cc", name: "Class1", isTransfer: false, children: [], }, { id: "775cb75d-aaab-ed11-8daf-85953743f5cc", ...

Can a callback in an ES6 class constructor ensure that the promise is either resolved or rejected?

I'm currently puzzled by the concept of using a callback function that always returns an empty object and then passing a parameter to this callback. Visit this link for more information My assumption is that this approach ensures that the Promise wi ...

What sets apart `var now = new Date();` and `var now = Date();` in JavaScript

I am specifically interested in exploring the impact of adding "new" on the variable, as well as understanding when and why it is used. I would also like to understand why I am obtaining identical answers when printing both versions. ...

What causes the ThreeJs plane to bend when the camera is moved along the y-axis?

I have a specific situation with a THREE.PlaneBufferGeometry where I am applying an image texture in the following way: var camera, scene, renderer; var geometry, material, mesh, light, floor; scene = new THREE.Scene(); THREE.ImageUtils.loadTexture( "so ...

Different Angular 2 components are resolved by routes

Consider this scenario: Upon navigating to the URL /product/123, the goal is to display the ProductComponent. This is how it's currently configured: RouterModule.forRoot([ { path: 'product/:productId', ...

What could be the reason my Backbone view is failing to render?

Can anyone help me troubleshoot why this template isn't rendering properly in my backbone views? Any insights would be greatly appreciated! Below, I've included the code from my jade file for the views and the main.js file for the backbone js scr ...

The pairing of Transpiller and Internet Explorer 8 is like a dynamic

In starting my new project, I am considering using BabelJS. However, there is a significant requirement that must be met: it needs to be compatible with IE8. ISSUE: Babel compiles ES6 to ES5, but the support for ES5 on IE8 is lacking. Are there any alter ...