我正在尝试使用three.js加载位于本地文件夹中的.gltf文件,但是在控制台上指控此错误:“不允许加载本地资源”。
已经尝试使用http服务器运行; 试图创建一个dist并从那里运行http-server; 试图给予浏览器许可(使用谷歌浏览器,使用--allow-file-access-from-files选项); 我还已经在在线渲染器中渲染了.gltf文件,只是为了确保3D模型没有问题并且一切正常。
typesccript类:
export class AppComponent {
title = 'three-app';
@ViewChild('rendererContainer') rendererContainer: ElementRef;
renderer = new THREE.WebGLRenderer();
scene = null;
camera = null;
mesh = null;
constructor() {
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
this.camera.position.z = 1000;
var loader = new GLTFLoader();
loader.load( 'C:\\Users\\x\\Desktop\\3dmodels\\box.gltf', function ( gltf ) {
this.scene.add( gltf.scene );
}, undefined, function ( error ) {
console.error( error );
} );
}
}
html:
<div #rendererContainer></div>
package.json:
{
"name": "three-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"core-js": "^2.5.4",
"ecstatic": "^3.0.0",
"rxjs": "~6.3.3",
"three": "^0.107.0",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.3",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
}
}
有人知道会发生什么吗?
答案 0 :(得分:0)
您可能想尝试使用文件URL。 “ C:\ Users \ x \ Desktop \ 3dmodels \ box.gltf”不是URL,但“ file:// C:\ Users \ x \ Desktop \ 3dmodels \ box.gltf”是URL。
请注意,我没有Windows环境可以尝试此操作,因此我不确定YMMV格式是否正确。
答案 1 :(得分:0)
我使用“ Chrome的Web服务器”扩展名解决了。 我创建了一个本地服务器,将3D文件丢到那里,然后通过传递本地服务器URL设法加载它。该扩展名具有“设置CORS标头”选项,该选项允许其他来源的XMLHttpRequests。