첫 번째 ThreeJS 프로젝트 설정 - 3단계 설정

오늘은 작업하기에 가장 유용한 라이브러리인 Three.js로 소개 장면을 만드는 방법을 알려드리겠습니다. 이미 많은 유용한 기능이 미리 작성되어 있어 많은 노력이 필요합니다.

기초



Three.js는 웹 페이지에서 3D 콘텐츠를 가능한 한 쉽게 얻을 수 있도록 하는 3D 라이브러리입니다. Three.js는 종종 WebGL과 혼동되는데, 항상 그런 것은 아니지만 three.js는 WebGL을 사용하여 3D를 그리기 때문입니다.

WebGL은 점, 선, 삼각형만 그리는 매우 낮은 수준의 시스템입니다. WebGL로 유용한 작업을 수행하려면 일반적으로 상당한 양의 코드가 필요하며 여기서 three.js가 필요합니다. WebGL은 장면, 조명, 그림자, 재료, 텍스처, 3d 수학, 직접 작성해야 하는 모든 것을 처리합니다. WebGL을 직접 사용한다면.

시작하기



계속해서 CodePen playground을 사용하여 새 프로젝트를 초기화하거나 src 폴더 아래에 다음 파일 구조를 사용하여 Visual Studio Code에서 자신의 프로젝트를 설정하십시오.

ThreeJS Scene Template
  |- /src
    |- index.html
    |- style.css
    |- Avatar.png
    |- spaceTexture.png
    |- moonTexture.png
    |- normalMap.png
    |- script.js


파트 1: HTML



HTML은 정말 기본입니다. index.html를 편집하고 다음 코드로 바꿉니다.

<canvas id="bg"></canvas>   


파트 2: CSS



다음 단계는 다음 스타일을 추가하고 style.css 파일을 완성하는 것입니다. 캔버스를 감싸는 div가 문서에 맞는지 확인하고 평면 div 요소에 원하는 크기를 적용하십시오.

@import url('https://fonts.googleapis.com/css2?family=Rubik:wght@700&display=swap');

html, 
body {
  height: 100vh;
}

body {
  background-color: #000;
  color: #fff;
  display: flex;
  align-items: center;
  font-family: 'Rubik', sans-serif;
  margin: 0;
  overflow: hidden;
}

div {
  position: absolute;
  left: 0;
  top: 0;
  padding: 1em;
}
canvas {
  position: fixed;
  top: 0;
  left: 0;
}


파트 3: 자바스크립트



이제 우리는 JavaScript 로직을 ThreeJS 설정에 구현할 수 있습니다.

import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';

//creating the scene
const scene = new THREE.Scene();
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);

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableKeys = false;
controls.maxDistance = 5000.0;
controls.enableDamping = true;
controls.dampingFactor = 0.16;
controls.target.set(0, 0, 0);

// If you use THREE.OrbitControls and you want to change the camera target or position you need to use the update method.
camera.position.set(0, 5, 0);
controls.update();

document.body.appendChild(renderer.domElement);

THREE.ImageUtils.crossOrigin = '';
const texture = THREE.ImageUtils.loadTexture('avatar.png');
texture.anisotropy = renderer.getMaxAnisotropy();

//Stars
function addStar() {
  const geometry = new THREE.SphereGeometry(0.25, 24, 24);
  const material = new THREE.MeshStandardMaterial({ color: 0xffffff });
  const star = new THREE.Mesh(geometry, material);

  const [x, y, z] = Array(3)
    .fill()
    .map(() => THREE.MathUtils.randFloatSpread(300));

  star.position.set(x, y, z);
  scene.add(star);
}

Array(200).fill().forEach(addStar);

// Lights
const pointLight = new THREE.PointLight(0xffffff, 2);
pointLight.position.set(5, 5, 5);

const ambientLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(pointLight, ambientLight);

// Background

