So basically i wrote a code and i got somewhere near it idk but i seem to think i need to add only a mouse tracker without me clicking or draging the mouse physically. I m a total newbie to three js so correct my code if I m wrong. Thanks
here is my code :
import './style.css'
import * as Three from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
const scene = new Three.Scene();
const camera = new Three.PerspectiveCamera(10, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new Three.WebGL1Renderer({
canvas: document.querySelector('#container'),
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
camera.position.setZ(25);
renderer.render(scene, camera);
const geometry = new Three.BoxGeometry(5, 5, 5)
const material = new Three.MeshStandardMaterial({ color: 0xFFA500 });
const cube = new Three.Mesh(geometry, material);
scene.add(cube)
const light = new Three.PointLight(0xffffff)
light.position.set(5, 5, 5)
const alight = new Three.AmbientLight(0xffffff)
scene.add(light, alight)
const controls = new OrbitControls(camera, renderer.domElement)
const lighthelper = new Three.PointLightHelper(light)
const gridhelper = new Three.GridHelper(200, 500)
scene.add(lighthelper, gridhelper)
function addstar() {
const geometry = new Three.TorusGeometry(0.9, 0.25, 16, 100)
const material = new Three.MeshStandardMaterial({ color: 0xFFA500 });
const torus = new Three.Mesh(geometry, material);
const [x, y, z] = Array(3).fill().map(() => Three.MathUtils.randFloatSpread(100));
torus.position.set(x, y, z)
scene.add(torus)
}
Array(200).fill().forEach(addstar)
function addcube(){
const geo = new Three.BoxGeometry(2,2,2)
const mat = new Three.MeshStandardMaterial({color: 0xffffff})
const square = new Three.Mesh(geo, mat)
const[x,y,z] = Array(3).fill().map(() => Three.MathUtils.randFloatSpread(100));
square.position.set(x,y,z)
scene.add(square)
}
Array(200).fill().forEach(addcube)
const texture = new Three.TextureLoader().load('dot.jpg')
scene.background = texture
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame(animate);
render();
stats.update();
}
animate()
function render() {
var timer = Date.now() * 0.0001;
camera.position.x = Math.cos( timer ) * 800;
camera.position.z = Math.sin( timer ) * 800;
camera.lookAt( scene.position );
scene.traverse( function ( object ) {
if ( object.isMesh === true ) {
object.rotation.x = timer * 5;
object.rotation.y = timer * 2.5;
}
} );
renderer.render( scene, camera );
}