Three.js Troubleshooting: Texture Loading Issue

Having trouble retrieving a texture saved in a folder? Here's the error message I'm encountering:
"GET file:///E:/Html/Expo%20Sites/Good%20Site/tex/dirt.jpg net::ERR_FILE_NOT_FOUND"

I've followed the steps to get the texture, store it in a variable, create the geometry and material, then assign the material to the object. However, as a beginner with Three.js library, I might be overlooking something obvious. Take a look at the code below.

var mousePos = {x: 0.0, y: 0.0};
var windowCenterX = window.innerWidth / 2;
var windowCenterY = window.innerHeight / 2;  
var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapenabled = true;
renderer.shadowMapType = THREE.PCFSoftShadowMap;

document.body.appendChild(renderer.domElement);

var dirtTex = new THREE.ImageUtils.loadTexture('tex/dirt.jpg');

var geometry = new THREE.BoxGeometry( 1, 1, 1);
var floor = new THREE.BoxGeometry(10, 1, 10);

var material = new THREE.MeshLambertMaterial({map: dirtTex});

var cube = new THREE.Mesh(geometry, material);

var floor = new THREE.Mesh(floor, material);

var directionalLight = new THREE.DirectionalLight(0xCCFFCC, 1.0);

var hemiLight = new THREE.HemisphereLight(0xCCFFCC, 0xCCFFCC, 0.6); 

cube.position.z = -5;
cube.castShadow = true;
cube.recieveShadow = true;
scene.add(cube);

floor.position.z = -5;
floor.position.y = -3;
floor.castShadow = true;
floor.recieveShadow = true;
scene.add(floor);

directionalLight.position.set(0, 1, 0);
directionalLight.shadowDarkness = 1.0;
directionalLight.castShadow = true;
directionalLight.shadowCameraVisible = true;
directionalLight.shadowCameraRight =  5;
directionalLight.shadowCameraLeft = -5;
directionalLight.shadowCameraTop =  5;
directionalLight.shadowCameraBottom = -5;
scene.add(directionalLight);

hemiLight.castShadow = true;
scene.add(hemiLight);

function Update()
{
    requestAnimationFrame(Update);

    if(mousePos.x == null || 0)
        mousePos.x = 1;
    if(mousePos.y == null || 0)
        mousePos.y = 1;

    cube.rotation.x = mousePos.y / 500;
    cube.rotation.y = mousePos.x / 500;

    renderer.render(scene, camera);
}
Update();

document.onmousemove = function (e)
{
    if(e.pageX != null)
        mousePos.x = e.pageX;
    if(e.pageY != null)
        mousePos.y = e.pageY;
    mousePos.x = (mousePos.x - windowCenterX);
    mousePos.y = (mousePos.y - windowCenterY);
}

Answer №1

How to access local files

When attempting to load models or textures from external sources, the "same origin policy" security measures in browsers can prevent file system loading and result in a security exception.

To circumvent this issue, there are two recommended solutions:

1) Adjust browser settings for local file access permissions

2) Utilize a local server to run the files

Answer №2

If you want to elaborate on Player's response, the next step would be to download a local server, launch it, and then access the server through your browser.

For those who may be unsure of how to do this, you can set up a local server by downloading Node.js - http://nodejs.org/download/.

Once you have installed Node.js, navigate to your project directory in the command line and enter the following:

npm install http-server -g

To start the server, run:

http-server

Next, visit the default local page:

http://localhost:8080/

You should now see your project displayed on the local server.

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

Assigning automatic roles based on the Discord.js v12 presence status of users

When someone is playing, I want to add a role using the presenceUpdate event However, every time I attempt this in discord.js v12, I encounter the following error: TypeError: Cannot read property 'activities' of undefined What mistake am I maki ...

Generate a dynamic jqplot chart using data retrieved from a variable and JSON data

I'm struggling with loading data into a jqplot chart via variables as it only displays the first value. I am puzzled as to why this is happening. JavaScript: $(document).ready(function () { var sin = [[20, 10, 0, 10, 15, 25, 35, 50, 48, ...

Mastering the art of iterating through arrays using node.js request()

After transitioning from passing single values to multiple values in my node API, I encountered an issue where the API no longer responded. Here is an example of single values for fields: tracking: "123", // Only one tracking number carrier: "usps" // On ...

Set a string data type as the output stream in C++

I have a method called prettyPrintRaw that seems to format the output in a visually appealing manner. However, I am having trouble understanding what this specific code does and what kind of data it returns. How can I assign the return value of this code t ...

