RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 853032
Accepted
pepel_xD
pepel_xD
Asked:2020-07-11 16:12:57 +0000 UTC2020-07-11 16:12:57 +0000 UTC 2020-07-11 16:12:57 +0000 UTC

重新计算对象顶点坐标

  • 772

根据答案
中的示例代码 ,我对代码进行了一些实验,我将点分散在屏幕上

textGeo.vertices.forEach(function (vertex) {
    vertex.x = THREE.Math.randFloat(-20, 20)//.copy(vertex.startPoint).addScaledVector(vertex.direction, 5 + Math.sin(Date.now() * 0.001) * 5);
    vertex.y = THREE.Math.randFloat(-20, 20)//.copy(vertex.startPoint).addScaledVector(vertex.direction, 5 + Math.sin(Date.now() * 0.001) * 5);
    vertex.z = THREE.Math.randFloat(-10, 10)//.copy(vertex.startPoint).addScaledVector(vertex.direction, 5 + Math.sin(Date.now() * 0.001) * 5);
  });

添加了几个功能并更改了渲染功能

function setPosition(vertex, curent, orign, step) {
  let curentPoint = Math.round(curent * 10) / 10;
  let orignPoint = Math.round(orign * 10) / 10;
  if (curentPoint !== orignPoint) {
    curent = orign < curent ? curent - step : curent + step;
  } else {
    curent = orign;
    vertex.complate = true;
  }
  return curent;
}

function isComplated(vertices) {
  let result = true;
  vertices.forEach(function(vertex, i) {
    if(!vertex.complate) {
      result = false;
    }
  });
  return result;
}

function render() {
  let animationId = requestAnimationFrame(render);
  if (!isComplated(textGeo.vertices)) {
    textGeo.vertices.forEach(function (vertex) {
      vertex.x = setPosition(vertex, vertex.x, vertex.startPoint.x, 0.1);
      vertex.y = setPosition(vertex, vertex.y, vertex.startPoint.y, 0.1);
      vertex.z = setPosition(vertex, vertex.z, vertex.startPoint.z, 0.1);
    });
  } else {
    cancelAnimationFrame(animationId);
  }

  textGeo.verticesNeedUpdate = true;
  renderer.render(scene, camera);
}

思路如下:
将点分散在屏幕周围,然后开始将它们返回到原来的位置,当所有点都回到初始位置时,通过停止 AnimationFrame 来完成动画。但问题是 cancelAnimationFrame(animationId) 在所有点都回到原位之前被调用。

工作示例:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 10, 20);
var renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// var controls = new OrbitControls(camera, renderer.domElement);
camera.lookAt(scene.position);

var light = new THREE.DirectionalLight(0xffffff, 2);
light.position.setScalar(100);
scene.add(light);

var textGeo = null;
var textPoints = null;
var loader = new THREE.FontLoader();
loader.load('https://threejs.org/examples/fonts/droid/droid_serif_bold.typeface.json', function (response) {
  var font = response;
  setText(font);
  render();
});

function setText(font) {
  textGeo = new THREE.TextGeometry('ABC', {
    font: font,
    size: 4,
    height: 0.5,
    curveSegments: 4,
    bevelEnabled: false,
    bevelSize: 10,
    bevelThickness: 50
  });
  textGeo.computeBoundingBox();
  textGeo.computeVertexNormals();
  textGeo.center();

  fillWithPoints(textGeo, 10);

  textGeo.vertices.forEach(function (vertex) {
    vertex.startPoint = vertex.clone();
    vertex.direction = vertex.clone().normalize();
  })

  textGeo.vertices.forEach(function (vertex) {
    vertex.x = THREE.Math.randFloat(-20, 20)//.copy(vertex.startPoint).addScaledVector(vertex.direction, 5 + Math.sin(Date.now() * 0.001) * 5);
    vertex.y = THREE.Math.randFloat(-20, 20)//.copy(vertex.startPoint).addScaledVector(vertex.direction, 5 + Math.sin(Date.now() * 0.001) * 5);
    vertex.z = THREE.Math.randFloat(-10, 10)//.copy(vertex.startPoint).addScaledVector(vertex.direction, 5 + Math.sin(Date.now() * 0.001) * 5);
  });
  window.p = textGeo.vertices[0]
  //console.log(textGeo.vertices[0]);

  //textGeo.verticesNeedUpdate = true;

  //textGeo.applyMatrix( new THREE.Matrix4().makeTranslation( 1, 1, 1 ) );

  textPoints = new THREE.Points(textGeo, new THREE.PointsMaterial({
    color: 0xf00008,
    size: 0.1
  }));
  scene.add(textPoints);
}

function fillWithPoints(geometry, pointNumber) {
  geometry.computeBoundingBox();
  for (var i = 0; i < pointNumber; i++) {
    setRandomPoint(geometry);
  }
}

function setRandomPoint(geometry) {
  var point = new THREE.Vector3(
    THREE.Math.randFloat(geometry.boundingBox.min.x, geometry.boundingBox.max.x),
    THREE.Math.randFloat(geometry.boundingBox.min.y, geometry.boundingBox.max.y),
    THREE.Math.randFloat(geometry.boundingBox.min.z, geometry.boundingBox.max.z)
  );
  //console.log(point);
  if (isPointInside(point, geometry)) {
    geometry.vertices.push(point);
  } else {
    setRandomPoint(geometry);
  }
}

