Navigating in Three.js as a First PersonIn this guide

Hello there! Greetings to everyone :-)

I apologize if my question isn't very interesting. I recently started coding, so I believe my issue is quite basic. I created a 3D city using Three.js with the help of Isaac Sukin's book titled "Game Development with Three.js".

The creation part was simple. But when I attempted to add movement options, I couldn't succeed. Here is the code that is working:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <style>
      body {
       background-color: #ffffff;
       margin: 0;
       overflow: hidden;
      }
     </style>
    </head>
    <body> 
      <script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r57/three.min.js">      
        </script>
      <script src="FirstPersonControls.js"></script>
      <script> 


  var camera, scene, renderer, light;
  var clock, controls;

  function setup() {
    document.body.style.backgroundColor = '#00BFFF';
    setupThreeJS();
    setupWorld();

    requestAnimationFrame(function animate() {
    renderer.shadowMapEnabled = true;
    renderer.shadowMapSoft = true;
    renderer.render(scene, camera);
    controls.update(clock.getDelta());
    requestAnimationFrame(animate);
      });
   }

  function setupThreeJS() { 
    // Code for setting up Three.js environment
   }

    function setupWorld() {
     // Code for setting up the 3D world
     }

     setup();  

      </script>
    </body>
  </html>

Displayed below is a screenshot with the directory containing my firstattempt.html and FirstPersonControls.js files.

Files Directory

The FirstPersonControls.js file was obtained from three.js/examples/js on github dot com.

The problem arises when I view my city in the browser as it "loses" its texture (as seen in the initial screenshot) and keyboard movements do not work. Additionally, I encountered this error in the Developer tools:

Error

I would greatly appreciate any assistance. I suspect the issue may lie in how I am using the FirstPersonControls.js file? Please forgive my limited knowledge of JavaScript :-(

Answer №1

Possibly initialize the camera before assigning it to controls

At this point, the camera is undefined type, so even if you assign it to controls and then make modifications, the controls won't have a direct reference to it.

controls = new THREE.FirstPersonControls(camera);

Ensure to declare the camera before assigning it like this...

camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
controls = new THREE.FirstPersonControls(camera);

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

Is it necessary for me to indicate that I need the password from results[0] when the MySQL query only returns one result?

When querying the database and receiving only one row/result, make sure to reference the password column correctly. Use this code to ensure you are getting the correct value: var hash = results[0].password; If you try to use var hash = results.password; ...

Scraping dynamic content with Python for websites using JavaScript-generated elements

Seeking assistance in using Python3 to fetch the Bibtex citation produced by . The URLs follow a predictable pattern, enabling the script to determine the URL without requiring interaction with the webpage. Attempts have been made using Selenium, bs4, amon ...

JSP - Submitting a form through a link click: A complete guide

I am having an issue with submitting a form when clicking on a link. Despite my attempts, the default action is not being prevented and I keep getting the error message: HTTP Status 405 - Request method 'POST' not supported. Here's what I ha ...

Difficulty encountered in displaying HTML within a React component

Struggling to display HTML in my React code, whenever I click 'signup' after starting the page, it shows the 'login' view instead. Could there be an issue with how I'm linking everything together? App.js class App extends Compon ...

Optimal method for retrieving data from a JSON object using the object's ID with a map

Can you teach me how to locate a json object in JavaScript? Here is a sample Json: { "Employees" : [ { "userId":"rirani", "jobTitleName":"Developer", "preferredFullName":"Romin Irani", "employeeCode":"E1", "region":"CA", "phoneNumber":"408-1234567", " ...

Accessing a file's source using the Box.net API and downloading the file contents

Recently, I've been busy working on a Web Application project (for fun) that focuses on editing files stored in the cloud. I'm utilizing the box.net API for this task, but I've come across a challenge - obtaining the source code of files. Un ...

What is the method for applying the action (hide) to every table cell that doesn't include a specific string in its ID?

I have a table with cells containing unique IDs such as "2012-01-01_841241" that include a date and a number. My goal is to filter the table to only display three specific numbers by sending a request and receiving those numbers. Is there a more efficien ...

Guide: Transform Bootstrap 4 Inline Checkboxes into Stacked Format When Screen Size Is Altered

The new layout of checkboxes and radio buttons in Bootstrap 4 really caught my attention. However, I am looking for a way to switch between "stack" and "inline" based on the screen size. Although size tagging has been added for various elements, I haven&ap ...

Enhancing jquery datatable functionality with data-* attributes

I successfully added an id to each row of my data table using the rowId property, as outlined in the documentation. $('#myTable').DataTable( { ajax: '/api/staff', rowId: 'staffId' } ); Now I am wondering how I can ad ...

Is it possible to automatically update views immediately after performing a CRUD operation?

In my basic CRUD application, I am facing an issue when making an AJAX call to delete a document. I want the view to be updated with the new list of documents immediately after deletion. I have experimented with using ajaxStop / ajaxSuccess and setTimeout ...

Why does the MEAN Stack continue to route using the '#' symbol in the URL?

Currently navigating the realm of back end development. Started off by following a guide on express from thinkster. In need of some clarification. Initially, I grasped that front-end and back-end routing serve different purposes. Front-end routing relates ...

Convert the color hex codes to JSON format without the use of quotation marks

Currently, I am populating a JavaScript array named "data" with values. This array contains two elements: value and color, formatted like this: var data = [{value:226,color:"#FFFFF"},{value:257,color:"#FFFFF"}]; The issue is that the color should be repr ...

Tips for specifying which project starts the setup process in Playwright

I have multiple projects in my playwright.config file, all of which have the same setup project as a dependency. Is there a way to determine at runtime which parent project is initiating the setup process? playwright.config projects: [ { name: " ...

Tips for aligning an image of varying dimensions in the center

Although I've done some research and attempted to splice together code from various sources, including here, here, and other places, I'm still struggling with centering an image on a webpage. The page in question features three layered .png imag ...

The latest alpha version of Angular2 Material Design (alpha.9-3) encountered a "404 not found" error when trying to access @angular

After carefully following the steps outlined in the angular material2 Getting Started guide to install @angular/material, I made updates to package.json, app.module, and systemjs.config using Atom. Specifically, I added the line '@angular/material&apo ...

Can an AJAX upload progress bar be implemented in Internet Explorer without using Flash?

I've been searching for solutions to upload files without using flash, but all of them either require flash or lack a progress bar on IE (7-8). I couldn't find any mention of an "progress" event in the MSDN documentation for XMLHTTPRequest. Is i ...

Steps for accessing a desktop folder using ElectronJS

Need assistance with a task involving opening data from the back-end in electron and displaying it as a desktop folder. https://i.sstatic.net/tCoHF.png Feeling a bit lost on how to accomplish this. For instance, I receive data like {id:1, foldername: &a ...

The ngModel controller did not trigger the $$updateEventHandler function

Currently in the process of transitioning the development environment from gulp to webpack for a hybrid app that does not use AngularJS and React. The application is quite large, currently consisting of 10mb worth of files. However, I have encountered an ...

Issues with implementing asynchronous calls within streams utilizing node.js

I've encountered an issue with async calls in my streams. It seems that when the middle stream makes an asynchronous call, the final stream does not receive the 'end' event. I was able to replicate this behavior using simple through streams ...

A guide on utilizing the React Suite range slider in rsuite

Hello there, I recently started working with the UI framework rsuite (React suite) and everything was going smoothly until I tried to use the Range slider component's API. Unfortunately, I am facing some issues with the labels and tooltips not display ...