Silhouettes dancing across handcrafted designs

I have been trying to create a custom geometry using vertices and faces, but I am facing an issue where the geometry does not cast shadows on itself and the faces all have the same color as it rotates. I have attempted various solutions but haven't had any success yet. Here is the link to what I have tried so far: http://jsfiddle.net/ZZsXP/.

Is there a way to make this work without having to resort to using multiple Plane meshes grouped together?

Below is the code snippet:

var camera, scene, renderer;
var geometry, material, mesh;

init();
animate();

function init() {

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);

    document.body.appendChild(renderer.domElement);

    width = window.innerWidth / 2
    height = window.innerHeight / 2
    camera = new THREE.OrthographicCamera(-width, width, height, -height, -500, 1000)
    camera.position = new THREE.Vector3(1, 1, 1)
    camera.scale.set(0.4, 0.4, 0.4)

    scene = new THREE.Scene();

    geometry = new THREE.Geometry();

    sizeX = 180
    sizeY = 120
    sizeZ = 100

    geometry.vertices.push(new THREE.Vector3(sizeX / 2, 0, -sizeZ / 2));
    geometry.vertices.push(new THREE.Vector3(-sizeX / 2, 0, -sizeZ / 2));
    geometry.vertices.push(new THREE.Vector3(-sizeX / 2, 0, sizeZ / 2));
    geometry.vertices.push(new THREE.Vector3(sizeX / 2, 0, sizeZ / 2));
    geometry.vertices.push(new THREE.Vector3(sizeX / 2, sizeY / 2, -sizeZ / 2));
    geometry.vertices.push(new THREE.Vector3(-sizeX / 2, sizeY / 2, -sizeZ / 2));
    geometry.vertices.push(new THREE.Vector3(-sizeX / 2, sizeY / 2, sizeZ / 2));

    geometry.faces.push(new THREE.Face3(0, 1, 2))
    geometry.faces.push(new THREE.Face3(0, 2, 3))
    geometry.faces.push(new THREE.Face3(0, 4, 5))
    geometry.faces.push(new THREE.Face3(0, 1, 5))
    geometry.faces.push(new THREE.Face3(1, 2, 6))
    geometry.faces.push(new THREE.Face3(1, 5, 6))

    material = new THREE.MeshBasicMaterial({
        color: 0xff0000,
        side: THREE.DoubleSide,
    });

    light = new THREE.DirectionalLight(0xffffff);
    light.position.set(0, 1, 0);
    scene.add(light);

    mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);
}

function animate() {

    // note: three.js includes requestAnimationFrame shim
    requestAnimationFrame(animate);

    mesh.rotation.x += 0.01;
    mesh.rotation.y += 0.02;

    renderer.render(scene, camera);

}

Answer №1

To ensure shadows are cast, switch the material to either THREE.MeshPhongMaterial or THREE.MeshLambertMaterial as Basic Material does not support shadow casting.

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

Add() function is not duplicating the formatting

I'm attempting to replicate the content below inside a DIV. <ul class="pie-legend"><li><span style="background-color:#0066CC"></span>10-0-1</li><li><span style="background-color:#33CC33&q ...

What is the best way to save the properties of elements in an array of objects within another array?

I have obtained attributes from objects within an array that I need to store in another array. Here is the data I am working with: https://i.sstatic.net/b0JtY.jpg My goal is to extract the `displays` name attribute and save it in the `opt[]` array, which ...

How to submit a textarea with the enter key without needing to refresh the page

I'm attempting to handle the submission of a textarea when the user hits the enter key, storing the text in a variable, then swapping out the form with the text and adding a new form at the end, all without refreshing. I had success doing this with an ...

Using external scripts that require access to the DOM and window object within a Javascript/Node.js Azure Function: Best practices

I am attempting to integrate external JavaScript into an Azure Function that uses the JavaScript/Node.js flavor. The external JavaScript library I need to use, Kendo, relies on a DOM and a window. To achieve this, I initially experimented with JSDOM, but I ...

Tips for preventing repetition in http subscribe blocks across various components

Imagine a scenario where there is a service used for making HTTP request calls. There are two different components (which could be more than two) that need to send the same request using the same observables via this service. After receiving the result, it ...

