Error message "The camera provided is invalid and not an instance of THREE.Camera" appears when attempting to render a cube using Three.js

I've been working on a small "engine" project using three.js to easily create and position objects. So far, I have set up the scene, renderer, and camera successfully. However, when attempting to render and attach a cube to my scene, I encounter an issue.

Creating and adding the cube to the scene works fine, but I face difficulties when utilizing the render() function in three.js. The error message I receive states:

THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.

This error has left me puzzled as I couldn't find much information online. I'm hoping someone here might be able to provide guidance.

During troubleshooting, I attempted to output the cube object to the console for insights. It appears that the cube attaches to the scene properly, with both the scene and camera serving as parent objects to the cube.

Below is the relevant code snippet:

function spnScene(alias, fov, x, y, z) {
    window.spnCreateScene = new THREE.Scene();
    var WIDTH = window.innerWidth, HEIGHT = window.innerHeight;

    if (alias == false) {
        console.warn('spnRenderer scene antialias set to ' + alias + ". Please check your code to make sure it's correct.");
    }

    spnRenderer = new THREE.WebGLRenderer({
        antialias: alias
    });
    spnRenderer.setSize(WIDTH, HEIGHT);
    document.body.appendChild(spnRenderer.domElement);

    // create camera

    spnCamera = new THREE.PerspectiveCamera(fov, WIDTH / HEIGHT, 2000);
    spnCamera.position.set(x, y, z);
    spnCreateScene.add(spnCamera);
}

function spnBasicCube(material, clr, l, w, depth, x, y, z) { // draw a basic cube and render it
    if (material == "basic") {
        var spnBasicCube = new THREE.Mesh(new THREE.CubeGeometry(l, w, depth), new THREE.MeshBasicMaterial({color: clr}));
        spnBasicCube.position.x = x;
        spnBasicCube.position.y = y;
        spnBasicCube.position.z = z;
        spnCreateScene.add(spnBasicCube);

        spnRenderer.render(spnBasicCube);

        console.log(spnBasicCube);

        // this should end up making a cube but throws an error rn
    }
}

I am looking for assistance in resolving this issue! Thank you in advance! :)

Answer №1

Instead of displaying the cube

spnRenderer.render(spnBasicCube);

Show the scene through the camera:

spnRenderer.render(spnCreateScene, spnCamera);

Refer to the documentation THREE.WebGLRenderer.render:

.render ( scene : Scene, camera : Camera ) : null

Display a scene using a specific camera view.

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

The Ajax POST request encounters failure, although it functions properly in the console

