当前位置: 首页 > news >正文

three入门,创建一个长方体、认识场景、模型、光源、相机?渲染器

版本号174

在这里插入图片描述

html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>
<body>
// 这样写就可以像es6那样 import 导入,比如 from "three",就会去找下面的three.js/build/three.module.js
    <script type="importmap">
        {
            "imports": {
                "three": "../../../three.js/build/three.module.js",
                "three/addons/":"../../../three.js/examples/jsm/"
             }
        
        }
    </script>
    <script type="module">
        
    </script>
    <script src="./index.js" type="module">
    
    </script>
</body>
</html>

js代码

import * as THREE from 'three';
// 引入相机控件
import { OrbitControls } from "three/addons/controls/OrbitControls.js"


// 定义相机输出的画布的尺寸 单位 像素
const width=800
const height=500
// 创建三维场景
const scene = new THREE.Scene();
// 给三维场景添加物体

// 定一个几何体 长方体 长宽高都是100
const geometry = new THREE.BoxGeometry(100, 100, 100);
// 创建一个材质对象 
/*
 * 网格材质对象
 */
const material = new THREE.MeshLambertMaterial({
  color: 0x78EE28,  // 设置颜色 红色
  
});
// 创建一个网格模型
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(0, 0, 0);

// 添加到场景中 
scene.add(mesh);
// 创建一个三维坐标轴
const axesHelper=new THREE.AxesHelper(150);
// 坐标轴对象添加到三维场景中
scene.add(axesHelper)
// 防创建一个点光源
// 设置点光源位置 参数1素颜色,参数2强度
/**
 * 
 * 参数1:点光源颜色
 * 参数2: 光照强度 默认1
 * 参数3 光源照射的最大距离 默认值为 0(无限远)。
 * 参数4 沿着光照距离的衰退量。默认值为 2。
 * 
 */
const pointLight=new THREE.PointLight(0xffffff,1)
pointLight.pointLight=1
pointLight.decay=0.0 //光源衰减 禁用
// 光源位置 x,y z 位置
pointLight.position.set(800,150,200)
//光源也需要添加三维场景
scene.add(pointLight)
// 创建一个相机
// 相机位置在网格模型上方 近裁截面1 远裁离3000
const camera=new THREE.PerspectiveCamera(40,width/height,1,3000)
// 设置相机位置
// camera.position.set(-2000,200,200);
camera.position.set(400,200,100)
// 相机的实现 观察点的坐标
camera.lookAt(0,0,0)  // 相机观察坐标原点 X Y Z 坐标
// camera.lookAt(mesh.position) //指向网格模型mesh


// 创建一个渲染器
const renderer =new THREE.WebGLRenderer()
renderer.setSize(width,height); // 设置渲染器的尺寸 canvas画布宽高度
renderer.render(scene,camera); //执行渲染操作 将场景中的内容 渲染到相机中
//、
document.body.appendChild(renderer.domElement);


// 创建相机控件对象
// 参数 相机 渲染器
const controls =new OrbitControls(camera,renderer.domElement);
// 监听相机的移动
controls.addEventListener('change',function(e){
  console.log("哈哈哈",e)
    renderer.render(scene,camera);
})

如图

在这里插入图片描述

这个内容可以根据鼠标旋转,位置改变


文章转载自:

http://1ELREcPB.dpqqg.cn
http://k5oPZ1Q8.dpqqg.cn
http://UJfQNvQi.dpqqg.cn
http://ujbeZaa9.dpqqg.cn
http://3cVJkNRy.dpqqg.cn
http://gMUm0uP4.dpqqg.cn
http://mCkNAQch.dpqqg.cn
http://5qlA0M9I.dpqqg.cn
http://vRV6NDGQ.dpqqg.cn
http://gcERgVCZ.dpqqg.cn
http://vSi7HDw1.dpqqg.cn
http://wJiRNkAZ.dpqqg.cn
http://8UcmYqIc.dpqqg.cn
http://OvwpzgxL.dpqqg.cn
http://43FhSk1F.dpqqg.cn
http://YkhQvuhc.dpqqg.cn
http://WpRL2AAn.dpqqg.cn
http://xcxkdiQJ.dpqqg.cn
http://x6nNgJYX.dpqqg.cn
http://9zcfIsLy.dpqqg.cn
http://ij68JF7o.dpqqg.cn
http://GPBbJm5F.dpqqg.cn
http://iFhg1jl3.dpqqg.cn
http://FRHv74I6.dpqqg.cn
http://EZAGngqh.dpqqg.cn
http://MjSBCkno.dpqqg.cn
http://cinSlglj.dpqqg.cn
http://eDHrQT1e.dpqqg.cn
http://eQ3ljEhp.dpqqg.cn
http://zcfTJ4CI.dpqqg.cn
http://www.dtcms.com/a/89778.html

相关文章:

  • 【蓝桥杯】真题 路径(数论+dp)
  • 苹果企业签名经常掉签吗?
  • 类与对象(三)
  • uv - Getting Started 开始使用 [官方文档翻译]
  • 1.认识Excel
  • LRU算法实现
  • 学习 - C++ 全栈聊天项目(1)架构概述和登录界面
  • 内核编程十三:进程状态详解
  • leetcode 用队列模拟栈
  • S32K144外设实验(七):FTM输出多路互补带死区PWM
  • 【MySQL】监控MySQL
  • C++值传递和引用传递
  • 【AI学习笔记】AI造神时代的潘式理论与智能进化
  • 定长内存池原理及实现
  • 电脑干货:万能驱动--EasyDrv8
  • Go 语言封装邮件发送功能
  • 亮数据爬取API爬取亚马逊电商平台实战教程
  • Springboot应用配置github自动流部署 深入理解CI/CD:构建、测试和部署的自动化完整流程
  • 千变万化的卷积
  • HarmonyOS 之 @Require 装饰器自学指南
  • 字符串常量,数组和指针的不同形式
  • 2000-2019年各省地方财政契税数据
  • 记一次线上程序宕机问题分析【写 GC 日志导致进程挂起】
  • 比较Linux的Shell的 `EOF` 与 `echo` 与 `printf` , 将文本输出到文件
  • 003-51单片机-最小系统
  • 科学计算(2):矩阵特征值计算
  • gradio调用多个CSS的HTML页
  • 【AI论文】MARS:融入苏格拉底引导的多代理框架实现自动化提示优化
  • [已解决]服务器CPU突然飙高98%----Java程序OOM问题 (2024.9.5)
  • C++高阶