2016-06-06 67 views
0

我正在讀一本書「學習Three.js - WebGL的JavaScript 3D庫 - 第二版」和書中的第一個例子,它根本不工作。three.js腳本將不起作用

有人可以請給我一個方向爲什麼?我試過"How can I enable WebGL in my browser?",它沒有幫助。但是three.js/examples上的示例正在運行。

我已經提取了three.js並將示例波紋管放置在three.js所在的生成文件夾中。

我需要本地Web服務器來測試WebGL嗎?

現在我嘗試在可視代碼中調試和編碼。還有其他建議嗎?

<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

網站上的例子是基於最新版本。本書中的例子可能來自舊版本。但是如果網站的例子工作,webgl在您的瀏覽器中啓用。 – gaitat

+0

感謝您的快速回復。現在我發現一個錯誤(Uncaught TypeError:renderer.setClearColorHex不是函數): renderer.setClearColorHex(); 我也發現了一個鏈接,並有相同的問題https://github.com/josdirksen/learning-threejs/issues/13。函數setClearColorHex()有沒有工作? –

+0

你會發現庫的版本之間存在許多差異。我會建議使用最新版本,並試圖找出在https://github.com/mrdoob/three.js/releases和https://github.com/mrdoob/three.js中記錄的版本之間的變化/ wiki/Migration – gaitat

回答

0

renderer.setClearColorHex()

應被刪除。

這裏是一個新的jsfiddle碼(利用從網上three.js所):

<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/