Diverse Cubes with various textures in three.js

Currently, I am exploring three.js on my own and experimenting with different materials applied to cubes. My goal is to place 3 cubes in a scene, each with a distinct material, so that I can compare their differences side by side.

The problem I am encountering is that only cube1 is rendering correctly, while the other two cubes are not visible in the browser. Any assistance in resolving this issue would be highly appreciated.

<body>
    <script src="../three.js-master/build/three.min.js"></script>
    <script src="../three.js-master/examples/js/controls/OrbitControls.js"></script>
    <script>
        var camera, scene, renderer;
        var controls;
        var cube1, cube2, cube3;

        init();
        animate();

        function init() {
            //Renderer
            renderer = new THREE.WebGLRenderer({ antialias: true });
            var width = window.innerWidth;
            var height = window.innerHeight;
            renderer.setSize(width, height);
            document.body.appendChild(renderer.domElement);

            scene = new THREE.Scene();

            //Create Cubes
            //Normal
            var cube1Geometry = new THREE.BoxGeometry(2, 1, 1);
            var cube1Material = new THREE.MeshNormalMaterial();
            cube1 = new THREE.Mesh(cube1Geometry, cube1Material);
            cube1.position.set(0, 0, 0);
            scene.add(cube1);

            //Lambert
            var cube2Geometry = new THREE.BoxGeometry(1, 2, 1);
            var cube2Material = new THREE.MeshLambertMaterial({ color: 0xff0000, transparent: true, opacity: 05 });
            cube2 = new THREE.Mesh(cube2Geometry, cube2Material);
            cube2.position.set(-10, 0, 0);
            scene.add(cube2);

            //Phong
            var cube3Geometry = new THREE.BoxGeometry(1, 1, 2);
            var cube3Material = new THREE.MeshPhongMaterial({ shininess: 1 });
            cube3 = new THREE.Mesh(cube3Geometry, cube3Material);
            cube3.position.set(10, 0, 0);
            scene.add(cube3);

            //Create Camera
            camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000);
            camera.position.y = 30;
            camera.position.z = 30;
            camera.lookAt(new THREE.Vector3(0, 0, 0));

            controls = new THREE.OrbitControls(camera, renderer.domElement);
        }

        function animate() {
            controls.update();
            requestAnimationFrame(animate);
            renderer.render(scene, camera);
        }

    </script>
</body>

Answer №1

Further investigation into the issue revealed that lambert and phong materials were not visible due to a lack of light source. By adding the following code snippet, the materials now properly render in the browser:

//Light Source
var light = new THREE.PointLight( 0xff0000, 1, 100 );
light.position.set( 20,20, 20 );
scene.add( light ); 

This simple addition made all the difference in resolving the visibility issue.

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

Revising variables in Java Script

How can I shuffle a list of variables in JS and maintain the order while changing their values? The following code snippet demonstrates what I am trying to achieve. <p id="demo"></p> <script> var gen = "male "; var race = "white"; var ...

Creating a Vue application utilizing a function with an unspecified purpose

Looking to create an app with a function that is currently undefined. On the production server, there is a function called __doPostBack which I am calling from my Vue app like this: getLabel(templateName) { __doPostBack(templateName, ""); } Afte ...

What is the correct way to retrieve the mouse coordinates in Javascript?

I created a JavaScript program that simulates ball physics, with the HTML canvas resizing to fit the window height. While I managed to resize the canvas successfully, issues arose with mouse position tracking and balls not appearing on screen upon click. I ...

Opt for utilizing a global npm package over repeatedly installing it

