How to overlay HTML text/buttons on three.js?

前端 未结 2 926
有刺的猬
有刺的猬 2021-02-14 19:00

Ok, very new to three.js here but I am trying to achieve what Google has with https://beinternetawesome.withgoogle.com/interland

See - they have their F

相关标签:
2条回答
  • 2021-02-14 19:41

    Creativity is up to you, as there are many ways to achieve the desired result.

    You could've started from looking at the source code of examples from the official web site of Three.js. There you can see how the info section of an example sets on a web page.

    var buttons = document.getElementsByTagName("button");
    for (let i = 0; i < buttons.length; i++) {
      buttons[i].addEventListener("click", onButtonClick, false);
    };
    
    function onButtonClick(event) {
      alert(event.target.id);
    }
    
    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 controls = new THREE.OrbitControls(camera, renderer.domElement);
    
    var geometry = new THREE.BoxGeometry(1, 1, 1);
    var material = new THREE.MeshBasicMaterial({
      color: 0x00ff00,
      wireframe: true
    });
    var cube = new THREE.Mesh(geometry, material);
    scene.add(cube);
    
    camera.position.z = 5;
    
    var animate = function() {
      requestAnimationFrame(animate);
      renderer.render(scene, camera);
    };
    
    animate();
    body {
      overflow: hidden;
      margin: 0;
    }
    
    .ui {
      position: absolute;
    }
    
    button{
      margin: 20px;
    }
    <script src="https://threejs.org/build/three.min.js"></script>
    <script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
    
    <div style="width:100%;height:50px;text-align: center; color:white;" class="ui">Info header</div>
    <button style="top:0" id="fire" class="ui">Fire</button>
    <button style="bottom:0" id="spell" class="ui">Spell</button>
    <button style="right:0" id="health" class="ui">Health</button>
    <button style="right:0; bottom:0;" id="shield" class="ui">Shield</button>

    0 讨论(0)
  • 2021-02-14 19:42

    z-index only works with position property except for the case position: static and position: initial.

    This button class worked fine with my threejs scene. You can edit left, top accordingly.

    button {
        position: absolute;
        display: block;
        z-index: 99;
        left: 50%;
        top: 50%;
        }
    
    0 讨论(0)
提交回复
热议问题