用three.js绘制一个坐标系

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../js/three.js"></script>
</head>
<body οnlοad="main()">
<script>
    function initTHREE(){
        width = window.innerWidth;
        height = window.innerHeight;
        renderer = new THREE.WebGLRenderer({
            antialias:true  //是否为抗锯齿
        });
        renderer.setSize(width,height);
        renderer.setClearColor(0xFFFFFF,1.0);
        document.body.appendChild(renderer.domElement);
    }
    function initCamera(){
        camera = new THREE.PerspectiveCamera(45,width/height,1,1000);
        camera.position.z = 15;
        camera.position.x = 3;//将相机的位置往右边挪5个单位
    }
    function initScene(){
        scene = new THREE.Scene();
    }
    function initGeometry(){
        var geometry = new THREE.CubeGeometry(2,2,2);
        var materail = new THREE.MeshBasicMaterial({color:0xff0000});
         cube = new THREE.Mesh(geometry,materail);
        //在网页上显示坐标系,其中坐标系的长度为6
        var axisHelper =new THREE.AxisHelper(6);
       /* scene.add(cube);*/         //-------
        //将坐标系加入到场景中                  |--->这个两个注释掉的主要原因是因为下面要演示将坐标系和我们的几何体绑定的例子
        /*scene.add(axisHelper);*/   //-------

        var object = new THREE.Object3D();//声明一个3d对象
        object.add(axisHelper);            //将坐标系加入到对象中
        object.add(cube);                   //将几何实体加入到对象中
        //得到一个绑定了实体和坐标系的对象,然后将它加入到场景当中
        scene.add(object);

    }
    function main(){
        initTHREE();
        initCamera();
        initScene();
        initGeometry();
        render();

    }
    function render(){
        requestAnimationFrame(render);
        cube.rotation.y += 0.01;//0.01是弧度值
        renderer.render(scene,camera);
    }
</script>
</body>
</html>

 

转载于:https://www.cnblogs.com/hzStudy/p/7712642.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Three.js 绘制3D路线图并带有坐标系的完整代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Three.js 3D路线图示例</title> <style> body { margin: 0; padding: 0; } canvas { width: 100%; height: 100%; } </style> </head> <body> <script src="https://cdn.jsdelivr.net/npm/three@0.131.0/build/three.min.js"></script> <script> // 初始化场景、相机、渲染器 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 添加坐标系 const axesHelper = new THREE.AxesHelper(5); scene.add(axesHelper); // 创建路径点数组 const points = []; points.push(new THREE.Vector3(-5, 0, 0)); points.push(new THREE.Vector3(-3, 3, 0)); points.push(new THREE.Vector3(0, 5, 0)); points.push(new THREE.Vector3(3, 3, 0)); points.push(new THREE.Vector3(5, 0, 0)); points.push(new THREE.Vector3(3, -3, 0)); points.push(new THREE.Vector3(0, -5, 0)); points.push(new THREE.Vector3(-3, -3, 0)); points.push(new THREE.Vector3(-5, 0, 0)); // 创建路径线条 const geometry = new THREE.BufferGeometry().setFromPoints(points); const material = new THREE.LineBasicMaterial({ color: 0x0000ff }); const line = new THREE.Line(geometry, material); scene.add(line); // 将相机位置设置为路径中心点,使路径居中显示 const center = new THREE.Vector3(); geometry.computeBoundingBox(); geometry.boundingBox.getCenter(center); camera.position.set(center.x, center.y, 20); // 添加光源 const light = new THREE.DirectionalLight(0xffffff, 1); light.position.set(1, 1, 1); scene.add(light); // 渲染循环 function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); </script> </body> </html> ``` 在这个示例中,我们首先初始化了场景、相机和渲染器,然后添加了一个坐标系。接着,我们创建了一个包含多个点的路径数组,并使用 `BufferGeometry` 和 `LineBasicMaterial` 创建了一条路径线条。我们还将相机的位置设置为路径中心点,使路径居中显示,并添加了一个光源。最后,我们开启了渲染循环,实现了动态渲染效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值