Guide to showcasing cubes in a 10 x 10 arrangement with uniform spacing using three.js

var container, camera, scene, renderer;
var scale = 100, N=1000;
var arr= [];
var width = 720, height = 405;
init();
animate();

function init() 
        {
            container = document.getElementById('theCanvas');
            camera = new THREE.PerspectiveCamera( 45, width / height, 0.1, 10000 );
            camera.position.x = 0;
            camera.position.y = 0;
            camera.position.z = 80;
            scene = new THREE.Scene();

            renderer = new THREE.WebGLRenderer({antialias:true});

            renderer.setClearColorHex(0x000000, 1);
            renderer.setSize( width, height );

            container.appendChild( renderer.domElement );

            document.getElementById('3dobjects').innerHTML = "Three.js Demo, The number of Cube Objects: " +N;

            var geometry = new THREE.CubeGeometry( 1, 1, 1 );

            var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
            for ( var i = 0; i < N; i++)
            {
                arr.push(new THREE.Mesh( geometry, material ));
                scene.add( arr[i] );
                arr[i].position.set( (Math.random()-0.5) * scale, (Math.random()-0.5) * scale, (Math.random()-0.5) * scale );
            }


            }

        function animate() 
        {
            requestAnimationFrame( animate );
            render();

       }

        function render() 
        {    
            renderer.render(scene, camera);          
        }
        ​

I have successfully implemented a random display, here is the working jsfiddle link: http://jsfiddle.net/sagh0900/BEWkQ/. However, I am looking to arrange the cubes in a 10 x 10 pattern as shown in the image below. The colors of the cubes can be disregarded, but I do need them arranged in the specified fashion.

Currently, I am facing difficulties in adjusting the positions of the cubes.

Answer №1

Tweaked your fiddle to help kickstart your project.

// ...
var cube = new THREE.Mesh( geometry, material );
arr.push(cube);
scene.add(cube);
cube.position.set(2* j, 2* i,40 );

Answer №2

Here's a neat little trick for you.

let myMesh = new THREE.Mesh( customGeo, myMaterial );
myMesh.position.x = ( d % 10 ) * 3;
myMesh.position.y = Math.ceil( d / 10 ) * 3;

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

What are the steps to ensure the equalizer start button functions properly?

I have created an application that mimics the functionality of an equalizer by displaying changes in audio frequencies. https://i.sstatic.net/W7Fzi.png Issues with animation arise when you click the "Start" button again. The animation resets and begins ...

vite.config.js failed to be loaded due to an error

Following the Vue docs, I decided to create a new Vue app using these steps: npm init vue@latest npm install After setting everything up, I attempted to run npm run dev, but encountered an issue. https://i.sstatic.net/Q22IX.png Here are my environments ...

JavaScript, detecting repeated characters

I need to create a script that checks an input box (password) for the occurrence of the same character appearing twice. This script should work alongside existing regex validation. I believe I will need to use a loop to check if any character appears twice ...

Can you explain the functionality of $scope.$apply()?

Lately, I've been incorporating $scope.$apply() into my Angular applications to refresh the bindings for my models when new data is received via websockets. It seems to be effective, but I'm curious about its actual functionality and why it' ...

I aim to capture user input values and store them in a variable for future use in other functions, or to perform operations on the input data

const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout, }) readline.question('Please enter the first number: ', function (a) { readline.question('Please enter the second number: & ...

Creating a multi-page app using React: A comprehensive guide

I am in the process of developing an application using NodeJS, and I have decided to incorporate React for creating interactive components within the app. However, my goal is to avoid turning it into a single-page application. How can I effectively distri ...

Ensure that the width of the table rows (tr) matches the width of the table header

I'm currently working on implementing a sticky table header. The issue I'm facing is that the width of tableHeaderRow does not match the width of tableHeader. Steps taken for creating sticky header: Make an Ajax call to populate the table with ...

Can you explain the purpose of Object3D's traverseVisible() method in Three.js?

Is the selection process based on: Making a simple view frustum selection of objects Performing a z-depth visibility check to determine which objects are fully or partially visible on the screen for a specific view ...

Iterate through an array and append individual elements to a fresh array - ensuring only a single item is present in the new

I have been working on a project where I need to call a backend API and retrieve a JSON response. I have come across various solutions, but none seem to address my specific problem. The JSON data returned from the API is structured like this: [ { ...

All elements in the array are being simultaneously updated with the same value in React

I am encountering an issue with my code. Whenever I draw rectangles by clicking and dragging, the new rectangle added to the array overwrites all previously stored rectangles. For example, if my array (named data) initially contains Rectangles as - [Rect ...

Is it possible to prevent users from clicking on a jQuery UI Datepicker?

Is it possible to disable the functionality of a jQuery UI Datepicker so that clicking on it does nothing but display the date? I attempted: $("#calendar").datepicker({ disabled: true, altField: '#note_date', maxDate: 0 }); Unfortu ...

Issue with displaying options in Angular2 v2.4.9 HTML select element

Ever since I made the transition from AngularJS to Angular2, I've been facing a peculiar issue. The select element's options data is fetched from a Solr query, which always returns a 200 response with the data in a timely manner. However, the pr ...

"Populate a textarea with the selected options from a multiple select dropdown

I am currently working on a form that contains several select boxes, some of which allow for multiple selections. I have successfully managed to pass the text value from each select box to a textarea. However, I am facing an issue where I am only able to p ...

Not every time you call the AngularJS run method does it actually execute

Working on a simple Angular app, I wanted to implement a user login check and redirection. However, I encountered an issue where accessing the home page from the form site resulted in inconsistent behavior - sometimes redirecting and other times showing ...

How to Execute Another JavaScript Script Using IF/ELSE Statement?

I am trying to dynamically call one of two JavaScript files based on a condition. Here is the code I have: <script type="text/javascript"> if(b_condition) <script type="text/javascript" src="http://script1.js"></script> else <scri ...

How can I retrieve the value of a sibling's child using jQuery/Ajax?

I'm currently troubleshooting an AJAX comment feature that should activate when a user clicks "open comments." My PHP script is successfully returning data and the ajax call status is "200 OK," so it seems to be functional. However, I am struggling t ...

Transform special characters into HTML entities

$scope.html = '&lt;script&gt;'; Is there a way for Javascript to convert &lt;script&gt; back to <script>, similar to how PHP does it? ...

Custom CSS for the Google Maps Circle Object

Currently, I am utilizing the Google Maps Javascript API v3 Circle object to display circles on the map. I am interested in customizing the CSS of this circle by incorporating some CSS animations. Although I am aware that custom overlays can be used for t ...

Exploring the Functionality of worker-loader Inline

It was my understanding that the Webpack worker-loader configuration below: ... module: { rules: [ { test: /worker\.js/, loader: "worker-loader", options: { inline: 'fallba ...

Ways to incorporate a php file based on the user's selection

I have numerous div elements, possibly a dozen or two, such as... <div class="mydivs" id="firstdiv"></div> <div class="mydivs" id="seconddiv"></div> <div class="mydivs" id="thirddiv"></div> <div class="mydivs" id="fo ...