首页 > 其他分享 >three.js基础之小案例

three.js基础之小案例

时间:2024-05-09 11:46:28浏览次数:10  
标签:scene const THREE three 案例 add position new js

 静态场景

<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>

 

标签:scene,const,THREE,three,案例,add,position,new,js
From: https://www.cnblogs.com/caroline2016/p/18181774

相关文章

  • Nginx负载均衡、动静分离Tomcat案例实战
    一、前言1)Tomcat是一款开源的、免费的WEB软件服务器,是隶属于Apache基金会旗下的,主要是用于去发布网站代码、提供网页信息服务的。用户通过浏览器可以实现网站页面的访问。2)TomcatWEB软件默认可以处理静态网页(Apache、Nginx),同时也可以处理动态网页,主要是处理JSP动态网页,JSP(Java......
  • P4407 [JSOI2009] 电子字典
    题目链接:https://www.luogu.com.cn/problem/P4407trie树+爆搜做法:对所有文本串建树。对于编辑距离要求的三种情况,分四类在trie树上爆搜即可。#definemaxn200010structtrie{intson[maxn][26];intcnt[maxn];intidx=0;map<string,bool>mm;intv......
  • python读写json文件
    1.新建json文件打开记事本,重命名为.json后缀使用的样例如下,注意看json文件格式:{"server":{"host":"example.com","port":443,"protocol":"https"},"authentication":{......
  • 232自由口转Profinet网关接基恩士扫码枪与PLC通讯案例
     232自由口转Profinet网关(XD-PNR100/300)是一款作用于将232自由口转换为Profinet协议,实现不同网络之间的无缝通信和数据交换。232自由口转Profinet网关具有极高的灵活性和可靠性,为工业控制系统提供了强大的支持。通过将自由口信号转换为Profinet协议,可以轻松实现不同设备之间的......
  • 案例02--scrapy综合练习--中大网校
    一抓取需求#抓取需求start_url='http://ks.wangxiao.cn/'抓取首页各分类下--->各种考试--->考点练习里各科目的练习题eg:工程类--->一级建造师--->建设工程经济--->章节#存储需求:文件存储题目中可能包含图片md格式比较合适按照分类依次存为......
  • 2024-05-08 js 常见案例
    1.表单验证functionvalidateForm(){varname=document.forms["myForm"]["name"].value;if(name==""){alert("Namemustbefilledout");returnfalse;}//更多的验证.........
  • 解决HtmlUnit执行JS报错提示ScriptException
    问题描述HtmlUnit作为一款比Selenium更轻量的HeadLess的Java版本浏览器模拟器,不需要在服务器上安装部署浏览器及其Driver程序。但是,众所周知,HtmlUnit对JS脚本的支持并不是很有话,GitHub中大部分的issue都和JS执行错误有关。笔者在实际使用(HtmlUnit4.1.0版本)过程中也遇到了JS执......
  • vue案例
    任务清单(单文件)<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>模板</title>......
  • 案例分析:通过两个学生项目的例子,推断出这些团队的血型:1、STG游戏的跳票(为了完美,推迟了
    案例分析:通过两个学生项目的例子,推断出这些团队的血型:1、STG游戏的跳票(为了完美,推迟了7天,但是7天之后也没有发布……)我怀着无比沉痛的心情宣布,我们的游戏因尚未达到预期的可玩性,为了不丢人现眼,延迟发布i天(i<=7)。我们在起初的计划中,以发布后一周的下载量作为项目衡量的标准。虽......
  • 第11周案例分析
    答:从这两个学生项目的例子中,我们可以推断出这些团队在项目管理和时间管理方面存在一些问题。以下是对这两个案例的分析:STG游戏跳票案例:项目管理问题:项目团队在项目初期就设定了一个明确的发布日期,但最终未能按时发布。这表明团队在时间估计和项目进度控制方面可能存在不......