webgl_geometry_teapot.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  31. let camera, scene, renderer;
  32. let cameraControls;
  33. let effectController;
  34. const teapotSize = 300;
  35. let ambientLight, light;
  36. let tess = - 1; // force initialization
  37. let bBottom;
  38. let bLid;
  39. let bBody;
  40. let bFitLid;
  41. let bNonBlinn;
  42. let shading;
  43. let teapot, textureCube;
  44. const materials = {};
  45. init();
  46. render();
  47. function init() {
  48. const container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. const canvasWidth = window.innerWidth;
  51. const canvasHeight = window.innerHeight;
  52. // CAMERA
  53. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 80000 );
  54. camera.position.set( - 600, 550, 1300 );
  55. // LIGHTS
  56. ambientLight = new THREE.AmbientLight( 0x333333 );
  57. light = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
  58. light.position.set( 0.32, 0.39, 0.7 );
  59. // RENDERER
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( canvasWidth, canvasHeight );
  63. container.appendChild( renderer.domElement );
  64. // EVENTS
  65. window.addEventListener( 'resize', onWindowResize );
  66. // CONTROLS
  67. cameraControls = new OrbitControls( camera, renderer.domElement );
  68. cameraControls.addEventListener( 'change', render );
  69. // TEXTURE MAP
  70. const textureMap = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  71. textureMap.wrapS = textureMap.wrapT = THREE.RepeatWrapping;
  72. textureMap.anisotropy = 16;
  73. textureMap.colorSpace = THREE.SRGBColorSpace;
  74. // REFLECTION MAP
  75. const path = 'textures/cube/pisa/';
  76. const urls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
  77. textureCube = new THREE.CubeTextureLoader().setPath( path ).load( urls );
  78. textureCube.colorSpace = THREE.SRGBColorSpace;
  79. materials[ 'wireframe' ] = new THREE.MeshBasicMaterial( { wireframe: true } );
  80. materials[ 'flat' ] = new THREE.MeshPhongMaterial( { specular: 0x000000, flatShading: true, side: THREE.DoubleSide } );
  81. materials[ 'smooth' ] = new THREE.MeshLambertMaterial( { side: THREE.DoubleSide } );
  82. materials[ 'glossy' ] = new THREE.MeshPhongMaterial( { side: THREE.DoubleSide } );
  83. materials[ 'textured' ] = new THREE.MeshPhongMaterial( { map: textureMap, side: THREE.DoubleSide } );
  84. materials[ 'reflective' ] = new THREE.MeshPhongMaterial( { envMap: textureCube, side: THREE.DoubleSide } );
  85. // scene itself
  86. scene = new THREE.Scene();
  87. scene.background = new THREE.Color( 0xAAAAAA );
  88. scene.add( ambientLight );
  89. scene.add( light );
  90. // GUI
  91. setupGui();
  92. }
  93. // EVENT HANDLERS
  94. function onWindowResize() {
  95. const canvasWidth = window.innerWidth;
  96. const canvasHeight = window.innerHeight;
  97. renderer.setSize( canvasWidth, canvasHeight );
  98. camera.aspect = canvasWidth / canvasHeight;
  99. camera.updateProjectionMatrix();
  100. render();
  101. }
  102. function setupGui() {
  103. effectController = {
  104. newTess: 15,
  105. bottom: true,
  106. lid: true,
  107. body: true,
  108. fitLid: false,
  109. nonblinn: false,
  110. newShading: 'glossy'
  111. };
  112. const gui = new GUI();
  113. gui.add( effectController, 'newTess', [ 2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50 ] ).name( 'Tessellation Level' ).onChange( render );
  114. gui.add( effectController, 'lid' ).name( 'display lid' ).onChange( render );
  115. gui.add( effectController, 'body' ).name( 'display body' ).onChange( render );
  116. gui.add( effectController, 'bottom' ).name( 'display bottom' ).onChange( render );
  117. gui.add( effectController, 'fitLid' ).name( 'snug lid' ).onChange( render );
  118. gui.add( effectController, 'nonblinn' ).name( 'original scale' ).onChange( render );
  119. gui.add( effectController, 'newShading', [ 'wireframe', 'flat', 'smooth', 'glossy', 'textured', 'reflective' ] ).name( 'Shading' ).onChange( render );
  120. }
  121. //
  122. function render() {
  123. if ( effectController.newTess !== tess ||
  124. effectController.bottom !== bBottom ||
  125. effectController.lid !== bLid ||
  126. effectController.body !== bBody ||
  127. effectController.fitLid !== bFitLid ||
  128. effectController.nonblinn !== bNonBlinn ||
  129. effectController.newShading !== shading ) {
  130. tess = effectController.newTess;
  131. bBottom = effectController.bottom;
  132. bLid = effectController.lid;
  133. bBody = effectController.body;
  134. bFitLid = effectController.fitLid;
  135. bNonBlinn = effectController.nonblinn;
  136. shading = effectController.newShading;
  137. createNewTeapot();
  138. }
  139. // skybox is rendered separately, so that it is always behind the teapot.
  140. if ( shading === 'reflective' ) {
  141. scene.background = textureCube;
  142. } else {
  143. scene.background = null;
  144. }
  145. renderer.render( scene, camera );
  146. }
  147. // Whenever the teapot changes, the scene is rebuilt from scratch (not much to it).
  148. function createNewTeapot() {
  149. if ( teapot !== undefined ) {
  150. teapot.geometry.dispose();
  151. scene.remove( teapot );
  152. }
  153. const geometry = new TeapotGeometry( teapotSize,
  154. tess,
  155. effectController.bottom,
  156. effectController.lid,
  157. effectController.body,
  158. effectController.fitLid,
  159. ! effectController.nonblinn );
  160. teapot = new THREE.Mesh( geometry, materials[ shading ] );
  161. scene.add( teapot );
  162. }
  163. </script>
  164. </body>
  165. </html>