Rendering in Three.js groups

Attempting to render a group of objects using three.js with a single function, render2, that takes the object as a parameter has been unsuccessful. While individual cubes can rotate when controlled by their own function, an error is encountered when trying to use the render2 function. The cubes created from the cubeGenerator function seem to align with what is passed into the groupRenderer function, which in turn matches the initial input for the render2 function. However, while render and render1 functions output cube and cube1 information correctly, render2 function displays a number and throws an error:

main.js:20 T…E.Mesh {uuid: "6902A0C3-7CFA-4B5C-A7BC-11259CE69113", name: "", type: "Mesh", parent: T…E.Scene, children: Array[0]…}
main.js:28 T…E.Mesh {uuid: "275129EB-BD99-4156-B208-CED6F49F6112", name: "", type: "Mesh", parent: T…E.Scene, children: Array[0]…}
main.js:36 408.2339999877149
main.js:38 Uncaught TypeError: Cannot read property 'x' of undefined

Following the "Intro to WebGL with Three.js" tutorial on worked smoothly until experimentation began. While cube and cube1 rotations work fine, the group of cubes appear on screen but fail to rotate. Assistance regarding this issue would be greatly appreciated. Here is the current code snippet:

Line    Code
1   var scene = new THREE.Scene();
2   var aspect = window.innerWidth / window.innerHeight;
3   var camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
4   var renderer = new THREE.WebGLRenderer();
5   renderer.setSize(window.innerWidth, window.innerHeight);
6   document.body.appendChild(renderer.domElement);
7   camera.position.z = 10;
8
9   var geometry = new THREE.BoxGeometry(1, 1, 1);
10  var material = new THREE.MeshNormalMaterial();
11  var cube = new THREE.Mesh(geometry, material);
12  cube.position.x = -1;
13
14  var geometry1 = new THREE.BoxGeometry(1, 1, 1);
15  var material1 = new THREE.MeshNormalMaterial();
16  var cube1 = new THREE.Mesh(geometry1, material1);
17  cube1.position.x = 1;
18
19  var render = function() {
20      console.log(cube);
21      requestAnimationFrame(render);
22      cube.rotation.x += 0.05;
23      cube.rotation.y += 0.05;
24      renderer.render(scene, camera);
25  };
26
27  var render1 = function() {
28      console.log(cube1);
29      requestAnimationFrame(render1);
30      cube1.rotation.x += 0.05;
31      cube1.rotation.y += 0.05;
32      renderer.render(scene, camera);
33  }
34
35  var render2 = function(object) {
36      console.log(object);
37      requestAnimationFrame(render2);
38      object.rotation.x += 0.05;
39      object.rotation.y += 0.05;
40      renderer.render(scene, camera);
41  }
42

...

74  groupRenderer(group);

An attempt was made to modify the code as follows, however, it did not resolve the issue:

Line    Code
43  var cubeGenerator = function(newGroup) {
44      console.log("--------------------\nGenerating Cubes");
45      for (var i = -2; i < 2; i++) {
46      // var newGeometry = new THREE.BoxGeometry(1, 1, 1);
47      // var newMaterial = new THREE.MeshNormalMaterial();
48      // var newCube = new THREE.Mesh(newGeometry, newMaterial);
49      // newCube.position.x = i;
50      // newGroup.add(newCube);
51      // console.log(newCube);
52      var clone = cube.clone();
53      clone.position.x = i;
54      newGroup.add(clone);
55      console.log(clone);
56      }
57      console.log("Done Generating Cubes\n--------------------");
58  };

Note: The tutorial may not always be visible on the threejs.org website; try refreshing or navigating away and back to access it. Multiple attempts might be needed.

Answer №1

I have successfully achieved my desired outcome:

let stage = new THREE.Scene();
let aspectRatio = window.innerWidth / window.innerHeight;
let camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 1000);
let renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
camera.position.z = 10;

let geometry = new THREE.BoxGeometry(1, 1, 1);
let material = new THREE.MeshNormalMaterial();
let cubeA = new THREE.Mesh(geometry, material);
cubeA.position.x = -1;

let geometry1 = new THREE.BoxGeometry(1, 1, 1);
let material1 = new THREE.MeshNormalMaterial();
let cubeB = new THREE.Mesh(geometry1, material1);
cubeB.position.x = 1;

let rotateObjects = function(object) {
    requestAnimationFrame(rotateObjects);
    object.rotation.x += 0.05;
    object.rotation.y += 0.05;
}

let group = new THREE.Group();

let createCubes = function(parentGroup) {
    console.log("--------------------\nGenerating Cubes");
    for (let i = -2; i < 3; i++) {
        let newGeometry = new THREE.BoxGeometry(1, 1, 1);
        let newMaterial = new THREE.MeshNormalMaterial();
        let newCube = new THREE.Mesh(newGeometry, newMaterial);
        newCube.position.x = i;
        parentGroup.add(newCube);
        console.log(newCube);
    }
    console.log("Cubes Generated\n--------------------");
};

createCubes(group);

stage.add(cubeA);
stage.add(cubeB);
stage.add(group);
rotateObjects(cubeA);
rotateObjects(cubeB);
rotateObjects(group);
renderer.render(stage, camera);

I consolidated the rendering functions to one that rotates each object individually. @dcromley's solution may result in the objects rotating as a single unit, but I wanted each object within the group to rotate independently around its own center. Perhaps there was some confusion on my part.

Answer №2

I believe your intention is to only render once. To rotate an object, you can use the following routine:

rotate1(object1);

If you want a group of objects to rotate together, you can add them to a scene (and add the scene to your base scene) and then rotate the scene. Your routine will look like this:

rotate2(scene1);

UPDATE: script included

"use strict";
var renderer, scene, camera, light, geometry, material, mesh;
var scene2, mesh1, mesh2, mesh3, mesh4;
window.onload = function() {
    renderer = new THREE.WebGLRenderer();
    renderer.setSize(750, 750);
    renderer.setClearColor(0x102030, 1);
    document.body.appendChild(renderer.domElement);
    scene = new THREE.Scene(); // the main scene
    camera = new THREE.PerspectiveCamera(30, 1);
    camera.position.set(0, 0, 30);
    camera.lookAt(new THREE.Vector3(0, 0, 0));
    light = new THREE.AmbientLight(0x444444);
    scene.add(light);
    light = new THREE.DirectionalLight(0xff8888, 1);
    light.position.set(10, 10, 20);
    scene.add(light);
    light = new THREE.DirectionalLight(0x88ff88, 1);
    light.position.set(-10, 10, 20);
    scene.add(light);

    mesh1 = makebox(-3, -3, 0); // individual objects
    scene.add(mesh1);
    mesh2 = makebox(-3, 3, 0);
    scene.add(mesh2);

    scene2 = new THREE.Scene(); // sub-scene, boxes on the right
    mesh3 = makebox(0, -3, 0);
    scene2.add(mesh3);
    mesh4 = makebox(0, 3, 0);
    scene2.add(mesh4);
    scene2.position.set(3, 0, 0);
    scene.add(scene2);

    fnloop();
}

function fnloop() {
    mesh1.rotateY(.01); // rotate individual object
    scene2.rotateY(-.01); // rotation of 2 objects in scene
    renderer.render(scene, camera);
    requestAnimationFrame(fnloop);
}

function makebox(x, y, z) {
    geometry = new THREE.BoxGeometry(1, 2, 3);
    material = new THREE.MeshPhongMaterial({ color: 0xffffff, wireframe: false });
    mesh = new THREE.Mesh(geometry, material);
    mesh.position.set(x, y, z);
    return mesh;
}

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

Posting Form Data with Ajax in CodeIgniter

Incorporating the CodeIgniter framework along with the jQuery Form plugin available at http://malsup.com/jquery/form/ Encountering challenges in ensuring proper functionality of a form. View <div class="row"> <div class="well c ...

How can you switch the property of an object within a VueJS array?

I'm currently working on developing a user feedback form that allows users to rate the quality of the food items they have ordered. I have an array called foodItems that displays the list of available food items along with predetermined reactions. My ...

Concentrate on a specific input within a list of inputs that are generated dynamically

My list contains dynamically generated inputs. input --> onClick new Input beneath [dynamically added]input input How can I focus on just the dynamically added input? The input has the textInput reference. This code partially achieves it: componentW ...

Is it possible that WebdriverIO is unable to retrieve the value for an input

Currently, I am in the process of automating tests specifically for a web application on Safari browser. However, I have encountered some challenges along the way. It appears that webdriverIO is not performing very well with Safari in my environment, which ...

Creating a JSON object in Node by merging various items

I am in the process of creating my own JSON object by gathering data from various online image/photography sources. The code snippet below outlines my objective: var searchUnsplash = require('./apis/unsplash'); var searchFlickr = require ...

AngularJS - Always keep an eye on a group of items

Looking to create a custom watcher for a collection within my application, I initially believed that Angular would have all the necessary tools at my disposal. I had access to $watch, both shallow and deep, as well as $watchCollection. A $digest cycle was ...

The Ajax request is not being triggered when using a different form submit method

Being a tech enthusiast, I have developed a handy function: function blur_slide_visit_count(){ $.ajax({ type: 'POST', url: 'add_save_slide_visitor_count.php', async: false, data: { fillvalue: fieldAr ...

Tips for incorporating real-time information into a highchart graph?

I'm currently working on a Highchart that utilizes AJAX and jQuery for receiving JSON data. However, I've noticed that the points on my chart only appear when I hover over it, and even then they all seem to be clustered at the top of the chart. I ...

Resolving a Routing Problem with moment.js

Hi everyone, I'm new to working with express.js and backend routing. I'm encountering an error with my server code and would appreciate any insights on what might be causing it. I've already tried using res.end() with plain text, but the err ...

"What is the best way to access and extract data from a nested json file on an

I've been struggling with this issue for weeks, scouring the Internet for a solution without success. How can I extract and display the year name and course name from my .json file? Do I need to link career.id and year.id to display career year cours ...

Fixing the error "require() is not defined" when trying to call a function from JavaScript to Node.js

As I work on building my website, I discovered that Javascript lacks a built-in way to communicate with a database. Through my research, I came across node.js and decided to implement it. Here is a snippet of the code I've written: var mysql; var con; ...

Encountered a problem while trying to upload a file using node

Hi there! I seem to be facing an issue with the code snippet below. Every time I attempt to upload a file, the browser keeps loading indefinitely. Any thoughts on what might be causing this problem? app.js var formidable = require('formidable') ...

Issues with XMLHTTP readyState 3 updates in Webkit browsers

This query has resurfaced twice in these forums, yet the solution offered does not seem to be effective for my situation. The dilemma I am facing involves a JSP page that is delivering and pushing out small bits of data. Here is the code snippet I am usi ...

Styling text in JavaScript and CSS with colored fonts and underlines

I am looking to customize the CSS for the font style. <div id="a" onmouseover="chbg('red','b')" onmouseout="chbg('white','b')">This will change b element</div> <div id="b">This is element b</div ...

Running a local project with the help of the Express framework

// Setting up the configuration for serving files from the source directory var express = require('express'); // Importing express module var path = require('path'); // Importing path module var open = require('open'); // Imp ...

The application monitored by nodemon has halted - awaiting modifications in files before restarting the process

1. My ProductController Implementation: const Product = require('../models/product') //Creating a new product => /ap1/v1/product/new exports.newProduct = async(req, res, next) => { const product = await Product.create(req.body); re ...

Tips on how to change a child component's attribute using props in Vue.js?

I have a situation where I have an addToCart component within a foodList component. Additionally, there is another component called Cart. My goal is to reset the addToCart component's counter value to 0 whenever the cart is emptied. App.vue data() { ...

What is the best way to eliminate excess space on the right side in Bootstrap 4 while in mobile view?

I'm struggling to understand why this layout is not responsive on mobile devices, triggering a bottom scroll bar at around 616px. I'm looking for a solution to hide the scroll bar at least until 414px for iPhones and other smartphones. I've ...

What could be causing errors with my addrole command in discord.jsV12?

After updating to discord.jsv13, my addrole command is no longer working properly. I keep getting errors like role.id is not a function or role.id is not defined, even though I can't seem to find any errors in my code. I have experience with JavaScrip ...

What is the significance of having 8 pending specs in E2E Protractor tests on Firefox?

Each time I execute my tests, the following results are displayed: There were 11 specs tested with 0 failures and there are 8 pending specs. The test execution took 56.861 seconds to complete. [launcher] There are no instances of WebDriver still running ...