2013-08-02 29 views
1

我正在嘗試使用頂點來製作自定義幾何的四分之一,只有長表面區域不是側面。我試着製作一個四邊形列表,但這個代碼不起作用。 http://jsfiddle.net/R7pQB/32/Threejs使用頂點的自定義3D幾何

for(i = 90; i > 0; i -= 2){ 
    geometry.vertices.push(new THREE.Vector3(0, quad1_y[i], quad1_z[i])); 
    geometry.vertices.push(new THREE.Vector3(L, quad1_y[i], quad1_z[i])); 
    geometry.vertices.push(new THREE.Vector3(L, quad1_y[i - 1], quad1_z[i - 1])); 
    geometry.vertices.push(new THREE.Vector3(0, quad1_y[i - 1],quad1_z[i - 1])); 
    geometry.faces.push(new THREE.Face4(0,1,2,3)); 
} 
+0

它是一個沒有大寫字母的圓柱體嗎? –

+0

是的,我只是想畫一個沒有帽子的圓筒。其實,我可能想要畫出一半的圓柱......因爲,我想在兩種不同的紋理之間滾動 – SleepyEngineer

回答

0

所有幾何體的面具有相同的頂點索引?

geometry.faces.push(new THREE.Face4(0,1,2,3)); 
+0

我想最好問一下,如何在THREEjs中製作一半或四分之一的圓柱體 – SleepyEngineer

+0

您的代碼錯誤因爲在幾何體的每個面上,索引都是geometry.vertices中的索引。所以如果你使用這個代碼,所有的面都有相同的頂點索引。 – newvalue

+0

我不確定如何使用當前的threejs文檔來完成索引,我只是從亂七八糟的例子中獲得了大部分內容。有三個js嗎? – SleepyEngineer

0

正如@newvalue所指出的那樣,您並不是遞增臉部指數。 您正在將頂點推向網格。接下來你需要將他們連接到一張臉上。 我發現使用第二個計數器會更容易,第二個計數器會爲Face4實例增加4。此外,而不是兩個迴路,可以電腦,並在同一循環添加頂點/面孔:

function initGeometry(R , L) { 
    var geometry = new THREE.Geometry(); 

    var deg; 
    var f = 0; 
    for(deg = 0; deg < 90; deg++,f += 4){ 
     var ca = deg * 0.01745329252;//cache current angle 
     var na = (deg+1) * 0.01745329252;//cache next angle (could do with a conditional and storing previous angle) 
     var ccos = Math.cos(ca);//current cos 
     var csin = Math.sin(ca);//current sin 
     var ncos = Math.cos(na);//next cos 
     var nsin = Math.sin(na);//next sin 

     var tl = new THREE.Vector3(R * ccos, L * .5, R * csin);//top left = current angle, positive y 
     var bl = new THREE.Vector3(R * ccos,-L * .5, R * csin);//bottom left = current angle, negative y 
     var tr = new THREE.Vector3(R * ncos, L * .5, R * nsin);//top right = next angle, positive y 
     var br = new THREE.Vector3(R * ncos,-L * .5, R * nsin);//bottom right = next angle, negative y 

     /* 
     tl--tr 
     | /| 
     |/| 
     |/ | 
     bl--br 
     */ 
     geometry.vertices.push(tl,tr,br,bl);//notice vertices are added in (cw) order 
     geometry.faces.push(new THREE.Face4(f,f+1,f+2,f+3));//add the correct face indices, easy with a second counter 

    } 

一些代碼的擴展,以便更容易理解頂點之間的連接,其在三維空間中的位置,以及它們在添加面時引用它們的頂點數組中的位置。

根據您的小提琴,這裏的完整清單:

var renderer; 
var camera; 
var scene; 
var object; 

initConfig(); 
initGeometry(50, 300); 
startRender(); 

function initConfig() { 
    renderer = new THREE.WebGLRenderer(); 
    renderer.setClearColor(0x000000, 1); 
    renderer.setSize(window.innerWidth,window.innerHeight); 
    document.body.appendChild(renderer.domElement); 
    scene = new THREE.Scene(); 
    camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 1, 100); 
    camera.position.set(0, 0, 100); 
    camera.lookAt(scene.position); 
    scene.add(camera); 
} 

function initGeometry(R , L) { 
    var geometry = new THREE.Geometry(); 

    var deg; 
    var f = 0; 
    for(deg = 0; deg < 90; deg++,f += 4){ 
     var ca = deg * 0.01745329252;//cache current angle 
     var na = (deg+1) * 0.01745329252;//cache next angle (could do with a conditional and storing previous angle) 
     var ccos = Math.cos(ca);//current cos 
     var csin = Math.sin(ca);//current sin 
     var ncos = Math.cos(na);//next cos 
     var nsin = Math.sin(na);//next sin 

     var tl = new THREE.Vector3(R * ccos, L * .5, R * csin);//top left = current angle, positive y 
     var bl = new THREE.Vector3(R * ccos,-L * .5, R * csin);//bottom left = current angle, negative y 
     var tr = new THREE.Vector3(R * ncos, L * .5, R * nsin);//top right = next angle, positive y 
     var br = new THREE.Vector3(R * ncos,-L * .5, R * nsin);//bottom right = next angle, negative y 

     /* 
     tl--tr 
     | /| 
     |/| 
     |/ | 
     bl--br 
     */ 
     geometry.vertices.push(tl,tr,br,bl);//notice vertices are added in (cw) order 
     geometry.faces.push(new THREE.Face4(f,f+1,f+2,f+3));//add the correct face indices, easy with a second counter 

    } 

    object = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({color:0x660033,   side:THREE.DoubleSide})); 
    object.position.set(0,0,0); 
    object.rotation.x = Math.PI * .35;//a bit of rotation to make the structure visible 
    object.scale.set(.35,.35,.35);//again, to make things easier to see 
    scene.add(object); 
} 

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

而且你的網是相當密集的(90面半圓柱體)。 您可能想嘗試使用較少的線段繪製網格,這將使您有機會使用頂點/面的索引進行練習。

+0

快速問題,爲什麼你將長度「L」乘以0.5 ..? – SleepyEngineer

+0

@SleepyEngineer我已經將L除以2:大小的一半,因爲當頂點偏移該大小時,樞軸將位於該軸的中心。這樣你的網格將表現得像3D中的大多數網格:) –

+0

aaah我明白了!我試圖用imgur的紋理包裹我的圓筒;儘管如此,它從來沒有出現過。我做了類似於將圓柱體偏移到中心的東西。 http://jsfiddle.net/C5hfe/ – SleepyEngineer