ThreeJS中此手柄形状所需的指导

时间:2017-11-23 14:42:41

标签: three.js

我必须在ThreeJS中创建这个句柄形状:

enter image description here

我认为它是平面几何体和每端扭曲/压扁锥体的组合,其z位置小于物体的其余部分。

我认为为此创建一个Shape更简单,而不是尝试将网格合并在一起。这是我到目前为止所做的:

(function onLoad() {
  var container, camera, scene, renderer;

  init();
  animate();

  function init() {
    container = document.getElementById('container');
    initScene();
    addGridHelper();
    addCamera();
    addRenderer();
    addOrbitControls();

    var knottedRibbonHandleTwo = createKnottedRibbonHandleTwo();
    knottedRibbonHandleTwo.position.x -= 250;
    scene.add(knottedRibbonHandleTwo);

    var sceneLight = new THREE.HemisphereLight(0xffffbb, 0x080820, 1);
    scene.add(sceneLight);
  }

  function createKnottedRibbonHandleTwo() {
    var belt = new THREE.Shape();
    belt.moveTo(0, 0);
    belt.lineTo(25, 0);
    belt.lineTo(25, 2.5);
    belt.lineTo(0, 2.5);

    var extrudeSettings = {
      steps: 1,
      amount: 1,
      bevelEnabled: false,
      bevelThickness: 1,
      bevelSize: 1,
      bevelSegments: 1
    };
    var geometry = new THREE.ExtrudeGeometry(belt, extrudeSettings);
    var material = new THREE.MeshPhongMaterial({
      color: 0xffd700,
      wireframe: false
    });
    var mesh = new THREE.Mesh(geometry, material);
    mesh.scale.multiplyScalar(20);

    return mesh;
  }

  /**** Basic Scene Setup ****/
  function initScene() {
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
  }

  function addCamera() {
    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.set(349.11334070460066, 405.44010726325604, 359.3111192889029);
    scene.add(camera);
  }

  function addGridHelper() {
    var planeGeometry = new THREE.PlaneGeometry(2000, 2000);
    planeGeometry.rotateX(-Math.PI / 2);

    var planeMaterial = new THREE.ShadowMaterial({
      opacity: 0.2
    });
    var plane = new THREE.Mesh(planeGeometry, planeMaterial);
    plane.position.y = -200;
    plane.receiveShadow = true;
    scene.add(plane);

    var helper = new THREE.GridHelper(2000, 100);
    // helper.position.y = -199;
    helper.material.opacity = 0.25;
    helper.material.transparent = true;
    scene.add(helper);

    var axis = new THREE.AxesHelper();
    scene.add(axis);
  }

  function addRenderer() {
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    container.appendChild(renderer.domElement);
  }

  function addOrbitControls() {
    var controls = new THREE.OrbitControls(camera, renderer.domElement);
  }

  function animate() {
    requestAnimationFrame(animate);
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
body {
  background: transparent;
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

#canvas {
  margin: 10px auto;
  width: 800px;
  height: 350px;
  margin-top: -44px;
}
<body>
  <div id="container"></div>
  <script src="https://threejs.org/build/three.js"></script>
  <script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
</body>

但我对如何实现最终部分感到困惑。

感谢。

1 个答案:

答案 0 :(得分:1)

创造力取决于你。您可以创建所需的any shape。我刚刚修改了你的代码片段:

&#13;
&#13;
(function onLoad() {
  var container, camera, scene, renderer;

  init();
  animate();

  function init() {
    container = document.getElementById('container');
    initScene();
    addGridHelper();
    addCamera();
    addRenderer();
    addOrbitControls();

    var knottedRibbonHandleTwo = createKnottedRibbonHandleTwo();
    knottedRibbonHandleTwo.position.x -= 250;
    scene.add(knottedRibbonHandleTwo);

    var sceneLight = new THREE.HemisphereLight(0xffffbb, 0x080820, 1);
    scene.add(sceneLight);
  }

  function createKnottedRibbonHandleTwo() {
    var belt = new THREE.Shape();
    belt.moveTo(0, 1.25);
    belt.lineTo(1.25, 0.5);
    belt.lineTo(2.5, 0);
    belt.lineTo(22.5, 0);
    belt.lineTo(23.75, 0.5);
    belt.lineTo(25, 1.25);
    belt.lineTo(23.75, 2);
    belt.lineTo(22.5, 2.5);
    belt.lineTo(2.5, 2.5);
    belt.lineTo(1.25, 2);

    var extrudeSettings = {
      steps: 1,
      amount: .25,
      bevelEnabled: false,
      bevelThickness: 1,
      bevelSize: 1,
      bevelSegments: 1
    };
    var geometry = new THREE.ExtrudeGeometry(belt, extrudeSettings);
    var material = new THREE.MeshPhongMaterial({
      color: 0xffd700,
      wireframe: false
    });
    var mesh = new THREE.Mesh(geometry, material);
    mesh.scale.setScalar(20);

    return mesh;
  }

  /**** Basic Scene Setup ****/
  function initScene() {
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
  }

  function addCamera() {
    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.set(349.11334070460066, 405.44010726325604, 359.3111192889029);
    scene.add(camera);
  }

  function addGridHelper() {
    var planeGeometry = new THREE.PlaneGeometry(2000, 2000);
    planeGeometry.rotateX(-Math.PI / 2);

    var planeMaterial = new THREE.ShadowMaterial({
      opacity: 0.2
    });
    var plane = new THREE.Mesh(planeGeometry, planeMaterial);
    plane.position.y = -200;
    plane.receiveShadow = true;
    scene.add(plane);

    var helper = new THREE.GridHelper(2000, 100);
    // helper.position.y = -199;
    helper.material.opacity = 0.25;
    helper.material.transparent = true;
    scene.add(helper);

    var axis = new THREE.AxesHelper();
    scene.add(axis);
  }

  function addRenderer() {
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    container.appendChild(renderer.domElement);
  }

  function addOrbitControls() {
    var controls = new THREE.OrbitControls(camera, renderer.domElement);
  }

  function animate() {
    requestAnimationFrame(animate);
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
&#13;
body {
  background: transparent;
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

#canvas {
  margin: 10px auto;
  width: 800px;
  height: 350px;
  margin-top: -44px;
}
&#13;
<body>
  <div id="container"></div>
  <script src="https://threejs.org/build/three.js"></script>
  <script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
</body>
&#13;
&#13;
&#13;