Three.js之模型对象、材质

news/2024/7/7 20:54:40

参考资料

  • 三维向量Vector3与模型位置
  • 克隆.clone()和复制.copy()

知识点

注:基于Three.jsv0.155.0

  • 三维向量Vector3与模型位置
  • 欧拉Euler与角度属性.rotation
  • 模型材质颜色(Color对象)
  • 模型材质父类Material:透明、面属性
  • 模型材质和几何体属性
  • 克隆.clone()和复制.copy()

代码实现

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Three.js</title>
</head>
  <body>
  </body>
  <!-- 具体路径配置,你根据自己文件目录设置,我的是课件中源码形式 -->
  <script type="importmap">
    {
      "imports": {
        "three": "./js/three.module.js",
        "three/addons/": "../three.js/examples/jsm/"
      }
    }
  </script>
  <script type="module">
    import * as THREE from 'three';
    import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

    const width = 800
    const height = 500

    // 场景
    const scene = new THREE.Scene();
    // 几何体
    const geometry = new THREE.BoxGeometry(100, 100, 100);
    // 材质 
    const material = new THREE.MeshBasicMaterial({
      color:0x0000ff,
    });

    // 网格模型:物体
    const mesh = new THREE.Mesh(geometry, material);

    // 位置属性
    mesh.position.set(0, 0, 0);
    // mesh.position.x = 100;
    // mesh.scale.set(2, 2, 2);
    // mesh.scale.x = 2;
    // mesh.translateOnAxis(new THREE.Vector3(1, 0, 1), 100);
    // mesh.translateX(100);
    scene.add(mesh);

    // 角度属性
    // mesh.rotation.x = Math.PI / 4;
    // mesh.rotation.set(0, 0, Math.PI / 4);
    // mesh.rotateX(Math.PI / 4);
    console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:46 ~ mesh.rolation:', mesh.rotation)

    // 材料颜色
    // mesh.material.color.set('red');
    // mesh.material.color.set('#ff0000');
    // mesh.material.color.set(0xff0000);
    mesh.material.color.r = 1;

    // 材料透明度
    mesh.material.opacity = 0.8;
    mesh.material.transparent = true; // 是否透明

    // 材料面属性
    // mesh.material.side = THREE.FrontSide; // 正面可见
    mesh.material.side = THREE.DoubleSide; // 两面可见
    console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:66 ~ mesh.material.side:', mesh.material.side) // 2
    
    console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:63 ~ mesh.material:', mesh.material)
    console.log('🚀 ~ file: 3.1三维向量Vector3与模型位置.html:89 ~ mesh.geometry:', mesh.geometry)

    // 克隆、复制
    const mesh2 = mesh.clone();
    mesh2.position.set(200, 0, 0);
    mesh2.material = mesh.material.clone();
    mesh2.material.color.set(0xff0000);
    mesh2.position.copy(mesh.position);
    mesh2.position.y += 150;
    scene.add(mesh2);

    // 坐标系
    const axes = new THREE.AxesHelper(200);
    scene.add(axes);

    // 相机
    const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
    camera.position.set(200, 200, 200);
    camera.lookAt(scene.position);

    // 渲染器
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height);
    renderer.render(scene, camera);
    document.body.appendChild(renderer.domElement);

    // 控制器
    const controls = new OrbitControls(camera, renderer.domElement);
    controls.addEventListener('change', () => {
      renderer.render(scene, camera);
    });

    // 渲染循环
    function render() {
        mesh.rotateY(0.01);// mesh旋转动画
        // 同步mesh2和mesh的姿态角度一样,不管mesh姿态角度怎么变化,mesh2始终保持同步
        mesh2.rotation.copy(mesh.rotation);
        renderer.render(scene, camera);
        requestAnimationFrame(render);
    }
    render();
  </script>
</html>

http://lihuaxi.xjx100.cn/news/1509760.html

相关文章

FFmpeg报错:Connection to tcp://XXX?timeout=XXX failed: Connection timed out

一、现象 通过FFmpeg&#xff08;FFmpeg的版本是5.0.3&#xff09;拉摄像机的rtsp流获取音视频数据&#xff0c;执行命令&#xff1a; ./ffmpeg -timeout 3000000 -i "rtsp://172.16.17.156/stream/video5" 报错&#xff1a;Connection to tcp://XXX?timeoutXXX …

微信小程序通过npm引入tdesign包进行构建的时候报错

问题 在通过npm 引入 tdesign时&#xff1a;https://tdesign.tencent.com/miniprogram/getting-started 通过微信小程序IDE进行npm构建的时候出现&#xff1a;无法构建&#xff0c;应该怎么办&#xff1f; 解决方法&#xff1a; 1 输入&#xff1a; npm init -y命令 2 重新点…

音频——硬件拓扑

文章目录 硬件拓扑I2S 数据通路五线模式四线模式两线 TX两线 RX 典型应用硬件连接数据流 硬件拓扑 控制路径&#xff1a;UART/I2C/SPI数据路径&#xff1a;I2S 简略图如下 I2S 数据通路 五线模式 四线模式 两线 TX 两线 RX 典型应用 硬件连接 控制信号&#xff1a;SPI 用…

说说HTTP 和 HTTPS 有什么区别?

分析&回答 http协议 超文本传输协议&#xff0c;是互联网上应用最多的协议&#xff0c;基于TCP/IP通讯协议来传递信息&#xff0c;用于从WWW服务器传输超文本到本地浏览器的传输协议。 https协议 我们可以将其看作是以安全为目标的http协议。在http协议的基础上增加了S…

kafka 3.5 kafka服务端接收生产者发送的数据源码

一、服务端接收生产者数据的方法二、遍历需要保存数据的topic分区&#xff0c;分别执行保存数据操作到topic分区Leader三、在数据写入分区Leader之前&#xff0c;先获一些锁1、首先获得leaderIsrUpdateLock的读锁&#xff0c;得到Leader日志读取权限&#xff0c;做一些验证2、在…

JS中的new操作符

文章目录 JS中的new操作符一、什么是new&#xff1f;二、new经历了什么过程&#xff1f;三、new的过程分析四、总结 JS中的new操作符 参考&#xff1a;https://www.cnblogs.com/buildnewhomeland/p/12797537.html 一、什么是new&#xff1f; 在JS中&#xff0c;new的作用是通过…

HttpClient默认重试机制

分析&回答 只有发生IOExecetion时才会发生重试InterruptedIOException、UnknownHostException、ConnectException、SSLException&#xff0c;发生这4中异常不重试get方法可以重试3次&#xff0c;post方法在socket对应的输出流没有被write并flush成功时可以重试3次。读/写超…

一不留神就掉坑

乘除顺序问题 在据卡特兰数[1]公式,解决leetcode-96 不同的二叉搜索树[2]时,遇到一个非常诡异的问题, package mainimport "fmt"func main() { for i : 0; i < 40; i { fmt.Printf("第%d个卡特兰数为:%d\n", i, numTrees(i)) }}func numTrees(n int) i…