๊ทธ๋ž˜ํ”ฝ์Šค/WebGl

[3D WebGL][three.js] three.js ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ํ•ด๋ณด๊ธฐ

Rainbow๐ŸŒˆCoder 2022. 5. 9. 02:06
728x90

 

Three.js – JavaScript 3D Library (threejs.org)

 

Three.js – JavaScript 3D Library

 

threejs.org

๋‹ค์šด๋กœ๋“œ ํด๋ฆญ!

BUILD ์•ˆ์—์„œ html์•ˆ์— ์Šคํฌ๋ฆฝํŠธ๋กœ ๋„ฃ๋Š” ์Šคํƒ€์ผ๋กœ ์ž‘์„ฑํ•˜๋ ค๋ฉด js ํ˜น์€ min.js(js์™€ ๊ฐ™์€๋ฐ ์••์ถ• ๋ฒ„์ „์ผ ๋ฟ),

๋ชจ๋“ˆ ๋ฐฉ์‹์„ ์“ด๋‹ค๋ฉด module.js๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

Learn -> documentation ๋“ค์–ด๊ฐ€๋ฉด

 

์ขŒ์ธก ๊ฒ€์ƒ‰ ๋ž€ ์˜†์— ์–ธ์–ด ์„ ํƒ 'ํ•œ๊ตญ์–ด'ํ•˜๋ฉด ์ผ๋ถ€ ๋ฒˆ์—ญ ๋˜์–ด์žˆ๋‹ค.

์žฅ๋ฉด ๋งŒ๋“ค๊ธฐ – three.js docs (threejs.org)

 

three.js docs

 

threejs.org

์œ„ ๋งํฌ์— ๊ธฐ์žฌ๋˜์–ด ์žˆ๋Š” ์˜ˆ์ œ๋งํฌ

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>My first three.js app</title>
		<style>
			body { margin: 0; }
			canvas { display: block; }
		</style>
	</head>
	<body>
		<script src="js/three.js"></script>
		<script>
			const scene = new THREE.Scene();
			const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

			const renderer = new THREE.WebGLRenderer();
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			const geometry = new THREE.BoxGeometry();
			const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
			const cube = new THREE.Mesh( geometry, material );
			scene.add( cube );

			camera.position.z = 5;

			function animate() {
				requestAnimationFrame( animate );

				cube.rotation.x += 0.01;
				cube.rotation.y += 0.01;

				renderer.render( scene, camera );
			};

			animate();
		</script>
	</body>
</html>

<๊ฒฐ๊ณผ>

์—ฐ๋‘์ƒ‰ ํ๋ธŒ๊ฐ€ ๋น™๊ธ€๋น™๊ธ€ ์ž˜ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ชจ๋“ˆ ๋ฐฉ์‹ : ๋˜‘๊ฐ™์€ ๊ฒฐ๊ณผ

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>My first three.js app</title>
    <style>
      body {
        margin: 0;
      }
      canvas {
        display: block;
      }
    </style>
  </head>
  <body>
    <script type="module">
      import * as THREE from "./three.module.js";
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(
        75,
        window.innerWidth / window.innerHeight,
        0.1,
        1000
      );

      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      const geometry = new THREE.BoxGeometry();
      const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
      const cube = new THREE.Mesh(geometry, material);
      scene.add(cube);

      camera.position.z = 5;

      function animate() {
        requestAnimationFrame(animate);

        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;

        renderer.render(scene, camera);
      }

      animate();
    </script>
  </body>
</html>

 

<ํ„ฐ๋ฏธ๋„ ์„ค์น˜ ๋ช…๋ น์–ด>

npm i three

728x90