wam
w__am 개발노트
wam
  • 분류 전체보기 (165)
    • CS 지식 (10)
      • 자료구조 (0)
      • 알고리즘 (0)
      • 컴퓨터 구조 (0)
      • 운영체제 (0)
      • 네트워크 (7)
      • 데이터베이스 (0)
      • 디자인 패턴 (3)
    • Frontend (131)
      • Three.js (64)
      • NPM (1)
      • Nest.js (19)
      • React (10)
      • Apollo (7)
      • TypeScript (2)
      • JavaScript (12)
      • HTML, CSS (1)
      • Jest (3)
      • E2E (5)
      • Cypress (7)
    • Database (12)
      • TypeORM (12)
    • IT 지식 (8)
      • 클라우드 서비스 (3)
      • 네트워크 (1)
      • 데이터 포맷 (2)
      • 기타 (2)
    • IT Book (2)
    • 유용한 사이트 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 🐱 Github

인기 글

태그

  • 함수 선언문
  • 스코프
  • math.sin()
  • 함수 리터럴
  • 함수 표현식
  • Decorators
  • 디자인 패턴
  • getelapsedtime()
  • getdelta()
  • mapped types
  • 원형적인 움직임
  • joi 에러
  • e.preventdefault()
  • Interface
  • API
  • 데이터 포맷
  • 렌더링 성능 최적화
  • 초기 환경설정
  • three.js 구성 요소
  • type-graphql
  • threejs 개발 할 때 도움을 줄 수 있는 유틸리티
  • math.cos()
  • axeshelper
  • isabstract
  • 삼각함수
  • 함수의 범위
  • reactive variables
  • react 성능 최적화
  • 오프-프레미스(off-premise) 방식
  • gridhelper

최근 글

관리자

글쓰기 / 스킨편집 / 관리자페이지
hELLO · Designed By 정상우.
wam

w__am 개발노트

빛(조명) 설정하기
Frontend/Three.js

빛(조명) 설정하기

2024. 4. 20. 23:39

 

src/ex04_light

import * as THREE from "three";

/* 주제: 빛(조명) 설정하기 */

export default function example() {
  /* Renderer 만들기 : html에 캔버스 미리 만들기 */
  const canvas = document.querySelector("#three-canvas");
  const renderer = new THREE.WebGLRenderer({
    canvas,
    antialias: true
    // alpha: true // 배경 투명도 설정
  });
  renderer.setSize(window.innerWidth, window.innerHeight);

  /*  디바이스 픽셀 비율을 설정 */
  renderer.setPixelRatio(window.devicePixelRatio > 1 ? 2 : 1);

  /*  배경색, 투명화 설정 */
  // renderer.setClearAlpha(0.5); // 불투명도 설정
  // renderer.setClearColor(0x00ff00);

  /*  Scene 만들기 */
  const scene = new THREE.Scene();
  // scene.background = new THREE.Color("blue");

  /*  Camera 만들기 */
  const camera = new THREE.PerspectiveCamera(
    75, // 시야각 (field of view)
    window.innerWidth / window.innerHeight, // 종횡비(aspect)
    0.1, // near
    1000 // far
  );
  camera.position.x = 2;
  camera.position.y = 2;
  camera.position.z = 5;
  scene.add(camera);

  /*  Light 만들기 */
  const light = new THREE.DirectionalLight(0xffffff, 2);
  light.position.x = 2;
  light.position.z = 2;
  scene.add(light);

  /*  Messh 만들기 */
  const geometry = new THREE.BoxGeometry(1, 1, 1);
  const material = new THREE.**MeshStandardMaterial**({
    // MeshBasicMaterial는 빛의 영향을 받지 않아 조명이 없어도 보인다.
    color: "red"
  });
  const mesh = new THREE.Mesh(geometry, material);
  scene.add(mesh);

  /*  그리기 */
  renderer.render(scene, camera);

  function setSize() {
    // 카메라
    camera.aspect = window.innerWidth / window.innerHeight; // window는 전역 객쳐여서 작성하지 않아도 됨. window.innerWidth === innerWidth
    // updateProjectionMatrix -  카메라 투영에 관련된 값에 변화가 있을 경우 실행해야 함
    camera.updateProjectionMatrix();
    // setSize - 변화된 것을 다시 그려주는 것까지 해야 작동된다.
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.render(scene, camera);
  }

  /*  이벤트 */
  window.addEventListener("resize", setSize);
}
  • 빛 추가 :  THREE.DirectionalLight(빛의 색상, 빛의 강도)
  • const light = new THREE.DirectionalLight(0xffffff, 1);

 

  • 빛도 scene에 추가해야 보인다. scene.add(light);
  • 재질이 MeshBasicMaterial 이면 빛에 영향을 받지 않는다.
    • MeshBasicMaterial 로 변경하기

 

 

AmbientLight 조명

// Light
const ambientLight = new THREE.AmbientLight('white', 0.5);
scene.add(ambientLight);

const directionalLight = new THREE.DirectionalLight('white', 1);
directionalLight.position.x = 1;
directionalLight.position.z = 2;
scene.add(directionalLight);

은은하게 전체적으로 조명을 적용해 줄 때

 

 

 

저작자표시

'Frontend > Three.js' 카테고리의 다른 글

애니메이션 성능 보정  (0) 2024.08.28
애니메이션 기본  (0) 2024.08.28
Renderer에 배경색, 투명화 설정  (0) 2024.04.20
브라우저 창 사이즈 변경에 대응하기  (0) 2024.04.20
직교 카메라(Orthographic Camera)  (0) 2024.04.20
    'Frontend/Three.js' 카테고리의 다른 글
    • 애니메이션 성능 보정
    • 애니메이션 기본
    • Renderer에 배경색, 투명화 설정
    • 브라우저 창 사이즈 변경에 대응하기
    wam
    wam

    티스토리툴바