THREE.js代码备份——线框cube、按键、鼠标控制

<!DOCTYPE html>
<html lang="en">
<head>
    <title>three.js canvas - geometry - cube</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;
        }
    </style>
</head>
<body>

<script src="../build/three.js"></script>

<script src="js/renderers/Projector.js"></script>
<script src="js/renderers/CanvasRenderer.js"></script>

<script src="js/libs/stats.min.js"></script>

<script>

    var container, stats;

    var camera, scene, renderer;

    var cube, plane;

    var fy=0.0;
    var fx=0.0;
    var zoom=20;
    var targetRotation = 0;
    var targetUp = 0;
    var targetRotationOnMouseDown = 0;
    var targetUpOnMouseDown = 0;
    var mouseX = 0;
    var mouseY=0;
    var mouseXOnMouseDown = 0;
    var mouseYOnMouseDown = 0;

    var windowHalfX = window.innerWidth / 2;
    var windowHalfY = window.innerHeight / 2;

    init();
    animate();

    function init() {

        container = document.createElement( ‘div‘ );
        document.body.appendChild( container );

        var info = document.createElement( ‘div‘ );
        info.style.position = ‘absolute‘;
        info.style.top = ‘10px‘;
        info.style.width = ‘100%‘;
        info.style.textAlign = ‘center‘;
        info.innerHTML = ‘Drag to spin the cube‘;
        container.appendChild( info );

        camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
        camera.maxDistance=7500;
        camera.position.y = 150;
        camera.position.z = 500;

        scene = new THREE.Scene();

        // Cube

        var geometry = new THREE.CubeGeometry(200, 200,200, 2, 2, 3 );

//        for ( var i = 0; i < geometry.faces.length; i += 2 ) {
//
//            var hex = Math.random() * 0xffffff;
//            geometry.faces[ i ].color.setHex( hex );
//            geometry.faces[ i + 1 ].color.setHex( hex );
//
//        }

        //var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, overdraw: 0.5 } );
          var material = new THREE.MeshBasicMaterial({
              color: 0xff0000,
              wireframe: true
          });
        cube = new THREE.Mesh( geometry, material );
        cube.position.y = 150;
        scene.add( cube );

        // Plane

        var geometry = new THREE.PlaneBufferGeometry( 200, 200 );
        geometry.rotateX( - Math.PI / 2 );

        var material = new THREE.MeshBasicMaterial( { color: 0xe0e0e0, overdraw: 0.5 } );

        plane = new THREE.Mesh( geometry, material );
        scene.add( plane );

        renderer = new THREE.CanvasRenderer();
        renderer.setClearColor( 0xf0f0f0 );
        renderer.setPixelRatio( window.devicePixelRatio );
        renderer.setSize( window.innerWidth, window.innerHeight );
        container.appendChild( renderer.domElement );

        stats = new Stats();
        container.appendChild( stats.dom );

        document.addEventListener( ‘mousedown‘, onDocumentMouseDown, false );
        document.addEventListener( ‘touchstart‘, onDocumentTouchStart, false );
        document.addEventListener( ‘touchmove‘, onDocumentTouchMove, false );
        document.addEventListener(‘keydown‘,onDocumentKeyDown,false);
        document.addEventListener( ‘mousewheel‘, onMouseWheel, false);

        window.addEventListener( ‘resize‘, onWindowResize, false );

    }

    function onWindowResize() {

        windowHalfX = window.innerWidth / 2;
        windowHalfY = window.innerHeight / 2;

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

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

    }

    //

    function onDocumentMouseDown( event ) {

        event.preventDefault();

        document.addEventListener( ‘mousemove‘, onDocumentMouseMove, false );
        document.addEventListener( ‘mouseup‘, onDocumentMouseUp, false );
        document.addEventListener( ‘mouseout‘, onDocumentMouseOut, false );

        mouseXOnMouseDown = event.clientX - windowHalfX;
        mouseYOnMouseDown = event.clientY - windowHalfY;
        targetRotationOnMouseDown = targetRotation;
        targetUpOnMouseDown = targetUp;

    }

    function onDocumentMouseMove( event ) {

        mouseX = event.clientX - windowHalfX;
        mouseY = event.clientY - windowHalfY;
        targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
        targetUp = targetUpOnMouseDown + (mouseY - mouseYOnMouseDown)*0.02;
    }

    function onDocumentMouseUp( event ) {

        document.removeEventListener( ‘mousemove‘, onDocumentMouseMove, false );
        document.removeEventListener( ‘mouseup‘, onDocumentMouseUp, false );
        document.removeEventListener( ‘mouseout‘, onDocumentMouseOut, false );

    }

    function onDocumentMouseOut( event ) {

        document.removeEventListener( ‘mousemove‘, onDocumentMouseMove, false );
        document.removeEventListener( ‘mouseup‘, onDocumentMouseUp, false );
        document.removeEventListener( ‘mouseout‘, onDocumentMouseOut, false );

    }

    function onDocumentTouchStart( event ) {

        if ( event.touches.length === 1 ) {

            event.preventDefault();

            mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
            mouseYOnMouseDown =  event.touches[ 0 ].pageY - windowHalfY;
            targetRotationOnMouseDown = targetRotation;
            targetUpOnMouseDown = targetup;

        }

    }

    function onDocumentTouchMove( event ) {

        if ( event.touches.length === 1 ) {

            event.preventDefault();

            mouseX = event.touches[ 0 ].pageX - windowHalfX;
            mouseY = event.touches[ 0 ].pageY - windowHalfY;
            targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
            targetup = targetUpOnMouseDown + ( mouseY - mouseYOnMouseDown)*0.05;

        }

    }

    function onMouseWheel(event){
        if(event.wheelDelta > 0){ //前滚
         cube.position.z-=zoom;
         plane.position.z-=zoom;

        }else
        {
            cube.position.z+=zoom;
            plane.position.z+=zoom;
        }
    }

    function onDocumentKeyDown(event){
        switch(event.keyCode){
            case 37: //左
                fy -= 0.1;
                break;
            case 39: //右
                fy += 0.1;
                break;
            case 38: //上
                //fx -= 0.1;
                cube.position.z-=10;

                break;
            case 40: //下
                //fx += 0.1;
                cube.position.z+=10;
                break;
            default:
                break;
        }
    }

    //

    function animate() {

        requestAnimationFrame( animate );

        stats.begin();
        render();
        stats.end();

    }

    function render() {

        cube.rotation.x +=( targetUp - cube.rotation.x ) * 0.05;  //鼠标控制上下旋转
        cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
        //cube.rotation.x+=0.1;

        //cube.rotation.y+=fy;
       // cube.rotation.x+=fx;

        plane.rotation.y = cube.rotation.y;
        renderer.render( scene, camera );

    }

