webgl_geometry_teapot.html 10 KB

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