Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <meta name="viewport" content="width=device-width, user-scalable=yes, minimum-scale=1.0, maximum-scale=1.0">
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/renderers/CSS3DRenderer.js"></script>

              
            
!

CSS

              
                 <style>
        body {
            margin: 0;
        }

        canvas {
            width: 100%;
            height: 100%
        }
    </style>
              
            
!

JS

              
                
    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);


    //group
    var group = new THREE.Group();

    scene.add(group)


    //message box
    var geometry = new THREE.PlaneGeometry(2, 1.5, 1, 1);
    //var texture = THREE.ImageUtils.loadTexture('image/bg_images.png')
    var material = new THREE.MeshBasicMaterial({
       // map: texture,
        color: '#0086bf',
        side: THREE.DoubleSide,
        opacity: 1,
        transparent: true
    });
    var boardMesh = new THREE.Mesh(geometry, material);
    boardMesh.position.set(-1, 0, 0)

    group.add(boardMesh);

    //icon
    var geometry = new THREE.BoxGeometry(1, 1, 1);
    var material = new THREE.MeshBasicMaterial({color: 0x00ff00});
    var iconMesh = new THREE.Mesh(geometry, material);
    iconMesh.position.set(3, 0, 0)
    group.add(iconMesh);

    //网格线
    var grid = new THREE.GridHelper(200, 400, 0xffffff, 0x000000);
    grid.material.opacity = 0.2;
    grid.material.transparent = true;
    scene.add(grid);


    var controls = new THREE.OrbitControls(camera, renderer.domElement);
    controls.autoRotate = true
    controls.maxPolarAngle = Math.PI * 0.49;
    controls.update()
    camera.position.z = 5;
    // boardMesh.updateMatrixWorld(true)

    var animate = function () {
        boardMesh.lookAt(camera.position)
        drawLine(iconMesh,boardMesh,group)

        renderer.render(scene, camera);

        renderer.setAnimationLoop(animate);


    };

    animate();


    function drawLine(iconMesh,boardMesh,group_) {

        var locationIndex=0 //点位置,0=左上角,1=右上角,2=左下角,3=右下角


        var distance1 = iconMesh.position.clone().distanceTo(boardMesh.geometry.vertices[0].clone().applyMatrix4( boardMesh.matrixWorld ));
        var distance2 = iconMesh.position.clone().distanceTo(boardMesh.geometry.vertices[1].clone().applyMatrix4( boardMesh.matrixWorld ));
        var distance3 = iconMesh.position.clone().distanceTo(boardMesh.geometry.vertices[2].clone().applyMatrix4( boardMesh.matrixWorld ));
        var distance4 = iconMesh.position.clone().distanceTo(boardMesh.geometry.vertices[3].clone().applyMatrix4( boardMesh.matrixWorld ));
        if (distance1<distance2 && distance1<distance3 && distance1<distance4){
            locationIndex=0
        }
        if (distance2<distance1 && distance2<distance3 && distance2<distance4){
            locationIndex=1
        }
        if (distance3<distance1 && distance3<distance2 && distance3<distance4){
            locationIndex=2
        }
        if (distance4<distance1 && distance4<distance3 && distance4<distance2){
            locationIndex=3
        }

        //board世界坐标
        var vector = boardMesh.geometry.vertices[locationIndex].clone();
        vector.applyMatrix4( boardMesh.matrixWorld );
        var points = [];
        points.push(iconMesh.position);
        points.push( new THREE.Vector3(vector.x-group_.position.x,vector.y-group_.position.y,vector.z-group_.position.z ) );
        var geometry = new THREE.BufferGeometry().setFromPoints( points );
        //判断是否存在line
        if(group_.getObjectByName("line")){
            //更新line

            group_.getObjectByName("line").geometry=geometry
        }else{

            //线
            var material = new THREE.LineBasicMaterial( { color: "#ff0000" } );

            var line = new THREE.Line( geometry, material );
            line.name="line"
            group_.add( line );
        }


    }


              
            
!
999px

Console