Mastering Anchors in AngularStrap ScrollSpy

Is there a way to effectively utilize AngularStrap's ScrollSpy feature to link to anchors within the same document? Upon reviewing the AngularStrap documentation, I noticed that when a link is clicked, a double hash is generated in the URL. For examp ...

What is the best way to create a directive that wraps the div elements utilized in an ng-repeat loop?

When working on my application, I utilize this HTML snippet for an ng-repeat: <div class="gridBody"> <div ng-class="{clicked: row.current == true}" ng-click="home.rowClicked($index)" ng-dblclick="ctrl.rowDoub ...

What is the most efficient method for sending query parameters through a URL in Vue.js with Axios?

My goal is to use Axios upon page load to retrieve a JSON object from the base URL. When a button is clicked, I want to append query parameters to the URL and fetch a different JSON object. For example, if the base URL is 'test.com', clicking the ...

Is there a way to implement JavaScript within my Blazor server razor page to modify the styling of a blazor bootstrap button after a form submission?

I am currently working on a Blazor server application where I am looking to add a special functionality. My goal is to have the accordion header change to red if validation fails, meaning if there is no input in the field. view image here //index.razor pa ...

Utilizing orbit controls in Three.js to enable zooming in to the specific location of the cursor

As a newcomer to three js, I am currently working on implementing a feature that allows users to zoom in at the cursor location. My plan involves using a raycaster to determine the point of intersection and updating the vector of the orbit controls each ti ...

Importing classes in ECMAScript 6 does not work properly, causing issues when running scripts

I am currently learning Selenium Webdriver. I am facing an issue where I can't run a script with imported classes, but I am able to run it without classes using import functions only. To execute it, I use babel-cli in the following manner: node ./babe ...

Encapsulating data with JSON.stringify

I'm currently working on creating an object that has the following structure: let outputOut = { "_id": id[i], "regNum": code[i], "sd": sd[i], "pd": ptOut, "p": p[i], ...} //output fs.writeFile('./output/file.json', JSON ...

The axios GET request failed to return a defined value

My current issue involves making a get request using the following code snippet: router.get('/marketUpdates',((request, response) => { console.log("market updates"); var data: Order[] axios.get('http://localhost:8082/marketUpdates& ...

How to effectively handle asynchronous calls in Node.js using readdir and stat functions

My current project involves implementing a post method on the server side to fetch all files within a specified directory (non-recursively). Below is my code snippet. I am encountering challenges in sending back the response (res.json(pathContent);) with ...

Double-checked in p:commandButton, encountering a race condition with oncomplete

When I execute the server action using p:commandButton, I then refresh the form to display hidden fields and show a dialog. Attempting to refresh only the panels with hidden buttons yields the same result: After I call dialog.show(), a second refresh is t ...

The postMessage function in my Javascript SlackBot seems to be flooding the channel with messages

I am currently setting up a Slack bot using JavaScript combined with several useful libraries. The main function of this bot is to execute a postMessageToChannel method whenever a user in the channel mentions the specific keyword "help." However, I am fa ...

React and SASS - issue with checkbox component not being properly aligned with its label

I'm brand new to React and I'm currently in the process of converting a pure HTML page into a React component. How can I adjust the SASS stylesheet to match the original HTML layout? Here is my current React setup (the checkbox displays on the r ...

JavaScript Alert: The Ajax XMLHttpRequest Response has Returned Null

Below is the code snippet: <script type="text/javascript"> var xmlhttp; $(document).ready(function (){ xmlhttp=new XMLHttpRequest(); ...

Encountering issues with implementing router.push in Reactjs

Currently, I am working on ReactJS and utilizing Next.js. My current task involves refreshing the page (using routes), but I encountered an error message stating: Error: No router instance found. You should only use "next/router" inside the client-side of ...

Sort a JSON object in ascending order with JavaScript

I have a JSON object that needs to be sorted in ascending order. [{ d: "delete the text" }, { c: "copy the text" }] The keys d and c are dynamically generated and may change. How can I sort this into? [{ c: "copy the text" }, { d: "delete the text" }] ...