首页 > 其他分享 >threejs 拖拽 画矩形

threejs 拖拽 画矩形

时间:2023-04-03 20:01:00浏览次数:39  
标签:threejs geometry corePositions THREE window var new 矩形 拖拽

import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

export function initThree() {
  THREE.Object3D.DefaultUp.set(0, 0, 1);

  var scene = new THREE.Scene();
  var camera = new THREE.PerspectiveCamera(
    60,
    window.innerWidth / window.innerHeight,
    1,
    1000000
  );
  camera.position.set(0, 0, 10);
  var renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0x999999);
  document.body.appendChild(renderer.domElement);

  var controls = new OrbitControls(camera, renderer.domElement);
  controls.mouseButtons = {
    LEFT: null as unknown as THREE.MOUSE,
    // LEFT: MOUSE.ROTATE,
    RIGHT: THREE.MOUSE.RIGHT,
    MIDDLE: THREE.MOUSE.MIDDLE,
  };

  var grid = new THREE.GridHelper(20, 40);
  grid.geometry.rotateX(Math.PI / 2);
  grid.position.set(0, 0.05, 0);
  scene.add(grid);

  var mouse = new THREE.Vector3();

  var started = false;

  var MAX_POINTS = 5;

  // geometry
  var geometry = new THREE.BufferGeometry();
  // attributes
  var positions = new Float32Array(MAX_POINTS * 3); // 3 vertices per point
  geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
  // geometry.setIndex()
  // draw range
  var drawCount = 3; // draw the first 2 points, only
  geometry.setDrawRange(0, drawCount);
  // material
  var material = new THREE.LineBasicMaterial({ color: 0xff0000 });
  // line
  var line = new THREE.Line(geometry, material);
  scene.add(line);

  var corePositions = line.geometry.attributes.position.array as any;

  var started = false;

  drawRect();

  function drawRect() {
    // geometry
    geometry = new THREE.BufferGeometry();
    // attributes
    positions = new Float32Array(MAX_POINTS * 3); // 3 vertices per point
    geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
    // draw range
    drawCount = 3; // draw the first 2 points, only
    geometry.setDrawRange(0, drawCount);

    // material
    material = new THREE.LineBasicMaterial({ color: 0xff0000 });
    // line
    line = new THREE.Line(geometry, material);
    scene.add(line);

    corePositions = line.geometry.attributes.position.array;
    // console.log(corePositions);

    window.addEventListener("mousedown", onm ouseDown, false);
    window.addEventListener("mousemove", onm ouseMove, false);
    window.addEventListener("mouseup", onm ouseUp, false);
  }

  render();

  //Functions
  function onm ouseDown(event: any) {
    if (!started) {
      mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
      mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
      mouse.z = 0;

      const raycaster = new THREE.Raycaster();
      var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);

      raycaster.setFromCamera(mouse, camera);
      var intersects = new THREE.Vector3();
      let planeIntersect = raycaster.ray.intersectPlane(plane, intersects)!;

      console.log(planeIntersect);

      //Pt1
      corePositions[0] = planeIntersect.x;
      corePositions[1] = planeIntersect.y;
      corePositions[2] = 0;

      // console.log(corePositions)

      line.geometry.setDrawRange(1, drawCount++);
      line.geometry.attributes.position.needsUpdate = true;

      started = true;
    }
  }

  function onm ouseMove(event: any) {
    if (started) {
      mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
      mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
      mouse.z = 0;

      const raycaster = new THREE.Raycaster();
      var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);

      raycaster.setFromCamera(mouse, camera);
      var intersects = new THREE.Vector3();
      let planeIntersect = raycaster.ray.intersectPlane(plane, intersects)!;

      // console.log(planeIntersect)
      // console.log(corePositions);

      //Pt2
      corePositions[3] = planeIntersect.x;
      corePositions[4] = corePositions[1];
      corePositions[5] = 0;
      //Pt3
      corePositions[6] = planeIntersect.x;
      corePositions[7] = planeIntersect.y;
      corePositions[8] = 0;
      //Pt4
      corePositions[9] = corePositions[0];
      corePositions[10] = planeIntersect.y;
      corePositions[11] = 0;
      //Pt5
      corePositions[12] = corePositions[0];
      corePositions[13] = corePositions[1];
      corePositions[14] = 0;

      line.geometry.setDrawRange(0, 5);
      line.geometry.attributes.position.needsUpdate = true;
    }
  }

  function onm ouseUp(event: any) {
    if (started) {
      mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
      mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
      mouse.z = 0;

      const raycaster = new THREE.Raycaster();
      var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);

      raycaster.setFromCamera(mouse, camera);
      var intersects = new THREE.Vector3();
      let planeIntersect = raycaster.ray.intersectPlane(plane, intersects)!;

      console.log(corePositions);
      console.log(planeIntersect);

      // --------------------
      geometry = new THREE.BufferGeometry();
      geometry.attributes.position = new THREE.BufferAttribute(
        corePositions,
        3
      );

      geometry.setIndex([0, 1, 2, 2, 3, 0]);

      let mesh = new THREE.Mesh(
        geometry,
        new THREE.MeshBasicMaterial({
          color: 0xfefefe,
          side: THREE.DoubleSide,
          transparent: true,
          opacity: 0.1,
        })
      );

      scene.add(mesh);

      window.removeEventListener("mousemove", onm ouseMove);
      window.removeEventListener("mousedown", onm ouseDown);
      window.removeEventListener("mouseup", onm ouseUp);

      started = false;
    }
  }

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