Is there a way to utilize the globally installed package without needing to reinstall it every time we run npm i? Here's the scenario I have: I've set up a docker image with a specific package already installed (installation command in the Docker ...

Using xpath to select and interact with all buttons on a webpage

When implementing my code on a webpage containing rows of records with drop-downs that display when clicked, I noticed that for some reason, it only initiates a click action on the first row of the page. How can I modify my code to ensure that every reco ...

TS7006: Argument 'duplicate' is assumed to have an 'any' data type

I am working on a function that will create a button to copy the content of a variable into the clipboard using TypeScript. Below is my attempted code: const [copySuccess, setCopySuccess] = useState(''); const copyToClipBoard = async copyMe => ...

What is the process for updating a particular div element?

I am currently developing a webpage that allows users to select an item, and the relevant information will be displayed. On this page, I have incorporated two buttons: btnBuy0 and btnBuy1. The functionality I am aiming for is that when BtnBuy0 is clicked ...

Generating a list of objects from MongoDB using Node.js

Hello everyone, I am currently learning about mongodb and node js for the first time, so please bear with me if my question is too basic. I am working with a schema that looks like this: var mongoose = require('mongoose'); var CatSchema = new m ...

Can JQuery's 'unslider' be customized to only change the backgrounds?

Check out my source at this link: http://codepen.io/anon/pen/Bvkjx Observe how the content and background images rotate correctly. Now, I'm curious if it's feasible to keep the following content static <p>SOME CONTENT1</p> while ...

Adjust window size while keeping the current zoom level

As I continue to explore three.js, I am becoming familiar with the operations of the camera and renderer. My current focus is on implementing the functionality to drag and resize the canvas that three.js utilizes. My goal is to enable the canvas to be dra ...

Assigning properties in html

When the status is equal to "complete", I need to disable an input tag based on a certain condition. One way to achieve this using javascript is by utilizing the setAttribute method. var element = document.querySelector("input"); element.setAttribute("d ...

Discovering nodes with changing identification values in polymers automatically

Is there a way to locate a node with a dynamic id value using Polymer's method of finding nodes by id? For instance <template> <div id="{{ id }}"></div> </template> and in JavaScript Polymer("my-element", { ready: f ...

Transform the Hue or Color of a PNG using ASP.Net, VB.Net, and jQuery

I am currently developing a web page that can combine multiple PNG images into one flat image for users to download. I am looking to incorporate a feature that allows for hue or color adjustments, similar to the color balance functionality in Photoshop. ...

Cookie Cutter Chrome Extensions

Is there a way to create a cookie that can be shared between my plugin and contentPage? I've tried creating one but it doesn't seem to appear on the cookie list of the tab page. Any suggestions or ideas on how to achieve this? ...

Scroll effect for read-only text input with long content inside a div

Is there a way to replicate the scroll functionality of a text input with readonly="readonly"? When the text exceeds the box size, you can highlight and scroll to view it all without a visible scrollbar. I am interested in achieving this effect within a p ...

Is there a way to assign a sessionStorage key by clicking on certain elements in HTML?

I have encountered an issue while attempting to set a value in the sessionStorage. The problem lies in storing the sessionStorage "key" differently based on the item clicked. For instance, if I click on the first view chat, I should store a key of "1", and ...

Receiving an "ERR_HTTP_HEADERS_SENT" error message when using Mailgun

One of the functions I have is quite simple - it returns a status along with some JSON data. This function involves saving user input to the database and simultaneously sending an email message using Mailgun. Everything seems to be working fine, however, e ...

Retrieve data from REST call to populate Material dropdown menu

I am looking to dynamically populate a dropdown menu with data retrieved from a Rest API. I attempted the following code: <Select id="country-helper"> {array.map((element) => ( <MenuItem value={element.code}>{element.country}& ...

Develop a secure Flutter client application communicating with a server equipped with SSL certificates for enhanced security

My back-end is an ASP.NET Web API server hosted as a secure server with a certificate. When I navigate to the URL, it is secure. This part looks good. However, when I FTP my browser app (Flutter or another framework like Angular or React) to the same ser ...

Adjusting size of a div as you scroll - Java Script

Using the codepen provided at http://codepen.io/anon/pen/fDqdJ, I am looking to enhance the functionality by implementing a feature where, upon reaching a specific distance from the top of the page, an already animated div undergoes a change in scale while ...