3d_website/main.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

/*
Author: Daniel Jones
IDE: VSCodium
Browsers tested: Firefox 125
Last change: 05/08/24
purpose: portfolio website to show to potential freelance customers/web developer positions
*/
2024-04-08 20:07:24 +00:00
import { PointLight } from 'three';
2024-05-12 00:59:23 +00:00
import { OBJLoader } from 'three/examples/jsm/Addons.js';
import { STLLoader } from 'three/examples/jsm/Addons.js';
2024-04-08 20:07:24 +00:00
import './style.css';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
2024-02-15 20:59:21 +00:00
import * as THREE from 'three';
2024-02-15 20:59:21 +00:00
const scene = new THREE.Scene();
2024-02-15 20:59:21 +00:00
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#bg'),
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
2024-04-12 01:47:34 +00:00
var zPos = 500;
camera.position.setZ(zPos);
camera.position.setY(20);
2024-02-15 20:59:21 +00:00
renderer.render(scene, camera);
2024-04-08 20:07:24 +00:00
//stl code to import stl 3d model
const thinkerMaterial = new THREE.MeshPhysicalMaterial({
color: 'FFFFFF',
metalness: 0.50,
roughness: 0.1,
opacity: 1.0,
transparent: false,
transmission: 0.99,
clearcoat: 1.0,
clearcoatRoughness: 0.25,
})
const thinkerLoader = new STLLoader();
thinkerLoader.load(
'assets/models/thinker.stl',
function (geometry) {
const thinkerMesh = new THREE.Mesh(geometry, thinkerMaterial)
scene.add(thinkerMesh)
},
(xhr) => {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
},
(error) => {
console.log(error)
}
)
2024-04-08 20:07:24 +00:00
const ambientLight = new THREE.AmbientLight(0xffffff);
scene.add(ambientLight);
2024-04-08 20:10:16 +00:00
2024-05-06 20:35:29 +00:00
2024-05-12 00:59:23 +00:00
//background texture
const spaceTexture = new THREE.TextureLoader().load('./assets/pics/galaxy.jpg');
2024-05-12 00:59:23 +00:00
spaceTexture.magFilter = THREE.NearestFilter;
scene.background = spaceTexture;
2024-05-06 20:35:29 +00:00
//turning off orbit controls and lighthelper. Not needed. Kept in code in case if I want to use it again.
//const lightHelper = new THREE.PointLightHelper(pointLight);
//const controls = new OrbitControls(camera, renderer.domElement);
//floor object that appears in the background
const floorTexture = new THREE.TextureLoader().load("./assets/pics/marble.jpg");
floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.wrapS = THREE.RepeatWrapping;
2024-05-12 00:59:23 +00:00
floorTexture.repeat.set(1, 100);
const floorGeometry = new THREE.PlaneGeometry(100, 10000, 1000, 1000);
const floorMaterial = new THREE.MeshStandardMaterial({
map: floorTexture,
})
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
2024-04-12 01:47:34 +00:00
floor.rotateX(Math.PI * -0.5);
scene.add(floor);
2024-04-08 20:10:16 +00:00
2024-05-06 20:35:29 +00:00
//when the user scrolls, walk down the hallway.
function updateCamera(ev){
let div1 = document.getElementById("div1");
camera.position.z = zPos - window.scrollY / 3;
}
window.addEventListener("scroll", updateCamera);
2024-05-06 20:35:29 +00:00
//animation loop every cycle.
2024-02-15 20:59:21 +00:00
function animate(){
requestAnimationFrame(animate);
2024-04-12 01:47:34 +00:00
renderer.render(scene, camera);
2024-02-15 20:59:21 +00:00
}
animate();