Exploring Three.js: Meshes, Triangles, and the Beauty of Lambert

I have a function that generates stars, here is the code:

function CreateStar( radius, thickness, isWireframe, starColor) {

            // material
            var starMaterial = new THREE.MeshLambertMaterial({
                    color: starColor,
                    wireframe: isWireframe,
                    shading: THREE.FlatShading
                  });

            // array for vertices
            var vertices = [];
            // set "zero" vertex for thickness
            vertices.push( new THREE.Vector3(0, 0, thickness) );

            // calculate vertices and pits for a star shape
            var deg = Math.PI / 180; // working with degrees for simplicity
            var maxR, minR; 
            maxR = radius; // radius for a vertex
            var  x4Rad = maxR * Math.cos( - 72 * deg );
            minR = x4Rad/Math.cos( - 36 * deg ); // radius for a pit
            var firstVertex;

            for ( var i = 0; i < 5; i++ ){

                // vertex
                var vertX = maxR * Math.cos( (90 + (72 * i)) * deg );
                var vertY = maxR * Math.sin( (90 + (72 * i)) * deg );
                if ( i == 0 ) firstVertex = new THREE.Vector3( vertX, vertY, 0 );
                vertices.push( new THREE.Vector3( vertX, vertY, 0 ));

                // pit
                var pitX = minR * Math.cos( (126 + (72 * i)) * deg );
                var pitY = minR * Math.sin( (126 + (72 * i)) * deg );
                vertices.push( new THREE.Vector3( pitX, pitY, 0 ));
            }
            vertices.push( firstVertex ); 

            var holes = []; // no holes in our contour
            var triangles, star;
            var geometry = new THREE.Geometry();

            geometry.vertices = vertices;
            triangles = THREE.Shape.Utils.triangulateShape( vertices, holes ); 

            for ( var j = 0; j < triangles.length; j++ ){
                geometry.faces.push( new THREE.Face3( triangles[j][0], triangles[j][1], triangles[j][2] ));
            } 

            star = new THREE.Mesh( geometry, starMaterial );
            return star;
}

After running this function, I encountered an issue. When I return a cube (commented in the code) and add it to the scene, everything works as expected with correct shading based on the light source. However, when I return a star and add it to the scene, I only see a black star with no color or shading. Why is it that I can apply a material to the cube but not to the star?

If anyone can provide some insight on what I might be doing wrong, I would greatly appreciate it.

Using Three.js r68

Answer №1

Perhaps a quick recalculation of the normals will do the trick. Give this a shot:

    spacing.computeAngles();
    spacing.calculateNormals();
    circle = new THREE.Mesh( spacing, circleMaterial );

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

Different ways to generate DOM element using jQuery

