custom-buffergeometry.html 23 KB

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