2016-06-06 11 views
0

Ich lese ein Buch "Lernen Three.js - Die JavaScript 3D-Bibliothek für WebGL - 2. Edition" und erstes Beispiel in Buch, das überhaupt nicht funktioniert.three.js Skript funktioniert nicht

Kann mir bitte jemand eine Richtung geben warum? Ich hatte versucht mit "How can I enable WebGL in my browser?" und es hat nicht geholfen. Aber Beispiele auf three.js/examples funktionieren wie sie sollten.

Ich habe three.js extrahiert und Beispiel bellow in Build-Ordner, wo three.js befindet.

Benötige ich einen lokalen Webserver, um WebGL zu testen?

Jetzt versuche ich in Visual Code zu debuggen und zu codieren. Irgendwelche anderen Empfehlungen?

<html> 

<head> 
    <title>Example 01.02 - First Scene</title> 
    <script type="text/javascript" src="three.js"></script> 
    <style> 
     body { 
      /* set margin to 0 and overflow to hidden, to go fullscreen */ 
      margin: 0; 
      overflow: hidden; 
     } 
    </style> 
</head> 
<body> 

<!-- Div which will hold the Output --> 
<div id="WebGL-output"> 
</div> 

<!-- Javascript code that runs our Three.js examples --> 
<script type="text/javascript"> 

    // once everything is loaded, we run our Three.js stuff. 
    function init() { 

     // create a scene, that will hold all our elements such as objects, cameras and lights. 
     var scene = new THREE.Scene(); 

     // create a camera, which defines where we're looking at. 
     var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000); 

     // create a render and set the size 
     var renderer = new THREE.WebGLRenderer(); 
     renderer.setClearColorHex(); 
     renderer.setClearColor(new THREE.Color(0xEEEEEE)); 
     renderer.setSize(window.innerWidth, window.innerHeight); 

     // show axes in the screen 
     var axes = new THREE.AxisHelper(20); 
     scene.add(axes); 

     // create the ground plane 
     var planeGeometry = new THREE.PlaneGeometry(60, 20); 
     var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc}); 
     var plane = new THREE.Mesh(planeGeometry, planeMaterial); 

     // rotate and position the plane 
     plane.rotation.x = -0.5 * Math.PI; 
     plane.position.x = 15; 
     plane.position.y = 0; 
     plane.position.z = 0; 

     // add the plane to the scene 
     scene.add(plane); 

     // create a cube 
     var cubeGeometry = new THREE.BoxGeometry(4, 4, 4); 
     var cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true}); 
     var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); 

     // position the cube 
     cube.position.x = -4; 
     cube.position.y = 3; 
     cube.position.z = 0; 

     // add the cube to the scene 
     scene.add(cube); 

     // create a sphere 
     var sphereGeometry = new THREE.SphereGeometry(4, 20, 20); 
     var sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true}); 
     var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); 

     // position the sphere 
     sphere.position.x = 20; 
     sphere.position.y = 4; 
     sphere.position.z = 2; 

     // add the sphere to the scene 
     scene.add(sphere); 

     // position and point the camera to the center of the scene 
     camera.position.x = -30; 
     camera.position.y = 40; 
     camera.position.z = 30; 
     camera.lookAt(scene.position); 

     // add the output of the renderer to the html element 
     document.getElementById("WebGL-output").appendChild(renderer.domElement); 

     // render the scene 
     renderer.render(scene, camera); 
    } 
    window.onload = init; 

</script> 
</body> 
</html> 
+0

die Beispiele auf der Website sind auf der neuesten Version basiert. Die Beispiele in diesem Buch stammen wahrscheinlich aus einer älteren Version. Wenn die Website-Beispiele funktionieren, ist Webgl in Ihrem Browser aktiviert. – gaitat

+0

