2
0

webgl_geometry_teapot.html 6.4 KB

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