gpt4 book ai didi

javascript - 为什么 FileReader 不将文件传递给 three.js 场景使用的 loader.load()?

转载 作者:行者123 更新时间:2023-11-30 18:13:51 28 4
gpt4 key购买 nike

我正在尝试使用 FileReader 将客户端 ASCII 文件传递​​给 loader.load(),但看起来文件永远不会到达那里。如果我使用 loader.load('server path to test_file.STL') 而不是 loader.load(fileObject),文件会出现在 3D 场景中。

我包含了一个 alert() 函数来显示文件的 ASCII 文本,它告诉我 JavaScript 正在抓取和处理文件并且没有 Chrome 安全屏障,但显然我没有正确地将文件传递给 loader .load()

<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color:#fea47c;
}

div {
position:relative;
left:200px;
top:0px;
background-color: #eeeeee;
border:1px solid black;
width:550px;
height:550px;
}

canvas {
width:550px;
height:550px;
}
</style>
</head>

<body>

<script src="https://raw.github.com/mrdoob/three.js/master/build/three.min.js"></script>
<script src="https://raw.github.com/mrdoob/three.js/master/examples/js/loaders/STLLoader.js"></script>
<script src="https://raw.github.com/mrdoob/three.js/master/examples/js/controls/TrackballControls.js"></script>

<input type="file" id="pickfile"></input>

<script>

document.getElementById('pickfile').addEventListener('change', readFile, false);

function readFile (evt)
{
var fileObject = evt.target.files[0];
var reader = new FileReader();
reader.onload = function() {alert(this.result)}; // verifies ASCII file contents were grabbed
reader.readAsText(fileObject)
}

var container, camera, scene, renderer, controls;

init();
animate();

function init() {

container = document.createElement( 'div' );
document.body.appendChild( container );

var width = container.clientWidth;
var height = container.clientHeight;

camera = new THREE.PerspectiveCamera( 35, width / height, .1 , 10000);

camera.position.set( 0, 0, 600);

scene = new THREE.Scene();

controls = new THREE.TrackballControls( camera , container);
controls.addEventListener( 'change', render );

// object

var loader = new THREE.STLLoader();
loader.addEventListener( 'load', function ( event ) {

var geometry = event.content;

var material = new THREE.MeshLambertMaterial( { ambient: 0xff5533, color: 0xff5533 } );

var mesh = new THREE.Mesh( geometry, material );

scene.add( mesh );

} );

loader.load(fileObject);

// lights

scene.add( new THREE.AmbientLight( 0x222222 ) );

var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
directionalLight.position = camera.position;
scene.add( directionalLight );

// renderer

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width , height );
container.appendChild( renderer.domElement );

window.addEventListener( 'resize', onWindowResize, false );


}

function addLight( x, y, z, color, intensity ) {

var directionalLight = new THREE.DirectionalLight( color, intensity );
directionalLight.position.set( x, y, z )
scene.add( directionalLight );

}

function onWindowResize() {

camera.aspect = width / height;
camera.updateProjectionMatrix();

renderer.setSize( width, height );
}

function animate() {

requestAnimationFrame( animate );
controls.update();
render();

}

function render() {

camera.lookAt( scene.position );
renderer.render( scene, camera );

}

</script>

</body>
</html>

最佳答案

好吧,我今天早上又试了一次,我认为问题是我试图从一个糟糕的相机 Angular 或其他什么 Angular 查看加载的结果...无论如何,这是一个基于 https://raw.github.com/mrdoob/three.js/master/examples/webgl_loader_stl.html 的示例

关键部分:

就像我上面提到的,loader.load 没有任何重载会占用文件的实际内容(认为它会这样做有点傻)。它只会为文件占用一个位置...您需要一些东西来从文件内容创建您的模型。那将是 loader.parse

例如,以下处理程序将您的模型添加到 scenereadFile 范围内的场景:

function readFile(evt)
{
var fileObject = evt.target.files[0];
var reader = new FileReader();
reader.onload = function ()
{
var loader = new THREE.STLLoader();
//alert(this.result)
var geometry = loader.parse(this.result);
var material = new THREE.MeshPhongMaterial(
{
ambient: 0xff5533,
color: 0xff5533,
specular: 0x111111,
shininess: 200
});
var mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
};
reader.readAsText(fileObject)
}

整个例子:

我会把它放在网上的某个地方,但是因为它使用 github 来托管一些脚本等。这可能不是最好的主意。

<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Monospace;
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#pickfile {
color: #fff;
position: absolute;
top: 40px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
a {
color: skyblue
}
</style>
</head>

