• 【前端知识】Three 学习日志(一)—— Three.js 的简单尝试


    Three 学习日志(一)—— Three.js 的简单尝试

    Three.js是一个使用JavaScript编写的轻量级3D图形库,它可以在浏览器中渲染出3D场景。在学习Three.js的过程中,建立基本场景是一个重要的第一步。通过设置相机、场景和渲染器等组件,可以创建一个简单的3D场景。在建立基本场景之后,可以通过添加模型、材质、光源等元素来丰富场景效果。此外,使用requestAnimationFrame方法可以让场景动起来,实现动画效果。

    一、简单尝试 Three.js

    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Learn Three.jstitle>
        
        <script src="../build/three.js">script>
    head>
    
    <body>
        <script>
            // 任意调用API,为了检测three.js是否引入成功
            console.log(THREE.Scene);
            // 创建3D场景对象Scene
            const scene = new THREE.Scene();
            // 创建一个长方体几何对象Geometry
            const geometry = new THREE.BoxGeometry(100, 100, 100);
            // 创建一个材质对象Material(MeshBasicMaterial 为网格基础材质)
            const material = new THREE.MeshBasicMaterial({
                color: 0xff0000,//0xff0000设置材质颜色为红色
            });
            // 创建物体:网格模型(Mesh为网格模型)
            // 两个参数分别为上面创建的几何体geometry、材质material
            const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
            // 设置网格模型在三维空间中的位置坐标,默认是坐标原点
            mesh.position.set(0, 10, 0);
            // 构建好物体后,将物体添加到场景中
            scene.add(mesh);
            // 至此,我们的物体已经添加到场景中,但我们并不能立即看到构建的物体,还需要通过虚拟相机进行渲染
            // 实例化一个透视投影相机对象
            const camera = new THREE.PerspectiveCamera();
            // 30:视场角度, width / height:Canvas画布宽高比, 1:近裁截面, 3000:远裁截面
            // const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
            // 根据需要设置相机位置具体值
            camera.position.set(200, 200, 200);
            //相机观察目标指向Threejs 3D空间中某个位置
            camera.lookAt(0, 0, 0); //坐标原点
            // camera.lookAt(mesh.position);//指向mesh对应的位置
            // 定义相机输出画布的尺寸(单位:像素px)
            const width = 800; //宽度
            const height = 500; //高度
            // 创建渲染器对象
            const renderer = new THREE.WebGLRenderer();
            // 设置渲染器宽高
            renderer.setSize(width, height);
            // 通过渲染器,将场景和相机进行结合、渲染,得到一个Canvas元素
            renderer.render(scene, camera); //执行渲染操作
            // 将渲染结果加入页面中
            document.body.appendChild(renderer.domElement);
        script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    二、效果展示
    在这里插入图片描述

  • 相关阅读:
    PMI-ACP练习题(11)
    Redis学习笔记(三)redis配置文件 & 持久化
    SpringAOP前备知识
    vsomeip环境搭建及helloworld测试例跑通
    简单工厂模式
    面向Java开发者的ChatGPT提示词工程(6)
    第十二天Tensorflow基础和简单的线性回归实现
    JVM-GC-基础知识
    Redis概述及基本数据结构
    HTML CSS大学生期末网页大作业 DW个人网页设计 人物介绍 历史人物岳飞介绍
  • 原文地址:https://blog.csdn.net/weixin_42919342/article/details/132975993