3d cube shapes using javascript

I got this code from webgl, I used it in the same way as they mentioned in their tutorial, but I can't get the result on my browser screen, only I can see that it is just a black background nothing else. I used the three.js library to create a 3d cube model.

 <pre>
          <!doctype html>

           <html>
       <head>
       <meta charset="utf-8">
       <style>
       canvas { width: 100%; height: 100% }
       </style>
       <title>Pryamid</title>
       </head>

<body>

      

     

 <script>
 var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera(75,    window.innerWidth/window.innerHeight, 0.1, 1000);

        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        var cubegeometry = new THREE.CubeGeometry(1,1,1);
        var cubematerial = new THREE.MeshBasicMaterial({wireframe: true, color: 0x000000});

        var cube = new THREE.Mesh(cubegeometry, cubematerial);

        scene.add(cube);

        camera.position.z = 5;

        var render = function () {
            requestAnimationFrame(render);

            cube.rotation.y += 0.01;
            cube.rotation.x += 0.01;
            cube.rotation.z += 0.01;

            renderer.render(scene, camera);
        };

        // Calling the render function
window.onload = function() {
render();
};

</script>
</body>
</html>

      

+3


source to share


1 answer


Is this your complete code? I think it would be helpful if you post the complete HTML context of what you are trying to achieve.

However, at first I see that you are either not adding a render target to your HTML page, or you are not using the canvas that you have in your HTML to render.



The easiest way to render a cube is to add a render canvas to the DOM page using: document.body.appendChild( renderer.domElement );

+4


source







All Articles