custom-buffergeometry.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <!DOCTYPE html><html lang="ja"><head>
  2. <meta charset="utf-8">
  3. <title>のカスタムバッファジオメトリ</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 – のカスタムバッファジオメトリ">
  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. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>のカスタムバッファジオメトリ</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p><a href="/docs/#api/ja/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>は全てのジオメトリを表現する方法です。
  29. BufferGeometryは<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>を使います。1つの<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>はジオメトリを作るための1種類のデータに対応しています。vertexの位置情報を格納するための<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>、color情報を格納するための<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>、normal情報を格納するための<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>がそれぞれあります。</p>
  30. <div class="threejs_center"><img src="../resources/threejs-attributes.svg" style="width: 700px"></div>
  31. <p>上の図では<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>それぞれのattribute情報を格納した<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>を表しています。これらは<em>並列な配列</em>です。<em>並列な配列</em>というのはN番目にあるデータはN番目のvertexに対応しており、それがattributeの数だけあるという意味です。図ではindex=4のattributeがハイライトされています。</p>
  32. <div class="threejs_center"><img src="../resources/cube-faces-vertex.svg" style="width: 500px"></div>
  33. <p>上の図のハイライトされたvertexには、このvertexに接する全ての面に異なるnormalが必要です。normalとはどの方向を向いているかの情報です。
  34. この図ではnormalは角の頂点の周りの矢印で示されており、その頂点に接する全ての面には異なる方向を指すnormalが必要です。</p>
  35. <p>同様に面ごとに違うUVも必要です。
  36. UVはテクスチャのどの部分に頂点位置が対応しているか指定するテクスチャ座標です。
  37. 緑の面はFテクスチャの右上に対応するUV、青い面は左上に対応するUV、赤の面は左下に対応したUVが必要な事が分かります。</p>
  38. <p>単一のvertexはこれらの情報の合成として表現されます。
  39. 頂点が異なる部分を必要とする場合、それは異なる頂点でなければなりません。</p>
  40. <p>簡単な例として<a href="/docs/#api/ja/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>を使って立方体を作ってみましょう。立方体を例にするのはvertexがfaceによって共有されているように見えて実は共有されていないからです。この例ではまずすべてのvertexの情報をリストアップして並列の配列に変換して<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>を作り、最後に<a href="/docs/#api/ja/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>を作ります。</p>
  41. <p>立方体に必要な情報をすべてリストアップします。<code class="notranslate" translate="no">Geometry</code>では1つのvertexを複数のfaceで共有できましたが今回は共有できないことに注意してください。つまり1つの立方体を作るために36個のvertexが必要になります。1つの面につき2つの三角形、1つの三角形につき3つのvertex、これが6面あるので36個のvertexが必要になる計算です。</p>
  42. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const vertices = [
  43. // front
  44. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 0], },
  45. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  46. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  47. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  48. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  49. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], },
  50. // right
  51. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], },
  52. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  53. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  54. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  55. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  56. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], },
  57. // back
  58. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], },
  59. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  60. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  61. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  62. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  63. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], },
  64. // left
  65. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], },
  66. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  67. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  68. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  69. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  70. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], },
  71. // top
  72. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], },
  73. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  74. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  75. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  76. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  77. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], },
  78. // bottom
  79. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], },
  80. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  81. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  82. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  83. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  84. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], },
  85. ];
  86. </pre>
  87. <p>次にこれを3つの並列な配列に変換します。
  88. (訳註:並列な配列<em>parallel arrays</em>とは例えば頂点を指定する配列と色を指定する配列があり1つの頂点をレンダリングするために2つの配列の同じインデックスの要素を指定するような使われ方をする配列のことです。次の例ではpositions, normals, uvsの3つの配列が並列の配列として使われています)</p>
  89. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const positions = [];
  90. const normals = [];
  91. const uvs = [];
  92. for (const vertex of vertices) {
  93. positions.push(...vertex.pos);
  94. normals.push(...vertex.norm);
  95. uvs.push(...vertex.uv);
  96. }
  97. </pre>
  98. <p>最後にそれぞれの配列に対して<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>を作り<a href="/docs/#api/ja/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>に指定します。</p>
  99. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const geometry = new THREE.BufferGeometry();
  100. const positionNumComponents = 3;
  101. const normalNumComponents = 3;
  102. const uvNumComponents = 2;
  103. geometry.setAttribute(
  104. 'position',
  105. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  106. geometry.setAttribute(
  107. 'normal',
  108. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  109. geometry.setAttribute(
  110. 'uv',
  111. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  112. </pre>
  113. <p>名前の付け方に注意してください。three.jsで決められている名前以外を指定することはできません(カスタムシェーダーを使用する場合は別です)。<code class="notranslate" translate="no">position</code>, <code class="notranslate" translate="no">normal</code>, <code class="notranslate" translate="no">uv</code>はthree.jsで決められている名前です。ここでは指定していませんが<code class="notranslate" translate="no">color</code>も指定可能です。</p>
  114. <p>上の例では<code class="notranslate" translate="no">positions</code>, <code class="notranslate" translate="no">normals</code>, <code class="notranslate" translate="no">uvs</code>の3つのJavaScriptのネイティブ配列を作りました。次に<code class="notranslate" translate="no">Float32Array</code>型の<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">TypedArrays</a>に変換します。<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>はネイティブ配列ではなくTypedArrayである必要があります。さらにそれぞれの<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>に対して「1つのvertexに対していくつの要素が必要か」を指定する必要があります。例えばpositionやnormalsは3次元なので1つのvertexつき3つの要素を必要とします。UVはテクスチャ上の2次元の点なので2つの要素を必要とします。</p>
  115. <p></p><div translate="no" class="threejs_example_container notranslate">
  116. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-cube.html"></iframe></div>
  117. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  118. </div>
  119. <p></p>
  120. <p>かなり大量のデータです。この配列からvertexを選ぶときにはインデックスを使います。1つの三角形は3つのvertexで構成されていて2つの三角形が1つのfaceを作っています。これが6枚で1つの立方体を構成しています。1つのfaceを構成する2つの三角形を作っているvertexは2つが同じデータを持っています。position, normal, UVすべて同じです。そこで重複しているデータを1つ消して1つにして、そのデータを別のインデックスで指定します。</p>
  121. <p>ではまず重複したデータを1つにします。</p>
  122. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const vertices = [
  123. // front
  124. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 0], }, // 0
  125. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], }, // 1
  126. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], }, // 2
  127. -
  128. - { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  129. - { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  130. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], }, // 3
  131. // right
  132. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], }, // 4
  133. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], }, // 5
  134. -
  135. - { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  136. - { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  137. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], }, // 6
  138. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], }, // 7
  139. // back
  140. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], }, // 8
  141. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], }, // 9
  142. -
  143. - { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  144. - { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  145. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], }, // 10
  146. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], }, // 11
  147. // left
  148. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], }, // 12
  149. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], }, // 13
  150. -
  151. - { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  152. - { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  153. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], }, // 14
  154. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], }, // 15
  155. // top
  156. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], }, // 16
  157. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], }, // 17
  158. -
  159. - { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  160. - { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  161. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], }, // 18
  162. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], }, // 19
  163. // bottom
  164. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], }, // 20
  165. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], }, // 21
  166. -
  167. - { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  168. - { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  169. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], }, // 22
  170. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], }, // 23
  171. ];
  172. </pre>
  173. <p>はい、24個になりました。これに対して36個のインデックスを指定して36個のvertexを作ります。<a href="/docs/#api/ja/core/BufferGeometry.setIndex"><code class="notranslate" translate="no">BufferGeometry.setIndex</code></a>により36個のインデックスを使って12個の三角形を作ります。</p>
  174. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.setAttribute(
  175. 'position',
  176. new THREE.BufferAttribute(positions, positionNumComponents));
  177. geometry.setAttribute(
  178. 'normal',
  179. new THREE.BufferAttribute(normals, normalNumComponents));
  180. geometry.setAttribute(
  181. 'uv',
  182. new THREE.BufferAttribute(uvs, uvNumComponents));
  183. +geometry.setIndex([
  184. + 0, 1, 2, 2, 1, 3, // front
  185. + 4, 5, 6, 6, 5, 7, // right
  186. + 8, 9, 10, 10, 9, 11, // back
  187. + 12, 13, 14, 14, 13, 15, // left
  188. + 16, 17, 18, 18, 17, 19, // top
  189. + 20, 21, 22, 22, 21, 23, // bottom
  190. +]);
  191. </pre>
  192. <p></p><div translate="no" class="threejs_example_container notranslate">
  193. <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>
  194. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube-indexed.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  195. </div>
  196. <p></p>
  197. <p><code class="notranslate" translate="no">Geometry</code>と同じように<a href="/docs/#api/ja/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>も<a href="/docs/#api/ja/core/BufferGeometry#computeVertexNormals"><code class="notranslate" translate="no">computeVertexNormals</code></a>メソッドを持っています。これは特に指定がない場合に自動的にnormalを計算するメソッドです。ただし<code class="notranslate" translate="no">Geometry</code>の場合と違いvertexがfaceによって共有されていないために<code class="notranslate" translate="no">computeVertexNormals</code>の結果も少し違います。</p>
  198. <div class="spread">
  199. <div>
  200. <div data-diagram="bufferGeometryCylinder"></div>
  201. </div>
  202. </div>
  203. <p>シリンダーで<code class="notranslate" translate="no">computeVertexNormals</code>の違いを比較してみましょう。よく見ると左のシリンダーには縫い目が見えると思います。これはvertexを共有することができないためにUVも異なるためです。ちょっとしたことですが、気になるときは自分でnormalを指定すれば良いだけです。</p>
  204. <p>ネイティブの配列を使う代わりに<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">TypedArrays</a>を使うこともできます。TypedArrayは最初に配列の大きさを指定する必要があるため少し面倒です。ネイティブの配列は<code class="notranslate" translate="no">push</code>で追加して<code class="notranslate" translate="no">length</code>で配列の長さを確認することができます。TypedArrayには<code class="notranslate" translate="no">push</code>メソッドがないのであらかじめ用意した配列に注意しながら要素を入れていく必要があります。</p>
  205. <p>この例では最初に大きなデータを使っているので配列の長さを意識することはそれほど大変ではありません。</p>
  206. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const positions = [];
  207. -const normals = [];
  208. -const uvs = [];
  209. +const numVertices = vertices.length;
  210. +const positionNumComponents = 3;
  211. +const normalNumComponents = 3;
  212. +const uvNumComponents = 2;
  213. +const positions = new Float32Array(numVertices * positionNumComponents);
  214. +const normals = new Float32Array(numVertices * normalNumComponents);
  215. +const uvs = new Float32Array(numVertices * uvNumComponents);
  216. +let posNdx = 0;
  217. +let nrmNdx = 0;
  218. +let uvNdx = 0;
  219. for (const vertex of vertices) {
  220. - positions.push(...vertex.pos);
  221. - normals.push(...vertex.norm);
  222. - uvs.push(...vertex.uv);
  223. + positions.set(vertex.pos, posNdx);
  224. + normals.set(vertex.norm, nrmNdx);
  225. + uvs.set(vertex.uv, uvNdx);
  226. + posNdx += positionNumComponents;
  227. + nrmNdx += normalNumComponents;
  228. + uvNdx += uvNumComponents;
  229. }
  230. geometry.setAttribute(
  231. 'position',
  232. - new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  233. + new THREE.BufferAttribute(positions, positionNumComponents));
  234. geometry.setAttribute(
  235. 'normal',
  236. - new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  237. + new THREE.BufferAttribute(normals, normalNumComponents));
  238. geometry.setAttribute(
  239. 'uv',
  240. - new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  241. + new THREE.BufferAttribute(uvs, uvNumComponents));
  242. geometry.setIndex([
  243. 0, 1, 2, 2, 1, 3, // front
  244. 4, 5, 6, 6, 5, 7, // right
  245. 8, 9, 10, 10, 9, 11, // back
  246. 12, 13, 14, 14, 13, 15, // left
  247. 16, 17, 18, 18, 17, 19, // top
  248. 20, 21, 22, 22, 21, 23, // bottom
  249. ]);
  250. </pre>
  251. <p></p><div translate="no" class="threejs_example_container notranslate">
  252. <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>
  253. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube-typedarrays.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  254. </div>
  255. <p></p>
  256. <p>TypedArrayはプログラムが走っている状態でvertexの編集をしたいときに便利です。</p>
  257. <p>良い例が思いつかないのでとりあえずメッシュの四角形が出たり入ったりする球体を作ってみます。</p>
  258. <p>球体の位置とindexを生成するコードです。四角形の中でvertexを共有していますが四角形と四角形でvertexを共有することはありません。共有してしまうと1つの四角形が出たり入ったりするたびに隣の四角形が移動してしまいます。今回は別々に移動させたいのでそうしています。</p>
  259. <p>面倒なので3つの<a href="/docs/#api/ja/core/Object3D"><code class="notranslate" translate="no">Object3D</code></a>階層を用意して球体のvertexを計算します。くわしくは<a href="optimize-lots-of-objects.html">たくさんのオブジェクトを最適化するこの記事</a>をご覧ください。</p>
  260. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeSpherePositions(segmentsAround, segmentsDown) {
  261. const numVertices = segmentsAround * segmentsDown * 6;
  262. const numComponents = 3;
  263. const positions = new Float32Array(numVertices * numComponents);
  264. const indices = [];
  265. const longHelper = new THREE.Object3D();
  266. const latHelper = new THREE.Object3D();
  267. const pointHelper = new THREE.Object3D();
  268. longHelper.add(latHelper);
  269. latHelper.add(pointHelper);
  270. pointHelper.position.z = 1;
  271. const temp = new THREE.Vector3();
  272. function getPoint(lat, long) {
  273. latHelper.rotation.x = lat;
  274. longHelper.rotation.y = long;
  275. longHelper.updateMatrixWorld(true);
  276. return pointHelper.getWorldPosition(temp).toArray();
  277. }
  278. let posNdx = 0;
  279. let ndx = 0;
  280. for (let down = 0; down &lt; segmentsDown; ++down) {
  281. const v0 = down / segmentsDown;
  282. const v1 = (down + 1) / segmentsDown;
  283. const lat0 = (v0 - 0.5) * Math.PI;
  284. const lat1 = (v1 - 0.5) * Math.PI;
  285. for (let across = 0; across &lt; segmentsAround; ++across) {
  286. const u0 = across / segmentsAround;
  287. const u1 = (across + 1) / segmentsAround;
  288. const long0 = u0 * Math.PI * 2;
  289. const long1 = u1 * Math.PI * 2;
  290. positions.set(getPoint(lat0, long0), posNdx); posNdx += numComponents;
  291. positions.set(getPoint(lat1, long0), posNdx); posNdx += numComponents;
  292. positions.set(getPoint(lat0, long1), posNdx); posNdx += numComponents;
  293. positions.set(getPoint(lat1, long1), posNdx); posNdx += numComponents;
  294. indices.push(
  295. ndx, ndx + 1, ndx + 2,
  296. ndx + 2, ndx + 1, ndx + 3,
  297. );
  298. ndx += 4;
  299. }
  300. }
  301. return {positions, indices};
  302. }
  303. </pre>
  304. <p>こんな感じです。</p>
  305. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const segmentsAround = 24;
  306. const segmentsDown = 16;
  307. const {positions, indices} = makeSpherePositions(segmentsAround, segmentsDown);
  308. </pre>
  309. <p>returnされているpositionは単位球(半径が1の球体)なのでそのままこのデータをnormalに使えます。</p>
  310. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const normals = positions.slice();
  311. </pre>
  312. <p>attributeも設定しましょう。</p>
  313. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const geometry = new THREE.BufferGeometry();
  314. const positionNumComponents = 3;
  315. const normalNumComponents = 3;
  316. +const positionAttribute = new THREE.BufferAttribute(positions, positionNumComponents);
  317. +positionAttribute.setUsage(THREE.DynamicDrawUsage);
  318. geometry.setAttribute(
  319. 'position',
  320. + positionAttribute);
  321. geometry.setAttribute(
  322. 'normal',
  323. new THREE.BufferAttribute(normals, normalNumComponents));
  324. geometry.setIndex(indices);
  325. </pre>
  326. <p>position attributeに対する参照を保存しています。dynamicに指定しているところも注意が必要です。これはTHREE.jsに「これからこのattributeは変更が加えられる」ことを教えます。renderループではpositionを毎度アップデートします。</p>
  327. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const temp = new THREE.Vector3();
  328. ...
  329. for (let i = 0; i &lt; positions.length; i += 3) {
  330. const quad = (i / 12 | 0);
  331. const ringId = quad / segmentsAround | 0;
  332. const ringQuadId = quad % segmentsAround;
  333. const ringU = ringQuadId / segmentsAround;
  334. const angle = ringU * Math.PI * 2;
  335. temp.fromArray(normals, i);
  336. temp.multiplyScalar(THREE.MathUtils.lerp(1, 1.4, Math.sin(time + ringId + angle) * .5 + .5));
  337. temp.toArray(positions, i);
  338. }
  339. positionAttribute.needsUpdate = true;
  340. </pre>
  341. <p>最後に<code class="notranslate" translate="no">positionAttribute.needsUpdate</code>を設定してTHREE.jsに変更が必要であることを伝えます。</p>
  342. <p></p><div translate="no" class="threejs_example_container notranslate">
  343. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-dynamic.html"></iframe></div>
  344. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-dynamic.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  345. </div>
  346. <p></p>
  347. <p><a href="/docs/#api/ja/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>を作って<a href="/docs/#api/ja/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>をアップデートする方法を紹介しました。</p>
  348. <p><canvas id="c"></canvas></p>
  349. <script type="module" src="../resources/threejs-custom-buffergeometry.js"></script>
  350. </div>
  351. </div>
  352. </div>
  353. <script src="../resources/prettify.js"></script>
  354. <script src="../resources/lesson.js"></script>
  355. </body></html>