New to threejs.
Followed the Option 1 install guide at threejs.org
Configuration
Ubuntu 22.04
Nodejs 21.1
Vite
textureLoader does not seem to work and breaks the scene. If I comment out the textureLoader code block like below, and go with a simple MeshBasicMaterial, the scene renders. The moon texture ‘moon_1024’ is in the same folder as the HTML and JS file for the scene.
--------------snippet-----------------------------------------------------------------------------
const textureLoader = new THREE.TextureLoader();
const moonGeometry = new THREE.SphereGeometry(MOON_RADIUS, 16, 16);
const moonMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
If the entire javascript breaks - check devtools console (in the browser, not in the terminal) and see what the errors say. In 99.9% they say precisely what the issue is (likely unrelated to three.js, and more related to the vite project setup.)
public/
The public/ folder is sometimes also called a “static” folder, because the files it contains are pushed to the website unchanged. Usually textures, audio, and 3D models will go here.
Not loading texture, using basic shader material. Smaller white sphere is rendered as it should be. Selection_036.bmp (393.7 KB)
Attempting to load a texture. The smaller sphere in front of the larger green sphere is rendered black i.e. the desired moon texture is not rendered. Selection_037.bmp (448.9 KB)
Yes, as part of my troubleshooting, I’ve read about assets should be in the Vite public folder. One thing I noticed is /public is missing from my project file tree. See below screenshot of the file system.
Just revisiting this thread.
As pointed out in my last reply the /public folder is missing. I’m assuming textures don’t load since a /public folder is missing.
I followed the instructions per the link below, Option 1. No mention of a /public folder. Did I miss a step? Perhaps the install did not completey fully? Welcome thoughts on how to correct this situation.
Using the code snippet by Akroob fixed the no render of the material assigned to the moon object. The texture “moon-1024.jpg” is in the same folder as the other scene files.
After some research I found a website which says a light must be added to the scene. I added a three point light and tested and the MeshPhongMaterial renders correctly.
Regarding use of a /public folder. As posted earlier my project file tree does not have a /public folder and as Akroob points all that is needed is the textures to be available and call the correct path.
Question: Is all the discussion I’ve seen about a /public folder applicable only to a live web server?
Just create the public folder. The three docs are a bit strange, you don’t use vite like that normally, you init it with npm create vite, and it will create all the necessary folders and files, see Getting Started | Vite
@donmccurdy maybe this should be amended. Things like that must be super confusing for beginners. wouldn’t it be better to follow vites own docs here? I know it was done to avoid the placeholder files it creates but these serve as a guide.
My (very much untested!) feeling is that npm create vite obscures the setup steps and discourages users who already have a project in progress from switching to Vite. Maybe I’m an outlier on this, I know every framework and dependency has their own npm create templates now… If anyone disagrees and would like to update the installation tutorial to use npm create vite, please feel free, I do not feel strongly either way.
As mentioned in the previous post, the issue was caused by me not understanding the scene lighting requirements for the MeshPhongMaterial - not due to folder structure.
Apparently, in a local environent (like mine e.g. node, vite) a /public folder is not necessary (??). I did check out the link Getting Started | Vite. Adding to my confusion is the statement:
Blockquote
One thing you may have noticed is that in a Vite project, index.html is front-and-central instead of being tucked away inside public. This is intentional: during development Vite is a server, and index.html is the entry point to your application.
The /public folder is still the most common place to put static resources like textures (.png, jpeg, …) and models (.glb, .fbx, …), so you’ll most likely want one eventually, though it isn’t required.