Okay, I think the problem that I'm having is that when I create my DirectionalLight
with three.js, when I then rotate my object with OrbitControls
the entire scene is rotating, not just the sphere I have in the middle of it ( the only object I have in my scene ).
I want to make it so that I can rotate my sphere with my mouse like OrbitControls
does, but not have the camera move. Or have the light not move with the camera? Here's a gif of my scene so far that might better explain my dilemma—
I want the light to be.. y'know.. more like the sun, coming from one point in space and shining at (0,0,0)
in my scene, while still being able to rotate the sphere independently. Am I using OrbitControls
wrong here? Here is what I have to get to the point I have in the gif above:
var width = section.offsetWidth,
height = section.offsetHeight;
var topOfElement = section.getBoundingClientRect().top - height;
var bottomOfElement = section.getBoundingClientRect().bottom;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
camera.position.z = 10;
camera.position.x = -2.5;
const controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableZoom = false;
controls.maxPolarAngle = Math.PI;
const renderer = new THREE.WebGLRenderer( { alpha: true } );
renderer.setClearColor( 0x000000, 0 );
renderer.setSize(width, height);
section.appendChild(renderer.domElement);
const onEverythingLoaded = () => {
render();
};
const loadingManager = new THREE.LoadingManager(onEverythingLoaded);
const loader = new THREE.TextureLoader(loadingManager);
const earthGeomerty = new THREE.SphereGeometry( 5.5, 32, 32 );
const earthDiffuse = loader.load('assets/drought_earth_texture.png');
const earthSpec = loader.load('assets/drought_earth_texture_bump.png');
const material = new THREE.MeshPhongMaterial( {
specular: 0xffffff,
map: earthDiffuse,
specularMap: earthSpec,
normalScale: new THREE.Vector2( 1, 1 ),
shininess: 30,
transparent: true,
depthTest: true,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: - 4,
wireframe: false
} );
const sphere = new THREE.Mesh( earthGeomerty, material );
const ambientLight = new THREE.AmbientLight( 0x667777 );
const directionalLight = new THREE.DirectionalLight( 0xffffdd, 2 );
directionalLight.position.set( 4, 0, 0 );
scene.add( sphere );
scene.add( ambientLight );
scene.add( directionalLight );
const render = () => {
animationFrame = requestAnimationFrame( render );
sphere.rotation.y += 0.001;
renderer.render( scene, camera );
};
For further clarification, I want to have my light work like this:
while still being able to rotate the earth with my mouse like with OrbitControls
. But the light doesn't move.
Is OrbitControls
the wrong tool here? Any documentation / helpful links would be appreciated I am so, so lost.
(I cross-posted this from stackoverflow because I thought maybe the people over here would know more about this, hope that's ok!)
Update
After much fussing about with the OrbitControls
functions I realised that what I'm trying to do isn't what OrbitControls
is for, because "orbiting" isn't what I'm trying to do— I'm trying to rotate the actual sphere with my mouse, which is entirely different. Welp.