webgl_geometry_teapot.html 6.2 KB

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