custom-buffergeometry.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <!DOCTYPE html><html lang="zh"><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. <link rel="stylesheet" href="/manual/zh/lang.css">
  14. </head>
  15. <body>
  16. <div class="container">
  17. <div class="lesson-title">
  18. <h1>自定义缓冲几何体</h1>
  19. </div>
  20. <div class="lesson">
  21. <div class="lesson-main">
  22. <p>在three.js中, <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> 是用来代表所有几何体的一种方式。 <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> 本质上是一系列 <a href="/docs/#api/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>s 的 <em>名称</em> 。每一个 <a href="/docs/#api/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a> 代表一种类型数据的数组:位置,法线,颜色,uv,等等…… 这些合起来, <a href="/docs/#api/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>s 代表每个顶点所有数据的 <em>并行数组</em> 。</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> 。
  25. 它们指的是 <em>并行数组</em> ,代表每个属性的第N个数据集属于同一个顶点。index=4的顶点被高亮表示贯穿所有属性的平行数据定义一个顶点。</p>
  26. <p>这就告诉我们,这是一个方块的数据图,高亮的地方代表一个角。</p>
  27. <div class="threejs_center"><img src="../resources/cube-faces-vertex.svg" style="width: 500px"></div>
  28. <p>考虑下方块的单个角,不同的面都需要一个不同的法线。法线是面朝向的信息。在图中,在方块的角周围用箭头表示的法线,代表共用顶点位置的面需要指向不同方向的法线。</p>
  29. <p>同理,一个角在不同的面需要不同的UVs。UVs是用来指定纹理区域中,画在相应顶点位置三角形的纹理坐标。你可以看到,绿色的面需要顶点的UV对应于F纹理的右上角,蓝色的面需要的UV对应于F纹理的左上角,红色的面需要的UV对应于F纹理的左下角。</p>
  30. <p>一个简单的 <em>顶点</em> 是所有组成部分的集合。如果顶点需要其中任一部分变得不同,那么它必须是一个不同的顶点。</p>
  31. <p>举一个简单的例子,让我们创建一个使用 <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> 的方块。方块很有趣,因为它看起来在角的地方共用顶点但实际上不是。在我们的例子中,我们将列出所有顶点数据,然后转化成并行数组,最后用它们创建 <a href="/docs/#api/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a>s 并添加到 <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> 。</p>
  32. <p>我们从方块所需的所有数据开始。再次记住如果顶点有任何独一无二的部分,它必须是不同的顶点。像这里创建一个方块需要36个顶点,每个面2个三角形,每个三角形3个顶点,6个面=36个顶点。</p>
  33. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const vertices = [
  34. // front
  35. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 0], },
  36. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  37. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  38. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  39. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  40. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], },
  41. // right
  42. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], },
  43. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  44. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  45. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  46. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  47. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], },
  48. // back
  49. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], },
  50. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  51. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  52. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  53. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  54. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], },
  55. // left
  56. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], },
  57. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  58. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  59. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  60. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  61. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], },
  62. // top
  63. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], },
  64. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  65. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  66. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  67. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  68. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], },
  69. // bottom
  70. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], },
  71. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  72. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  73. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  74. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  75. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], },
  76. ];
  77. </pre>
  78. <p>然后我们能将它们全部转换成3个并行数组</p>
  79. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const positions = [];
  80. const normals = [];
  81. const uvs = [];
  82. for (const vertex of vertices) {
  83. positions.push(...vertex.pos);
  84. normals.push(...vertex.norm);
  85. uvs.push(...vertex.uv);
  86. }
  87. </pre>
  88. <p>最终我们能创建一个 <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> ,然后为每个数组创建一个 <a href="/docs/#api/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a> 并添加到 <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> 。</p>
  89. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const geometry = new THREE.BufferGeometry();
  90. const positionNumComponents = 3;
  91. const normalNumComponents = 3;
  92. const uvNumComponents = 2;
  93. geometry.setAttribute(
  94. 'position',
  95. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  96. geometry.setAttribute(
  97. 'normal',
  98. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  99. geometry.setAttribute(
  100. 'uv',
  101. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  102. </pre>
  103. <p>注意名字很重要。你必须将属性的名字命名成three.js所期望的(除非你正在创建自定义着色器),在这里是 <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> 。</p>
  104. <p>在上面我们创建了3个JavaScript原生数组, <code class="notranslate" translate="no">positions</code>, <code class="notranslate" translate="no">normals</code> 和 <code class="notranslate" translate="no">uvs</code> 。
  105. 然后我们将他们转换为 <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/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a> 是类型数组而不是原生数组。同时 <a href="/docs/#api/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a> 需要你设定每个顶点有多少组成成分。对于位置和法线,每个顶点我们需要3个组成成分,x、y和z。对于UVs我们需要2个,u和v。</p>
  106. <p></p><div translate="no" class="threejs_example_container notranslate">
  107. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-cube.html"></iframe></div>
  108. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube.html" target="_blank">点击此处在新标签页中打开</a>
  109. </div>
  110. <p></p>
  111. <p>那会是大量的数据。我们可以做点改善,可以用索引来代表顶点。看回我们的方块数据,每个面由2个三角形组成,每个三角形3个顶点,总共6个,但是其中2个是完全一样的;同样的位置,同样的法线,和同样的uv。因此,我们可以移除匹配的顶点,然后用索引代表他们。首先我们移除匹配的顶点。</p>
  112. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const vertices = [
  113. // front
  114. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 0], }, // 0
  115. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], }, // 1
  116. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], }, // 2
  117. -
  118. - { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  119. - { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  120. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], }, // 3
  121. // right
  122. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], }, // 4
  123. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], }, // 5
  124. -
  125. - { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  126. - { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  127. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], }, // 6
  128. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], }, // 7
  129. // back
  130. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], }, // 8
  131. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], }, // 9
  132. -
  133. - { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  134. - { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  135. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], }, // 10
  136. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], }, // 11
  137. // left
  138. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], }, // 12
  139. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], }, // 13
  140. -
  141. - { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  142. - { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  143. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], }, // 14
  144. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], }, // 15
  145. // top
  146. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], }, // 16
  147. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], }, // 17
  148. -
  149. - { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  150. - { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  151. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], }, // 18
  152. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], }, // 19
  153. // bottom
  154. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], }, // 20
  155. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], }, // 21
  156. -
  157. - { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  158. - { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  159. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], }, // 22
  160. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], }, // 23
  161. ];
  162. </pre>
  163. <p>现在我们有24个唯一的顶点。然后我们为36个要画的顶点设定36个索引,通过调用 <a href="/docs/#api/zh/core/BufferGeometry.setIndex"><code class="notranslate" translate="no">BufferGeometry.setIndex</code></a> 并传入索引数组来创建12个三角形。</p>
  164. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.setAttribute(
  165. 'position',
  166. new THREE.BufferAttribute(positions, positionNumComponents));
  167. geometry.setAttribute(
  168. 'normal',
  169. new THREE.BufferAttribute(normals, normalNumComponents));
  170. geometry.setAttribute(
  171. 'uv',
  172. new THREE.BufferAttribute(uvs, uvNumComponents));
  173. +geometry.setIndex([
  174. + 0, 1, 2, 2, 1, 3, // front
  175. + 4, 5, 6, 6, 5, 7, // right
  176. + 8, 9, 10, 10, 9, 11, // back
  177. + 12, 13, 14, 14, 13, 15, // left
  178. + 16, 17, 18, 18, 17, 19, // top
  179. + 20, 21, 22, 22, 21, 23, // bottom
  180. +]);
  181. </pre>
  182. <p></p><div translate="no" class="threejs_example_container notranslate">
  183. <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>
  184. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube-indexed.html" target="_blank">点击此处在新标签页中打开</a>
  185. </div>
  186. <p></p>
  187. <p>如果你没有提供法线数据的话, <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> 有个方法<a href="/docs/#api/zh/core/BufferGeometry#computeVertexNormals"><code class="notranslate" translate="no">computeVertexNormals</code></a>可以用来计算法线。不幸的是,因为如果顶点的其他数据不同的话,位置数据不能被共享,调用 <code class="notranslate" translate="no">computeVertexNormals</code> 会让你的几何体像球面或者圆筒一样连接自身。</p>
  188. <div class="spread">
  189. <div>
  190. <div data-diagram="bufferGeometryCylinder"></div>
  191. </div>
  192. </div>
  193. <p>对于上面的圆筒,法线是通过 <code class="notranslate" translate="no">computeVertexNormals</code> 方法创建的。
  194. 如果你仔细观察会发现在圆筒上有条缝。这是因为在圆筒的开始和结束的地方没有办法共享顶点数据,需要不同的UVs,所以该方法不知道它们是同样的顶点以平滑过度。只要知道一点,解决方法是应用自己的法线数据。</p>
  195. <p>我们同样可以在一开始使用类型数组<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">TypedArrays</a>取代JavaScript的原生数组。
  196. 缺点是你必须在一开始定义数组的大小。当然那不是很难,但是使用原生数组我们只需要用 <code class="notranslate" translate="no">push</code> 将数据加入数组并最后通过 <code class="notranslate" translate="no">length</code> 查看数组大小。使用类型数组我们没有这样的方法,所以需要记录添加的数据。</p>
  197. <p>在这个例子,提前计算数组长度很简单,因为我们一开始使用一大块静态数据。</p>
  198. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const positions = [];
  199. -const normals = [];
  200. -const uvs = [];
  201. +const numVertices = vertices.length;
  202. +const positionNumComponents = 3;
  203. +const normalNumComponents = 3;
  204. +const uvNumComponents = 2;
  205. +const positions = new Float32Array(numVertices * positionNumComponents);
  206. +const normals = new Float32Array(numVertices * normalNumComponents);
  207. +const uvs = new Float32Array(numVertices * uvNumComponents);
  208. +let posNdx = 0;
  209. +let nrmNdx = 0;
  210. +let uvNdx = 0;
  211. for (const vertex of vertices) {
  212. - positions.push(...vertex.pos);
  213. - normals.push(...vertex.norm);
  214. - uvs.push(...vertex.uv);
  215. + positions.set(vertex.pos, posNdx);
  216. + normals.set(vertex.norm, nrmNdx);
  217. + uvs.set(vertex.uv, uvNdx);
  218. + posNdx += positionNumComponents;
  219. + nrmNdx += normalNumComponents;
  220. + uvNdx += uvNumComponents;
  221. }
  222. geometry.setAttribute(
  223. 'position',
  224. - new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  225. + new THREE.BufferAttribute(positions, positionNumComponents));
  226. geometry.setAttribute(
  227. 'normal',
  228. - new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  229. + new THREE.BufferAttribute(normals, normalNumComponents));
  230. geometry.setAttribute(
  231. 'uv',
  232. - new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  233. + new THREE.BufferAttribute(uvs, uvNumComponents));
  234. geometry.setIndex([
  235. 0, 1, 2, 2, 1, 3, // front
  236. 4, 5, 6, 6, 5, 7, // right
  237. 8, 9, 10, 10, 9, 11, // back
  238. 12, 13, 14, 14, 13, 15, // left
  239. 16, 17, 18, 18, 17, 19, // top
  240. 20, 21, 22, 22, 21, 23, // bottom
  241. ]);
  242. </pre>
  243. <p></p><div translate="no" class="threejs_example_container notranslate">
  244. <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>
  245. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-cube-typedarrays.html" target="_blank">点击此处在新标签页中打开</a>
  246. </div>
  247. <p></p>
  248. <p>一个使用类型数组的好理由,是如果你想动态更新顶点数据的任何一部分。</p>
  249. <p>因为想不起动态更新顶点数据的好例子,所以我决定创建一个球面并从中央开始进进出出地移动每个四边形。但愿它是个有用的例子。</p>
  250. <p>这里是用来产生球面的位置和索引数据的代码。代码共享了四边形内的顶点数据,但是四边形之间的没有共享,因为我们需要分别地移动每个四边形。</p>
  251. <p>因为我懒,所以我通过3个 <a href="/docs/#api/zh/core/Object3D"><code class="notranslate" translate="no">Object3D</code></a> 对象的层级关系,计算球面的点。关于如何计算在这篇文章有解释<a href="optimize-lots-of-objects.html">the article on optimizing lots of objects</a>。</p>
  252. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeSpherePositions(segmentsAround, segmentsDown) {
  253. const numVertices = segmentsAround * segmentsDown * 6;
  254. const numComponents = 3;
  255. const positions = new Float32Array(numVertices * numComponents);
  256. const indices = [];
  257. const longHelper = new THREE.Object3D();
  258. const latHelper = new THREE.Object3D();
  259. const pointHelper = new THREE.Object3D();
  260. longHelper.add(latHelper);
  261. latHelper.add(pointHelper);
  262. pointHelper.position.z = 1;
  263. const temp = new THREE.Vector3();
  264. function getPoint(lat, long) {
  265. latHelper.rotation.x = lat;
  266. longHelper.rotation.y = long;
  267. longHelper.updateMatrixWorld(true);
  268. return pointHelper.getWorldPosition(temp).toArray();
  269. }
  270. let posNdx = 0;
  271. let ndx = 0;
  272. for (let down = 0; down &lt; segmentsDown; ++down) {
  273. const v0 = down / segmentsDown;
  274. const v1 = (down + 1) / segmentsDown;
  275. const lat0 = (v0 - 0.5) * Math.PI;
  276. const lat1 = (v1 - 0.5) * Math.PI;
  277. for (let across = 0; across &lt; segmentsAround; ++across) {
  278. const u0 = across / segmentsAround;
  279. const u1 = (across + 1) / segmentsAround;
  280. const long0 = u0 * Math.PI * 2;
  281. const long1 = u1 * Math.PI * 2;
  282. positions.set(getPoint(lat0, long0), posNdx); posNdx += numComponents;
  283. positions.set(getPoint(lat1, long0), posNdx); posNdx += numComponents;
  284. positions.set(getPoint(lat0, long1), posNdx); posNdx += numComponents;
  285. positions.set(getPoint(lat1, long1), posNdx); posNdx += numComponents;
  286. indices.push(
  287. ndx, ndx + 1, ndx + 2,
  288. ndx + 2, ndx + 1, ndx + 3,
  289. );
  290. ndx += 4;
  291. }
  292. }
  293. return {positions, indices};
  294. }
  295. </pre>
  296. <p>然后我们像这样调用。</p>
  297. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const segmentsAround = 24;
  298. const segmentsDown = 16;
  299. const {positions, indices} = makeSpherePositions(segmentsAround, segmentsDown);
  300. </pre>
  301. <p>因为返回的位置数据是单位球面位置,所以它们跟我们需要的法线数据完全一样,我们只需要复制它们。</p>
  302. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const normals = positions.slice();
  303. </pre>
  304. <p>然后我们像之前一样设置属性</p>
  305. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const geometry = new THREE.BufferGeometry();
  306. const positionNumComponents = 3;
  307. const normalNumComponents = 3;
  308. +const positionAttribute = new THREE.BufferAttribute(positions, positionNumComponents);
  309. +positionAttribute.setUsage(THREE.DynamicDrawUsage);
  310. geometry.setAttribute(
  311. 'position',
  312. + positionAttribute);
  313. geometry.setAttribute(
  314. 'normal',
  315. new THREE.BufferAttribute(normals, normalNumComponents));
  316. geometry.setIndex(indices);
  317. </pre>
  318. <p>我已经高亮一些区别。我们保存了位置属性的引用。
  319. 同时我们标记它为动态。这是提示THREE.js我们将会经常改变属性的内容。</p>
  320. <p>在我们的渲染循环中,每一帧我们基于它们的法线更新位置</p>
  321. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const temp = new THREE.Vector3();
  322. ...
  323. for (let i = 0; i &lt; positions.length; i += 3) {
  324. const quad = (i / 12 | 0);
  325. const ringId = quad / segmentsAround | 0;
  326. const ringQuadId = quad % segmentsAround;
  327. const ringU = ringQuadId / segmentsAround;
  328. const angle = ringU * Math.PI * 2;
  329. temp.fromArray(normals, i);
  330. temp.multiplyScalar(THREE.MathUtils.lerp(1, 1.4, Math.sin(time + ringId + angle) * .5 + .5));
  331. temp.toArray(positions, i);
  332. }
  333. positionAttribute.needsUpdate = true;
  334. </pre>
  335. <p>我们设置 <code class="notranslate" translate="no">positionAttribute.needsUpdate</code> 告诉THREE.js更新我们的改变。</p>
  336. <p></p><div translate="no" class="threejs_example_container notranslate">
  337. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-buffergeometry-dynamic.html"></iframe></div>
  338. <a class="threejs_center" href="/manual/examples/custom-buffergeometry-dynamic.html" target="_blank">点击此处在新标签页中打开</a>
  339. </div>
  340. <p></p>
  341. <p>我希望这些例子能对如何使用 <a href="/docs/#api/zh/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> 直接创建你自己的几何体和如何动态更新 <a href="/docs/#api/zh/core/BufferAttribute"><code class="notranslate" translate="no">BufferAttribute</code></a> 的内容发挥作用。</p>
  342. <!-- needed in English only to prevent warning from outdated translations -->
  343. <p><a href="resources/threejs-geometry.svg"></a>
  344. <a href="custom-geometry.html"></a></p>
  345. <p><canvas id="c"></canvas></p>
  346. <script type="module" src="../resources/threejs-custom-buffergeometry.js"></script>
  347. </div>
  348. </div>
  349. </div>
  350. <script src="/manual/resources/prettify.js"></script>
  351. <script src="/manual/resources/lesson.js"></script>
  352. </body></html>