Production Level Knowledge & Tips

  1. 未分類
  2. 6 view

[Javascript]Three.js trial

Three.js allows us to draw 3D CG on browser.
threejs.org I’ll draw cube using it.
Reference page is here. First is download.
I’ll use three.min.js in build directory.
git clone https://github.com/mrdoob/three.js.git
Next is html file.
Three.js seems to make canvas element.
<!DOCTYPE html>
<html>
        <head>
                <title>Cube</title>
                <!-- three.js library -->
                <script src="./three.min.js" type="text/javascript"></script>
                <!-- main processing part -->
                <script src="./main.js" type="text/javascript"></script>
        </head>
        <body>
                <!-- Cube will be drawn here -->
                <div id="canvas"></div>
        </body>
</html>
Next is main.js.
window.onload = function() {
        // Canvas size
        var width = 640;
        var height = 480;

        // Cube Size
        var geometry = new THREE.CubeGeometry(100, 100, 100);
        // Cube Material Info(not metal)
        var material = new THREE.MeshLambertMaterial({color: 0x0000aa});
        // Create Cube
        var mesh = new THREE.Mesh(geometry, material);
        // Rotate Cube
        mesh.rotation = {x: 0.5, y: 0.5, z: 0.0};

        // Setting Camera
        var camera = new THREE.PerspectiveCamera(40, width / height, 1, 1000);
        // Camera Position
        camera.position.z = -400;
        // Camera Angle
        camera.lookAt(mesh.position);

        // Create Space
        var scene = new THREE.Scene();
        // Add cube to Space
        scene.add(mesh);

        // Create Light element
        var light    = new THREE.DirectionalLight(0xffffff, 1.5);
        // Light element position
        light.position = {x: 0, y: 0.2, z: -1};
        // Add light element to the space
        scene.add(light);

        // Create renderer
        var renderer = new THREE.WebGLRenderer();
        // Set canvas size
        renderer.setSize(width, height);

        // Set canvas to div element
        document.getElementById('canvas').appendChild(renderer.domElement);
        renderer.render(scene, camera);
}
Result is like this.
Easy to write 🙂
Demo

未分類 recent post

  1. Install sbt 1.0.0 and run sample template

  2. Visualization of Neural Network and its Train…

  3. [Machine Learning]Created docker image includ…

  4. [Node.js]How to write batch script with Node.…

  5. [Hive]Get top n items for each category

関連記事

PAGE TOP