webgl_geometry_teapot.html 10 KB

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