custom-buffergeometry.html 25 KB

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