var a = new THREE.Vector3();
var b = new THREE.Vector3();
var c = new THREE.Vector3();
var face = new THREE.Face3();

function isPointInside(point, geometry) {
  var retVal = false;
  for (var i = 0; i < geometry.faces.length; i++) {
    face = geometry.faces[i];
    a = geometry.vertices[face.a];
    b = geometry.vertices[face.b];
    c = geometry.vertices[face.c];
    //console.log(face, a, b, c);
    if (ptInTriangle(point, a, b, c)) {
      var retVal = true;
      break;
    }
  }
  return retVal;
}

function ptInTriangle(p, p0, p1, p2) {
  // credits: http://jsfiddle.net/PerroAZUL/zdaY8/1/
  var A = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  var sign = A < 0 ? -1 : 1;
  var s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  var t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;

  return s > 0 && t > 0 && (s + t) < 2 * A * sign;
}

function setPosition(vertex, curent, orign, step) {
  let curentPoint = Math.round(curent * 10) / 10;
  let orignPoint = Math.round(orign * 10) / 10;
  if (curentPoint !== orignPoint) {
    curent = orign < curent ? curent - step : curent + step;
  } else {
    curent = orign;
    vertex.complate = true;
  }
  return curent;
}

function isComplated(vertices) {
  let result = true;
  vertices.forEach(function(vertex, i) {
    if(!vertex.complate) {
      result = false;
    }
  });
  return result;
}
function render() {
  let animationId = requestAnimationFrame(render);
  if (!isComplated(textGeo.vertices)) {
    textGeo.vertices.forEach(function (vertex) {
      vertex.x = setPosition(vertex, vertex.x, vertex.startPoint.x, 0.1);
      vertex.y = setPosition(vertex, vertex.y, vertex.startPoint.y, 0.1);
      vertex.z = setPosition(vertex, vertex.z, vertex.startPoint.z, 0.1);
    });
  } else {
    cancelAnimationFrame(animationId);
  }
  
  textGeo.verticesNeedUpdate = true;
  
  /* complated = isComplated(textGeo.vertices);
  if (complated) {
    //console.log(complated);
    cancelAnimationFrame(animationId);
    console.log('end')
  } */
  

  renderer.render(scene, camera);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>

我走对了吗?还是这些事情做的不同?

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    prisoner849
    2020-07-11T17:49:23Z2020-07-11T17:49:23Z

    点的随机位置已知,点的最终位置已知,它们之间的距离已知,速度已知。我们可以通过将所有距离中的最大值除以速度来找到动画时间,一旦当前动画时间大于或等于计算出的时间,我们就执行一些动作。

    此外,还有一种方法.clampLength()可以简化生活,并且不允许增量向量的长度超出指定的限制:

    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
    camera.position.set(2, 3, 5);
    var renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
    
    var controls = new THREE.OrbitControls(camera, renderer.domElement);
    
    var tempDist = new THREE.Vector3();
    var boxGeom = new THREE.BoxGeometry(2, 2, 2, 10, 10, 10);
    boxGeom.vertices.forEach(v => {
      v.init = v.clone();
      v.random = new THREE.Vector3(THREE.Math.randFloatSpread(10), THREE.Math.randFloatSpread(10), THREE.Math.randFloatSpread(5));
      v.dir = new THREE.Vector3().copy(v.init).sub(v.random).normalize();
      v.dist = tempDist.copy(v.init).sub(v.random).length();
      v.copy(v.random);
    });
    
    var boxMat = new THREE.PointsMaterial({
      size: 0.1,
      color: "red"
    });
    
    var box = new THREE.Points(boxGeom, boxMat);
    scene.add(box);
    
    var speed = 2; // единиц в секунду
    var longestDist = 0;
    boxGeom.vertices.forEach(v => {
      longestDist = Math.max(longestDist, v.dist);
    });
    var fullTime = longestDist / speed; // продолжительность анимации определяется по самой длинной дистанции, так как скорость постоянная
    console.log({
      fullTime
    });
    
    var clock = new THREE.Clock();
    var delta = 0;
    var globalTime = 0;
    var clampedDirLength = new THREE.Vector3();
    
    render();
    
    function render() {
      let req = requestAnimationFrame(render);
      delta = clock.getDelta();
      globalTime += delta;
    
      boxGeom.vertices.forEach(v => {
        clampedDirLength.copy(v.dir).multiplyScalar(globalTime * speed).clampLength(0, v.dist); // clamp the length!
        v.copy(v.random).add(clampedDirLength);
      });
      boxGeom.verticesNeedUpdate = true;
      if (globalTime >= fullTime) { // если текущая продолжительность больше или равна заданной, то останавливаем цикл прорисовки или делаем что-то другое
        //cancelAnimationFrame(req);
        boxMat.color.set("aqua");
      }
      renderer.render(scene, camera);
    }
    body {
      overflow: hidden;
      margin: 0;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>
    <script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

    参考

    • 3

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5