custom-buffergeometry.html 25 KB

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