webgl_geometry_teapot.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - teapot buffer geometry</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - the Utah Teapot<br />
  12. from <a href="https://www.udacity.com/course/interactive-3d-graphics--cs291" target="_blank" rel="noopener">Udacity Interactive 3D Graphics</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  30. let camera, scene, renderer;
  31. let cameraControls;
  32. let effectController;
  33. const teapotSize = 300;
  34. let ambientLight, light;
  35. let tess = - 1; // force initialization
  36. let bBottom;
  37. let bLid;
  38. let bBody;
  39. let bFitLid;
  40. let bNonBlinn;
  41. let shading;
  42. let teapot, textureCube;
  43. const materials = {};
  44. init();
  45. render();
  46. function init() {
  47. const container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. const canvasWidth = window.innerWidth;
  50. const canvasHeight = window.innerHeight;
  51. // CAMERA
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 80000 );
  53. camera.position.set( - 600, 550, 1300 );
  54. // LIGHTS
  55. ambientLight = new THREE.AmbientLight( 0x7c7c7c, 3.0 );
  56. light = new THREE.DirectionalLight( 0xFFFFFF, 3.0 );
  57. light.position.set( 0.32, 0.39, 0.7 );
  58. // RENDERER
  59. renderer = new THREE.WebGLRenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( canvasWidth, canvasHeight );
  62. container.appendChild( renderer.domElement );
  63. // EVENTS
  64. window.addEventListener( 'resize', onWindowResize );
  65. // CONTROLS
  66. cameraControls = new OrbitControls( camera, renderer.domElement );
  67. cameraControls.addEventListener( 'change', render );
  68. // TEXTURE MAP
  69. const textureMap = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  70. textureMap.wrapS = textureMap.wrapT = THREE.RepeatWrapping;
  71. textureMap.anisotropy = 16;
  72. textureMap.colorSpace = THREE.SRGBColorSpace;
  73. // REFLECTION MAP
  74. const path = 'textures/cube/pisa/';
  75. const urls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
  76. textureCube = new THREE.CubeTextureLoader().setPath( path ).load( urls );
  77. materials[ 'wireframe' ] = new THREE.MeshBasicMaterial( { wireframe: true } );
  78. materials[ 'flat' ] = new THREE.MeshPhongMaterial( { specular: 0x000000, flatShading: true, side: THREE.DoubleSide } );
  79. materials[ 'smooth' ] = new THREE.MeshLambertMaterial( { side: THREE.DoubleSide } );
  80. materials[ 'glossy' ] = new THREE.MeshPhongMaterial( { side: THREE.DoubleSide } );
  81. materials[ 'textured' ] = new THREE.MeshPhongMaterial( { map: textureMap, side: THREE.DoubleSide } );
  82. materials[ 'reflective' ] = new THREE.MeshPhongMaterial( { envMap: textureCube, side: THREE.DoubleSide } );
  83. // scene itself
  84. scene = new THREE.Scene();
  85. scene.background = new THREE.Color( 0xAAAAAA );
  86. scene.add( ambientLight );
  87. scene.add( light );
  88. // GUI
  89. setupGui();
  90. }
  91. // EVENT HANDLERS
  92. function onWindowResize() {
  93. const canvasWidth = window.innerWidth;
  94. const canvasHeight = window.innerHeight;
  95. renderer.setSize( canvasWidth, canvasHeight );
  96. camera.aspect = canvasWidth / canvasHeight;
  97. camera.updateProjectionMatrix();
  98. render();
  99. }
  100. function setupGui() {
  101. effectController = {
  102. newTess: 15,
  103. bottom: true,
  104. lid: true,
  105. body: true,
  106. fitLid: false,
  107. nonblinn: false,
  108. newShading: 'glossy'
  109. };
  110. const gui = new GUI();
  111. gui.add( effectController, 'newTess', [ 2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50 ] ).name( 'Tessellation Level' ).onChange( render );
  112. gui.add( effectController, 'lid' ).name( 'display lid' ).onChange( render );
  113. gui.add( effectController, 'body' ).name( 'display body' ).onChange( render );
  114. gui.add( effectController, 'bottom' ).name( 'display bottom' ).onChange( render );
  115. gui.add( effectController, 'fitLid' ).name( 'snug lid' ).onChange( render );
  116. gui.add( effectController, 'nonblinn' ).name( 'original scale' ).onChange( render );
  117. gui.add( effectController, 'newShading', [ 'wireframe', 'flat', 'smooth', 'glossy', 'textured', 'reflective' ] ).name( 'Shading' ).onChange( render );
  118. }
  119. //
  120. function render() {
  121. if ( effectController.newTess !== tess ||
  122. effectController.bottom !== bBottom ||
  123. effectController.lid !== bLid ||
  124. effectController.body !== bBody ||
  125. effectController.fitLid !== bFitLid ||
  126. effectController.nonblinn !== bNonBlinn ||
  127. effectController.newShading !== shading ) {
  128. tess = effectController.newTess;
  129. bBottom = effectController.bottom;
  130. bLid = effectController.lid;
  131. bBody = effectController.body;
  132. bFitLid = effectController.fitLid;
  133. bNonBlinn = effectController.nonblinn;
  134. shading = effectController.newShading;
  135. createNewTeapot();
  136. }
  137. // skybox is rendered separately, so that it is always behind the teapot.
  138. if ( shading === 'reflective' ) {
  139. scene.background = textureCube;
  140. } else {
  141. scene.background = null;
  142. }
  143. renderer.render( scene, camera );
  144. }
  145. // Whenever the teapot changes, the scene is rebuilt from scratch (not much to it).
  146. function createNewTeapot() {
  147. if ( teapot !== undefined ) {
  148. teapot.geometry.dispose();
  149. scene.remove( teapot );
  150. }
  151. const geometry = new TeapotGeometry( teapotSize,
  152. tess,
  153. effectController.bottom,
  154. effectController.lid,
  155. effectController.body,
  156. effectController.fitLid,
  157. ! effectController.nonblinn );
  158. teapot = new THREE.Mesh( geometry, materials[ shading ] );
  159. scene.add( teapot );
  160. }
  161. </script>
  162. </body>
  163. </html>