美文网首页
threejs简单demo测试 a放到b的中间

threejs简单demo测试 a放到b的中间

作者: 吉凶以情迁 | 来源:发表于2023-11-05 15:14 被阅读0次
image.png

经过测试 进行缩放并不会影响中心点的定位,但是实际项目无法居中,搞不懂


<html>

<head>
    <script type="importmap">
        {
        "imports": {
            "three": "https://cdn.jsdelivr.net/npm/three@0.157.0/build/three.module.js",
            "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.157.0/examples/jsm/"
        }
    }

    
    
</script>
<!-- {
    "imports": {
        "three": "../build/three.module.js",
        "three/addons/": "./jsm/"
    }
} -->



    <script type="module">
        import * as THREE from "three";
        // 导入轨道控制器
        
        // import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.157.0/examples/jsm/controls/OrbitControls.js';
       import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        // console.log(THREE);

        // 目标:使用控制器查看3d物体

        // 1、创建场景
        const scene = new THREE.Scene();

        // 2、创建相机
        const camera = new THREE.PerspectiveCamera(
            75,
            window.innerWidth / window.innerHeight,
            0.1,
            1000
        );

        // 设置相机位置
        camera.position.set(0, 0, 10);
        scene.add(camera);


// 创建物体 A
const geometryA = new THREE.BoxGeometry(10, 1, 1); // 举例:使用盒子几何体
const materialA = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const objectA = new THREE.Mesh(geometryA, materialA);
scene.add(objectA); // 将物体 A 添加到场景
objectA.position.set(-2,0,0)//故意把A不妨到世界原点
// 创建物体 B
const geometryB = new THREE.BoxGeometry(1, 1, 10); // 举例:使用盒子几何体
const materialB = new THREE.MeshBasicMaterial({ color: 0x00FF00 }); // 设置颜色为红色
const objectB = new THREE.Mesh(geometryB, materialB);

// 计算物体 B 相对于物体 A 的位置,使其居中
const relativePositionB = new THREE.Vector3(0, 0, 0); // 通过调整这些值来设置 B 相对于 A 的位置
objectB.position.set(5,0,0)//故意把设置到其他为止
objectB.scale.set(0.01,1,1)
// 设置物体 B 的位置中间
objectB.position.copy(relativePositionB);

// 将物体 B 添加为物体 A 的子物体
objectA.add(objectB);

        // 初始化渲染器
        const renderer = new THREE.WebGLRenderer();
        // 设置渲染的尺寸大小
        renderer.setSize(window.innerWidth, window.innerHeight);
        // console.log(renderer);
        // 将webgl渲染的canvas内容添加到body
        document.body.appendChild(renderer.domElement);

        // // 使用渲染器,通过相机将场景渲染进来
        // renderer.render(scene, camera);

        // 创建轨道控制器
        const controls = new OrbitControls(camera, renderer.domElement);

        // 添加坐标轴辅助器
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        function render() {
            renderer.render(scene, camera);
            //   渲染下一帧的时候就会调用render函数
            requestAnimationFrame(render);
        }

        render();
    </script>
</head>

<body>

</body>

</html>

相关文章

  • Hello Three.js 之 Hello world!

    Threejs介绍 Demo查看 threejs 最早是 Ricardo Cabello(一个西班牙小伙) 在 G...

  • threejs 详解(一)

    1. 开始 1.1 开使用threejs写一个最简洁的demo 1.2 threejs常用对象或对象属性 1.2...

  • vue+three.js开发,引入ThreeBSP问题

    初学threejs,使用vue开发,按照网上教程写demo的需要引入ThreeBSP这个库,按照demo安装Thr...

  • RxJava 1.x 源码分析

    Demo 分析 最简单的 demo 没有进行简写,每个中间变量都赋予了名字,方便后面说明. demo的输出结果就是...

  • Threejs 简介

    Threejs 是什么 官网对 Threejs 的介绍非常简单:“Javascript 3D library”。o...

  • 升级版ThreeJS 3D粒子波浪动画

    效果图 升级版demo 2:改变粒子的形状 升级版demo 1:改变粒子大小,颜色 ThreeJS官方示例:htt...

  • 2-1、处理ts-axios的url参数

    编写基础请求代码中最后的测试demo是 我们希望最后请求的url是/api/demo1/get?a=1&b=2,这...

  • ThreeJS 学习笔记——JavaScript 中的函数与对象

    一、前言 ThreeJs 的代码不管是源码还是 demo 的代码,都是相对规范的。而对于 JavaScript 基...

  • icomet 使用

    简介 一个消息推送中间件 高并发 使用简单 安装和运行 使用 curl demo python demo tes...

  • A/B test

    一、A/B 测试是什么? A/B 测试的本质是分离式组间试验,也叫对照试验。简单来说,A/B 测试在产品优化中的应...

网友评论

      本文标题:threejs简单demo测试 a放到b的中间

      本文链接:https://www.haomeiwen.com/subject/gegzidtx.html