three.js基础之小案例

news/2024/10/3 0:34:32

 静态场景

<canvas id="mainCanvas"></canvas>
<script type="importmap">{"imports": {"three": "./js/build/three.module.js","three/addons/": "./js/jsm/"}}
</script>
<script type="module">import * as THREE from "three";import { TrackballControls } from "three/addons/controls/TrackballControls.js";import { GUI } from "three/addons/libs/lil-gui.module.min.js";function init() {const gui = new GUI();const renderer = new THREE.WebGLRenderer({canvas: document.getElementById("mainCanvas"),antialias: true,});renderer.setClearColor(new THREE.Color(0x000000));renderer.setSize(window.innerWidth, window.innerHeight);renderer.setPixelRatio(window.devicePixelRatio);renderer.shadowMap.enabled = true;const scene = new THREE.Scene();const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);camera.position.set(-30, 30, 30);camera.lookAt(scene.position);const spotLight = new THREE.SpotLight(0xffffff, 20, 0, Math.PI / 3, 0, 0.5);spotLight.position.set(-30, 30, -30);spotLight.castShadow = true;// spotLight.shadow.mapSize = new THREE.Vector2(1024, 1024);// spotLight.shadow.camera.far = 130;// spotLight.shadow.camera.near = 40;scene.add(spotLight);gui.add(spotLight.position, "x", -50, 100).name("SpotLight光源位置x");gui.add(spotLight.position, "y", 0, 100).name("SpotLight光源位置y");gui.add(spotLight.position, "z", -50, 100).name("SpotLight光源位置z");gui.add(spotLight, "intensity", 0, 100).name("SpotLight光源强度");gui.add(spotLight, "angle", 0, 2 * Math.PI).name("SpotLight光源照射范围的角度");gui.add(spotLight, "distance", 0, 5000).name("SpotLight光源照射的最大距离");gui.add(spotLight, "decay", 0, 10).name("SpotLight光源沿着光照距离的衰退量");const ambienLight = new THREE.AmbientLight(0xffffff, 1);scene.add(ambienLight);gui.add(ambienLight, "intensity", 0, 100).name("AmbientLight光源强度");createTree(scene);createHouse(scene);createGroundPlane(scene);createBoundingWall(scene);const cubeGeometry = new THREE.BoxGeometry(4, 4, 4);const cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000,});const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);cube.castShadow = true;cube.position.set(-4, 2, 0);scene.add(cube);const sphereGeometry = new THREE.SphereGeometry(4, 20, 20);const sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff,});const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);sphere.position.set(20, 4, 2);sphere.castShadow = true;scene.add(sphere);const planeGeometry = new THREE.PlaneGeometry(60, 20);const planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa,});const plane = new THREE.Mesh(planeGeometry, planeMaterial);plane.rotation.x = -0.5 * Math.PI;plane.position.set(15, 0, 0);plane.receiveShadow = true;scene.add(plane);render();function render() {requestAnimationFrame(render);renderer.render(scene, camera);}}function createBoundingWall(scene) {const wallLeft = new THREE.BoxGeometry(70, 2, 2);const wallRight = new THREE.BoxGeometry(70, 2, 2);const wallTop = new THREE.BoxGeometry(2, 2, 50);const wallBottom = new THREE.BoxGeometry(2, 2, 50);const wallMaterial = new THREE.MeshLambertMaterial({color: 0xa0522d,});const wallLeftMesh = new THREE.Mesh(wallLeft, wallMaterial);const wallRightMesh = new THREE.Mesh(wallRight, wallMaterial);const wallTopMesh = new THREE.Mesh(wallTop, wallMaterial);const wallBottomMesh = new THREE.Mesh(wallBottom, wallMaterial);wallLeftMesh.position.set(15, 1, -25);wallRightMesh.position.set(15, 1, 25);wallTopMesh.position.set(-19, 1, 0);wallBottomMesh.position.set(49, 1, 0);scene.add(wallLeftMesh);scene.add(wallRightMesh);scene.add(wallBottomMesh);scene.add(wallTopMesh);}function createGroundPlane(scene) {const planeGeometry = new THREE.PlaneGeometry(70, 50);const planeMaterial = new THREE.MeshLambertMaterial({color: 0x9acd32,});const plane = new THREE.Mesh(planeGeometry, planeMaterial);plane.receiveShadow = true;plane.rotation.x = -0.5 * Math.PI;plane.position.set(15, 0, 0);scene.add(plane);}function createHouse(scene) {const roof = new THREE.ConeGeometry(5, 4);const base = new THREE.CylinderGeometry(5, 5, 6);const roofMesh = new THREE.Mesh(roof,new THREE.MeshLambertMaterial({color: 0x8b7213,}));const baseMesh = new THREE.Mesh(base,new THREE.MeshLambertMaterial({color: 0xffe4c4,}));roofMesh.position.set(25, 8, 0);baseMesh.position.set(25, 3, 0);roofMesh.receiveShadow = true;baseMesh.receiveShadow = true;roofMesh.castShadow = true;baseMesh.castShadow = true;scene.add(roofMesh);scene.add(baseMesh);}function createTree(scene) {const trunk = new THREE.BoxGeometry(1, 8, 1);const leaves = new THREE.SphereGeometry(4);const trunkMesh = new THREE.Mesh(trunk,new THREE.MeshLambertMaterial({color: 0x8b4513,}));const leavesMesh = new THREE.Mesh(leaves,new THREE.MeshLambertMaterial({color: 0x00ff00,}));trunkMesh.position.set(-10, 4, 0);leavesMesh.position.set(-10, 12, 0);trunkMesh.castShadow = true;trunkMesh.receiveShadow = true;leavesMesh.castShadow = true;leavesMesh.receiveShadow = true;scene.add(trunkMesh);scene.add(leavesMesh);}init();
</script>

