【three.js练习程序】随机生成100个方块

DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ceshititle>
    <script type="text/javascript" src="C:\Users\asus\Desktop\three.js-master\three.js-master\build\three.js">script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    style>
head>
<body>


<div id="WebGL-output">
div>


<script type="text/javascript">

    init();

    function  init() {

        var scene = new THREE.Scene();

        var camera = new THREE.PerspectiveCamera(45,
            window.innerWidth / window.innerHeight, 0.1, 1000);

        camera.position.x = 100;
        camera.position.y = 100;
        camera.position.z = 100;
        camera.lookAt(scene.position);

        //创建一个WebGL渲染器并设置其大小
        var renderer = new THREE.WebGLRenderer();
        renderer.setClearColor(new THREE.Color(0xEEEEEE));
        renderer.setSize(window.innerWidth, window.innerHeight);

        var axes = new THREE.AxisHelper(200);
        scene.add(axes);

        for(var i=0;i<100;i++)
        {
            var geometry = new THREE.BoxGeometry( 5, 5, 5 );
            for ( var j = 0; j < geometry.faces.length; j +=2 ) {

                var hex = Math.random() * 0xffffff;
                geometry.faces[ j ].color.setHex( hex );
                geometry.faces[ j + 1 ].color.setHex( hex );

            }
            var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, overdraw: 0.5 } );

            cube = new THREE.Mesh( geometry, material );

            cube.position.x = Math.random()*100-50;
            cube.position.y = Math.random()*100-50;
            cube.position.z = Math.random()*100-50;

            scene.add( cube );

        }
        document.getElementById("WebGL-output").appendChild(renderer.domElement);

        //渲染场景
        renderer.render(scene, camera);
    }
    
script>
body>
html>

【three.js练习程序】随机生成100个方块_第1张图片

 

转载于:https://www.cnblogs.com/tiandsp/p/8445821.html

你可能感兴趣的:(【three.js练习程序】随机生成100个方块)