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