The ajax function below is encountering an error message with little information, simply stating "error": var form = formInfo; var url = $(formInfo).attr("action"); var data = $(formInfo).serialize(); $.ajax({ type: "post", url: ur ...

Angular does not seem to be triggering $watch for changes in arrays

Trying to activate a $watch function after a delay in Angular: Controller Information .controller('MyCtrl', ['$scope', function ($scope) { $scope.chickens = ["Jim", "Joe", "Fred"]; $scope.chickens.push("Steve"); setTimeou ...

Syntax for retrieving response with jQuery AJAX

Currently, I am in the process of working on an AJAX request that triggers a URL when a specific button is clicked. The fundamental aspects are running smoothly, ensuring that the GET request is activated towards the URL upon button click. I have also cros ...

What are the steps to globalize the ng-bootstrap datepicker?

For my current project, I am utilizing the ng-bootstrap datePicker component. The demo for my simple datePicker widget can be found here. However, I am now seeking to internationalize it by setting it to use the Russian language. Any assistance with this ...

Using gmaps4rails: A guide on extracting JSON data from the controller

I have a model named shop and I want to create a simple alert box using JavaScript that shows the name of the shop when a marker on the map is clicked. Here's my code: # controller @json = Shop.all.to_gmaps4rails do |shop, marker| marker.json({ id ...

What could be causing the linter in vue js to not properly lint the template?

I'm struggling to get the linter to properly lint the template section of my .vue files. Any suggestions on how I can configure this? Basically, I want the linter to format something like this: <template> <v-container> <h1>Ho ...

Update image attributes (such as src, alt, etc.) after a successful AJAX request (and also help me troubleshoot my AJAX

The image link I am currently using is: echo "<br/><div class='right' id='post" . $id . "'> <img id='thumb' src='/images/like.png' title='Like it?' alt='Like button' onC ...

React Select only enabling single selection at a time

Currently, I am in the process of updating my react app to version 16.8 and also updating all associated libraries. One issue that has come up is with two drop-down selects from Material UI. Since the update, the multi-select option no longer allows multip ...

Issue: The initiation function fails to start data

I have created an app using Ionic framework that displays articles. When a single article is opened, I use the init function in the UserService to initialize user data: angular.module('coop.services') .factory('UserService', function( ...

A Complication Arises with Browser Functionality When Embedding an Iframe within

I am experiencing a peculiar problem while embedding an iframe with a specific src inside an absolutely positioned div. Here's the basic structure of the webpage: .container { position: relative; overflow: hidden; width: 100%; heigh ...

Updating the id token in VueJs using Axios interceptor when it expires

I need to implement an axios interceptor that will add the idToken as an authorization header to every axios call, and also refresh the idToken if it has expired before making any call. My current code for this task is as follows: axios.interceptors.requ ...

Using AJAX to load a PHP file in CodeIgniter is a great way to

I'm a beginner in the world of web development and I need help with loading my PHP file containing HTML content to update the span element. My framework of choice is CodeIgniter. Every time I try, I encounter an error. Below is my JavaScript code: ...

How can JavaScript allow access to files outside of a web server environment? Could .htaccess provide a

Currently, I'm facing a challenge with my local server on Mac 10.8. I am trying to serve files such as videos and images from an external hard drive. The path for the external hard drive is HD: /Volumes/ while the web server's path is /Library/Se ...

Interacting Shadows with BufferGeometry in react-three-fiber

I've been working on my custom bufferGeometry in react-three-fiber, but I can't seem to get any shadows to show up. All the vertices, normals, and UVs are set correctly in my bufferGeometry, and I even tried adding indices to faces, but that just ...

Arrange objects in an array according to the order specified in another array

Here is my array of car makes: const makes = [ {id: "4", name: "Audi"}, {id: "5", name: "Bmw"}, {id: "6", name: "Porsche"}, {id: "31", name: "Seat"}, {id: "32", name: "Skoda"}, {id: "36", name: "Toyota"}, {id: "38", name: "Volkswagen"} ] Now, I want to o ...

Automatically formatting text upon entering it in Vue.js

I need assistance with auto-formatting the postal code entered by the user. The rule for the postal code is to use the format A0A 0A0 or 12345. If the user inputs a code like L9V0C7, it should automatically reformat to L9V 0C7. However, if the postal code ...

Navigating using Javascript library in Angular 2 framework

I am currently utilizing Parse, an external JS library, within Angular JS 2. Nevertheless, I am encountering issues when attempting to call the function gotoMain() from within a callback function of Parse. It appears that certain elements are not being l ...

Tips for creating a single div element on a React form

I have a form that is generated using the array map method in React. I am trying to add either 'box-one' or 'box-two' div element when clicking on the add button, but currently both div elements are being added. How can I ensure that on ...

Tips for styling Ajax.ActionLink elements with CSS

Hi there, I'm trying to style a navigation bar using CSS. I'm pulling the navigation bar values from a database using Ajax.ActionLink. I attempted to use JavaScript but encountered an error stating "jQuery is not defined", Here's the cod ...

Tips for updating multiple fields in Prisma ORM

Is there a way to upsert multiple fields in Prisma ORM using just one query? I'm looking for a solution that allows me to upsert all fields at once, without having to do it individually. Is this possible? ...