2
0

FirstSteps.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>First Steps &mdash; three.js v48dev documentation</title>
  7. <link rel="stylesheet" href="../_static/nature.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '48dev',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../_static/jquery.js"></script>
  19. <script type="text/javascript" src="../_static/underscore.js"></script>
  20. <script type="text/javascript" src="../_static/doctools.js"></script>
  21. <link rel="top" title="three.js v48dev documentation" href="../index.html" />
  22. </head>
  23. <body>
  24. <div class="related">
  25. <h3>Navigation</h3>
  26. <ul>
  27. <li class="right" style="margin-right: 10px">
  28. <a href="../genindex.html" title="General Index"
  29. accesskey="I">index</a></li>
  30. <li><a href="../index.html">three.js v48dev documentation</a> &raquo;</li>
  31. </ul>
  32. </div>
  33. <div class="document">
  34. <div class="documentwrapper">
  35. <div class="bodywrapper">
  36. <div class="body">
  37. <div class="section" id="first-steps">
  38. <h1>First Steps<a class="headerlink" href="#first-steps" title="Permalink to this headline">¶</a></h1>
  39. <p>Three.js scenes are very easy to setup and only require a few lines of code to initialize. Scenes are constructed using a few different types of objects: cameras, lights, and meshes.</p>
  40. <p>The first step in rendering a three.js scene is creating the WebGL renderer object. The following code creates an HTML canvas object 800x400 pixels, adds it to the document&#8217;s body, and initializes a three.js scene.</p>
  41. <div class="highlight-javascript"><div class="highlight"><pre><span class="kd">var</span> <span class="nx">renderer</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">THREE</span><span class="p">.</span><span class="nx">WebGLRenderer</span><span class="p">();</span>
  42. <span class="nx">renderer</span><span class="p">.</span><span class="nx">setSize</span><span class="p">(</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">640</span> <span class="p">);</span>
  43. <span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span> <span class="nx">renderer</span><span class="p">.</span><span class="nx">domElement</span> <span class="p">);</span>
  44. <span class="kd">var</span> <span class="nx">scene</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">THREE</span><span class="p">.</span><span class="nx">Scene</span><span class="p">();</span>
  45. </pre></div>
  46. </div>
  47. <p>The second step is to define a camera which the renderer object will use in rendering.</p>
  48. <div class="highlight-javascript"><div class="highlight"><pre><span class="kd">var</span> <span class="nx">camera</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">THREE</span><span class="p">.</span><span class="nx">Camera</span><span class="p">(</span>
  49. <span class="mi">35</span><span class="p">,</span> <span class="c1">// Field of view</span>
  50. <span class="mi">800</span> <span class="o">/</span> <span class="mi">640</span><span class="p">,</span> <span class="c1">// Aspect ratio</span>
  51. <span class="p">.</span><span class="mi">1</span><span class="p">,</span> <span class="c1">// Near</span>
  52. <span class="mi">10000</span> <span class="c1">// Far</span>
  53. <span class="p">);</span>
  54. <span class="nx">camera</span><span class="p">.</span><span class="nx">position</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span> <span class="o">-</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">15</span> <span class="p">);</span>
  55. </pre></div>
  56. </div>
  57. <p>The first parameter passed determines how wide the field of view is. The second parameter is the aspect ratio which is calculated by dividing the viewing area&#8217;s width by its height. The third and fourth parameters specify cut-off points for objects in the camera&#8217;s view. If an object&#8217;s distance from the camera does not fall in the range between NEAR and FAR then that object is not rendered. The last line sets the camera&#8217;s XYZ coordinates to -15, 10, and 15 respectively.</p>
  58. <p>Step three creates a white cube that is 5 units wide, tall and deep, adds the Lambert material, and adds it to the scene.</p>
  59. <div class="highlight-javascript"><div class="highlight"><pre><span class="kd">var</span> <span class="nx">cube</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">THREE</span><span class="p">.</span><span class="nx">Mesh</span><span class="p">(</span>
  60. <span class="k">new</span> <span class="nx">THREE</span><span class="p">.</span><span class="nx">CubeGeometry</span><span class="p">(</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span> <span class="p">),</span>
  61. <span class="k">new</span> <span class="nx">THREE</span><span class="p">.</span><span class="nx">MeshLambertMaterial</span><span class="p">(</span> <span class="p">{</span> <span class="nx">color</span><span class="o">:</span> <span class="mh">0xFF0000</span> <span class="p">}</span> <span class="p">)</span>
  62. <span class="p">);</span>
  63. <span class="nx">scene</span><span class="p">.</span><span class="nx">addChild</span><span class="p">(</span> <span class="nx">cube</span> <span class="p">);</span>
  64. </pre></div>
  65. </div>
  66. <p>For the last step in setting up a scene we create a yellow light source and add it to the scene.</p>
  67. <div class="highlight-javascript"><div class="highlight"><pre><span class="kd">var</span> <span class="nx">light</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">THREE</span><span class="p">.</span><span class="nx">PointLight</span><span class="p">(</span> <span class="mh">0xFFFF00</span> <span class="p">);</span>
  68. <span class="nx">light</span><span class="p">.</span><span class="nx">position</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span> <span class="p">);</span>
  69. <span class="nx">scene</span><span class="p">.</span><span class="nx">addLight</span><span class="p">(</span> <span class="nx">light</span> <span class="p">);</span>
  70. </pre></div>
  71. </div>
  72. <p>Finally we render the scene which displays our scene through the camera&#8217;s eye.</p>
  73. <div class="highlight-javascript"><div class="highlight"><pre><span class="nx">renderer</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span><span class="nx">scene</span><span class="p">,</span> <span class="nx">camera</span><span class="p">);</span>
  74. </pre></div>
  75. </div>
  76. <p>Everything together in a working example with a minimal HTML template:</p>
  77. <div class="highlight-javascript"><pre>&lt;!DOCTYPE html&gt;
  78. &lt;html&gt;
  79. &lt;head&gt;
  80. &lt;title&gt;Getting Started with Three.js&lt;/title&gt;
  81. &lt;script type="text/javascript" src="Three.js"&gt;&lt;/script&gt;
  82. &lt;script type="text/javascript"&gt;
  83. window.onload = function() {
  84. var renderer = new THREE.WebGLRenderer();
  85. renderer.setSize( 800, 400 );
  86. document.body.appendChild( renderer.domElement );
  87. var scene = new THREE.Scene();
  88. var camera = new THREE.Camera(
  89. 35, // Field of view
  90. 800 / 400, // Aspect ratio
  91. .1, // Near
  92. 10000 // Far
  93. );
  94. camera.position.set( -15, 10, 15 );
  95. var cube = new THREE.Mesh(
  96. new THREE.CubeGeometry( 5, 5, 5 ),
  97. new THREE.MeshLambertMaterial( { color: 0xFF0000 } )
  98. );
  99. scene.addChild( cube );
  100. var light = new THREE.PointLight( 0xFFFF00 );
  101. light.position.set( 10, 0, 10 );
  102. scene.addLight( light );
  103. renderer.render(scene, camera);
  104. };
  105. &lt;/script&gt;
  106. &lt;/head&gt;
  107. &lt;body&gt;&lt;/body&gt;
  108. &lt;/html&gt;</pre>
  109. </div>
  110. <p>That&#8217;s how simple and easy three.js makes WebGL. Only 20 lines of Javascript to initialize and render a scene.</p>
  111. </div>
  112. </div>
  113. </div>
  114. </div>
  115. <div class="sphinxsidebar">
  116. <div class="sphinxsidebarwrapper">
  117. <h3>This Page</h3>
  118. <ul class="this-page-menu">
  119. <li><a href="../_sources/tutorials/FirstSteps.txt"
  120. rel="nofollow">Show Source</a></li>
  121. </ul>
  122. <div id="searchbox" style="display: none">
  123. <h3>Quick search</h3>
  124. <form class="search" action="../search.html" method="get">
  125. <input type="text" name="q" size="18" />
  126. <input type="submit" value="Go" />
  127. <input type="hidden" name="check_keywords" value="yes" />
  128. <input type="hidden" name="area" value="default" />
  129. </form>
  130. <p class="searchtip" style="font-size: 90%">
  131. Enter search terms or a module, class or function name.
  132. </p>
  133. </div>
  134. <script type="text/javascript">$('#searchbox').show(0);</script>
  135. </div>
  136. </div>
  137. <div class="clearer"></div>
  138. </div>
  139. <div class="related">
  140. <h3>Navigation</h3>
  141. <ul>
  142. <li class="right" style="margin-right: 10px">
  143. <a href="../genindex.html" title="General Index"
  144. >index</a></li>
  145. <li><a href="../index.html">three.js v48dev documentation</a> &raquo;</li>
  146. </ul>
  147. </div>
  148. <div class="footer">
  149. &copy; Copyright 2010-2012, three.js Authors.
  150. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
  151. </div>
  152. </body>
  153. </html>