WebGL 3D Model Viewer Using three.js

webgl-3d-model-viewer-using-three.js-master.zip

· 4 Minutes / 1,101 Words

You can create a WebGL 3D model viewer in just a few lines of code using three.js. This tutorial shows you all you need to get started. The final code is on GitHub and you can see the viewer in action showing a low-poly model that I made for an unfinished Space Western game.

You may want to open the above WebGL 3D model viewer to experience it full screen and toggle three-point lighting by pressing the L key.

What do you need? 

  • Your model in OBJ format. If you do not know how to export your model, please take a look at the manual of Blender, 3ds Max, Maya, Lightwave, Modo or whatever modeling software you are using.

  • You textures in a format that three.js can handle (PNG, JPG, DDS…), preferably as square power-of-two textures.

  • An accompanying MTL file, which your software will generate automatically. This file references your textures and specifies the materials of your model.

  • The latest build of the three.js library, mine is r76.

  • Some other scripts that are hidden in the treasure trove that is the three.js GitHub repository.

Put the files in a folder and reference them in a simple HTML page as in the code snippet below. I also recommend you to set a neutral color for the background.

<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="three.js"></script>
    <script src="Detector.js"></script>
    <script src="OrbitControls.js"></script>
    <script src="OBJLoader.js"></script>
    <script src="MTLLoader.js"></script>

    <style>
        body {
            overflow: hidden;
            margin: 0;
            padding: 0;
            background: hsl(0, 0%, 10%);
        }
    </style>

</head>
<body>

    <script>
        // You can put all the example code in a separate file or just add it here for simplicity.
        // Also no ES6 today, so we don't need a build setup ;)
    </script>

</body>
</html>

Setup 

This three.js application is structured into initialization (init()) and a render loop (render()). We also need a container to put our renderer in, so we create an empty element. You could of course also select an already existing element.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

// The detector will show a warning if the current browser does not support WebGL.
if (!Detector.webgl) {
    Detector.addGetWebGLMessage();
}

// All of these variables will be needed later, just ignore them for now.
var container;
var camera, controls, scene, renderer;
var lighting, ambient, keyLight, fillLight, backLight;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();

function init() {
    container = document.createElement('div');
    document.body.appendChild(container);
    // Code...
}

function render() {
    // Code...
}

Camera 

The first thing we do in our init() function is creating a perspective camera. The arguments are sensible defaults. If your model is very large you may have to adjust the last two arguments, which are the frustum near plane and frustum far plane. They basically state that nothing nearer than one unit and nothing farther than a thousand units will be rendered.

camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 3;

Scene 

A scene holds your camera(s), lights and models, as it would do in real life. The code below adds a white ambient light which illuminates the whole scene without any shading.

scene = new THREE.Scene();
ambient = new THREE.AmbientLight(0xffffff, 1.0);
scene.add(ambient);

Lighting 

Do you want your scene to have more sophisticated lighting? It’s very easy to add three-point lighting to your scene using directional lights. You should dim the ambient light by setting the second argument to 0.25 or remove it altogether to see the effect.

keyLight = new THREE.DirectionalLight(new THREE.Color('hsl(30, 100%, 75%)'), 1.0);
keyLight.position.set(-100, 0, 100);

fillLight = new THREE.DirectionalLight(new THREE.Color('hsl(240, 100%, 75%)'), 0.75);
fillLight.position.set(100, 0, 100);

backLight = new THREE.DirectionalLight(0xffffff, 1.0);
backLight.position.set(100, 0, -100).normalize();

scene.add(keyLight);
scene.add(fillLight);
scene.add(backLight);

You can toggle three-point lighting by pressing the L key in my example, but that is just because the model is intended to be rendered without any lighting, as the light information is painted into the texture.

If you want to further enhance your scene you might want to upgrade the directional lights to spot lights, but they require some additional setup.

Model 

The MTLLoader and OBJLoader are pretty self-explanatory. They both require a file path and a callback function. If you want to translate, rotate or scale your object simply change the properties listed in the Object3Ddocumentation. You can do so by setting the x, y and z values of the Vector3 independently or with the set(x, y, z) method.