How can we arrange a two-dimensional array in descending order of string length using the first string in the sub-array as the basis for

If I have an array with elements like these: var array = [["This should be last", 1], ["This should be first I think", 1], ["This is the middle one", 1]]; The second value in each sub-array, which is always 1 in this case, doesn ...

Error: The property 'match' is undefined and cannot be read by Object.j during rendering in angular-datatables.min.js at line 6

I am currently using angular-datatables.min.js for my data table, but I have encountered an error that I have been unable to resolve. Is there anyone who can provide assistance with this issue? App.controller('DailyTaskListController', ['$s ...

Can you explain how differential inheritance works in JavaScript?

This response to a question about the Object.create() method in JavaScript on SO discusses the concept of differential inheritance. The explanation given is as follows: This particular technique enables you to easily establish differential inheritance, ...

In a Next.js project, Typescript seems to be overlooking errors related to proptype definitions and function types

Greetings everyone! I am currently working on a project using TypeScript and have implemented various rules and elements. However, I am facing issues with type errors for functions and props. Essentially, when using any function, it is necessary to specify ...

The issue with updating values in the jQuery UI datepicker persists

When using the jquery datepicker ui, you may notice that the value attributes of the associated html fields do not update immediately. For example: http://jsfiddle.net/4tXP4/ Check out this link for more details: http://jqueryui.com/demos/datepicker/al ...

Using an Angular variable in a JavaScript function may seem challenging at first, but with

Currently, I have an Angular component that utilizes a JavaScript library (FullCalendar V4). The library triggers a function when certain events occur (such as mouseover or click) and provides an argument that allows me to access the calendar values (start ...

Leverage the power of React-PDF to smoothly magnify or minimize your

I am working on a ReactJS project where I want to implement zoom in/zoom out functionality. To display PDF files, I am utilizing the react-pdf package in my web application. According to the documentation, there is a scale prop that allows me to adjust the ...

How can I dismiss a popup confirmation in React?

The confirmation popup should close when clicking anywhere outside of it. However, the popup does not disappear as expected when the outside area is clicked. Additionally, I am getting the error message "Line 2:8: 'Backdrop' is defined but never ...

Tips for utilizing XMLHttpRequest to instruct a website to take action

Although I have no prior experience with Ajax, I am in need of some guidance to create a simple Chrome extension. Despite searching online, I have not been able to find much information on this topic, which I believe should be straightforward. Request URL ...

Incorporating a feature that displays one show at a time and includes a sliding animation into an

Hey there! I have this show/hide script on my website that's been working well. However, there are a couple of things I need help with. I want to modify the script so that only one div can be shown at a time. When a div appears, I'd love for it ...

Create a new JSON file to preserve the value of a JSON object

I am working with a JSON file that looks like this: { "soils": [{ "mukey": "658854", "mukeyName": "Meggett-Kenansville-Garcon-Eunola-Blanton-Bigbee (s1517)", "sl_source": "Fl soil map", "cokey": "3035468", "soilName": "Eunola", " ...

Learning the basics of JavaScript: Displaying the last number in an array and restarting a function

Hey there, I'm diving into the world of JavaScript and have set myself a challenge to create a simple bingo number machine. Check out my CodePen project here. I've successfully generated an array of 20 numbers, shuffled them, and added a marker t ...

Is it possible to modify the stroke color of the progress circle in ng-zorro?

I am working on an Angular project where I aim to create a dashboard displaying various progress circles. Depending on the progress, I need to change the color of the line. Current appearance: https://i.sstatic.net/hR2zZ.png Desired appearance: https://i. ...

Error encountered while uploading image on django through jquery and ajax

Looking for help on posting a cropped image via Jquery and ajax. I've been trying to implement a solution from a question on cropping images using coordinates, but I'm facing issues with receiving the image on Django's end. The CSRF token an ...

Using the useState hook with an array of objects is not functioning as intended

I have initialized a useState object in my file like this: const [comments, setComments] = useState({ step_up: [], toe_walking: [], toe_touches: [], squat: [], side_to_side: [], rolling: [], leg_lifts: [], hand_to_knees: [], floo ...

Is there a way to ensure that the vertical scrollbar remains visible within the viewport even when the horizontal content is overflowing?

On my webpage, I have multiple lists with separate headers at the top. The number of lists is dynamic and can overflow horizontally. When scrolling horizontally (X-scrolling), the entire page moves. However, when scrolling vertically within the lists, I wa ...