2014-04-20 37 views
0
Design.prototype.get3dPointZAxis = function(event) { 
    var mouseX = event.clientX - this.container.getClientRects()[0].left; 
    var mouseY = event.clientY - this.container.getClientRects()[0].top; 
    var vector = new THREE.Vector3((mouseX/this.container.offsetWidth) * 2 - 1, mouseY /this.container.offsetHeight) * 2 + 1, 0.5); 
    this.projector.unprojectVector(vector, this.camera); 
    var dir = vector.sub(this.camera.position).normalize(); 
    var distance = - this.camera.position.z/dir.z; 
    var pos = this.camera.position.clone().add(dir.multiplyScalar(distance));  
    return pos; 

};如何將運行時繪製的二維線(例如如何在Microsoft繪製中繪製)轉換爲使用Three.js的三維矩形?

Design.prototype.stopDraw= function(event) { 
    this.twoDLine.push(this.tempLine.geometry); 
    this.tempLine = null; 
    this.lastPoint = null; 

};

Design.prototype.startDraw= function(event) { 
    this.lastPoint = this.get3dPointZAxis(event);  

};

Design.prototype.handleMouseMove = function(event) { 
    if (this.lastPoint) { 
    var pos = this.get3dPointZAxis(event); 
    if (!this.tempLine) { 
     var material = new THREE.LineBasicMaterial({ 
      color: 0x0089ff 
     }); 
     var geometry = new THREE.Geometry(); 
     geometry.vertices.push(this.lastPoint); 
     geometry.vertices.push(pos);    
     this.tempLine = new THREE.Line(geometry, material); 
     this.scene.add(this.tempLine); 
    } 
    else { 
     this.tempLine.geometry.verticesNeedUpdate = true; 
     this.tempLine.geometry.vertices[1] = pos; 
    } 
} 

};

我正在使用鼠標事件在容器內繪製2D線條和形狀。我需要將這些線條和形狀轉換爲3D。我正在嘗試以下技術......但沒有結果。請請幫忙!

Design.prototype.convertTo3D = function() { 
console.log("convertTo3D"); 
this.tempLine.geometry.vertices.z = 10; 

};

回答

0
Design.prototype.convertTo3D = function() { 
for (var i in this.linesToDraw) { 
    var line = this.linesToDraw[i]; 
    this.scene.remove(line); 
    var x1 = line.geometry.vertices[0].x; 
    var y1 = line.geometry.vertices[0].y; 
    var x2 = line.geometry.vertices[1].x; 
    var y2 = line.geometry.vertices[1].y; 
    var length = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2)); 
    var slope = (y2 - y1)/(x2 - x1); 
    var xMid = (x2 + x1)/2; 
    var yMid = (y2 + y1)/2; 
    slope = Math.atan(slope); 
    var material = new THREE.MeshPhongMaterial({ambient: 0x030303,shading: THREE.FlatShading}); 
    var geometry = new THREE.CubeGeometry(length, 1, 6); 
    var cube = new THREE.Mesh(geometry, material); 
    cube.addEventListener("click", function (event) { 
     console.log("CUBE CLICKED"); 
    }, false); 
    cube.rotateZ(slope); 
    cube.position = new THREE.Vector3(xMid, yMid, 3); 
    cube.castShadow = true; 
    cube.receiveShadow = true; 
    this.scene.add(cube); 
} 
+0

我已經將2D行存儲在數組行TARarw []中。在事件觸發器上,創建的線將消失,並將被一個長方體替換爲與2D線相同的方向和相同的位置 – user3359133