Skip to content
Snippets Groups Projects
Fanzorviewer.tsx 1.51 KiB
Newer Older
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
import React, { useEffect } from 'react';
import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';

const ProteinViewer: React.FC = () => {
  useEffect(() => {
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    //Set up the scene, camera, and renderer
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    //Add lighting to the scene
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    const light = new THREE.DirectionalLight(0xffffff, 1);
    light.position.set(0, 1, 2);
    scene.add(light);

Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    //Load the GLB model using GLTFLoader
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    const loader = new GLTFLoader();
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    loader.load(`${process.env.PUBLIC_URL}/models/Fanzorviewer.glb`, (gltf) => {
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
      scene.add(gltf.scene);  // Add the protein model to the scene
      renderer.render(scene, camera);
    }, undefined, (error) => {
      console.error('An error happened while loading the GLB model:', error);
    });

    camera.position.z = 5; // Adjust the camera position

Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    //Render loop
Asal Sahami Moghaddam's avatar
Asal Sahami Moghaddam committed
    const animate = () => {
      requestAnimationFrame(animate);
      renderer.render(scene, camera);
    };
    animate();

    // Clean up on component unmount
    return () => {
      document.body.removeChild(renderer.domElement);
    };
  }, []);

  return null; // No JSX to return as we're appending the canvas directly to the body
};

export default ProteinViewer;