threejs-example.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Simple Example ThreeJS</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <style>
  7. body {
  8. font-family: Monospace;
  9. background-color: #f0f0f0;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #material {
  14. bottom:5px;
  15. left:5px;
  16. position:absolute;
  17. }
  18. </style>
  19. <script src="../../build/three.min.js"></script>
  20. <script src='../../examples/js/libs/dat.gui.min.js'></script>
  21. <script>
  22. var ExampleScene = function() {
  23. //The current selected material saved to the hash
  24. var selectedMaterial = window.location.hash.substring(1) || "MeshPhongMaterial";
  25. this.renderer = undefined;
  26. this.controls = undefined;
  27. this.div = document.getElementById( 'clean' );
  28. this.scene = new THREE.Scene();
  29. this.camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); //(fov, aspect ratio, near, far frustrum)
  30. this.camera.position.z = 50;
  31. this.gui = new dat.GUI();
  32. this.addRenderer();
  33. this.addControls();
  34. this.addLights();
  35. //this.addSpheresBasic();
  36. this.addSpheresWithMaterial( selectedMaterial );
  37. //this.addGeometry();
  38. this.addGrid();
  39. this.addEventListeners();
  40. this.loop();
  41. };
  42. ExampleScene.prototype = {
  43. addLights : function() {
  44. this.lights = [];
  45. this.lights[0] = new THREE.AmbientLight( 0xffffff );
  46. this.lights[1] = new THREE.PointLight( 0xffffff, 1, 0 );
  47. this.lights[2] = new THREE.PointLight( 0xffffff, 1, 0 );
  48. this.lights[3] = new THREE.PointLight( 0xffffff, 1, 0 );
  49. this.lights[1].position.set(0, 200, 0);
  50. this.lights[2].position.set(100, 200, 100);
  51. this.lights[3].position.set(-100, -200, -100);
  52. //this.scene.add( this.lights[0] );
  53. this.scene.add( this.lights[1] );
  54. this.scene.add( this.lights[2] );
  55. this.scene.add( this.lights[3] );
  56. },
  57. addGeometry : function() {
  58. //-------------------------------------------------------------------------------
  59. // Various types of geometry to play with, only un-comment 1 geometry at a time
  60. //var geometry = new THREE.CircleGeometry(10, 20, 0, 2 * Math.PI);
  61. //var geometry = new THREE.CubeGeometry(15, 10, 10, 1, 1, 1); //(width, height, depth, widthSegments, heightSegments, depthSegments)
  62. var geometry = new THREE.CylinderGeometry(10, 12, 20, 32, 2, false); //(radiusTop, radiusBottom, height, radiusSegments, heightSegments, openEnded)
  63. //var geometry = new THREE.CircleGeometry();
  64. /*
  65. // Extruded Geometry
  66. var extrudeSettings = {
  67. bevelEnabled: true,
  68. bevelSegments: 2,
  69. steps: 100,
  70. extrudePath : new THREE.SplineCurve3([
  71. new THREE.Vector3( 0, -8, 0),
  72. new THREE.Vector3( 3, -1.66, 0),
  73. new THREE.Vector3( -3, 1.66, 0),
  74. new THREE.Vector3( 0, 8, 0)
  75. ])
  76. };
  77. var extrudeBend = new THREE.Shape([ //Closed
  78. new THREE.Vector3( 0.05, 0.00, 0.00),
  79. new THREE.Vector3( 0.85, 0.00, 0.00),
  80. new THREE.Vector3( 1.00, 0.05, 0.00),
  81. new THREE.Vector3( 1.00, 0.85, 0.00),
  82. new THREE.Vector3( 0.85, 1.00, 0.00),
  83. new THREE.Vector3( 0.05, 1.00, 0.00),
  84. new THREE.Vector3( 0.00, 0.85, 0.00),
  85. new THREE.Vector3( 0.00, 0.05, 0.00)
  86. ]);
  87. var geometry = new THREE.ExtrudeGeometry(extrudeBend, extrudeSettings);
  88. */
  89. var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry, [
  90. new THREE.MeshNormalMaterial( {} ),
  91. new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, wireframeLinewidth: 2, opacity: 0.1, transparent: true } ) ]
  92. );
  93. this.scene.add(mesh);
  94. },
  95. addRenderer : function() {
  96. this.renderer = new THREE.WebGLRenderer();
  97. this.renderer.setSize( window.innerWidth, window.innerHeight );
  98. this.div.appendChild( this.renderer.domElement );
  99. },
  100. addControls : function() {
  101. this.controls = new THREE.OrbitControls( this.camera, this.renderer.domElement );
  102. },
  103. addSpheresBasic : function() {
  104. var sphere = new THREE.SphereGeometry( 5, 16, 8 ); // (radius, widthSegments, heightSegments)
  105. this.spheres = [];
  106. this.spheres[0] = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) );
  107. this.spheres[1] = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) );
  108. this.spheres[2] = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) );
  109. this.spheres[3] = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  110. this.spheres[0].position.set(10, 0, 0);
  111. this.spheres[1].position.set( 0, 10, 0);
  112. this.spheres[2].position.set( 0, 0, 10);
  113. this.spheres[3].position.set(10, 10, 0);
  114. this.scene.add(this.spheres[0]);
  115. this.scene.add(this.spheres[1]);
  116. this.scene.add(this.spheres[2]);
  117. this.scene.add(this.spheres[3]);
  118. },
  119. addGenericGui : function( material ) {
  120. gui.add( material, "opacity", 0, 1 );
  121. gui.add( material, "transparent" );
  122. gui.add( blending, {
  123. "THREE.NoBlending" : THREE.NoBlending,
  124. "THREE.NormalBlending" : THREE.NormalBlending,
  125. "THREE.AdditiveBlending" : THREE.AdditiveBlending,
  126. "THREE.SubtractiveBlending" : THREE.SubtractiveBlending,
  127. "THREE.MultiplyBlending" : THREE.MultiplyBlending,
  128. "THREE.CustomBlending" : THREE.CustomBlending
  129. });
  130. gui.add( side, {
  131. "THREE.FrontSide" : THREE.FrontSide,
  132. "THREE.BackSide" : THREE.BackSide,
  133. "THREE.DoubleSide" : THREE.DoubleSide
  134. });
  135. },
  136. addSpheresWithMaterial : function( materialType ) {
  137. var sphere, colors, material;
  138. sphere = new THREE.SphereGeometry( 5, 64 / 2, 32 / 2 ); // (radius, widthSegments, heightSegments)
  139. this.spheres = [];
  140. colors = [
  141. 0x9f0040,
  142. 0x00409f,
  143. 0x809f80,
  144. 0x9faa00
  145. ];
  146. for(var i=0; i < 4; ++i) {
  147. //Non-shiny material
  148. if(materialType === "MeshLambertMaterial") {
  149. material = new THREE.MeshLambertMaterial( {
  150. color: colors[i],
  151. emissive : new THREE.Color( 0x000510 ),
  152. wireframe : false,
  153. wireframeLinewidth : 3
  154. })
  155. this.spheres.push(
  156. new THREE.Mesh( sphere, material)
  157. );
  158. this.gui.add( material, 'speed', 0, 2 );
  159. } else if(materialType === "MeshPhongMaterial") {
  160. this.spheres.push(
  161. new THREE.Mesh( sphere, new THREE.MeshPhongMaterial( {
  162. color: colors[i],
  163. specular : new THREE.Color( 0xffffff ),
  164. shininess : Math.pow(10, i),
  165. emissive : new THREE.Color( 0x000510 ),
  166. shading : THREE.NoShading,
  167. wireframe : false,
  168. wireframeLinewidth : 3
  169. }))
  170. );
  171. } else if(materialType === "MeshNormalMaterial") {
  172. this.spheres.push(
  173. new THREE.Mesh( sphere, new THREE.MeshNormalMaterial( {
  174. }))
  175. );
  176. } else if(materialType === "MeshDepthMaterial") {
  177. this.spheres.push(
  178. new THREE.Mesh( sphere, new THREE.MeshDepthMaterial( {
  179. shading: THREE.FlatShading,
  180. wireframe: true
  181. }))
  182. );
  183. this.camera.near = 20;
  184. this.camera.far = 100;
  185. } else if(materialType === "MeshFaceMaterial") {
  186. /* Can't get to work
  187. var materials = [],
  188. colorIterator;
  189. //Create 6 materials
  190. for (var j=0; j<6; j++) {
  191. var img = new Image();
  192. img.src = "textures/cube/clean/" + i + '.jpg';
  193. var tex = new THREE.Texture(img);
  194. img.tex = tex;
  195. img.onload = function() {
  196. this.tex.needsUpdate = true;
  197. };
  198. colorIterator = Math.floor(Math.random() * colors.length);
  199. materials[j] = new THREE.MeshBasicMaterial({
  200. color: 0xffffff,
  201. transparent: true,
  202. blending: THREE.AdditiveBlending,
  203. map: tex
  204. });
  205. }
  206. console.log(materials);
  207. this.spheres.push(
  208. new THREE.Mesh( sphere, new THREE.MeshFaceMaterial( materials ))
  209. );
  210. */
  211. } else {
  212. if(materialType !== "MeshBasicMaterial") {
  213. console.log(material + " material not implemented by this script. Using MeshBasicMaterial");
  214. }
  215. this.spheres.push(
  216. new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color : colors[i] } ) )
  217. );
  218. }
  219. }
  220. //Set the position
  221. this.spheres[0].position.set( 0, 0, 0);
  222. this.spheres[1].position.set( 10, 0, 0);
  223. this.spheres[2].position.set(-10, 0, 0);
  224. this.spheres[3].position.set( 0, 10, 0);
  225. this.scene.add(this.spheres[0]);
  226. this.scene.add(this.spheres[1]);
  227. this.scene.add(this.spheres[2]);
  228. this.scene.add(this.spheres[3]);
  229. },
  230. addGrid : function() {
  231. var lineMaterial = new THREE.LineBasicMaterial( { color: 0x303030 } ),
  232. geometry = new THREE.Geometry(),
  233. floor = -75, step = 25;
  234. for ( var i = 0; i <= 40; i ++ ) {
  235. geometry.vertices.push( new THREE.Vector3( - 500, floor, i * step - 500 ) );
  236. geometry.vertices.push( new THREE.Vector3( 500, floor, i * step - 500 ) );
  237. geometry.vertices.push( new THREE.Vector3( i * step - 500, floor, -500 ) );
  238. geometry.vertices.push( new THREE.Vector3( i * step - 500, floor, 500 ) );
  239. }
  240. this.grid = new THREE.Line( geometry, lineMaterial, THREE.LinePieces );
  241. this.scene.add( this.grid );
  242. },
  243. addEventListeners : function() {
  244. window.onresize = this.resizeHandler.bind(this);
  245. },
  246. resizeHandler : function() {
  247. this.camera.aspect = window.innerWidth / window.innerHeight;
  248. this.camera.updateProjectionMatrix();
  249. this.renderer.setSize( window.innerWidth, window.innerHeight );
  250. },
  251. loop : function() {
  252. requestAnimationFrame( this.loop.bind(this) );
  253. this.render();
  254. },
  255. render : function() {
  256. this.renderer.render( this.scene, this.camera );
  257. }
  258. };
  259. window.onload = function() {
  260. window.exampleScene = new ExampleScene();
  261. };
  262. </script>
  263. </head>
  264. <body>
  265. <div id='clean'></div>
  266. <select id='material'>
  267. <option>MeshBasicMaterial</option>
  268. <option>MeshLambertMaterial</option>
  269. <option>MeshPhongMaterial</option>
  270. <option>MeshNormalMaterial</option>
  271. <option>MeshDepthMaterial</option>
  272. <!-- <option>MeshFaceMaterial</option> -->
  273. </select>
  274. </body>
  275. </html>