相机转动时面向用户的文本几何

时间:2015-04-17 09:35:41

标签: javascript node.js three.js

我正在创建代表人际网络的3D图表。网络中的节点是人的名称,在它们之间绘制线条,表示连接。这个网络的目的是人们可以转过身来看看它们的网络状况,然而,当我在网络上转动相机时,我用文本几何学创建的3d文本被粘在同一个位置,因此,当他们想要从另一侧查看网络时,用户无法读取。我知道这个问题之前已被问过几次了,我已经在这里阅读了大部分答案,但我仍然无法让它工作。
我遇到的问题主要是当我尝试做这样的事情时:

nodeRenderer.lookAt(camera.position);

我遇到了未定义相机的问题。摄像机已添加到场景中,但这发生在另一个.js文档中。这是创建场景并添加相机的地方(/ngraph.three/index.js):

var THREE = require('./lib/three');

module.exports = function (graph, settings) {
  var merge = require('ngraph.merge');
  settings = merge(settings, {
    interactive: true
  });

  var beforeFrameRender;
  var isStable = false;
  var disposed = false;
  var layout = createLayout(settings);
  var renderer = createRenderer(settings);
  var camera = createCamera(settings);
  var scene = settings.scene || new THREE.Scene();

  (...)

  function renderNode(nodeId) {
     nodeRenderer(nodeUI[nodeId]);
  }

  (...)

 function initNode(node) {
     var ui = nodeUIBuilder(node);
     if (!ui) return;
     // augment it with position data:
     ui.pos = layout.getNodePosition(node.id);
     // and store for subsequent use:
     nodeUI[node.id] = ui;

     scene.add(ui);
 }

 (...)

 function createCamera(settings) {
     if (settings.camera) {
       return settings.camera;
     }
     var container = renderer.domElement;
     var camera = new THREE.PerspectiveCamera(75, container.clientWidth/container.clientHeight, 0.1, 3000);
     camera.position.z = 400;

     return camera;
 }

这是创建节点的地方(ngraph.three / lib / defaults.js):

var THREE = require('./three');

module.exports.createNodeUI = createNodeUI;
module.exports.createLinkUI = createLinkUI;
module.exports.nodeRenderer = nodeRenderer;
module.exports.linkRenderer = linkRenderer;

function createNodeUI(node) {
  var nodeMaterial = new THREE.MeshFaceMaterial( [
     new THREE.MeshPhongMaterial( { color: 0x00cccc, shading: THREE.FlatShading } ), // front
     new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
  ] );

  var nodeGeometry = new THREE.TextGeometry( node.data, { size: 5, height: 2, curveSegments: 6, font: "helvetiker", weight: "normal", style: "normal" });
  var nodeDirection = new THREE.Quaternion (THREE.Camera.Quaternion);

  return new THREE.Mesh(nodeGeometry, nodeMaterial);
}

function createLinkUI(link) {

  var linkGeometry = new THREE.Geometry();
  linkGeometry.vertices.push(new THREE.Vector3(0, 0, 0));
  linkGeometry.vertices.push(new THREE.Vector3(0, 0, 0));

  var linkMaterial = new THREE.LineBasicMaterial({ color: 0x00cccc });
  return new THREE.Line(linkGeometry, linkMaterial);
}

(...)

function nodeRenderer(node) {
    node.position.x = node.pos.x;
    node.position.y = node.pos.y;
    node.position.z = node.pos.z;
}

(...)

我在/ngraph.three/index.js中尝试了 ui.quaternion = camera.quaternion; ,但这并没有做任何事,我已经尝试了 nodeUI.lookAt(camera.position); 但是这给出了一个错误: TypeError:nodeUI.lookAt不是函数

PS:我使用的是Three.js Rev. 68,Ngraph和node.js.

1 个答案:

答案 0 :(得分:1)

我只能猜测,因为很多代码都丢失但你的createNodeUI会返回THREE.MeshObject3D.lookAt(vector)的子类,其函数TypeError: nodeUI.lookAt is not a function可以对齐它给定向量的局部z轴。

你说你收到此错误:.lookAt看起来你试图调用数组的nodeUI[node.id].lookAt(camera.position)而不是节点本身。

所以请尝试{{1}}