【问题标题】:Issue in loading the .obj using three.js使用 three.js 加载 .obj 的问题
【发布时间】:2017-11-25 02:15:58
【问题描述】:

我正在尝试使用 three.js 加载 .obj 文件, 但不幸的是,错误提示“加载资源失败:服务器响应状态为 404(未找到)”

下面是我正在使用的示例链接

查看源代码:http://mrdoob.github.io/three.js/examples/webgl_loader_obj.html

代码如下

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>three.js webgl - loaders - OBJ loader</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                font-family: Monospace;
                background-color: #000;
                color: #fff;
                margin: 0px;
                overflow: hidden;
            }
            #info {
                color: #fff;
                position: absolute;
                top: 10px;
                width: 100%;
                text-align: center;
                z-index: 100;
                display:block;
            }
            #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
        </style>
    </head>

<body>
    <div id="info">
    <a href="http://threejs.org" target="_blank">three.js</a> - OBJLoader test
    </div>

    <script src="../build/three.min.js"></script>
    <script src="js/loaders/OBJLoader.js"></script>

    <script src="js/Detector.js"></script>
    <script src="js/libs/stats.min.js"></script>

    <script>

        var container, stats;

        var camera, scene, renderer;

        var mouseX = 0, mouseY = 0;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;


        init();
        animate();


        function init() {

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

            camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
            camera.position.z = 100;

            // scene

            scene = new THREE.Scene();

            var ambient = new THREE.AmbientLight( 0x101030 );
            scene.add( ambient );

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

            // texture

            var manager = new THREE.LoadingManager();
            manager.onProgress = function ( item, loaded, total ) {

                console.log( item, loaded, total );

            };

            var texture = new THREE.Texture();

            var loader = new THREE.ImageLoader( manager );
            loader.load( 'textures/UV_Grid_Sm.jpg', function ( image ) {

                texture.image = image;
                texture.needsUpdate = true;

            } );

            // model

            var loader = new THREE.OBJLoader( manager );
            loader.load( 'obj/male02/male02.obj', function ( object ) {

                object.traverse( function ( child ) {

                    if ( child instanceof THREE.Mesh ) {

                        child.material.map = texture;

                    }

                } );

                object.position.y = - 80;
                scene.add( object );

            } );

            //

            renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            container.appendChild( renderer.domElement );

            document.addEventListener( 'mousemove', onDocumentMouseMove, false );

            //

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

        }

        function onWindowResize() {

            windowHalfX = window.innerWidth / 2;
            windowHalfY = window.innerHeight / 2;

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );

        }

        function onDocumentMouseMove( event ) {

            mouseX = ( event.clientX - windowHalfX ) / 2;
            mouseY = ( event.clientY - windowHalfY ) / 2;

        }

        //

        function animate() {

            requestAnimationFrame( animate );
            render();

        }

        function render() {

            camera.position.x += ( mouseX - camera.position.x ) * .05;
            camera.position.y += ( - mouseY - camera.position.y ) * .05;

            camera.lookAt( scene.position );

            renderer.render( scene, camera );

        }

    </script>

</body>

如果有人有上面的例子,请告诉我你是怎么做的?

谢谢, 普拉提克

【问题讨论】:

  • 您说您正在尝试加载 .obj 文件....那么为什么要链接到 threejs 示例?该示例不适合您吗?它对我来说很好。 404错误只是意味着文件的路径错误。
  • 是的,示例不是为我加载对象....您能否将示例的工作副本发送给我。
  • 你想在本地机器上运行它吗?
  • 是的,我正在本地机器上运行...
  • 嗯,很简单。该错误是因为在您的本地计算机上找不到文件。路径应以“mrdoob.github.io/three.js/examples/”开头,例如“mrdoob.github.io/three.js/examples/obj/male02/male02.obj”

标签: c# javascript asp.net object three.js


【解决方案1】:

在这种情况下,我的问题是在 Windows Server 上允许 Mime Type。

您可以手动或在代码中完成:

手动方式:

  1. 进入 IIS 设置
  2. 打开 Mime 类型设置
  3. 点击添加
  4. Mime 类型扩展:.obj
  5. Mime 类型:application/octet-stream
  6. 点击添加
  7. 重新启动您的站点或应用程序池

代码 - 将以下内容放入 Web.config:

<system.webServer>
    <staticContent>
        <mimeMap fileExtension=".obj" mimeType="application/octet-stream" /> 
    </staticContent>    
</system.webServer>

【讨论】:

  • 我正在研究 Asp.Net 和 Three.js。我试过这个解决方案,效果很好
【解决方案2】:

404 错误表示无法在您定义的位置找到该文件。如果您在 localhost 中运行,那么在与您的 demo.html 文件相同的文件夹中,您应该有:

  • 名为 obj 的目录包含名为male02 的目录,其中包含名为male02.obj 的文件
  • 一个名为纹理的目录,其中包含一个名为 UV_Grid_Sm.jpg 的文件

错误表示你目前没有。

如果您是一位经验丰富的开发人员,我想您现在可以解决此问题。如果您是开发新手并且难以理解当前链接指向的位置,不妨花点时间重新了解绝对链接和相对链接:http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm

【讨论】:

  • 如何在loader.load('obj/male02/male02.obj', function(object)中给出物理路径?
  • 我尝试穿上 obj/male02/male02.obj 但路径还是一样的错误
【解决方案3】:

我遇到了这个问题并没有成功更改 Mime 设置。似乎解决该问题的方法是直接从我的网络主机面板重新创建一个新文件,并将 .obj 和 .mtl 代码复制到其中并使用适当的后缀重新命名。

【讨论】:

    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 2015-11-01
    • 2021-02-17
    • 1970-01-01
    • 2018-04-30
    • 2011-11-15
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多