The glTF file lacks visible content

My goal is to showcase a glTF file using Three.js. To bypass CORS policy, I have opted for a local server called Servez. While everything runs smoothly on Mozilla Firefox without errors, the content does not appear. However, when attempting the same on Chrome, the message "Not allowed to load local resource:" pops up. I even tried using the Chrome Web Server extension, but the outcome remains unchanged across both browsers.

PS- I understand that this issue is related to security concerns with Chrome, but I prefer not to tamper with any security configurations.

<!DOCTYPE html>
<html>
  <head>
    <meta charset=UTF-8>
  </head>
  <body>
    <script type="module" src="https://threejs.org/build/three.js"></script>

    </script>

    <script type="module">

    import {GLTFLoader} from 'C:/Users/Prinzu/three.js-master/examples/jsm/loaders/GLTFLoader.js';
    import { OrbitControls } from 'C:/Users/Prinzu/three.js-master/examples/jsm/controls/OrbitControls.js';

      let scene, camera, renderer;

      function init() {
       scene = new THREE.Scene();
       scene.background = new THREE.Color(0xdddddd);
       camera = new THREE.PerspectiveCamera(40,window.innerWidth/window.innerHeight,1,5000);
       camera.rotation.y = 45/180*Math.PI;
       camera.position.x = 800;
       camera.position.y = 100;
       camera.position.z = 1000;
       controls = new THREE.OrbitControls(camera);
       controls.addEventListener('change', renderer);
       hlight = new THREE.AmbientLight (0x404040,100);
       scene.add(hlight);
       directionalLight = new THREE.DirectionalLight(0xffffff,100);
       directionalLight.position.set(0,1,0);
       directionalLight.castShadow = true;
       scene.add(directionalLight);
       light = new THREE.PointLight(0xc4c4c4,10);
       light.position.set(0,300,500);
       scene.add(light);
       light2 = new THREE.PointLight(0xc4c4c4,10);
       light2.position.set(500,100,0);
       scene.add(light2);
       light3 = new THREE.PointLight(0xc4c4c4,10);
       light3.position.set(0,100,-500);
       scene.add(light3);
       light4 = new THREE.PointLight(0xc4c4c4,10);
       light4.position.set(-500,300,500);
       scene.add(light4);
       renderer = new THREE.WebGLRenderer({antialias:true});
       renderer.setSize(window.innerWidth,window.innerHeight);
       document.body.appendChild(renderer.domElement);
       let loader = new THREE.GLTFLoader();
       loader.load('scene.gltf', function(gltf){
         car = gltf.scene.children[0];
         car.scale.set(0.5,0.5,0.5);
         scene.add(gltf.scene);
         animate();
       });
     }
     function animate() {
       renderer.render(scene,camera);
       requestAnimationFrame(animate);
     }
     init();
    </script>
  </body>
</html>

Answer №1

your paths are scattered everywhere

this

<script type="module" src="https://threejs.org/build/three.js"></script>
<script type="module">

import {GLTFLoader} from 'C:/Users/Prinzu/three.js-master/examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from 'C:/Users/Prinzu/three.js-master/examples/jsm/controls/OrbitControls.js';

should be something similar to this

<script type="module>
import * as THREE from './three.js-master/build/three.module.js';
import {GLTFLoader} from './three.js-master/examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from './three.js-master/examples/jsm/controls/OrbitControls.js';

Notice that now there is only one script tag. It's loading three.module.js instead of three.js and it's being loaded with import.

Then organize your files in a structure like this

+-somefolder
  +-yourpage.html
  +-scene.gltf
  +-scene.bin
  +-three.js-master
    +-build
    | +-three.module.js
    +-examples
      +-jsm
        +-controls
        | +-OrbitControls.js
        +-loaders
          +-GLTFLoader.js

In summary, place all your project files under somefolder. Then host somefolder and access yourpage.html

Also, instead of new THREE.OrbitControls(... and new THREE.GLTFLoader(..., use new OrbitControls(... and new GLTFLoader(.... When imported, they are directly available, not under the THREE object.

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

I am encountering difficulties with hosting a project that was created using Next.js

I am currently working on a Next.js project and I encountered some version compatibility issues when integrating MUI. While my project runs smoothly in localhost, it fails to work when deployed in hosting. I attempted to resolve this by using the --legacy ...

Is it better to modify attributes during the ng-repeat cycle or after the cycle ends?

Consider a scenario where a simple directive is defined as follows: app.directive('seo', function() { return { template: '<meta ng-repeat="tag in data" {{tag.attribute}}="{{tag.name}}" content="{{tag.content}}" />', ...

Displaying different elements using an anchor <a> tag

Within this example, I've created two functional components that mimic separate pages with differently colored <div> boxes. const Home = () => <div style={{background:'red', height: 100, width: 100}}></div>; const ...

Adding the location of the onClick event to the hook - a step-by-step guide

Here is the code I am working with: import { MapContainer, TileLayer } from "react-leaflet"; import React, { useState } from 'react'; export default function App() { const [positionLat, setPositionLat] = useState(null); ...

Displaying gratitude message using AJAX and executing PHP script

I've created a form where I want the "thank you" message to be displayed after submission and also run a PHP script to insert data into the database. However, currently, although the values are being passed correctly via the URL, the upis.php script i ...

Encountered a network error: A rogue token < was found in JSON at position 0 within a new Apollo

https://i.sstatic.net/D25ai.png const httpLink = createHttpLink({ uri: 'http://localhost:3090/' }) const client = new ApolloClient({ link: httpLink, cache: new InMemoryCache() }) client.query({ query: gql` query users { ...

Guide on how to dynamically add AJAX JSON array response to an HTML table

Hey! I need some advice on how to dynamically append a JSON Array response to an HTML table after submitting a form using AJAX. Here's the scenario: This is my form : <form id="myForm" method="POST"> <input type=" ...

Table borders not displaying properly in Chrome when cells are hidden

I'm dealing with a peculiar issue regarding the display of borders in Chrome when individual cells are hidden within an HTML table. The borders disappear, despite being defined for the row and table elements. Does anyone have experience with this spec ...

Are you interested in using jQuery and/or AJAX to retrieve the latest images from a website?

I had to utilize a Wikipedia API in order to retrieve images from the New Jersey website, and I devised two methods to carry out similar tasks. The initial approach involved using JSON, but it ended up pulling the entire page content. <script type="tex ...

What causes the variance in behavior between the Angular-formly directive and type?

I am facing an issue with two input fields that are both generated using the same template. I have set the required attribute to true for both of them by using the following code snippet: ... templateOptions: { ... required: true } One input fiel ...

Is it possible to alter CSS based on the width of the webpage using javascript

We are currently developing a web application that will be deployed on various devices such as mobile phones, iPads, iPhones, and Android. Instead of using user agents to show different views, I prefer having CSS that adjusts based on the screen width. We ...

Attempting to upload an item using ThreeJs

Can someone assist me with loading an object file from my local browser in Threejs ( Rev 71)? I keep encountering an error that says loadModel.html:1 Uncaught SyntaxError: Unexpected token #. Even after trying to load the object file using chrome --allow- ...

Instructions for sketching a square at predetermined coordinates

Looking for a simple function to draw a box at specific coordinates x and y. I've searched for guides, but they all seem to provide excessive or insufficient information. Thanks in advance! ...

Display a modal before leaving the page?

I need to display a modal message when a user navigates away from the page, triggered by a successful AJAX call. The issue is that if the message loads too quickly, the user may not have enough time to read it before being directed to another page. This is ...

React is unable to access and retrieve data from a web API

Currently, I have a controller set up in web-api for my project. I am using React v18.2.0 and NextJS v13.3.0 to work with this setup: public List<ReturnDat> Get() { List<ReturnDat> lst = new List<ReturnDat>(); lst.Add(new ReturnD ...

Adjusting body styles in a NextJS application using localStorage prior to hydration: A step-by-step guide

If you visit the React website and toggle the theme, your choice will be saved in localStorage. Upon refreshing the page, the theme remains persistent. In my NextJS project, I am attempting to achieve a similar outcome by applying styles to the body eleme ...

Expand the clickable area of the checkbox

Is it possible to make clicking on an empty space inside a table column (td) with checkboxes in it check or uncheck the checkbox? I am limited to using only that specific td, and cannot set event handlers to the surrounding tr or other tds. The functional ...

Total number of goals scored by a single team (extracted from JSON data)

My data consists of football games in a JSON file [ { "game_id":"258716", "game_date_start":"2016-08-15", "season": "2016", "team_1_id":"119", "team_2_id":"120", "team_1_goals_quantity":"2", "team_2_goals ...

Error: Accessing Undefined Property in Node-SQLite

I recently started developing a REST API for garden-related data collected by an Arduino using node-sqlite3. Basic queries like retrieving all the database information are working fine. However, I am facing challenges when trying to retrieve data based on ...

Turning a string array into a basic array can be achieved through a few simple steps

While I am aware that this question has been posed multiple times, my scenario is slightly unique. Despite exhausting numerous methods, I have yet to discover a suitable workaround. $array = ["9","8","7","6","5"]; //result of javascript JSON.stringify() ...