When it comes to creating a new DOM element from JavaScript or jQuery code, there are numerous methods to consider. Some examples include: $('<div>').prop('id','someid').addClass('someclass'); $('<div& ...

What is the best way to extract "true" values from an array and store them in a new array during iteration?

I am currently enrolled in a Codecademy course and I am facing a roadblock. My main goal is to gain a solid grasp of JavaScript. The current task at hand is as follows: "There is an array of unnecessary words. Your goal is to iterate over the array and fi ...

Show the entire phrase within a Bootstrap modal with the help of jQuery, instead of just the initial

I have a PHP script that fetches a name from a database, and I'm using jQuery to display that name as HTML inside a Bootstrap modal when a button is clicked. However, the issue I'm facing is that when the name contains spaces, only the first word ...

What role does a promise play in rendering code asynchronous?

While we often use promises to avoid the dreaded function callback hell, a question remains: where exactly in the event loop does the promise code execute and is it truly asynchronous? Does the mere fact that the code is within a promise make it asynchron ...

Retrieve both positive and negative reviews using the Steam API

I'm trying to calculate the percentage of positive reviews, but I haven't been able to find the necessary data. I've been using this endpoint to retrieve game information: "", but it only provides the total number of reviews. I am aware of a ...

Using the ES6 object spread operator in the JSX syntax of ReactJS

// modules/NavLink.js import React from 'react' import { Link } from 'react-router' export default React.createClass({ render() { //for example this.props={from: "home", to: "about"} return <Link {...this.props} a ...

Delete an entry in a singular mapping in a one-to-one connection [TypeORM]

Is there a way to remove an index from a one-to-one relationship in TypeORM? @OneToOne(() => Customer, { cascade: true }) @JoinColumn({ name: 'customer', referencedColumnName: 'uid' }) customer: Customer I searched the d ...

The depth buffer in Webgl FrameBuffer is not being cleared properly

Currently, I am working on developing a 2D sprite renderer that utilizes render textures for custom compositing. However, I have encountered an issue where the depth buffer on the FrameBuffer is not clearing properly. Due to this, all the sprites leave a p ...

Can you explain the concept of peer dependencies and plugins to me?

After reading numerous articles and posts on the topic of peer dependencies, I still find myself struggling to fully understand the concept. For instance, if coffee 1.0 has a dependency on milk 1.0, then logically coffee 1.0 would be included in my packa ...

What could be causing Nextjs13 to fail in recognizing an authentication route, resulting in a 404 error?

I have been working on a project utilizing NextJs v13 with Strapi v4 as the backend. My project includes separate layouts for the site, login, and dashboard. While working on the login section, I implemented NextAuth for authentication. However, upon submi ...

Angular tutorial: Organizing data by date only

This is my first time building an Angular app. I am fetching JSON data from an API using a factory service to get football match fixtures. My goal is to group these fixtures by date while disregarding the time component. The date string in the JSON is fo ...

Setting a default value for Autocomplete in MaterialUI and React.js

Is there a way to set a default value for an Autocomplete TextField component from Material UI in React.js? I want to load a pre-populated value from the user's profile that can then be changed by selecting another option from a list. Check out my co ...

Utilizing jQuery to target a select element's two specific children

I am trying to target the parent element by matching two specific children elements. Here is my code: $('span:contains("11:00am"), span.name:contains("Tom")').parents("a").css("background-color","rgb(255, 255, 255)"); <script src="https://c ...

Interactive data visualization with hover-over details

I am utilizing datamaps to showcase the countries of the world, however, I want the graph to be centered. The issue arises when I hover over a country and the pop up appears all the way to the left, aligned with where the country would be if it wasn't ...

Is there a more efficient method for incorporating artists' animations in ThreeJS?

As of now, I am dealing with a collada file that contains animations created by the artist. My goal is to control specific parts of the animation using buttons on my webpage. Do you think collada is the right format for this task, or should I consider us ...

Guide on how to assign a masterpage DOM element to a content page using JavaScript

Within an ASP.NET master page, there is a specific div element that I want to hide in one of its content pages. To achieve this, I included the following JavaScript code at the end of the content page: if (document.getElementById('sitemap')) { ...

Determining the container height for every two-image row

I am currently working on a project for this website. My focus right now is on resizing vertical images using the following script: function Gallery(selector) { this.add_module = function (type, image) { var portrait_text = image.next('.portr ...

Steps for displaying a pop-up window and showcasing a JSF Response

In my current project, I am utilizing JSF 1.2 without any component libraries for a JSF Application. One specific scenario I am tackling involves having a JSF-rendered page where clicking on a link should trigger the opening of a pop-up page displaying d ...

The method to disable the dropdown for a select tag in a Polymer Dom-module is not functioning properly

I need help modifying the country dropdown based on a value retrieved from backend code If the disabled_value is 0, I want to hide the dropdown and make it unselectable If the disabled_value is 1, I want to enable the dropdown for selecting countries &l ...

The radio input is not being properly checked using ng-checked in Angular

The ng-checked attribute is causing issues in the application, specifically with radio buttons. It seems to be working fine for checkboxes but not for radio buttons. <input type="radio" class="radio" name="job_class_radio" ng-checked="key===jobClassDat ...