标签:threejs,geometry,corePositions,THREE,window,var,new,矩形,拖拽
From: https://www.cnblogs.com/zhuoss/p/17284218.html

相关文章

  • Vue3 v-drag 拖拽指令的简单使用
    文档官网文档:https://www.npmjs.com/package/v-drag使用安装、引入npminstallv-drag--saveimportdragfrom"v-drag"使用直接使用:<divv-drag>Dragme!</div>注意:对原本绝对定位水平居中的div,其居中的实现方式应为:div{ position:absolute; left:50%; trans......
  • 坐标轴中两矩形重叠面积
    1、先排除没有重叠的情况2、找出重叠的面积的宽和长用在x轴方向上两矩形左边两个边x值x1、x2,求max(x1,x2)右边两条边x值x3,x4,求min(x3,x4)length=abs(max(x1,x2)-min(x3,x4)width同理,在y轴方向上操作最后area=length+width  1intgetSpace(intx1,inty1,intx2,inty2......
  • 算法随想Day53【单调栈】| LC84-柱状图中最大的矩形
    intlargestRectangleArea(vector&heights){intresult=0;stackst;heights.insert(heights.begin(),0);heights.push_back(0);st.push(0);for(inti=1;i<heights.size();i++){if(heights[i]>heights[st.top()]){st.push(......
  • WinForm的Listbox控件实现拖拽数据功能
    WinForm的Listbox控件实现拖拽数据功能相关函数:MouseDownDragDropDragEnter思路:当左侧的空间鼠标按下(MouseDown)以后,触发DragDrop操作,然后移动到右侧控件中,将会触发右侧控件的DragEnter事件。右侧同理MouseDown:触发拖动操作DragDrop:从当前容器中拖拽动作完成时的操作(发......
  • HTML + javascript implement a draggable list 一个可以拖拽交换顺序的列表
    Reference:https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragover_event<body><styletype="text/css">.draggable{text-align:center;background:red;width:20px;......
  • Mapboxgl draw 自定义标绘:圆、矩形、自由多边形、上传读取geojson
    还没做文字标绘,累了,以后有需要有机会再说自定义标绘方法Mapboxgl标绘相关库我当前使用的版本是:"@mapbox/mapbox-gl-draw":"^1.4.1","@mapbox/mapbox-gl-draw-static-mode":"^1.0.1","mapbox-gl-draw-circle":"^1.1.2",&quo......
  • threejs中渲染html
    背景最近中看threejs的时候发现一个好玩的事情,可以在threejs中渲染普通的html。threejs本身可以做各种炫酷的界面,但是与用户交互的时候写起来没有用dom实现方便,但是如果可以将已有的dom渲染到threejs中,那么就可以实现非常炫酷的界面,也能提高用户的体验。依赖介绍这里使用react......
  • threejs-camera&controls&renderer(WebGLRenderer)
    ArrayCamera:一般用于,展示益、一个场景存在多个物体,每个物体各自拥有自己的视角的这种场景。CubeCamera:一次性创建六个方位的相机(类似于正方体六个面,立方全景图中所有方......
  • 【二维装箱】基于BL算法求解矩形地块二维装箱放置优化问题附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • 面向对象-拖拽
    <script>//一个页面上实现两个拖拽//不同的效果:一个有边界限定,一个没有functionDrag(ele){this.ele=ele;//因为使用事件监听式......