webgl_geometry_teapot.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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="http://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 src="../build/three.js"></script>
  15. <script src="js/controls/OrbitControls.js"></script>
  16. <script src="js/WebGL.js"></script>
  17. <script src='js/libs/dat.gui.min.js'></script>
  18. <script src='js/geometries/TeapotBufferGeometry.js'></script>
  19. <script>
  20. ////////////////////////////////////////////////////////////////////////////////
  21. // Utah/Newell Teapot demo
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /*global THREE, WEBGL, dat, window */
  24. if ( WEBGL.isWebGLAvailable() === false ) {
  25. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  26. }
  27. var camera, scene, renderer;
  28. var cameraControls;
  29. var effectController;
  30. var teapotSize = 400;
  31. var ambientLight, light;
  32. var tess = - 1; // force initialization
  33. var bBottom;
  34. var bLid;
  35. var bBody;
  36. var bFitLid;
  37. var bNonBlinn;
  38. var shading;
  39. var wireMaterial, flatMaterial, gouraudMaterial, phongMaterial, texturedMaterial, reflectiveMaterial;
  40. var teapot, textureCube;
  41. // allocate these just once
  42. var diffuseColor = new THREE.Color();
  43. var specularColor = new THREE.Color();
  44. init();
  45. render();
  46. function init() {
  47. var container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. var canvasWidth = window.innerWidth;
  50. var 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( 0x333333 ); // 0.2
  56. light = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
  57. // direction is set in GUI
  58. // RENDERER
  59. renderer = new THREE.WebGLRenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( canvasWidth, canvasHeight );
  62. renderer.gammaInput = true;
  63. renderer.gammaOutput = true;
  64. container.appendChild( renderer.domElement );
  65. // EVENTS
  66. window.addEventListener( 'resize', onWindowResize, false );
  67. // CONTROLS
  68. cameraControls = new THREE.OrbitControls( camera, renderer.domElement );
  69. cameraControls.addEventListener( 'change', render );
  70. // TEXTURE MAP
  71. var textureMap = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
  72. textureMap.wrapS = textureMap.wrapT = THREE.RepeatWrapping;
  73. textureMap.anisotropy = 16;
  74. // REFLECTION MAP
  75. var path = "textures/cube/skybox/";
  76. var urls = [
  77. path + "px.jpg", path + "nx.jpg",
  78. path + "py.jpg", path + "ny.jpg",
  79. path + "pz.jpg", path + "nz.jpg"
  80. ];
  81. textureCube = new THREE.CubeTextureLoader().load( urls );
  82. // MATERIALS
  83. var materialColor = new THREE.Color();
  84. materialColor.setRGB( 1.0, 1.0, 1.0 );
  85. wireMaterial = new THREE.MeshBasicMaterial( { color: 0xFFFFFF, wireframe: true } );
  86. flatMaterial = new THREE.MeshPhongMaterial( { color: materialColor, specular: 0x000000, flatShading: true, side: THREE.DoubleSide } );
  87. gouraudMaterial = new THREE.MeshLambertMaterial( { color: materialColor, side: THREE.DoubleSide } );
  88. phongMaterial = new THREE.MeshPhongMaterial( { color: materialColor, side: THREE.DoubleSide } );
  89. texturedMaterial = new THREE.MeshPhongMaterial( { color: materialColor, map: textureMap, side: THREE.DoubleSide } );
  90. reflectiveMaterial = new THREE.MeshPhongMaterial( { color: materialColor, envMap: textureCube, side: THREE.DoubleSide } );
  91. // scene itself
  92. scene = new THREE.Scene();
  93. scene.background = new THREE.Color( 0xAAAAAA );
  94. scene.add( ambientLight );
  95. scene.add( light );
  96. // GUI
  97. setupGui();
  98. }
  99. // EVENT HANDLERS
  100. function onWindowResize() {
  101. var canvasWidth = window.innerWidth;
  102. var canvasHeight = window.innerHeight;
  103. renderer.setSize( canvasWidth, canvasHeight );
  104. camera.aspect = canvasWidth / canvasHeight;
  105. camera.updateProjectionMatrix();
  106. render();
  107. }
  108. function setupGui() {
  109. effectController = {
  110. shininess: 40.0,
  111. ka: 0.17,
  112. kd: 0.51,
  113. ks: 0.2,
  114. metallic: true,
  115. hue: 0.121,
  116. saturation: 0.73,
  117. lightness: 0.66,
  118. lhue: 0.04,
  119. lsaturation: 0.01, // non-zero so that fractions will be shown
  120. llightness: 1.0,
  121. // bizarrely, if you initialize these with negative numbers, the sliders
  122. // will not show any decimal places.
  123. lx: 0.32,
  124. ly: 0.39,
  125. lz: 0.7,
  126. newTess: 15,
  127. bottom: true,
  128. lid: true,
  129. body: true,
  130. fitLid: false,
  131. nonblinn: false,
  132. newShading: "glossy"
  133. };
  134. var h;
  135. var gui = new dat.GUI();
  136. // material (attributes)
  137. h = gui.addFolder( "Material control" );
  138. h.add( effectController, "shininess", 1.0, 400.0, 1.0 ).name( "shininess" ).onChange( render );
  139. h.add( effectController, "kd", 0.0, 1.0, 0.025 ).name( "diffuse strength" ).onChange( render );
  140. h.add( effectController, "ks", 0.0, 1.0, 0.025 ).name( "specular strength" ).onChange( render );
  141. h.add( effectController, "metallic" ).onChange( render );
  142. // material (color)
  143. h = gui.addFolder( "Material color" );
  144. h.add( effectController, "hue", 0.0, 1.0, 0.025 ).name( "hue" ).onChange( render );
  145. h.add( effectController, "saturation", 0.0, 1.0, 0.025 ).name( "saturation" ).onChange( render );
  146. h.add( effectController, "lightness", 0.0, 1.0, 0.025 ).name( "lightness" ).onChange( render );
  147. // light (point)
  148. h = gui.addFolder( "Lighting" );
  149. h.add( effectController, "lhue", 0.0, 1.0, 0.025 ).name( "hue" ).onChange( render );
  150. h.add( effectController, "lsaturation", 0.0, 1.0, 0.025 ).name( "saturation" ).onChange( render );
  151. h.add( effectController, "llightness", 0.0, 1.0, 0.025 ).name( "lightness" ).onChange( render );
  152. h.add( effectController, "ka", 0.0, 1.0, 0.025 ).name( "ambient" ).onChange( render );
  153. // light (directional)
  154. h = gui.addFolder( "Light direction" );
  155. h.add( effectController, "lx", - 1.0, 1.0, 0.025 ).name( "x" ).onChange( render );
  156. h.add( effectController, "ly", - 1.0, 1.0, 0.025 ).name( "y" ).onChange( render );
  157. h.add( effectController, "lz", - 1.0, 1.0, 0.025 ).name( "z" ).onChange( render );
  158. h = gui.addFolder( "Tessellation control" );
  159. h.add( effectController, "newTess", [ 2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50 ] ).name( "Tessellation Level" ).onChange( render );
  160. h.add( effectController, "lid" ).name( "display lid" ).onChange( render );
  161. h.add( effectController, "body" ).name( "display body" ).onChange( render );
  162. h.add( effectController, "bottom" ).name( "display bottom" ).onChange( render );
  163. h.add( effectController, "fitLid" ).name( "snug lid" ).onChange( render );
  164. h.add( effectController, "nonblinn" ).name( "original scale" ).onChange( render );
  165. // shading
  166. gui.add( effectController, "newShading", [ "wireframe", "flat", "smooth", "glossy", "textured", "reflective" ] ).name( "Shading" ).onChange( render );
  167. }
  168. //
  169. function render() {
  170. if ( effectController.newTess !== tess ||
  171. effectController.bottom !== bBottom ||
  172. effectController.lid !== bLid ||
  173. effectController.body !== bBody ||
  174. effectController.fitLid !== bFitLid ||
  175. effectController.nonblinn !== bNonBlinn ||
  176. effectController.newShading !== shading ) {
  177. tess = effectController.newTess;
  178. bBottom = effectController.bottom;
  179. bLid = effectController.lid;
  180. bBody = effectController.body;
  181. bFitLid = effectController.fitLid;
  182. bNonBlinn = effectController.nonblinn;
  183. shading = effectController.newShading;
  184. createNewTeapot();
  185. }
  186. // We're a bit lazy here. We could check to see if any material attributes changed and update
  187. // only if they have. But, these calls are cheap enough and this is just a demo.
  188. phongMaterial.shininess = effectController.shininess;
  189. texturedMaterial.shininess = effectController.shininess;
  190. diffuseColor.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  191. if ( effectController.metallic ) {
  192. // make colors match to give a more metallic look
  193. specularColor.copy( diffuseColor );
  194. } else {
  195. // more of a plastic look
  196. specularColor.setRGB( 1, 1, 1 );
  197. }
  198. diffuseColor.multiplyScalar( effectController.kd );
  199. flatMaterial.color.copy( diffuseColor );
  200. gouraudMaterial.color.copy( diffuseColor );
  201. phongMaterial.color.copy( diffuseColor );
  202. texturedMaterial.color.copy( diffuseColor );
  203. specularColor.multiplyScalar( effectController.ks );
  204. phongMaterial.specular.copy( specularColor );
  205. texturedMaterial.specular.copy( specularColor );
  206. // Ambient's actually controlled by the light for this demo
  207. ambientLight.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness * effectController.ka );
  208. light.position.set( effectController.lx, effectController.ly, effectController.lz );
  209. light.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
  210. // skybox is rendered separately, so that it is always behind the teapot.
  211. if ( shading === "reflective" ) {
  212. scene.background = textureCube;
  213. } else {
  214. scene.background = null;
  215. }
  216. renderer.render( scene, camera );
  217. }
  218. // Whenever the teapot changes, the scene is rebuilt from scratch (not much to it).
  219. function createNewTeapot() {
  220. if ( teapot !== undefined ) {
  221. teapot.geometry.dispose();
  222. scene.remove( teapot );
  223. }
  224. var teapotGeometry = new THREE.TeapotBufferGeometry( teapotSize,
  225. tess,
  226. effectController.bottom,
  227. effectController.lid,
  228. effectController.body,
  229. effectController.fitLid,
  230. ! effectController.nonblinn );
  231. teapot = new THREE.Mesh(
  232. teapotGeometry,
  233. shading === "wireframe" ? wireMaterial : (
  234. shading === "flat" ? flatMaterial : (
  235. shading === "smooth" ? gouraudMaterial : (
  236. shading === "glossy" ? phongMaterial : (
  237. shading === "textured" ? texturedMaterial : reflectiveMaterial ) ) ) ) ); // if no match, pick Phong
  238. scene.add( teapot );
  239. }
  240. </script>
  241. </body>
  242. </html>