2016-06-09 215 views
0

我想呈現一組對象與three.js使用一個單一的函數,該對象被渲染爲參數(render2,第35行),但它不工作。只要有一個爲控制該多維數據集而構建的函數,我就可以使單個多維數據集旋轉,但是當我嘗試使用render2函數時出現錯誤。由cubeGenerator函數製作的立方體似乎與進入groupRenderer函數的內容相匹配,該函數也與最初進入render2函數的內容相匹配。在此之後,渲染和render1功能打印立方體和預期cube1信息,但render2功能打印出一個數字和一個錯誤:Three.js組渲染

main.js:20 T…E.Mesh {uuid: "6902A0C3-7CFA-4B5C-A7BC-11259CE69113", name: "", type: "Mesh", parent: T…E.Scene, children: Array[0]…} 
main.js:28 T…E.Mesh {uuid: "275129EB-BD99-4156-B208-CED6F49F6112", name: "", type: "Mesh", parent: T…E.Scene, children: Array[0]…} 
main.js:36 408.2339999877149 
main.js:38 Uncaught TypeError: Cannot read property 'x' of undefined 

我經歷了去「介紹與three.js所到WebGL的」教程在http://threejs.org/,它工作得很好,直到我試着玩它。立方體和立方體1正在工作,但立方體組顯示在屏幕上並不旋轉。我感謝任何幫助。下面是代碼我現在所擁有的:

Line Code 
1 var scene = new THREE.Scene(); 
2 var aspect = window.innerWidth/window.innerHeight; 
3 var camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000); 
4 var renderer = new THREE.WebGLRenderer(); 
5 renderer.setSize(window.innerWidth, window.innerHeight); 
6 document.body.appendChild(renderer.domElement); 
7 camera.position.z = 10; 
8 
9 var geometry = new THREE.BoxGeometry(1, 1, 1); 
10 var material = new THREE.MeshNormalMaterial(); 
11 var cube = new THREE.Mesh(geometry, material); 
12 cube.position.x = -1; 
13 
14 var geometry1 = new THREE.BoxGeometry(1, 1, 1); 
15 var material1 = new THREE.MeshNormalMaterial(); 
16 var cube1 = new THREE.Mesh(geometry1, material1); 
17 cube1.position.x = 1; 
18 
19 var render = function() { 
20  console.log(cube); 
21  requestAnimationFrame(render); 
22  cube.rotation.x += 0.05; 
23  cube.rotation.y += 0.05; 
24  renderer.render(scene, camera); 
25 }; 
26 
27 var render1 = function() { 
28  console.log(cube1); 
29  requestAnimationFrame(render1); 
30  cube1.rotation.x += 0.05; 
31  cube1.rotation.y += 0.05; 
32  renderer.render(scene, camera); 
33 } 
34 
35 var render2 = function(object) { 
36  console.log(object); 
37  requestAnimationFrame(render2); 
38  object.rotation.x += 0.05; 
39  object.rotation.y += 0.05; 
40  renderer.render(scene, camera); 
41 } 
42 
43 var cubeGenerator = function(newGroup) { 
44  console.log("--------------------\nGenerating Cubes"); 
45  for (var i = -2; i < 2; i++) { 
46  var newGeometry = new THREE.BoxGeometry(1, 1, 1); 
47  var newMaterial = new THREE.MeshNormalMaterial(); 
48  var newCube = new THREE.Mesh(newGeometry, newMaterial); 
49  newCube.position.x = i; 
50  newGroup.add(newCube); 
51  console.log(newCube); 
52  } 
53  console.log("Done Generating Cubes\n--------------------"); 
54 }; 
55 
56 var groupRenderer = function(renderGroup) { 
57  console.log("--------------------\nRendering Group"); 
58  for (object of group.children) { 
59  console.log(object); 
60  render2(object); 
61  } 
62  console.log("Done Rendering Group\n--------------------"); 
63 }; 
64 
65 var group = new THREE.Group(); 
66 cubeGenerator(group); 
67 
68 scene.add(cube); 
69 scene.add(cube1); 
70 scene.add(group); 
71 render(); 
72 render1(); 
73 groupRenderer(group); 

我嘗試這樣修改,但它也沒有工作:

Line Code 
43 var cubeGenerator = function(newGroup) { 
44  console.log("--------------------\nGenerating Cubes"); 
45  for (var i = -2; i < 2; i++) { 
46  // var newGeometry = new THREE.BoxGeometry(1, 1, 1); 
47  // var newMaterial = new THREE.MeshNormalMaterial(); 
48  // var newCube = new THREE.Mesh(newGeometry, newMaterial); 
49  // newCube.position.x = i; 
50  // newGroup.add(newCube); 
51  // console.log(newCube); 
52  var clone = cube.clone(); 
53  clone.position.x = i; 
54  newGroup.add(clone); 
55  console.log(clone); 
56  } 
57  console.log("Done Generating Cubes\n--------------------"); 
58 }; 

注: 本教程並不總是在threejs露面.org網站,但它應該是刷新頁面或導航離開並返回到它。您可能需要嘗試多次。

+0