You can safely omit the two filter settings as I have needed them for the pixelated look of my model to keep the texture sharp. You can read about texture filtering in the three.js documentation.

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setBaseUrl('assets/');
mtlLoader.setPath('assets/');
mtlLoader.load('female-croupier-2013-03-26.mtl', function (materials) {

    materials.preload();

    materials.materials.default.map.magFilter = THREE.NearestFilter;
    materials.materials.default.map.minFilter = THREE.LinearFilter;

    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.setPath('assets/');
    objLoader.load('female-croupier-2013-03-26.obj', function (object) {

        scene.add(object);

    });

});

Renderer 

Up until now, nothing is rendered onto the canvas. We have to create a renderer and add it as a child of our container element. The render loop itself is an infinite loop calling itself at 60 frames per second using window.requestAnimationFrame(). It updates the controls that we are going to add in the next step and tells the renderer to render the scene using our only camera.

renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(new THREE.Color("hsl(0, 0%, 10%)"));

container.appendChild(renderer.domElement);
function render() {
    requestAnimationFrame(render);
    controls.update();
    renderer.render(scene, camera);
}

Controls 

The last thing we have to do is to initialize the orbit controls. You can specify a damping to smooth out camera movement, as well as some other options. Have a look at the variables in the beginning of the source code to see what’s possible.

controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = false;

Conclusion 

Congratulations on creating a WebGL 3D model viewer with three.js. The full example is available on GitHub to be forked and modified. If you want to dive deeper into three.js head over to the extensive documentation and browse the examples. The three.js home page also features a lot of inspirational projects which are worth checking out.

 

[출처] https://manu.ninja/webgl-3d-model-viewer-using-three-js

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
20 [nest.js] [NestJS] NestJS 구조 이해를 위한 필수 개념 정리 - Node.js/TypeScript/Express 비교 포함 file 졸리운_곰 2025.12.12 501
19 [node.js 개발] Apache Reverse Proxy 설정(아파치와 노드 연동) file 졸리운_곰 2024.03.17 300
18 [node.js 개발] PM2로 Node.js 앱 프로세스 배포하기 file 졸리운_곰 2024.03.16 405
17 [node.js 개발] PM2를 활용한 Node.js 무중단 서비스하기 file 졸리운_곰 2024.03.16 517
16 [node.js 응용] Next.js : Next.js14에 Mysql연결하기 졸리운_곰 2024.03.03 379
15 [node.js 응용] Node.js에서 다른 파일의 함수를 "include" 하는 방법 졸리운_곰 2024.02.28 428
14 [node.js 응용] NodeJS 에서 mqtt 사용하기 file 졸리운_곰 2024.02.23 399
13 [node.js 응용] Next.js 기본 개념정리 file 졸리운_곰 2024.02.23 432
12 [node.js 응용] ejs 사용설명서 file 졸리운_곰 2023.11.25 371
11 [node.js 응용] Build a Node.js Proxy Server in Under 10 minutes! file 졸리운_곰 2023.05.07 468
10 [node.js 응용] node - pm2로 node.js 프로세스 관리하기 - 기본 명령어, 실행하기 file 졸리운_곰 2023.04.25 407
9 [node.js 응용] Node.js | MySQL과 연동(mysql모듈) - CRUD 2/2 졸리운_곰 2023.03.31 231
8 [node.js 응용] Node.js | MySQL과 연동(mysql모듈) - CRUD 1/2 file 졸리운_곰 2023.03.31 484
7 [node.js 응용] PM2 - Node.js 프로세스 관리 도구 file 졸리운_곰 2021.12.10 410
6 [node.js][nodejs] [Linux] 리눅스 내 Node.js 및 NPM 최신 버전으로 유지하기 file 졸리운_곰 2021.10.11 484
5 [node.js][typescript] 5분 안에 보는 TypeScript file 졸리운_곰 2021.07.03 428
4 Getting started with RabbitMQ and Node.js file 졸리운_곰 2019.05.09 466
3 [Node.js + RabbitMQ] Node.js + socket.io + RabbitMQ 이용한 실시간 메시지 처리 file 졸리운_곰 2019.05.09 352
2 node.js 서버 장애시 자동 재시작 설정 [forever 사용] 졸리운_곰 2019.01.24 884
1 Express 앱용 프로세스 관리자 졸리운_곰 2018.10.16 603
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED