dir.by  
Поиск >>

Компьютер, программы
→
Three.js - JavaScript library for drawing graphics 3D in the pages HTML
 Example "Flying cubes" library Three.js 
посмотрели 8625 раз
обновлено: 22 November 2018
Example we look, we test
Source code
  JavaScript  
<!DOCTYPE html>
<html lang="en">

     <head>
          <title>three.js webgl - layers</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
          <style>
               body
               {
                    font-family: Monospace;
                    background-color: #f0f0f0;
                    margin: 0px;
                    overflow: hidden;
               }

               #info
               {
                    position: absolute;
                    top: 10px;
                    width: 100%;
                    text-align: center;
                    z-index: 0; // to not conflict with dat.gui
                    display:block;
               }
          </style>
     </head>

     <body>

          <!-- connect the library three.js -->
          <script src="https://dir.by/example_lib/tree_js/three.js"></script>

          <div id="info">
               <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl layers
          </div>

          <script>
               var container, stats;
               var camera, scene, renderer;

               var radius = 100, theta = 0;

               init();
               animate();

               function init()
               {
                    container = document.createElement( 'div' );
                    document.body.appendChild( container );

                    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
                    camera.layers.enable( 0 ); // enabled by default
                    camera.layers.enable( 1 );
                    camera.layers.enable( 2 );

                    scene = new THREE.Scene();
                    scene.background = new THREE.Color( 0xf0f0f0 );

                    var light = new THREE.PointLight( 0xffffff, 1 );
                    light.layers.enable( 0 );
                    light.layers.enable( 1 );
                    light.layers.enable( 2 );

                    scene.add( camera );
                    camera.add( light );

                    var colors = [ 0xff0000, 0x00ff00, 0x0000ff ];
                    var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
                    var layer;

                    for ( var i = 0; i < 300; i ++ )
                    {
                         layer = ( i % 3 );

                         var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: colors[ layer ] } ) );

                         object.position.x = Math.random() * 800 - 400;
                         object.position.y = Math.random() * 800 - 400;
                         object.position.z = Math.random() * 800 - 400;

                         object.rotation.x = Math.random() * 2 * Math.PI;
                         object.rotation.y = Math.random() * 2 * Math.PI;
                         object.rotation.z = Math.random() * 2 * Math.PI;

                         object.scale.x = Math.random() + 0.5;
                         object.scale.y = Math.random() + 0.5;
                         object.scale.z = Math.random() + 0.5;

                         object.layers.set( layer );

                         scene.add( object );
                    }

                    raycaster = new THREE.Raycaster();

                    renderer = new THREE.WebGLRenderer();
                    renderer.setPixelRatio( window.devicePixelRatio );
                    renderer.setSize( window.innerWidth, window.innerHeight );
                    container.appendChild(renderer.domElement);

                    window.addEventListener( 'resize', onWindowResize, false );
               }

               function onWindowResize()
               {
                    camera.aspect = window.innerWidth / window.innerHeight;
                    camera.updateProjectionMatrix();

                    renderer.setSize( window.innerWidth, window.innerHeight );
               }

               function animate()
               {
                    requestAnimationFrame( animate );

                    render();
               }

               function render()
               {
                    theta += 0.1;

                    camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
                    camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
                    camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
                    camera.lookAt( scene.position );

                    renderer.render( scene, camera );
               }
          </script>

     </body>
</html>
Note!
The example works locally!

Run the example on your computer:
1) Create a new file my.html
2) Copy source code in the my.html file
3) Run in browser my.html
 
← Previous topic
What the Three.js and how to download?
 
Next topic →
Example "Roller coaster (roller coaster)" library Three.js
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

 
• What the Three.js and how to download?
• Example "Flying cubes" library Three.js
• Example "Roller coaster (roller coaster)" library Three.js
• Example "Rotating cube with texture (rotate cube)" library Three.js
• Example "We spin the picture ourselves svg (svg image)" library Three.js
• Example "The moon revolves around the earth (moon rotate around earth)" library Three.js
• Example "Drawing a picture with 3D cubes (interactive voxel painter)" library Three.js
• Example "Reflection balls (effects anaglyph)" library Three.js
• Create your own file .js The file contains an object with export variables and functions

  Ваши вопросы присылайте по почте: info@dir.by