簡體   English   中英

三個沒有定義; 我該如何解決?

[英]THREE is not defined; how do I fix this?

我目前正在試驗 Three.js 但我似乎無法修復此錯誤,我已經嘗試並搜索了很多答案,但我無法修復它。

我已經包含了threejs庫,並且在我的腳本中一切都寫得很好

這是我的 html 索引文件

<body>
    <script src="script.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242637/TrackballProjectorDetector.js"></script>
    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242637/AsciiEffect.js"></script>

</body> 

這是腳本文件的一部分

function init() {
      var width = window.innerWidth || 2;
      var height = window.innerHeight || 2;
      container = document.createElement('div');
      document.body.appendChild(container);
      var info = document.createElement('div');
      info.style.position = 'absolute';
      info.style.top = '20px';
      info.style.width = '100%';
      info.style.textAlign = 'center';
      info.innerHTML = 'Drag to change the view';
      container.appendChild(info);
      camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000);
      camera.position.y = 150;
      camera.position.z = 500;
      controls = new THREE.TrackballControls(camera);
      scene = new THREE.Scene();
      var light = new THREE.PointLight(0xffffff);
      light.position.set(500, 500, 500);
      scene.add(light);
      var light = new THREE.PointLight(0xffffff, 0.25);
      light.position.set(-500, -500, -500);
      scene.add(light);
      sphere = new THREE.Mesh(new THREE.SphereGeometry(200, 20, 10), new THREE.MeshLambertMaterial());
      scene.add(sphere);

我收到一條錯誤消息:

camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000);

但是當我刪除它時,錯誤會下降到下一個THREE元素。

<script src="script.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>

我想說訂單在這里可能很重要。 如果在加載script.js (a)時運行init function,則尚未定義THREE ,因為這將在下一行發生。

在這種情況下,解決方案是在腳本文件之前加載three.js


(a)這通常在js文件的末尾完成,但在這種情況下我們不能確定,因為我們沒有整個文件。 因此,我會查看該文件以查看它是否類似於:

function init() {
      blahBlahBlah();
}

init();

多年來,我一直試圖弄清楚,顯然有數百個 three.js 文件或每次我使用 import 它幾乎可以工作,但是我需要將 three.js 加載到服務器,我必須鏈接到它依賴的路徑文件,然后它說沒有定義meshstandardmaterial,並且three.js中的示例文件夾具有額外的權限,因此您必須將依賴項移動到根文件夾並重新鏈接,然后它說您導入的不僅僅是一個three.js,我切換到three.min並拋出其他 function 我使用調試器並突出顯示變量並在控制台中評估並查看三個變量中的內容,然后將 three.js 中的一堆變量鏈接到本地 scope 之類的事情,然后更好地為頁面導入變量在頁面頂部,但它劑量' t 在本地主機上工作,所以你必須說 import * as THREE from "http://localhost/three.js' 但我有 window.THREE = THREE;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM