The OOP functionality in Three.JS seems to be malfunctioning, as certain elements are not being properly accessed and displayed

I've run into some issues while trying to implement OOP in my Three.js project. The original script displays three y-rotational planes, but it seems like some objects I've created aren't being called when I check the console. Can someone please help me identify where I may have gone wrong?

I've been troubleshooting using the console log on my live server, and it seems like the problem might be related to the classes card or mesh. However, every time I make adjustments, more errors seem to pop up.

So far, here's what I have:

var renderer, scene, camera;
      var speed = 0.03; // speed for rotation

      class Plane {
          constructor(geometryFront) {
            this.geometryFront = new THREE.PlaneGeometry(90, 110, 1, 1);
          }
          applyMatrix() {
            this.geometryFront.applyMatrix(
              new THREE.Matrix4().makeRotationY(Math.PI)
            );
          }
        }

        class Material {
          constructor(material, imgMaterial) {
            this.material = new THREE.MeshBasicMaterial({
              map: THREE.ImageUtils.loadTexture(imgMaterial),
            });
          }
        }

        class Card {
          constructor(addCard, xCard, yCard, zCard) {
            this.addCard = new THREE.Object3D();
            scene.add(this.addCard);
            this.addCard.position.set(xCard, yCard, zCard);
            this.addCard.rotation.y += speed;
          }
          cardMesh(addMesh) {
            this.addCard.add(addMesh);
          }
        }

        class Mesh {
          constructor(addMesh, geometry, material, linkCard) {
            this.addMesh = new THREE.Mesh(this.geometry, this.material);
            linkCard.cardMesh(this.addMesh);
          }
        }

      preSet();
      cardAssets();
      animate(); 

      function preSet() {
        // renderer
        renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        // scene
        scene = new THREE.Scene();

        // setting background
        const loaderBG = new THREE.TextureLoader();
        const bgTexture = loaderBG.load("temp-bg.jpeg");
        scene.background = bgTexture;

        // camera
        camera = new THREE.PerspectiveCamera(
          40,
          window.innerWidth / window.innerHeight,
          1,
          10000
        );
        camera.position.z = 300;
        camera.lookAt(scene.position);
      }

      function cardAssets() {
        
        var geometryA = new Plane(geometryA);
        var geometryB = new Plane(geometryB);
        geometryB.applyMatrix();

        var materialA = new Material(materialA, "a-crime-to-design.jpg");
        var materialB = new Material(materialB, "bg-crime.jpg");

        var cardA = new Card(cardA, -120, 0, 0);
        var cardB = new Card(cardB, -120, 0, 0);

        var meshA = new Mesh(meshA, geometryA, materialA, cardA);
        var meshB = new Mesh(meshB, geometryB, materialB, cardB); 


      } // end cardAssets

      function animate() {
          requestAnimationFrame(animate); //asking animation frames
          renderer.render(scene, camera); //rendering
        }

Other than a missing favicon, the console isn't showing any errors.

Answer №1

When creating a new instance of THREE.Mesh(...), there appears to be an error. It seems that in your Mesh class, the properties this.geometry and this.material are not defined anywhere. As a result, when you instantiate a new THREE.Mesh, you are passing in undefined for both properties.

var meshA = new Mesh(meshA, geometryA, materialA, cardA);
class Mesh {
  constructor(addMesh, geometry, material, linkCard) {
    this.addMesh = new THREE.Mesh(this.geometry, this.material);
    linkCard.cardMesh(this.addMesh);
  }
}

Furthermore, when using geometryA and materialA, you are passing instances of your custom Plane and Material classes as arguments. However, the THREE.Mesh constructor only accepts instances of THREE.BufferGeometry and THREE.Material. To resolve this issue, you should pass in the appropriate properties like geometryFront and material:

new THREE.Mesh(geometry.geometryFront, material.material);

Overall, the structure of your code seems unconventional. It is unclear why you are passing in the variable within the constructor, especially since it is still undefined at that point and not utilized anywhere within the constructor method.

var meshA = new Mesh(meshA, ...

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

Troubleshooting problem with list rendering in a Nativescript-vue GridLayout

As a newcomer to nativescript, I am utilizing GridLayout in an attempt to optimize layout nesting for better performance. In my template, I have an image followed by a vertical list of four items next to it. However, only the first item on the list is visi ...

CSS classes designed to mimic JavaScript object attribute-value pairs

I stumbled upon some interesting css class-value combinations within HTML tags. It seems like a specific JavaScript code is interpreting this, but it's something I haven't encountered before. I came across this on www.woothemes.com/flexslider/ (y ...

Once the AJAX callback is complete, the onblur event will revert back to the original field or the updated field

I am currently working with an AJAX callback: Here is the HTML snippet: <a onCLick="loadXMLDoc(id1,id2)">(Add)</a> This function triggers an AJAX method that replaces the "(Add)" text with a basic HTML input field. Subsequently, when there ...

Localizing strings that are not saved in a database

Our web app will soon support multiple languages, a new feature we are excited to roll out! Currently, we utilize Handlebars for front-end templating and Node + Jade for back-end templating. As we prepare to implement language support, we're conside ...

The fonts in node.js are not functioning as expected, without displaying any error messages

I'm having trouble implementing custom fonts on my website. Despite following the correct file structure, the fonts do not seem to be loading. My project files are organized in the following manner: https://gyazo.com/5ee766f030290e5b2fa42320cc39f10b ...

Any ideas for handling ProtractorJS timeouts while clicking an element?

The Issue at Hand I am currently facing a challenge with clicking a straightforward 'New Booking' button in my Angular 5 Material 2 Application. The code snippet for the button is as follows: <button _ngcontent-c9="" class="mat-menu-item" ma ...

Dynamically inserting templates into directives

I've been attempting to dynamically add a template within my Angular directive. Following the guidance in this answer, I utilized the link function to compile the variable into an HTML element. However, despite my efforts, I haven't been success ...

Under what circumstances would you need to manually specify the displayName of a React component?

After coming across an article (linked here: https://medium.com/@stevemao/do-not-use-anonymous-functions-to-construct-react-functional-components-c5408ec8f4c7) discussing the drawbacks of creating React components with arrow functions, particularly regardi ...

Steps for launching a browser using Capybara and Selenium

I am completely new to utilizing Capybara and have not had any experience with Selenium in the past. Currently, I am working on a Ruby on Rails project on MacOSX and encountering an issue where the browser window does not open when I execute my test. My te ...

retrieve data types from an array of object values

I am looking to extract types from an array of objects. const names = [ { name: 'Bob' }, { name: 'Jane' }, { name: 'John' }, { name: 'Mike' }, ] The desired result should resemble thi ...

Changing the color of a highlighted face on a PlaneGeometry in ThreeJS

I am trying to figure out how to change the color of a specific face when hovering over it in my PlaneGeometry, but I am having trouble getting the selected face. Below is the code I have so far: //THREE.WebGLRenderer 69 // Creating a plane var geometr ...

Caution: Exercise caution when rendering components in React due to unstable_flushDiscreteUpdates

Trying to utilize map to render a component, but encountering a warning: Warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering. MyBooks.js import React, { useState, useEffect } from 'react'; import Action ...

AngularJS is incapable of detaching forms from objects

Creating forms dynamically using ng-repeat and adding them to the vm.forms object is causing issues. When an item is removed from the array, the corresponding form is deleted but the key in the vm.forms object remains, leading to undefined errors. angul ...

Unlocking the power of localhost on your mobile device

Currently, I am working on a web application that utilizes Laravel for APIs and React for the frontend. My goal is to test this application on a mobile device. While testing React in isolation works fine, I am encountering issues with backend data such a ...

What is the best way to showcase my subscription form on a web browser?

I am encountering an issue with my code. My goal is to generate a subscribe form modal. Although I have incorporated the subscribe form in my HTML document, upon running the code in the browser, the subscribe section is not visible. Can anyone offer assist ...

Error message: Please provide an expression with const in React JS component

Can you assist me with this issue? I am trying to determine if the user is registered. If they are registered, I want to display the home URL, and if they are not registered, I want to display the registration URL. To do this, I am checking the saved dat ...

What is the best way to fetch Ajax information for use in several drop-down menus?

Is there a way to populate one multiple select drop-down based on the selection made in another multiple select drop-down using jQuery or ajax? Here is an example of how I am attempting to achieve this using ajax: function demo1() { var emp_id = docu ...

Guide to verifying a Django form with AngularJs

I have a very simple form with 1 field and a submit button. I want to implement validation such that the submit button is disabled when the field is empty and enabled when it is not. I am trying to achieve this dynamically using AngularJs but seem to be mi ...

Ways to verify if both the select and input fields are selected and populated with jQuery

In my html form, I have a select field and an input box with two classes being used: work_phone_class and work_phone_class_input. I am triggering ajax by clicking a save button with a specific class whenever the value in the select field is changed or any ...

Switching between different CSS files based on the URL using jQuery or another method

Is it feasible to apply specific styles based on the ID or load various CSS files depending on the URL you are visiting? For example: <script> if(location.href == 'http://jpftest2.tumblr.com/about'){ document.write('<style type= ...