webgl_geometry_teapot.html 6.1 KB

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