1
0

custom-buffergeometry.html 23 KB

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