管道动画

<canvas id="mainCanvas"></canvas>
<script type="importmap">{"imports": {"three": "./js/build/three.module.js","three/addons/": "./js/jsm/"}}
</script>
<script type="module">import * as THREE from "three";const renderer = new THREE.WebGLRenderer({canvas: document.getElementById("mainCanvas"),antialias: true,});renderer.setClearColor(new THREE.Color(0x000000));renderer.setSize(window.innerWidth, window.innerHeight);renderer.setPixelRatio(window.devicePixelRatio);renderer.shadowMap.enabled = true;const scene = new THREE.Scene();const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);camera.position.set(-30, 30, 30);camera.lookAt(scene.position);const spotLight = new THREE.SpotLight(0xffffff, 20, 0, Math.PI / 3, 0, 0.5);spotLight.position.set(-30, 30, -30);spotLight.castShadow = true;scene.add(spotLight);const ambienLight = new THREE.AmbientLight(0xffffff, 1);scene.add(ambienLight);const path = new THREE.CatmullRomCurve3([new THREE.Vector3(-5, 2, 9),new THREE.Vector3(-1, 4, 4),new THREE.Vector3(0, 0, 0),new THREE.Vector3(6, -6, 0),new THREE.Vector3(9, -4, 6),new THREE.Vector3(12, 3, 3),]);// 三维样条曲线作为TubeGeometry参数生成管道const geometry = new THREE.TubeGeometry(path, 100, 5, 30);const texLoader = new THREE.TextureLoader();//纹理贴图texLoader.load("../images/square.jpg", (texture) => {//UV坐标U方向阵列模式texture.wrapS = THREE.RepeatWrapping;// 纹理沿着管道方向阵列(UV坐标U方向)texture.repeat.x = 100;const material = new THREE.MeshBasicMaterial({map: texture,side: THREE.DoubleSide,});const mesh = new THREE.Mesh(geometry, material);scene.add(mesh);});let i = 0;render();function render() {const pointsArr = path.getSpacedPoints(500);if (i < pointsArr.length - 1) {camera.position.copy(pointsArr[i]);camera.lookAt(pointsArr[i + 1]);i += 1;} else {i = 0;}renderer.render(scene, camera);requestAnimationFrame(render);}
</script>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/29296.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

国密算法SM2-java实现

Maven依赖<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.56</version> </dependency>工具类import java.math.BigInteger;public class Util {/*** 整形转换成网络传输…

黑客精神和白帽子

在当今数字化的世界里,黑客精神和白帽子的角色变得愈发重要。本文将探讨黑客精神的本质,介绍白帽子的概念和职责。 1、黑客精神 所谓的“黑客精神”,主要指的是一种探索计算机软件和硬件极限,追求技术创新和完善的文化态度和哲学理念。 黑客精神强调的是对知识的渴求,对于…

NFS工作原理(重要)

NFS工作流程 1.NFS服务端启动后、将自己的端口信息,注册到rpcbind服务中 2.NFS客户端通过TCP/IP的方式,连接到NFS服务端提供的rpcbind服务,并且从该服务中获取具体的端口信息 3.NFS客户端拿到具体端口信息后,将自己需要执行的函数,通过网络发给NFS服务端对应的端口 4.NFS服…

mysql多表查询

1. 多表查询项目开发中,在进行数据库表结构设计时,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在着各种联系,基本上分为三种:一对多(多对一) 多对多 一对一2. 分类连接查询内连接:相当于查询A、B交集部分数据外…

text-generation-webui 推理模型相关报错问题解决

推理代码 text-generation-webui 推理模型 Qwen1.5-7B-Chat sys info nvcc --versioncuda 11.8 import torch >>> print(torch.__version__) 1 路径错误2 依赖没安装 ImportError: This modeling file requires the following packages that were not found in your …

通过 pip 安装自己的代码包

以前通过 pip 安装的时候总是很羡慕,别人的代码使用起来好方便啊,那时候觉得代码要提交到 pip 平台去管理肯定需要审核吧? 后来了解到自己的代码要可以 pip 安装不需要审核,只需要遵循几个步骤就能轻松实现:准备代码包 通过 setuptools 打包 通过 twine 上传 (需要 pypi …

微信小程序使用微信云托管添加自定义域名并转发到pexels.com

背景:我要在小程序上显示pexels.com上的图片,然后我得先把pexels.com的域名添加到小程序的request合法域名中,但是pexels.com是国外的,在国内没有备案所以添加不了。解决方案就是:用一个已经备案好的域名进行转发,转发的服务器我选择的是微信云托管,备案好的域名还需要s…

【攻防技术系列】-- JNDI注入

JNDI概念首先第一个问题,什么是 JNDI?JNDI (Java Naming and Directory Interface),是Java平台提供的一个API,它允许Java应用程序访问不同的命名和目录服务。简而言之,JNDI为Java应用提供了一种统一的方式来查询和访问外部资源,如数据库、文件系统、远程对象等。 虽然有点…