You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB
JavaScript

import * as THREE from 'three';
// import { Scene, PerspectiveCamera } from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const geometry = new THREE.BoxGeometry(2, 2, 2);
var cubeMaterials = [
new THREE.MeshLambertMaterial ({color: 0x00ff00}), // right
new THREE.MeshLambertMaterial ({color: 0xff0000}), // left
new THREE.MeshLambertMaterial ({color: 0x0000ff}), // top
new THREE.MeshLambertMaterial ({color: 0xffff00}), // bottom
new THREE.MeshLambertMaterial ({color: 0xff00ff}), // front
new THREE.MeshLambertMaterial ({color: 0x00ffff}) // back
];
// const material = new THREE.MeshBasicMaterial( { color: 0x555555 } );
const material = new THREE.MeshFaceMaterial( cubeMaterials );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );
const light = new THREE.AmbientLight( 0xFFFFFF, 0.1 ); // soft white light
scene.add( light );
camera.position.z = 5;
import { OrbitControls } from 'lesca-threejs-orbitcontrols';
// create function
const Orb = new OrbitControls(THREE);
// then create controls object
const controls = new Orb(camera, renderer.domElement);
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
controls.update();
}
animate();
// set angle to fixed polar,azimuthal
const p1 = { polar: 0, azimuth: 0};
const p2 = { polar: 0, azimuth: Math.PI/4};
const p3 = { polar: Math.PI/8, azimuth: Math.PI/4};
document.addEventListener("keydown", function (e) {
console.log("keydown", e);
if (e.key == "c") {
console.log("calling controls.setPolarAngle + setAzimuthalAngle");
controls.setPolarAngle(p1.polar);
controls.setAzimuthalAngle(p1.azimuth);
} else if (e.key == "d") {
console.log("calling controls.setPolarAngle + setAzimuthalAngle");
controls.setPolarAngle(p2.polar);
controls.setAzimuthalAngle(p2.azimuth);
} else if (e.key == "e") {
console.log("calling controls.setPolarAngle + setAzimuthalAngle");
controls.setPolarAngle(p3.polar);
controls.setAzimuthalAngle(p3.azimuth);
}
})