</script>

ript>

</body>
</html>
时间: 2024-10-09 22:33:44

THREE.js代码备份——线框cube、按键、鼠标控制的相关文章

THREE.js代码备份——webgl - materials - cube refraction [balls](以上下左右前后6张图片构成立体场景、透明球体效果)

<!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - materials - cube refraction [balls]</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,

THREE.js代码备份——canvas - lines - colors(希尔伯特曲线3D、用HSL设置线颜色)

<!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - lines - colors</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, m

THREE.js代码备份——canvas_ascii_effect(以AscII码显示图形)

<!DOCTYPE html> <html lang="en"> <head> <title>three.js - ASCII Effect</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-sc

THREE.js代码备份——webgl - custom attributes [lines](自定义字体显示、控制字图的各个属性)

<!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - custom attributes [lines]</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scal

THREE.js代码备份——canvas_lines(随机点、画线)

<!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - lines - random</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, m

js代码备份

<SCRIPT>function togoogle(){fh1=document.getElementById("f1");fh1.action="https://www.google.com/search";th1=document.getElementById("kww");th1.name="q";}function todict(){fh1=document.getElementById("f1&

鼠标选择文字事件js代码,增加层问题

在页面中增加一个js代码,当用户用鼠标选择文字(鼠标拖动涂蓝文字)时,会出现一个层,提示与这个选择文字有个的信息<script type="text/javascript">document.onmouseup = document.ondbclick= function(){ var txt; if(document.selection){  txt = document.selection.createRange().text }else{  txt = window.

table中td超出内容隐藏,鼠标悬停全部显示(完整版,含js代码)

一.CSS语法: text-overflow:clip | ellipsis 默认值:clip 适用于:所有元素 clip: 当对象内文本溢出时不显示省略标记(...),而是将溢出的部分裁切掉. ellipsis: 当对象内文本溢出时显示省略标记(...). 在使用的时候,有时候发现不会出现省略标记效果,经过测试发现,使用ellipsis的时候,必须配合overflow:hidden; white-space:nowrap; width:50%;这三个样式共同使用才会有效果 实例: table

鼠标点击自定义文字展现特效JS代码

JS特效使用方法 只需将如下JS代码放到</body>之前就好了 var a_idx = 0; jQuery(document).ready(function($) { $("body").click(function(e) { var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正&q