const spaceTexture = new THREE.TextureLoader().load('https://media.istockphoto.com/photos/aerosol-clouds-space-haze-or-cosmic-rays-pink-pastel-blue-space-sky-picture-id1306540124?b=1&k=20&m=1306540124&s=170667a&w=0&h=Cpdu6iLmqYM7BFjoq0xvTnR6ws12ba6v7yxAFhCenyI=');
scene.background = spaceTexture;

// Moon
const moonTexture = new THREE.TextureLoader().load('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCB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DWbe+iY+2NipawIIDAbAeyqyjJtPl7wjARyVmgRGmfiiAFmc+YsEanRBVgP1oreCCj8NJyXMwxGYR2vHNE4RGaBOphWOIynkcvMQquw4A/FveqbJGUIbjPf6QBo0r7n0R6VM8W5VQYR6dWEDL6EnoMpz1y/tI1NgOx8pn/IPKd0Nz9UC8ZSO+S5zG57+YCKTOitpkgVcNRKGyRoRzlNtYFAaNu+iBU0pvE96KXUienimmiMgiGrugRNMNzbbp8qKFJpMEW5D9lOPqoJd4IM3/EawktHEb/yDfYZJV/5GxAORj8hH/wAzrOi2XNB08kF1FuyBDgcciRbQEHznLlCmjSe0iQ2Dmdf1K0+OBEDyuuDpzv1QCpkBwOV5sB+oTzgxwBDHTvp4BLiJmJ5dlFp1S35E28kCD6JmIi+py6KRTAF8/D2T1eHGQAOV1T7bYyQZ773gSchHwka9Jzv9oj2yy3Wx/jiZhGbRadI6IrBZQvm7LLLrfVGqCNPQBaD8I0LjRjKwQN22UObZFDCrfbdugWDOSkBG+0VH2SgFwqCSimiVPByQAgrgj/bO3qu+2dvVBQN5ldAV/tFR9ooKBWlSWFcGlBCgFX4Co4CgiV3Eu4CpDEEWV2kbKA1cZQTxBWaAqBingQEEbKRCGZUcR2CAsKHQhSdl0HZBLoVZVuFdDt0A+EK0KftndWDCgqAFxYFP2zup4SgC5ijzRoKgtKAUKRAROAqppFBVzJURG6JwFd9o9lAMKQFb7ZVg0oIPRU4UWCu4EH//2Q==');
const normalTexture = new THREE.TextureLoader().load('https://www.filterforge.com/filters/9158-normal.jpg');

const moon = new THREE.Mesh(
  new THREE.SphereGeometry(3, 32, 32),
  new THREE.MeshStandardMaterial({
    map: moonTexture,
    normalMap: normalTexture,
  })
);

scene.add(moon);

moon.position.z = 60;
moon.position.setX(-10);


//Torus
const ringGeometry = new THREE.TorusGeometry(10,3,16,100);
const material = new THREE.MeshBasicMaterial({color: 'red'});
const torus = new THREE.Mesh(ringGeometry,material);
scene.add(torus);

//cube
const cubeGeometry = new THREE.BoxGeometry(3, 3, 3);   
let materials = [];
for(let counter = 0; counter < 6; counter++) {
  materials.push(new THREE.MeshBasicMaterial({color: 'white',  map: texture}));
}

var cube = new THREE.Mesh(cubeGeometry, materials);
scene.add(cube);

//loop
var animate = function () {
  requestAnimationFrame( animate );
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  torus.rotation.x += 0.01;
  torus.rotation.y += 0.005;
  torus.rotation.z += 0.01;
  moon.rotation.x += 0.005;
  renderer.render(scene, camera);
}; 

animate();


시원한! 이제 저장하면 브라우저에 다음과 같이 표시됩니다.



요약



따라했다면 프로젝트를 완료하고 ThreeJS로 기본 장면 설정을 완료했을 것입니다.

이제 여기까지 했다면 코드를 내Sandbox에 연결하여 포크하거나 복제하면 작업이 완료됩니다.

유용한 리소스



https://threejs.org/

좋은 웹페이지 즐겨찾기