custom-geometry.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Custom Geometry</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 – Custom Geometry">
  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>Custom Geometry</h1>
  18. </div>
  19. <div class="lesson">
  20. <div class="lesson-main">
  21. <div class="warning">
  22. <strong>NOTE!</strong> This article is deprecated. Three.js r125
  23. removed support for <code class="notranslate" translate="no">Geometry</code>. Please refer to
  24. the article on <a href="custom-buffergeometry.html">custom BufferGeometry</a>.
  25. </div>
  26. <p>A <a href="primitives.html">previous article</a> gave a tour of
  27. the various built in primitives included in THREE.js. In this
  28. article we'll cover making our own geometry.</p>
  29. <p>Just to be clear, if you are serious about making 3D content,
  30. the most common way is to use a 3D modeling package like
  31. <a href="https://blender.org">Blender</a>,
  32. <a href="https://www.autodesk.com/products/maya/overview">Maya</a>,
  33. <a href="https://www.autodesk.com/products/3ds-max/overview">3D Studio Max</a>,
  34. <a href="https://www.maxon.net/en-us/">Cinema4D</a>, etc...
  35. You'd build a model and then export to <a href="load-gltf.html">gLTF</a>
  36. or <a href="load-obj.html">.obj</a> and load them up.
  37. Whichever one you choose, expect to spend 2 or 3 weeks going through
  38. their respective tutorials as all of them have a learning curve
  39. to be useful.</p>
  40. <p>Still, there are times when we might want to generate our own
  41. 3D geometry in code instead of using a modeling package.</p>
  42. <p>First let's just make a cube. Even though three.js already
  43. provides us with <a href="/docs/#api/en/geometries/BoxGeometry"><code class="notranslate" translate="no">BoxGeometry</code></a> and <a href="/docs/#api/en/geometries/BoxGeometry"><code class="notranslate" translate="no">BoxGeometry</code></a> a
  44. cube is easy to understand so let's start there.</p>
  45. <p>There are 2 ways to make custom geometry in THREE.js. One
  46. is with the <code class="notranslate" translate="no">Geometry</code> class, the other is <a href="/docs/#api/en/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>.
  47. Each has their advantages. <code class="notranslate" translate="no">Geometry</code> is arguably easier to
  48. use but slower and uses more memory. For few 1000s triangles
  49. it's a great choice but for 10s of thousands of triangles
  50. it might be better to use <a href="/docs/#api/en/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>.</p>
  51. <p><a href="/docs/#api/en/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a> is arguably harder to use but uses less
  52. memory and is faster. If quick rule of thumb might be
  53. if you're going to generate more than 10000 triangles
  54. consider using <a href="/docs/#api/en/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>.</p>
  55. <p>Note when I say <code class="notranslate" translate="no">Geometry</code> is slower I mean it is slower to
  56. start and slower to modify but it is not slower to draw so
  57. if you're not planning on modifying your geometry then
  58. as long as it's not too large there will only be slightly more
  59. delay for your program to start using <code class="notranslate" translate="no">Geometry</code> vs using
  60. <a href="/docs/#api/en/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>. We'll go over both eventually. For now
  61. though let's use geometry as it's easier to understand IMO.</p>
  62. <p>First let's make a cube with <code class="notranslate" translate="no">Geometry</code>. We'll start
  63. with an example from <a href="responsive.html">the article on responsiveness</a>.</p>
  64. <p>Let's remove the part that uses <a href="/docs/#api/en/geometries/BoxGeometry"><code class="notranslate" translate="no">BoxGeometry</code></a> and replace it with
  65. a <code class="notranslate" translate="no">Geometry</code>.</p>
  66. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const boxWidth = 1;
  67. -const boxHeight = 1;
  68. -const boxDepth = 1;
  69. -const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  70. +const geometry = new THREE.Geometry();
  71. </pre>
  72. <p>Now let's add the 8 corners of a cube. Here are the 8 corners.</p>
  73. <div class="threejs_center"><img src="../resources/cube-vertex-positions.svg" style="width: 500px"></div>
  74. <p>Centered around the origin we can add the vertex positions like this</p>
  75. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const geometry = new THREE.Geometry();
  76. +geometry.vertices.push(
  77. + new THREE.Vector3(-1, -1, 1), // 0
  78. + new THREE.Vector3( 1, -1, 1), // 1
  79. + new THREE.Vector3(-1, 1, 1), // 2
  80. + new THREE.Vector3( 1, 1, 1), // 3
  81. + new THREE.Vector3(-1, -1, -1), // 4
  82. + new THREE.Vector3( 1, -1, -1), // 5
  83. + new THREE.Vector3(-1, 1, -1), // 6
  84. + new THREE.Vector3( 1, 1, -1), // 7
  85. +);
  86. </pre>
  87. <p>We then need to make triangles, 2 for each face of the cube</p>
  88. <div class="threejs_center"><img src="../resources/cube-triangles.svg" style="width: 500px"></div>
  89. <p>We do that by creating <a href="/docs/#api/en/core/Face3"><code class="notranslate" translate="no">Face3</code></a> objects and specifying the indices
  90. of the 3 vertices that make up that face.</p>
  91. <p>The order we specify the vertices is important. To be pointing toward the
  92. outside of the cube they must be specified in a counter clockwise direction
  93. when that triangle is facing the camera.</p>
  94. <div class="threejs_center"><img src="../resources/cube-vertex-winding-order.svg" style="width: 500px"></div>
  95. <p>Following that pattern we can specify the 12 triangles that make
  96. the cube like this</p>
  97. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.faces.push(
  98. // front
  99. new THREE.Face3(0, 3, 2),
  100. new THREE.Face3(0, 1, 3),
  101. // right
  102. new THREE.Face3(1, 7, 3),
  103. new THREE.Face3(1, 5, 7),
  104. // back
  105. new THREE.Face3(5, 6, 7),
  106. new THREE.Face3(5, 4, 6),
  107. // left
  108. new THREE.Face3(4, 2, 6),
  109. new THREE.Face3(4, 0, 2),
  110. // top
  111. new THREE.Face3(2, 7, 6),
  112. new THREE.Face3(2, 3, 7),
  113. // bottom
  114. new THREE.Face3(4, 1, 0),
  115. new THREE.Face3(4, 5, 1),
  116. );
  117. </pre>
  118. <p>A few other minor changes to the original code and it should
  119. work.</p>
  120. <p>These cubes are twice as large as the <a href="/docs/#api/en/geometries/BoxGeometry"><code class="notranslate" translate="no">BoxGeometry</code></a> we were
  121. using before so let's move the camera back a little</p>
  122. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const fov = 75;
  123. const aspect = 2; // the canvas default
  124. const near = 0.1;
  125. -const far = 5;
  126. +const far = 100;
  127. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  128. -camera.position.z = 2;
  129. +camera.position.z = 5;
  130. </pre>
  131. <p>and let's separate them a little more and I changed their colors just because</p>
  132. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cubes = [
  133. - makeInstance(geometry, 0x44aa88, 0),
  134. - makeInstance(geometry, 0x8844aa, -2),
  135. - makeInstance(geometry, 0xaa8844, 2),
  136. + makeInstance(geometry, 0x44FF44, 0),
  137. + makeInstance(geometry, 0x4444FF, -4),
  138. + makeInstance(geometry, 0xFF4444, 4),
  139. ];
  140. </pre>
  141. <p>One last thing is we haven't added normals yet so we
  142. can't do any lighting. Let's change the material
  143. to something that doesn't need lights.</p>
  144. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeInstance(geometry, color, x) {
  145. - const material = new THREE.MeshPhongMaterial({color});
  146. + const material = new THREE.MeshBasicMaterial({color});
  147. const cube = new THREE.Mesh(geometry, material);
  148. scene.add(cube);
  149. ...
  150. </pre>
  151. <p>and we get cubes we made ourselves.</p>
  152. <p></p><div translate="no" class="threejs_example_container notranslate">
  153. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-geometry-cube.html"></iframe></div>
  154. <a class="threejs_center" href="/manual/examples/custom-geometry-cube.html" target="_blank">click here to open in a separate window</a>
  155. </div>
  156. <p></p>
  157. <p>We can specify a color per face by setting the <code class="notranslate" translate="no">color</code> property of
  158. each face.</p>
  159. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.faces[ 0].color = geometry.faces[ 1].color = new THREE.Color('red');
  160. geometry.faces[ 2].color = geometry.faces[ 3].color = new THREE.Color('yellow');
  161. geometry.faces[ 4].color = geometry.faces[ 5].color = new THREE.Color('green');
  162. geometry.faces[ 6].color = geometry.faces[ 7].color = new THREE.Color('cyan');
  163. geometry.faces[ 8].color = geometry.faces[ 9].color = new THREE.Color('blue');
  164. geometry.faces[10].color = geometry.faces[11].color = new THREE.Color('magenta');
  165. </pre>
  166. <p>note we need to tell the material we want to use vertex colors</p>
  167. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const material = new THREE.MeshBasicMaterial({color});
  168. +const material = new THREE.MeshBasicMaterial({vertexColors: true});
  169. </pre>
  170. <p></p><div translate="no" class="threejs_example_container notranslate">
  171. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-geometry-cube-face-colors.html"></iframe></div>
  172. <a class="threejs_center" href="/manual/examples/custom-geometry-cube-face-colors.html" target="_blank">click here to open in a separate window</a>
  173. </div>
  174. <p></p>
  175. <p>We can instead set the color of each individual vertex by setting the <code class="notranslate" translate="no">vertexColors</code>
  176. property of a <code class="notranslate" translate="no">Face</code> to an array of the 3 colors for the 3 vertices.</p>
  177. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.faces.forEach((face, ndx) =&gt; {
  178. face.vertexColors = [
  179. (new THREE.Color()).setHSL(ndx / 12 , 1, 0.5),
  180. (new THREE.Color()).setHSL(ndx / 12 + 0.1, 1, 0.5),
  181. (new THREE.Color()).setHSL(ndx / 12 + 0.2, 1, 0.5),
  182. ];
  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-geometry-cube-vertex-colors.html"></iframe></div>
  187. <a class="threejs_center" href="/manual/examples/custom-geometry-cube-vertex-colors.html" target="_blank">click here to open in a separate window</a>
  188. </div>
  189. <p></p>
  190. <p>To use lighting we need normals. Normals are vectors that specify direction.
  191. Just like the colors we can specify a normal for the face by setting the <code class="notranslate" translate="no">normal</code>
  192. property on each face with</p>
  193. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">face.normal = new THREE.Vector3(...)
  194. </pre>
  195. <p>or we can specify a normal for each vertex by setting the <code class="notranslate" translate="no">vertexNormals</code>
  196. property with something like</p>
  197. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">face.vertexNormals = [
  198. new THREE.Vector3(...),
  199. new THREE.Vector3(...),
  200. new THREE.Vector3(...),
  201. ]
  202. </pre>
  203. <p>but often it's much easier to just ask THREE.js to compute normals
  204. for us based on the positions we specified.</p>
  205. <p>For face normals we'd call <code class="notranslate" translate="no">Geometry.computeFaceNormals</code> as in</p>
  206. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.computeFaceNormals();
  207. </pre>
  208. <p>Removing the vertex color stuff and changing the material back to <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a></p>
  209. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const material = new THREE.MeshBasicMaterial({vertexColors: true});
  210. +const material = new THREE.MeshPhongMaterial({color});
  211. </pre>
  212. <p>and now our cubes can be lit.</p>
  213. <p></p><div translate="no" class="threejs_example_container notranslate">
  214. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-geometry-cube-face-normals.html"></iframe></div>
  215. <a class="threejs_center" href="/manual/examples/custom-geometry-cube-face-normals.html" target="_blank">click here to open in a separate window</a>
  216. </div>
  217. <p></p>
  218. <p>Using face normals will always give us a faceted look. We can use
  219. vertex normals for a smoother look by calling <code class="notranslate" translate="no">Geometry.computeVertexNormals</code></p>
  220. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-geometry.computeFaceNormals();
  221. +geometry.computeVertexNormals();
  222. </pre>
  223. <p>Unfortunately a cube is not a good candidate for vertex normals since it
  224. means each vertex gets its normal from the
  225. normals of all the faces it shares.</p>
  226. <p></p><div translate="no" class="threejs_example_container notranslate">
  227. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-geometry-cube-vertex-normals.html"></iframe></div>
  228. <a class="threejs_center" href="/manual/examples/custom-geometry-cube-vertex-normals.html" target="_blank">click here to open in a separate window</a>
  229. </div>
  230. <p></p>
  231. <p>Adding texture coordinates, sometimes called UVs, is done via an array of
  232. layers of parallel arrays to the <code class="notranslate" translate="no">faces</code> array which is set via <code class="notranslate" translate="no">Geometry.faceVertexUvs</code>.
  233. For our cube we could do something like</p>
  234. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">geometry.faceVertexUvs[0].push(
  235. // front
  236. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
  237. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
  238. // right
  239. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
  240. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
  241. // back
  242. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
  243. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
  244. // left
  245. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
  246. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
  247. // top
  248. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
  249. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
  250. // bottom
  251. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
  252. [ new THREE.Vector2(0, 0), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1) ],
  253. );
  254. </pre>
  255. <p>It's important to notice <code class="notranslate" translate="no">faceVertexUvs</code> is an array of layers. Each layer
  256. is another set of UV coordinates. By default there is one layer of UV coordinates,
  257. layer 0, so we just add our UVs to that layer.</p>
  258. <p>Let's <a href="textures.html">add a texture</a> to our material and switch back to compute face normals</p>
  259. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-geometry.computeVertexNormals();
  260. +geometry.computeFaceNormals();
  261. +const loader = new THREE.TextureLoader();
  262. +const texture = loader.load('resources/images/star.png');
  263. function makeInstance(geometry, color, x) {
  264. - const material = new THREE.MeshPhongMaterial({color});
  265. + const material = new THREE.MeshPhongMaterial({color, map: texture});
  266. const cube = new THREE.Mesh(geometry, material);
  267. scene.add(cube);
  268. ...
  269. </pre>
  270. <p></p><div translate="no" class="threejs_example_container notranslate">
  271. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-geometry-cube-texcoords.html"></iframe></div>
  272. <a class="threejs_center" href="/manual/examples/custom-geometry-cube-texcoords.html" target="_blank">click here to open in a separate window</a>
  273. </div>
  274. <p></p>
  275. <p>Putting that all together, let's make a simple heightmap based
  276. terrain mesh.</p>
  277. <p>A heightmap based terrain is where you have a 2D array of heights
  278. that you apply them to a grid. An easy way to get a 2D array of heights
  279. is to draw them in an image editing program. Here's an image I drew.
  280. It's 96x64 pixels</p>
  281. <div class="threejs_center"><img src="../examples/resources/images/heightmap-96x64.png" style="width: 512px; image-rendering: pixelated;"></div>
  282. <p>We'll load that and then generate a heightmap mesh from it.
  283. We can use the <a href="/docs/#api/en/loaders/ImageLoader"><code class="notranslate" translate="no">ImageLoader</code></a> to load the image.</p>
  284. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const imgLoader = new THREE.ImageLoader();
  285. imgLoader.load('resources/images/heightmap-96x64.png', createHeightmap);
  286. function createHeightmap(image) {
  287. // extract the data from the image by drawing it to a canvas
  288. // and calling getImageData
  289. const ctx = document.createElement('canvas').getContext('2d');
  290. const {width, height} = image;
  291. ctx.canvas.width = width;
  292. ctx.canvas.height = height;
  293. ctx.drawImage(image, 0, 0);
  294. const {data} = ctx.getImageData(0, 0, width, height);
  295. const geometry = new THREE.Geometry();
  296. </pre>
  297. <p>We extracted the data from the image, now we'll make a grid of cells.
  298. The cells are the squares formed by the center points of each pixel
  299. from the image</p>
  300. <div class="threejs_center"><img src="../resources/heightmap-points.svg" style="width: 500px"></div>
  301. <p>For each cell we'll generate 5 vertices. One for each corner of the cell
  302. and one at the center point of the cell with the average height of the 4
  303. corner heights.</p>
  304. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cellsAcross = width - 1;
  305. const cellsDeep = height - 1;
  306. for (let z = 0; z &lt; cellsDeep; ++z) {
  307. for (let x = 0; x &lt; cellsAcross; ++x) {
  308. // compute row offsets into the height data
  309. // we multiply by 4 because the data is R,G,B,A but we
  310. // only care about R
  311. const base0 = (z * width + x) * 4;
  312. const base1 = base0 + (width * 4);
  313. // look up the height for the for points
  314. // around this cell
  315. const h00 = data[base0] / 32;
  316. const h01 = data[base0 + 4] / 32;
  317. const h10 = data[base1] / 32;
  318. const h11 = data[base1 + 4] / 32;
  319. // compute the average height
  320. const hm = (h00 + h01 + h10 + h11) / 4;
  321. // the corner positions
  322. const x0 = x;
  323. const x1 = x + 1;
  324. const z0 = z;
  325. const z1 = z + 1;
  326. // remember the first index of these 5 vertices
  327. const ndx = geometry.vertices.length;
  328. // add the 4 corners for this cell and the midpoint
  329. geometry.vertices.push(
  330. new THREE.Vector3(x0, h00, z0),
  331. new THREE.Vector3(x1, h01, z0),
  332. new THREE.Vector3(x0, h10, z1),
  333. new THREE.Vector3(x1, h11, z1),
  334. new THREE.Vector3((x0 + x1) / 2, hm, (z0 + z1) / 2),
  335. );
  336. </pre>
  337. <p>We'll then make 4 triangles from those 5 vertices</p>
  338. <div class="threejs_center"><img src="../resources/heightmap-triangles.svg" style="width: 500px"></div>
  339. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> // create 4 triangles
  340. geometry.faces.push(
  341. new THREE.Face3(ndx + 0, ndx + 4, ndx + 1),
  342. new THREE.Face3(ndx + 1, ndx + 4, ndx + 3),
  343. new THREE.Face3(ndx + 3, ndx + 4, ndx + 2),
  344. new THREE.Face3(ndx + 2, ndx + 4, ndx + 0),
  345. );
  346. // add the texture coordinates for each vertex of each face
  347. const u0 = x / cellsAcross;
  348. const v0 = z / cellsDeep;
  349. const u1 = (x + 1) / cellsAcross;
  350. const v1 = (z + 1) / cellsDeep;
  351. const um = (u0 + u1) / 2;
  352. const vm = (v0 + v1) / 2;
  353. geometry.faceVertexUvs[0].push(
  354. [ new THREE.Vector2(u0, v0), new THREE.Vector2(um, vm), new THREE.Vector2(u1, v0) ],
  355. [ new THREE.Vector2(u1, v0), new THREE.Vector2(um, vm), new THREE.Vector2(u1, v1) ],
  356. [ new THREE.Vector2(u1, v1), new THREE.Vector2(um, vm), new THREE.Vector2(u0, v1) ],
  357. [ new THREE.Vector2(u0, v1), new THREE.Vector2(um, vm), new THREE.Vector2(u0, v0) ],
  358. );
  359. }
  360. }
  361. </pre>
  362. <p>and finish it up</p>
  363. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> geometry.computeFaceNormals();
  364. // center the geometry
  365. geometry.translate(width / -2, 0, height / -2);
  366. const loader = new THREE.TextureLoader();
  367. const texture = loader.load('resources/images/star.png');
  368. const material = new THREE.MeshPhongMaterial({color: 'green', map: texture});
  369. const cube = new THREE.Mesh(geometry, material);
  370. scene.add(cube);
  371. }
  372. </pre>
  373. <p>A few minor changes to make it easier to view.</p>
  374. <ul>
  375. <li>include the <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a></li>
  376. </ul>
  377. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from '/build/three.module.js';
  378. +import {OrbitControls} from '/examples/jsm/controls/OrbitControls.js';
  379. </pre>
  380. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const fov = 75;
  381. const aspect = 2; // the canvas default
  382. const near = 0.1;
  383. -const far = 100;
  384. +const far = 200;
  385. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  386. -camera.position.z = 5;
  387. +camera.position.set(20, 20, 20);
  388. +const controls = new OrbitControls(camera, canvas);
  389. +controls.target.set(0, 0, 0);
  390. +controls.update();
  391. </pre>
  392. <p>add 2 lights</p>
  393. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-{
  394. +function addLight(...pos) {
  395. const color = 0xFFFFFF;
  396. const intensity = 1;
  397. const light = new THREE.DirectionalLight(color, intensity);
  398. - light.position.set(-1, 2, 4\);
  399. + light.position.set(...pos);
  400. scene.add(light);
  401. }
  402. +addLight(-1, 2, 4);
  403. +addLight(1, 2, -2);
  404. </pre>
  405. <p>and we deleted the code related to spinning the cubes.</p>
  406. <p></p><div translate="no" class="threejs_example_container notranslate">
  407. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/custom-geometry-heightmap.html"></iframe></div>
  408. <a class="threejs_center" href="/manual/examples/custom-geometry-heightmap.html" target="_blank">click here to open in a separate window</a>
  409. </div>
  410. <p></p>
  411. <p>I hope that was a useful instruction to making your own
  412. geometry using <code class="notranslate" translate="no">Geometry</code>.</p>
  413. <p>In <a href="custom-buffergeometry.html">another article</a> we'll go over <a href="/docs/#api/en/core/BufferGeometry"><code class="notranslate" translate="no">BufferGeometry</code></a>.</p>
  414. </div>
  415. </div>
  416. </div>
  417. <script src="/manual/resources/prettify.js"></script>
  418. <script src="/manual/resources/lesson.js"></script>
  419. </body></html>