<body>
<script src="https://raw.github.com/mrdoob/three.js/master/build/three.min.js"></script>
<script src="https://raw.github.com/mrdoob/three.js/master/examples/js/loaders/STLLoader.js"></script>
<script src="https://raw.github.com/mrdoob/three.js/master/examples/js/controls/TrackballControls.js"></script>
<script src="https://raw.github.com/mrdoob/three.js/master/examples/js/Detector.js"></script>
<script src="https://raw.github.com/mrdoob/three.js/master/examples/js/libs/stats.min.js"></script>
<input type="file" id="pickfile"></input>
<script>
document.getElementById('pickfile').addEventListener('change', readFile, false);

function readFile(evt)
{
var fileObject = evt.target.files[0];
var reader = new FileReader();
reader.onload = function ()
{
var loader = new THREE.STLLoader();
//alert(this.result)
var geometry = loader.parse(this.result);
var material = new THREE.MeshPhongMaterial(
{
ambient: 0xff5533,
color: 0xff5533,
specular: 0x111111,
shininess: 200
});
var mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
};
reader.readAsText(fileObject)
}
if (!Detector.webgl) Detector.addGetWebGLMessage();
var container, stats;
var camera, scene, renderer, objects;
init();
animate();

function init()
{
container = document.createElement('div');
document.body.appendChild(container);
camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 15);
camera.position.set(3, 0.5, 3);
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xffffff, 2, 15);
scene.fog.color.setHSV(0.06, 0.2, 0.45);
// Grid
var size = 20,
step = 0.25;
var geometry = new THREE.Geometry();
var material = new THREE.LineBasicMaterial(
{
color: 0x000000
});
for (var i = -size; i <= size; i += step)
{
geometry.vertices.push(new THREE.Vector3(-size, -0.04, i));
geometry.vertices.push(new THREE.Vector3(size, -0.04, i));
geometry.vertices.push(new THREE.Vector3(i, -0.04, -size));
geometry.vertices.push(new THREE.Vector3(i, -0.04, size));
}
var line = new THREE.Line(geometry, material, THREE.LinePieces);
line.position.y = -0.46;
scene.add(line);
// Ground
var plane = new THREE.Mesh(new THREE.PlaneGeometry(40, 40), new THREE.MeshPhongMaterial(
{
ambient: 0x999999,
color: 0x999999,
specular: 0x101010
}));
plane.rotation.x = -Math.PI / 2;
plane.position.y = -0.5;
scene.add(plane);
plane.receiveShadow = true;
// Object
// Lights
scene.add(new THREE.AmbientLight(0x777777));
addShadowedLight(1, 1, 1, 0xffffff, 1.35);
addShadowedLight(0.5, 1, -1, 0xffaa00, 1);
// renderer
renderer = new THREE.WebGLRenderer(
{
antialias: true,
clearColor: 0x111111,
clearAlpha: 1,
alpha: false
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(scene.fog.color, 1);
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.physicallyBasedShading = true;
renderer.shadowMapEnabled = true;
renderer.shadowMapCullFrontFaces = false;
container.appendChild(renderer.domElement);
// stats
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild(stats.domElement);
//
window.addEventListener('resize', onWindowResize, false);
}

function addShadowedLight(x, y, z, color, intensity)
{
var directionalLight = new THREE.DirectionalLight(color, intensity);
directionalLight.position.set(x, y, z)
scene.add(directionalLight);
directionalLight.castShadow = true;
//directionalLight.shadowCameraVisible = true;
var d = 1;
directionalLight.shadowCameraLeft = -d;
directionalLight.shadowCameraRight = d;
directionalLight.shadowCameraTop = d;
directionalLight.shadowCameraBottom = -d;
directionalLight.shadowCameraNear = 1;
directionalLight.shadowCameraFar = 4;
directionalLight.shadowMapWidth = 2048;
directionalLight.shadowMapHeight = 2048;
directionalLight.shadowBias = -0.005;
directionalLight.shadowDarkness = 0.15;
}

function onWindowResize()
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
//
function animate()
{
requestAnimationFrame(animate);
render();
stats.update();
}

function render()
{
var timer = Date.now() * 0.0005;
camera.position.x = Math.cos(timer) * 5;
camera.position.z = Math.sin(timer) * 5;
camera.lookAt(scene.position);
renderer.render(scene, camera);
}
</script>
</body>
</html>

关于javascript - 为什么 FileReader 不将文件传递给 three.js 场景使用的 loader.load()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13853301/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com