我不確定你想要做什麼,但是......你首先調用render()並在那個調用requestAnimationFrame(render)裏面調用,所以這個函數將被連續調用。然後你在'render1'和'render2'中做同樣的事情。 AFAIK這意味着所有3個函數將被調用每一幀。 – 2pha

+0

如果你不想要這個,請去掉'requestAnimationFrame' – 2pha

回答

0

這實現了我想要的東西:

Line Code 1 var scene = new THREE.Scene(); 2 var aspect = window.innerWidth/window.innerHeight; 3 var camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000); 4 var renderer = new THREE.WebGLRenderer(); 5 renderer.setSize(window.innerWidth, window.innerHeight); 6 document.body.appendChild(renderer.domElement); 7 camera.position.z = 10; 8 9 var geometry = new THREE.BoxGeometry(1, 1, 1); 10 var material = new THREE.MeshNormalMaterial(); 11 var cube = new THREE.Mesh(geometry, material); 12 cube.position.x = -1; 13 14 var geometry1 = new THREE.BoxGeometry(1, 1, 1); 15 var material1 = new THREE.MeshNormalMaterial(); 16 var cube1 = new THREE.Mesh(geometry1, material1); 17 cube1.position.x = 1; 18 19 var render = function() { 20 requestAnimationFrame(render); 21 cube.rotation.x += 0.05; 22 cube.rotation.y += 0.05; 23 cube1.rotation.x += 0.05; 24 cube1.rotation.y += 0.05; 25 for (object of group.children) { 26 object.rotation.x += 0.05; 27 object.rotation.y += 0.05; 28 } 29 renderer.render(scene, camera); 30 }; 31 32 var cubeGenerator = function(newGroup) { 33 console.log("--------------------\nGenerating Cubes"); 34 for (var i = -2; i < 3; i++) { 35 var newGeometry = new THREE.BoxGeometry(1, 1, 1); 36 var newMaterial = new THREE.MeshNormalMaterial(); 37 var newCube = new THREE.Mesh(newGeometry, newMaterial); 38 newCube.position.x = i; 39 newGroup.add(newCube); 40 console.log(newCube); 41 } 42 console.log("Done Generating Cubes\n--------------------"); 43 }; 44 45 var group = new THREE.Group(); 46 cubeGenerator(group); 47 48 scene.add(cube); 49 scene.add(cube1); 40 scene.add(group); 51 render();

我結合這三個渲染功能於一體的是使每個對象。 @dcromley我認爲你的解決方案會導致我的一組對象旋轉,就像它們是一個分段對象一樣,但我想要的是一組對象以相同的方式旋轉,但彼此分開(即五個立方體,每個都圍繞自己的中心旋轉,而不是圍繞中間立方體中心旋轉的五個立方體)。不過,也許我錯了。

0

我想你只想渲染一次。如果你想旋轉一個對象,你的例程將是:

rotate1(object1);

如果您想讓一組對象一起旋轉,請將對象添加到場景中(並將場景添加到基本場景中)並旋轉場景。你的例程將是:

rotate2(scene1);

編輯:腳本添加

"use strict"; 
var renderer, scene, camera, light, geometry, material, mesh; 
var scene2, mesh1, mesh2, mesh3, mesh4; 
window.onload = function() { 
    renderer = new THREE.WebGLRenderer(); 
    renderer.setSize(750, 750); 
    renderer.setClearColor(0x102030, 1); 
    document.body.appendChild(renderer.domElement); 
    scene = new THREE.Scene(); // the base scene 
    camera = new THREE.PerspectiveCamera(30, 1); 
    camera.position.set(0, 0, 30); 
    camera.lookAt(new THREE.Vector3(0,0,0)); 
    light = new THREE.AmbientLight(0x444444); 
    scene.add(light); 
    light = new THREE.DirectionalLight(0xff8888, 1); 
    light.position.set(10, 10, 20); 
    scene.add(light); 
    light = new THREE.DirectionalLight(0x88ff88, 1); 
    light.position.set(-10, 10, 20); 
    scene.add(light); 

    mesh1 = makebox(-3, -3, 0); // a couple of individual objects 
    scene.add(mesh1); 
    mesh2 = makebox(-3, 3, 0); 
    scene.add(mesh2); 

    scene2 = new THREE.Scene(); // a sub-scene, the boxes on the right 
    mesh3 = makebox(0, -3, 0); 
    scene2.add(mesh3); 
    mesh4 = makebox(0, 3, 0); 
    scene2.add(mesh4); 
    scene2.position.set(3, 0, 0); 
    scene.add(scene2); 

    fnloop(); 
} 
function fnloop() { 
    mesh1.rotateY(.01); // individual object 
    scene2.rotateY(-.01); // scene of 2 objects 
    renderer.render(scene, camera); 
    requestAnimationFrame(fnloop); 
} 
function makebox(x, y, z) { 
    geometry = new THREE.BoxGeometry(1, 2, 3); 
    material = new THREE.MeshPhongMaterial({ color:0xffffff, wireframe:false }); 
    mesh = new THREE.Mesh(geometry, material); 
    mesh.position.set(x, y, z); 
    return mesh; 
} 
+0

@ 2pha - 我敢打賭你15點他會喜歡這個模型(有一個子場景)。 – dcromley

+0

你可能是對的。我仍然不確定OP真正需要什麼 – 2pha