webgl_geometry_teapot.html 11 KB

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