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

인기 글

태그

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

최근 글

관리자

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

w__am 개발노트

재질, 텍스쳐 이미지 로드하기
Frontend/Three.js

재질, 텍스쳐 이미지 로드하기

2024. 10. 16. 04:59

 

텍스쳐가 있는 사이트에서 다운로드하기

https://3dtextures.me/

3d texture 구글에 검색해  원하는 텍스쳐 다운로드 하기 

 

기본 예제

  /* 텍스쳐 이미지 로드 */
  const textureLoader = new THREE.TextureLoader();
  const texture = textureLoader.load(
    "./textures/watermelon/Watermelon_001_basecolor.jpg"
  );
  
    /* Messh 만들기 */
  const geometry = new THREE.BoxGeometry(2, 2, 2);
  const material = new THREE.MeshStandardMaterial({
    // color: 'orangered',
    map: texture
  });
  • MeshBasicMaterial로 설정해 줄 경우 그림자나 조명 등 효과가 없다.
  • 텍스쳐를 적용할 때 어울리는 mesh를 설정해 주면 된다.

 

앞면과 뒷면에 다른 텍스처 적용

import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

/* 주제: 텍스쳐 이미지 로드하기 */

const onLoad = () => {
  console.log("로드 완료");
};

const onProgress = () => {
  console.log("로드 중");
};

const onError = () => {
  console.log("로드 에러");
};

export default function example() {
  /* 텍스쳐 이미지 로드 */
  // 주의 : webpack.config.js파일 CopyWebpackPlugin에서 textures의 경로 설정이 되었는지 체크하기
  const textureLoader = new THREE.TextureLoader();
  const frontTexture = textureLoader.load(
    "/textures/watermelon/Watermelon_001_basecolor.jpg",
    onLoad, // 로드 완료 시 호출
    onProgress, // 로드 중일 때 호출
    onError // 로드 에러 시 호출
  );
  const backTexture = textureLoader.load(
    "/textures/watermelon/Watermelon_001_roughness.jpg",
    onLoad, // 로드 완료 시 호출
    onProgress, // 로드 중일 때 호출
    onError // 로드 에러 시 호출
  );

	...
    
  /* Messh 만들기 */
  const geometry = new THREE.BoxGeometry(2, 2, 2);

  // MeshBasicMaterial는 조명이나 그림자의 영향을 받지 않는다.
  // material 배열 생성, 앞면과 뒷면에 다른 텍스처 적용

  // 외부 면
  const frontMaterial = new THREE.MeshStandardMaterial({ map: frontTexture });
  const outsideMaterials = [
    frontMaterial, // 오른쪽 면 (x+)
    frontMaterial, // 왼쪽 면 (x-)
    frontMaterial, // 위쪽 면 (y+)
    frontMaterial, // 아래쪽 면 (y-)
    frontMaterial, // 앞면 (z+)
    frontMaterial // 뒷면 (z-)
  ];

  // 내부 면
  const backMaterial = new THREE.MeshStandardMaterial({
    // map: backTexture,
    color: "red",
    side: THREE.BackSide
  });
  const insideMaterials = [
    backMaterial, // 오른쪽 면 (x+)
    backMaterial, // 왼쪽 면 (x-)
    backMaterial, // 위쪽 면 (y+)
    backMaterial, // 아래쪽 면 (y-)
    backMaterial, // 앞면 (z+)
    backMaterial // 뒷면 (z-)
  ];

  // 두 개의 정육면체를 생성
  const outsideMesh = new THREE.Mesh(geometry, outsideMaterials);
  const insideMesh = new THREE.Mesh(geometry, insideMaterials);

  // 두 정육면체를 씬에 추가
  scene.add(outsideMesh);
  scene.add(insideMesh);

  /* 그리기 */
 ...
}

 

 

텍스쳐가 안보이는 경우

webpack을 사용하고 있기 때문에 경로 설정을 해주어야 한다.

// webpack.config.js

...

new CopyWebpackPlugin({
	patterns: [
		{ from: "./src/main.css", to: "./main.css" },
		{ from: "./src/textures", to: "./textures" },
		// { from: "./src/models", to: "./models" },
		// { from: "./src/sounds", to: "./sounds" }
	],
})

경로 설정 후 서버 재실행해주기

 

 

저작자표시 변경금지 (새창열림)

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

재질, 텍스쳐 변환  (0) 2024.10.22
재질, 로딩 매니저(여러개의 텍스쳐 이미지)  (0) 2024.10.16
재질, side - Mesh의 앞 뒷면  (0) 2024.10.16
재질, flatShading - 각지게 표현  (0) 2024.10.16
재질, MeshStandardMaterial (MeshPhongMaterial과 비교)  (0) 2024.09.30
    'Frontend/Three.js' 카테고리의 다른 글
    • 재질, 텍스쳐 변환
    • 재질, 로딩 매니저(여러개의 텍스쳐 이미지)
    • 재질, side - Mesh의 앞 뒷면
    • 재질, flatShading - 각지게 표현
    wam
    wam

    티스토리툴바