Vielen Dank für die schnelle Antwort. Jetzt habe ich einen Fehler gefunden (Uncaught TypeError: renderer.setClearColorHex ist keine Funktion) in: renderer.setClearColorHex(); und ich fand auch einen Link mit dem gleichen Problem https://github.com/josdirksen/learning-threejs/issues/13. Funktionierte die Funktion setClearColorHex() überhaupt? –

+0

werden Sie viele Diskrepanzen zwischen Versionen der Bibliothek finden. Ich würde vorschlagen, die neueste Version zu verwenden und herauszufinden, was sich zwischen den Versionen geändert hat, die unter https://github.com/mrdoob/three.js/releases und https://github.com/mrdoob/three.js dokumentiert sind/wiki/Migration – gaitat

Antwort

0

die Linie

renderer.setClearColorHex()

sollten entfernt werden.

Hier neuen Code mit einem jsfiddle (three.js aus dem Netz verwendet wird):

<html> 

    <head> 
    <title>Example 01.02 - First Scene</title> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.js"></script> 
    <style> 
     body { 
     /* set margin to 0 and overflow to hidden, to go fullscreen */ 
     margin: 0; 
     overflow: hidden; 
     } 

    </style> 
    </head> 

    <body> 

    <!-- Div which will hold the Output --> 
    <div id="WebGL-output"> 
    </div> 

    <!-- Javascript code that runs our Three.js examples --> 
    <script type="text/javascript"> 
     // once everything is loaded, we run our Three.js stuff. 
     function init() { 

     // create a scene, that will hold all our elements such as objects, cameras and lights. 
     var scene = new THREE.Scene(); 

     // create a camera, which defines where we're looking at. 
     var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000); 

     // create a render and set the size 
     var renderer = new THREE.WebGLRenderer(); 
     //renderer.setClearColorHex(); 
     renderer.setClearColor(new THREE.Color(0xEEEEEE)); 
     renderer.setSize(window.innerWidth, window.innerHeight); 

     // show axes in the screen 
     var axes = new THREE.AxisHelper(20); 
     scene.add(axes); 

     // create the ground plane 
     var planeGeometry = new THREE.PlaneGeometry(60, 20); 
     var planeMaterial = new THREE.MeshBasicMaterial({ 
      color: 0xcccccc 
     }); 
     var plane = new THREE.Mesh(planeGeometry, planeMaterial); 

     // rotate and position the plane 
     plane.rotation.x = -0.5 * Math.PI; 
     plane.position.x = 15; 
     plane.position.y = 0; 
     plane.position.z = 0; 

     // add the plane to the scene 
     scene.add(plane); 

     // create a cube 
     var cubeGeometry = new THREE.BoxGeometry(4, 4, 4); 
     var cubeMaterial = new THREE.MeshBasicMaterial({ 
      color: 0xff0000, 
      wireframe: true 
     }); 
     var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); 

     // position the cube 
     cube.position.x = -4; 
     cube.position.y = 3; 
     cube.position.z = 0; 

     // add the cube to the scene 
     scene.add(cube); 

     // create a sphere 
     var sphereGeometry = new THREE.SphereGeometry(4, 20, 20); 
     var sphereMaterial = new THREE.MeshBasicMaterial({ 
      color: 0x7777ff, 
      wireframe: true 
     }); 
     var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); 

     // position the sphere 
     sphere.position.x = 20; 
     sphere.position.y = 4; 
     sphere.position.z = 2; 

     // add the sphere to the scene 
     scene.add(sphere); 

     // position and point the camera to the center of the scene 
     camera.position.x = -30; 
     camera.position.y = 40; 
     camera.position.z = 30; 
     camera.lookAt(scene.position); 

     // add the output of the renderer to the html element 
     document.getElementById("WebGL-output").appendChild(renderer.domElement); 

     // render the scene 
     renderer.render(scene, camera); 
     } 
     window.onload = init; 

    </script> 
    </body> 

</html> 

https://jsfiddle.net/90zzka64/

Verwandte Themen