custom-buffergeometry.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <!DOCTYPE html><html lang="ko"><head>
  2. <meta charset="utf-8">
  3. <title>사용자 지정 BufferGeometry</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – 사용자 지정 BufferGeometry">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. <link rel="stylesheet" href="/manual/ko/lang.css">
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="lesson-title">
  25. <h1>사용자 지정 BufferGeometry</h1>
  26. </div>
  27. <div class="lesson">
  28. <div class="lesson-main">
  29. <p><a href="/docs/#api/ko/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>는 Three.js 내의 모든 <code class="notranslate" translate="no">geometry</code>를 나타냅니다(r125에서부터 <code class="notranslate" translate="no">Geometry</code>가 제거되었습니다). 좀 더 자세히 말하면 특정 <a href="/docs/#api/ko/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>라고 부르는 <strong>속성</strong>의 집합이죠.</p>
  30. <p>각 <a href="/docs/#api/ko/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>는
  31. 위치(positions), 법선(normals), 색(colors), uv 데이터의 배열이고, 이들을 모으면 각 꼭지점에
  32. 대한 <em>평행 배열</em> 형식의 데이터가 됩니다.</p>
  33. <div class="threejs_center"><img src="../resources/threejs-attributes.svg" style="width: 700px"></div>
  34. <p>그림을 보면 총 4개의 속성(attribute), <code class="notranslate" translate="no">position</code>, <code class="notranslate" translate="no">normal</code>, <code class="notranslate" translate="no">color</code>, <code class="notranslate" translate="no">uv</code>가 있습니다.
  35. 이들은 평행 배열로 각 속성의 N 번째 데이터 묶음이 한 꼭지점의 데이터를 나타냅니다. 표시한
  36. index = 4 위치의 꼭지점 데이터를 보세요. 이 묶음이 하나의 꼭지점을 정의합니다.</p>
  37. <p>이해를 돕기 위해 정육면체의 한 모서리를 강조해보겠습니다.</p>
  38. <div class="threejs_center"><img src="../resources/cube-faces-vertex.svg" style="width: 500px"></div>
  39. <p>이 경우 맞닿은 면의 색이 다르기에, 이 모서리는 각 면에 다른 법선을 제공해야 합니다.
  40. 마찬가지로 UV도 달라야 하죠.
  41. 하나의 <em>꼭지점</em>은 위 4개 속성의 묶음이고, 때문에 속성이 달라진다면 그건 다른 꼭지점이
  42. 되는 것이죠.</p>
  43. <p>이전과 마찬가지로 <a href="/docs/#api/ko/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>로 정육면체를 만들어보겠습니다. 굳이 정육면체를 쓰는
  44. 이유는 모서리의 꼭지점을 공유하는 듯해도 사실 그렇지 않기 때문이죠. 필요한 꼭지점을
  45. 전부 생성한 후, 꼭지점 데이터를 평행 배열로 변환해 <a href="/docs/#api/ko/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>를 만들고, 이를
  46. <a href="/docs/#api/ko/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>에 추가해야 합니다.</p>
  47. <p>먼저 정육면체를 만드는 데 필요한 데이터를 정렬합니다.
  48. 아까 말했듯 꼭지점의 속성 중 하나라도 다르다면 별도의 꼭지점으로 분리해야 합니다. 정육면체의
  49. 경우는 총 꼭지점 36개가 필요하죠. 면 6개, 면 하나당 삼각형 2개, 삼각형 하나 당 꼭지점 3개,
  50. 총 36개입니다.</p>
  51. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const vertices = [
  52. // 앞쪽
  53. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 0], },
  54. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  55. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  56. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  57. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  58. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], },
  59. // 오른쪽
  60. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], },
  61. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  62. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  63. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  64. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  65. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], },
  66. // 뒤쪽
  67. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], },
  68. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  69. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  70. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  71. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  72. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], },
  73. // 왼쪽
  74. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], },
  75. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  76. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  77. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  78. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  79. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], },
  80. // 상단
  81. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], },
  82. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  83. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  84. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  85. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  86. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], },
  87. // 하단
  88. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], },
  89. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  90. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  91. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  92. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  93. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], },
  94. ];
  95. </pre>
  96. <p>다음으로 이 배열을 3개의 평행 배열로 변환합니다.</p>
  97. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const positions = [];
  98. const normals = [];
  99. const uvs = [];
  100. for (const vertex of vertices) {
  101. positions.push(...vertex.pos);
  102. normals.push(...vertex.norm);
  103. uvs.push(...vertex.uv);
  104. }
  105. </pre>
  106. <p>이제 <a href="/docs/#api/ko/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>를 만듭니다. 그리고 각 배열로 <a href="/docs/#api/ko/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a> 인스턴스를 생성한
  107. 뒤 <a href="/docs/#api/ko/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>에 추가합니다.</p>
  108. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const geometry = new THREE.BufferGeometry();
  109. const positionNumComponents = 3;
  110. const normalNumComponents = 3;
  111. const uvNumComponents = 2;
  112. geometry.setAttribute(
  113. 'position',
  114. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  115. geometry.setAttribute(
  116. 'normal',
  117. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  118. geometry.setAttribute(
  119. 'uv',
  120. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  121. </pre>
  122. <p>이때 정확히 Three.js가 원하는 속성 이름을 써야 합니다(사용자 지정 쉐이더를 만들 때와는
  123. 달리). 이 경우에는 <code class="notranslate" translate="no">position</code>, <code class="notranslate" translate="no">normal</code>, <code class="notranslate" translate="no">uv</code>이죠. 꼭지점 색을 지정하려면 <code class="notranslate" translate="no">color</code> 속성을
  124. 지정해야 합니다.</p>
  125. <p>아까 <code class="notranslate" translate="no">positions</code>, <code class="notranslate" translate="no">normals</code>, <code class="notranslate" translate="no">uvs</code>, 3개의 자바스크립트 순수 배열을 생성했습니다.
  126. 그리고 위에서 해당 배열을 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">형식화 배열</a>,
  127. <code class="notranslate" translate="no">Float32Array</code>로 변환했죠. <a href="/docs/#api/ko/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>는 순수 배열이 아니라 형식화 배열을
  128. 인자로 받습니다. 또한 하나의 꼭지점에 몇 개의 요소를 사용할 지 지정해줘야 하죠.
  129. 위치(position)과 법선(normal)의 경우 x, y, z 총 3개이고, UV는 u, v 총 2개입니다.</p>
  130. <p></p><div translate="no" class="threejs_example_container notranslate">
  131. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-cube.html"></iframe></div>
  132. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube.html" target="_blank">새 탭에서 보기</a>
  133. </div>
  134. <p></p>
  135. <p>데이터가 너무 많네요. 크게 구조를 바꾸긴 어렵지만, 꼭지점을 인덱스로 참조하게끔 바꾸면
  136. 조금 나을 듯합니다. 정육면체 데이터를 다시 봅시다. 각 면은 2개의 삼각형, 삼각형 하나에는
  137. 꼭지점이 3개 있으므로 면 하나에는 총 6개의 꼭지점이 있습니다. 하지만 이 중 꼭지점 2개는
  138. 완전히 같죠(같은 위치, 같은 법선, 같은 uv). 중복된 꼭지점을 제거하고 인덱스로 데이터를
  139. 불러오도록 바꿀 수 있겠네요. 먼저 중복된 꼭지점을 제거하겠습니다.</p>
  140. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const vertices = [
  141. // 앞쪽
  142. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 0], }, // 0
  143. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], }, // 1
  144. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], }, // 2
  145. -
  146. - { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  147. - { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  148. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], }, // 3
  149. // 오른쪽
  150. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], }, // 4
  151. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], }, // 5
  152. -
  153. - { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  154. - { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  155. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], }, // 6
  156. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], }, // 7
  157. // 뒤쪽
  158. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], }, // 8
  159. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], }, // 9
  160. -
  161. - { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  162. - { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  163. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], }, // 10
  164. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], }, // 11
  165. // 왼쪽
  166. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], }, // 12
  167. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], }, // 13
  168. -
  169. - { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  170. - { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  171. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], }, // 14
  172. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], }, // 15
  173. // 상단
  174. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], }, // 16
  175. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], }, // 17
  176. -
  177. - { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  178. - { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  179. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], }, // 18
  180. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], }, // 19
  181. // 하단
  182. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], }, // 20
  183. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], }, // 21
  184. -
  185. - { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  186. - { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  187. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], }, // 22
  188. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], }, // 23
  189. ];
  190. </pre>
  191. <p>이제 꼭지점 24개만 남았습니다. 중복되는 꼭지점은 없죠. 이제 <a href="/docs/#api/ko/core/BufferGeometry.setIndex"><code class="notranslate" translate="no">BufferGeometry.setIndex</code></a>
  192. 메서드에 각 꼭지점 데이터의 인덱스값 36개-삼각형이 12개이므로-를 넘겨줍니다.</p>
  193. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.setAttribute(
  194. 'position',
  195. new THREE.BufferAttribute(positions, positionNumComponents));
  196. geometry.setAttribute(
  197. 'normal',
  198. new THREE.BufferAttribute(normals, normalNumComponents));
  199. geometry.setAttribute(
  200. 'uv',
  201. new THREE.BufferAttribute(uvs, uvNumComponents));
  202. +geometry.setIndex([
  203. + 0, 1, 2, 2, 1, 3, // 앞쪽
  204. + 4, 5, 6, 6, 5, 7, // 오른쪽
  205. + 8, 9, 10, 10, 9, 11, // 뒤쪽
  206. + 12, 13, 14, 14, 13, 15, // 왼쪽
  207. + 16, 17, 18, 18, 17, 19, // 상단
  208. + 20, 21, 22, 22, 21, 23, // 하단
  209. +]);
  210. </pre>
  211. <p></p><div translate="no" class="threejs_example_container notranslate">
  212. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-cube-indexed.html"></iframe></div>
  213. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube-indexed.html" target="_blank">새 탭에서 보기</a>
  214. </div>
  215. <p></p>
  216. <p><a href="/docs/#api/ko/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>에 법선을 지정하지 않았다면 <a href="/docs/#api/ko/core/BufferGeometry#computeVertexNormals"><code class="notranslate" translate="no">computeVertexNormals</code></a>
  217. 메서드를 호출해 자동으로 법선을 지정할 수 있습니다. 다만 데이터가 조금이라도 다르다면
  218. 꼭지점을 공유할 수 없기에 구체나 원통 같은 물체를 만들려 하는 경우에는 이음새가 보일 수 있어요.</p>
  219. <div class="spread">
  220. <div>
  221. <div data-diagram="bufferGeometryCylinder"></div>
  222. <div class="code">BufferGeometry</div>
  223. </div>
  224. <div>
  225. <div data-diagram="geometryCylinder"></div>
  226. <div class="code">Geometry</div>
  227. </div>
  228. </div>
  229. <p>위 두 원통은 <code class="notranslate" translate="no">computeVertexNormals</code> 메서드를 호출해 법선을 생성했습니다. 자세히 보면
  230. 왼쪽 원통에 이음매가 있는 게 보일 텐데, 이는 원통의 끝과 시작점의 UV가 달라 꼭지점을
  231. 공유할 수 없기 때문입니다. 그다지 큰 문제는 아니지만, 알아두어 나쁠 건 없죠. 이를 해결하려면
  232. 법선을 직접 지정해야 합니다.</p>
  233. <p>아까는 처음에 순수 배열을 썼지만, 처음부터 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">형식화 배열</a>을
  234. 사용할 수도 있습니다. 다만 형식화 배열은 초기화할 때 미리 크기를 지정해줘야 합니다.
  235. 그렇게 어려운 일은 아니지만, 순수 배열은 <code class="notranslate" translate="no">push</code> 메서드로 요소를 추가하고 바뀐
  236. 길이를 <code class="notranslate" translate="no">length</code> 속성으로 확인할 수 있습니다. 하지만 형식화 배열을 사용하면 어디서
  237. 요소를 추가했는지 직접 일일이 기록해야 하죠.</p>
  238. <p>예제의 경우는 사전에 정해진 데이터를 사용하기에 배열 길이를 미리 구하는 게 어렵지
  239. 않습니다.</p>
  240. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const positions = [];
  241. -const normals = [];
  242. -const uvs = [];
  243. +const numVertices = vertices.length;
  244. +const positionNumComponents = 3;
  245. +const normalNumComponents = 3;
  246. +const uvNumComponents = 2;
  247. +const positions = new Float32Array(numVertices * positionNumComponents);
  248. +const normals = new Float32Array(numVertices * normalNumComponents);
  249. +const uvs = new Float32Array(numVertices * uvNumComponents);
  250. +let posNdx = 0;
  251. +let nrmNdx = 0;
  252. +let uvNdx = 0;
  253. for (const vertex of vertices) {
  254. - positions.push(...vertex.pos);
  255. - normals.push(...vertex.norm);
  256. - uvs.push(...vertex.uv);
  257. + positions.set(vertex.pos, posNdx);
  258. + normals.set(vertex.norm, nrmNdx);
  259. + uvs.set(vertex.uv, uvNdx);
  260. + posNdx += positionNumComponents;
  261. + nrmNdx += normalNumComponents;
  262. + uvNdx += uvNumComponents;
  263. }
  264. geometry.setAttribute(
  265. 'position',
  266. - new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  267. + new THREE.BufferAttribute(positions, positionNumComponents));
  268. geometry.setAttribute(
  269. 'normal',
  270. - new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  271. + new THREE.BufferAttribute(normals, normalNumComponents));
  272. geometry.setAttribute(
  273. 'uv',
  274. - new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  275. + new THREE.BufferAttribute(uvs, uvNumComponents));
  276. geometry.setIndex([
  277. 0, 1, 2, 2, 1, 3, // 앞쪽
  278. 4, 5, 6, 6, 5, 7, // 오른쪽
  279. 8, 9, 10, 10, 9, 11, // 뒤쪽
  280. 12, 13, 14, 14, 13, 15, // 왼쪽
  281. 16, 17, 18, 18, 17, 19, // 상단
  282. 20, 21, 22, 22, 21, 23, // 하단
  283. ]);
  284. </pre>
  285. <p></p><div translate="no" class="threejs_example_container notranslate">
  286. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-cube-typedarrays.html"></iframe></div>
  287. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube-typedarrays.html" target="_blank">새 탭에서 보기</a>
  288. </div>
  289. <p></p>
  290. <p>꼭지점의 일부를 수정하고 싶다면 형식화 배열을 사용하는 게 좋습니다.</p>
  291. <p>뭔가 꼭지점을 동적으로 수정하는 예제를 찾아보려고 노력했는데, 찾기가 어렵네요.
  292. 그냥 구체를 만들고 구체의 각 사분면(quad)을 중심으로부터 안팎으로 움직여보겠습니다.</p>
  293. <p>아래는 구체의 위치값과 인덱스를 생성하는 코드입니다. 사분면 내의 꼭지점은 서로
  294. 공유할 수 있지만, 사분면은 각각 움직여야 하므로 사분면끼리는 공유하지 않도록
  295. 합니다.</p>
  296. <p>구체의 정점을 구하기 위해 좀 더 복잡한 방법을 쓸 수도 있지만, 귀찮으니 그냥
  297. <a href="/docs/#api/ko/core/Object3D"><code class="notranslate" translate="no">Object3D</code></a> 3개를 계층 구조로 배열해 쓰겠습니다. 이 방식에 대해서는 <a href="optimize-lots-of-objects.html">많은 물체를
  298. 최적화하는 방법</a>에서 더 자세히 다루겠습니다.</p>
  299. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeSpherePositions(segmentsAround, segmentsDown) {
  300. const numVertices = segmentsAround * segmentsDown * 6;
  301. const numComponents = 3;
  302. const positions = new Float32Array(numVertices * numComponents);
  303. const indices = [];
  304. const longHelper = new THREE.Object3D();
  305. const latHelper = new THREE.Object3D();
  306. const pointHelper = new THREE.Object3D();
  307. longHelper.add(latHelper);
  308. latHelper.add(pointHelper);
  309. pointHelper.position.z = 1;
  310. const temp = new THREE.Vector3();
  311. function getPoint(lat, long) {
  312. latHelper.rotation.x = lat;
  313. longHelper.rotation.y = long;
  314. longHelper.updateMatrixWorld(true);
  315. return pointHelper.getWorldPosition(temp).toArray();
  316. }
  317. let posNdx = 0;
  318. let ndx = 0;
  319. for (let down = 0; down &lt; segmentsDown; ++down) {
  320. const v0 = down / segmentsDown;
  321. const v1 = (down + 1) / segmentsDown;
  322. const lat0 = (v0 - 0.5) * Math.PI;
  323. const lat1 = (v1 - 0.5) * Math.PI;
  324. for (let across = 0; across &lt; segmentsAround; ++across) {
  325. const u0 = across / segmentsAround;
  326. const u1 = (across + 1) / segmentsAround;
  327. const long0 = u0 * Math.PI * 2;
  328. const long1 = u1 * Math.PI * 2;
  329. positions.set(getPoint(lat0, long0), posNdx); posNdx += numComponents;
  330. positions.set(getPoint(lat1, long0), posNdx); posNdx += numComponents;
  331. positions.set(getPoint(lat0, long1), posNdx); posNdx += numComponents;
  332. positions.set(getPoint(lat1, long1), posNdx); posNdx += numComponents;
  333. indices.push(
  334. ndx, ndx + 1, ndx + 2,
  335. ndx + 2, ndx + 1, ndx + 3,
  336. );
  337. ndx += 4;
  338. }
  339. }
  340. return { positions, indices };
  341. }
  342. </pre>
  343. <p>만든 함수를 다음처럼 호출합니다.</p>
  344. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const segmentsAround = 24;
  345. const segmentsDown = 16;
  346. const { positions, indices } = makeSpherePositions(segmentsAround, segmentsDown);
  347. </pre>
  348. <p>여기서 반환된 위치값(positions)은 구체의 위치값을 기반으로 합니다. 이는 법선의 값(normals)과
  349. 같으니 법선을 따로 구할 필요 없이 위치값을 복사해서 쓰면 됩니다.</p>
  350. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const normals = positions.slice();
  351. </pre>
  352. <p>다음으로 각 속성을 지정합니다.</p>
  353. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const geometry = new THREE.BufferGeometry();
  354. const positionNumComponents = 3;
  355. const normalNumComponents = 3;
  356. +const positionAttribute = new THREE.BufferAttribute(positions, positionNumComponents);
  357. +positionAttribute.setUsage(THREE.DynamicDrawUsage);
  358. geometry.setAttribute(
  359. 'position',
  360. + positionAttribute);
  361. geometry.setAttribute(
  362. 'normal',
  363. new THREE.BufferAttribute(normals, normalNumComponents));
  364. geometry.setIndex(indices);
  365. </pre>
  366. <p>예제에서 차이점이 있는 부분을 표시해 두었습니다. 위치 속성에 참조값을 넘겨주었고, 이
  367. 속성이 동적이라고 명시했습니다. 이는 Three.js에게 해당 속성을 자주 변경될 수 있음을
  368. 알려주는 역할이죠.</p>
  369. <p>이제 <code class="notranslate" translate="no">render</code> 함수에서 매 프레임마다 법선을 기준으로 위치값을 변경해줍니다.</p>
  370. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const temp = new THREE.Vector3();
  371. ...
  372. for (let i = 0; i &lt; positions.length; i += 3) {
  373. const quad = (i / 12 | 0);
  374. const ringId = quad / segmentsAround | 0;
  375. const ringQuadId = quad % segmentsAround;
  376. const ringU = ringQuadId / segmentsAround;
  377. const angle = ringU * Math.PI * 2;
  378. temp.fromArray(normals, i);
  379. temp.multiplyScalar(THREE.MathUtils.lerp(1, 1.4, Math.sin(time + ringId + angle) * .5 + .5));
  380. temp.toArray(positions, i);
  381. }
  382. positionAttribute.needsUpdate = true;
  383. </pre>
  384. <p>마지막으로 <code class="notranslate" translate="no">positionAttribute.needsUpdate</code> 속성을 활성화해 변화를 감지하도록 합니다.</p>
  385. <p></p><div translate="no" class="threejs_example_container notranslate">
  386. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-dynamic.html"></iframe></div>
  387. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-dynamic.html" target="_blank">새 탭에서 보기</a>
  388. </div>
  389. <p></p>
  390. <p>이 글이 <a href="/docs/#api/ko/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>로 사용자 지정 geometry를 만들고, <a href="/docs/#api/ko/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>를 다루는
  391. 데 도움이 되었으면 좋겠습니다.</p>
  392. <p><canvas id="c"></canvas></p>
  393. <script type="module" src="../resources/threejs-custom-buffergeometry.js"></script>
  394. </div>
  395. </div>
  396. </div>
  397. <script src="../resources/prettify.js"></script>
  398. <script src="../resources/lesson.js"></script>
  399. </body></html>