This sounds like a really basic question, but it has me stumped. I’ve been out of the game for a while, but I don’t think my skills have rusted that far…
A lot of StackOverflow questions are starting to relate to modules. Stack Overflow’s snippets don’t support JS modules within the JavaScript pane, so I’m updating my answer template code to use script tags which let me use type="importmap"
/type="module"
. This works, mostly. Here’s an example:
<script type="importmap">
{
"imports": {
"three": "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.163.0/three.module.js"
}
}
</script>
<script type="module">
import {
WebGLRenderer,
Scene,
PerspectiveCamera,
PointLight,
SphereGeometry,
MeshBasicMaterial,
Mesh
} from 'three'
const renderer = new WebGLRenderer()
document.body.appendChild( renderer.domElement )
const camera = new PerspectiveCamera( 25, 1, 1, 100 )
function resizeToWindow () {
if ( renderer && camera ) {
renderer.setSize( window.innerWidth, window.innerHeight )
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
}
}
window.addEventListener( 'resize', resizeToWindow )
//resizeToWindow()
/* DEBUG */ renderer.setSize( 500, 500 )
const scene = new Scene()
const light = new PointLight( 0xffffff )
camera.add( light )
scene.add( camera )
camera.position.set( 0, 0, 50 )
camera.lookAt( scene.position )
const geo1 = new SphereGeometry( 5 )
const mat1 = new MeshBasicMaterial( {
color: 'red'
} )
const msh1 = new Mesh( geo1, mat1 )
scene.add( msh1 )
renderer.render( scene, camera )
</script>
This is literally all I have in the snippet, and it works great! I see a single, red, solid-color sphere in the center of my viewport.
The problem occurs when I replace MeshBasicMaterial
with MeshPhongMaterial
. That’s it. That’s the only change, and then the sphere no longer renders.
- I have a light
- It’s attached to my camera
- The light (in the camera) is in the scene
- The mesh is in the scene
I tried adding the light to the scene directly, but it didn’t make a difference.
I copied my code to a standalone HTML file, dropping the script tags into the <body>
, but I got the same results as in the snippet.
I double-checked the docs, and it seems like I should be able to define MeshPhongMaterial
with only color
, or even with no parameters (the docs say it should default to white). I removed the parameter object, but it didn’t make a difference.
Edit: I don’t get any errors in the console. CORS obviously isn’t an issue, otherwise it wouldn’t be rendering with MeshBasicMaterial
. I have also double-checked for typos.
So, what super-simple thing am I missing or doing wrong?