Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function renderMesh(faces, vertices, normals) {
- let camera, scene, renderer;
- let mesh;
- init();
- render();
- function init() {
- camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 2000);
- camera.position.z = 400;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x050505 );
- const light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 );
- scene.add( light );
- const geometry = new THREE.BufferGeometry();
- geometry.setIndex( new THREE.Uint32BufferAttribute(faces, 3) );
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
- geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
- const axesHelper = new THREE.AxesHelper( 100 );
- scene.add( axesHelper );
- const material = new THREE.MeshPhongMaterial({
- color: 0xAAAAAA,
- flatShading: true,
- side: THREE.DoubleSide
- });
- mesh = new THREE.Mesh( geometry, material );
- // Center object
- // See further down at https://threejsfundamentals.org/threejs/lessons/threejs-primitives.html
- geometry.computeBoundingBox();
- geometry.boundingBox.getCenter(mesh.position).multiplyScalar(-1);
- const parent = new THREE.Object3D();
- parent.add(mesh);
- scene.add(parent);
- // Rotate object so the face faces the user
- parent.rotateZ(Math.PI / 2) // Math.PI / 2 = 90° in radian
- parent.rotateX(-Math.PI / 2)
- const box = new THREE.BoxHelper( parent, 0xffff00 );
- scene.add( box );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.addEventListener( 'change', render );
- controls.update();
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function render() {
- renderer.render( scene, camera );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement