首页 > 其他分享 >three.js之Group

three.js之Group

时间:2024-05-23 16:31:44浏览次数:13  
标签:function Group three controls js add const 20 THREE

Group

<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";
  import { initRenderer, initPerspectiveCamera, initAmbientLight, initSpotLight, addPlane } from "./init.js";

  function init() {
    const gui = new GUI();

    const renderer = initRenderer("mainCanvas");

    const scene = new THREE.Scene();

    const camera = initPerspectiveCamera();
    scene.add(camera);

    const spotLight = initSpotLight();
    scene.add(spotLight);

    const ambientLight = initAmbientLight();
    scene.add(ambientLight);

    const plane = addPlane({ width: 1000, height: 100 });
    scene.add(plane);

    let sphere;
    let cube;
    let group;
    let box;
    let arrow;
    const step = 0.03;
    const controls = setupControls();
    controls.redraw();

    render();

    function render() {
      if (controls.grouping && controls.rotate) {
        group.rotation.y += step;
      }

      if (controls.rotate && !controls.grouping) {
        sphere.rotation.y += step;
        cube.rotation.y += step;
      }

      requestAnimationFrame(render);
      renderer.render(scene, camera);
    }

    function setupControls() {
      const controls = new (function () {
        this.cubePosX = 0;
        this.cubePosY = 3;
        this.cubePosZ = 10;

        this.spherePosX = 10;
        this.spherePosY = 5;
        this.spherePosZ = 0;

        this.groupPosX = 10;
        this.groupPosY = 5;
        this.groupPosZ = 0;

        this.grouping = false;
        this.rotate = false;

        this.groupScale = 1;
        this.cubeScale = 1;
        this.sphereScale = 1;

        this.redraw = function () {
          scene.remove(group);

          sphere = createMesh(new THREE.SphereGeometry(5, 10, 10));
          cube = createMesh(new THREE.BoxGeometry(6, 6, 6));

          sphere.position.set(controls.spherePosX, controls.spherePosY, controls.spherePosZ);
          sphere.scale.set(controls.sphereScale, controls.sphereScale, controls.sphereScale);
          cube.position.set(controls.cubePosX, controls.cubePosY, controls.cubePosZ);
          cube.scale.set(controls.cubeScale, controls.cubeScale, controls.cubeScale);

          group = new THREE.Group();
          group.position.set(controls.groupPosX, controls.groupPosY, controls.groupPosZ);
          group.scale.set(controls.groupScale, controls.groupScale, controls.groupScale);
          group.add(sphere);
          group.add(cube);

          scene.add(group);
          controls.positionBoundingBox();

          if (arrow) scene.remove(arrow);
          arrow = new THREE.ArrowHelper(new THREE.Vector3(0, 1, 0), group.position, 10, 0x0000ff);
          scene.add(arrow);
        };

        this.positionBoundingBox = function () {
          scene.remove(box);
          const cbox = new THREE.Box3().setFromObject(group);
          const size = cbox.getSize(new THREE.Vector3());
          const pos = cbox.getCenter(new THREE.Vector3());

          box = new THREE.Mesh(
            new THREE.BoxGeometry(size.x, size.y, size.z),
            new THREE.MeshBasicMaterial({
              color: 0x000000,
              vertexColors: false,
              wireframeLinewidth: 2,
              wireframe: true,
            })
          );
          box.position.copy(pos);
          scene.add(box);
        };
      })();

      const sphereFolder = gui.addFolder("sphere");
      sphereFolder.add(controls, "spherePosX", -20, 20).onChange(function (e) {
        sphere.position.x = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      sphereFolder.add(controls, "spherePosZ", -20, 20).onChange(function (e) {
        sphere.position.z = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      sphereFolder.add(controls, "spherePosY", -20, 20).onChange(function (e) {
        sphere.position.y = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      sphereFolder.add(controls, "sphereScale", 0, 3).onChange(function (e) {
        sphere.scale.set(e, e, e);
        controls.positionBoundingBox();
        controls.redraw();
      });

      const cubeFolder = gui.addFolder("cube");
      cubeFolder.add(controls, "cubePosX", -20, 20).onChange(function (e) {
        cube.position.x = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      cubeFolder.add(controls, "cubePosZ", -20, 20).onChange(function (e) {
        cube.position.z = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      cubeFolder.add(controls, "cubePosY", -20, 20).onChange(function (e) {
        cube.position.y = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      cubeFolder.add(controls, "cubeScale", 0, 3).onChange(function (e) {
        cube.scale.set(e, e, e);
        controls.positionBoundingBox();
        controls.redraw();
      });

      const groupFolder = gui.addFolder("group");
      groupFolder.add(controls, "groupPosX", -20, 20).onChange(function (e) {
        group.position.x = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      groupFolder.add(controls, "groupPosZ", -20, 20).onChange(function (e) {
        group.position.z = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      groupFolder.add(controls, "groupPosY", -20, 20).onChange(function (e) {
        group.position.y = e;
        controls.positionBoundingBox();
        controls.redraw();
      });
      groupFolder.add(controls, "groupScale", 0, 3).onChange(function (e) {
        group.scale.set(e, e, e);
        controls.positionBoundingBox();
        controls.redraw();
      });

      gui.add(controls, "grouping");
      gui.add(controls, "rotate");

      return controls;
    }
  }

  init();

  function createMesh(geom) {
    const meshMaterial = new THREE.MeshNormalMaterial();
    meshMaterial.side = THREE.DoubleSide;

    const plane = new THREE.Mesh(geom, meshMaterial);
    return plane;
  }
</script>

标签:function,Group,three,controls,js,add,const,20,THREE
From: https://www.cnblogs.com/caroline2016/p/18208838

相关文章

  • NodeJS-高性能编程-全-
    NodeJS高性能编程(全)原文:zh.annas-archive.org/md5/DF276329F6BD35B176ABE023A386AF47译者:飞龙协议:CCBY-NC-SA4.0前言在像Node.js这样的平台上实现高性能意味着要了解如何充分利用硬件的各个方面,并帮助内存管理发挥最佳作用,并正确决定如何设计复杂的应用程序。如果您的......
  • mysql8.0+版本在使用group by 出现的问题
    起因:由于想使用MySQL8中的函数,手动将项目中的数据库从5.7升级到了8.0.20社区版本,但是升级完之后部分查询报错了,错误信息如下 whichisnotfunctionallydependentoncolumnsinGROUPBYclause;thisisincompatiblewithsql_mode=only_full_group_by去搜了一下,推荐的几......
  • nodejs + express + mysql + redis 基础功能实现
    nodejs+express+mysql+redis基础功能实现yeyue  9人赞同了该文章本文大体介绍了nodejs项目的创建、express框架的使用、mysql数据库的连接、以及redis的数据交互等方法,并举例了些简单的例子进行说明,代码都是亲自重头跑了一遍的,拿来可用。 一、......
  • Meteor-JS-框架入门指南-全-
    MeteorJS框架入门指南(全)原文:zh.annas-archive.org/md5/A6A998711E02B953FECB90E097CD1168译者:飞龙协议:CCBY-NC-SA4.0序言我们生活在一个惊人的时代。医学、通信、物理以及所有其他科学领域的进步为我们提供了创建一些在短短一段时间前简直是无法创造的事物的机会。然......
  • IDocList/IDocDict JSON for Delphi and FPC
    IDocList/IDocDictJSONforDelphiandFPC【英文原文】多年来,我们的开源mORMot框架提供了多种方式,以处理在运行时定义的任何数组/对象文档组合,例如通过JSON,它具备许多功能,并且非常高的性能。我们的TDocVariant自定义变体类型是处理这类无模式数据的一种强大方式,但一些用户......
  • FullCalendar插件js原生用法
    1.先看下要实现的效果图,左侧栏为当日时间,顶部为部门所有人员,表格内容是人员事件,要求数据多的时候,左侧栏和顶部固定,支持横竖滚动条,如图:  2.这里用的js原生写法:<html><head><title>工作记录详情</title><metaname="decorator"content="default"/><s......
  • JSP九大内置对象详解
    *JSP九大内置对象详解*JSP内置对象(9个内置对象):1.PageContextjavax.servlet.jsp.PageContextJSP的页面容器2.requestjavax.servlet.http.HttpServletrequest获取用户的请求信息3.responsejavax.servlet.http.HttpServletResponse服务器向客户端的回应信息4.sessionj......
  • JSP九大内置对象
    JSP九大内置对象JSP提供了由容器实现和管理的内置对象,也可以称之为隐含对象,由于JSP使用Java作为脚本语言,所以JSP将具有强大的对象处理能力,并且可以动态创建Web页面内容。但Java语法在使用一个对象前,需要先实例化这个对象,这其实是一件比较烦琐的事情。JSP为了简化开发,提供了一些内......
  • Nodejs Playwright 2Captcha 验证码识别实现自动登陆
    NodejsPlaywright2Captcha验证码识别实现自动登陆需求日常工作当中,为了提高工作效率,我们可能会写脚本来自动执行任务。有些网站因为需要用户登陆,所以脚本的自动登陆功能必不可少。不过我们在登陆网站的时候经常会出现验证码,验证码的目的就是为了防止机器登陆、自动化脚本操......
  • uniapp中使用mqtt.js的踩坑记录
    最近在uniapp的vue3.0版本中使用mqtt.js库时遇到了一些坑,经过亲身踩坑,现在把实际能够实现在uniapp的app端能够使用mqtt.js的方法步骤记录如下:一、安装首先安装mqtt.js,建议使用较为稳定的3.0.0版本:[email protected]二、引入mqtt.jsimportmqttfrom'mqtt/dist/mqtt.......