You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
9.8 KiB

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