【问题标题】:adding texture to a json object - three.js向 json 对象添加纹理 - three.js
【发布时间】:2018-08-20 04:53:23
【问题描述】:

我正在尝试为我从 clara.io 导出的 3D Dog JSON 模型添加纹理。我使用 Ojectloader 加载 3D 模型,然后尝试使用 Textureloader 加载纹理,但纹理似乎没有加载到模型上。

也许我的代码是错误的,或者是在错误的地方。我曾尝试查看其他示例,说明人们如何做到这一点,但运气不佳。正如我所说,问题是纹理似乎没有将图像加载到模型上。 chrome的控制台也没有错误。

如果有人可以提供任何帮助,谢谢。

 <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>three.js webgl - loaders - Clara.io JSON 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>

            <script src="three.js"></script>
            <script src="Detector.js"></script>
            <script src="stats.min.js"></script>
            <script src="UnpackDepthRGBAShader.js"></script>
            <script src="ShadowMapViewer.js"></script>
            <script src="dat.gui.min.js"></script>
            <script src="OrbitControls.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 = 4;
                    // scene
                    scene = new THREE.Scene();
                    var ambient = new THREE.AmbientLight( 0x444444 );
                    scene.add( ambient );
                    var directionalLight = new THREE.DirectionalLight( 0xffeedd );
                    directionalLight.position.set( 0, 0, 1 ).normalize();
                    scene.add( directionalLight );

                    //new attempt at a texture and object loader
                    var loader = new THREE.TextureLoader();
                    loader.load("dogtexture.jpg", function ( texture ) {
                        var material = new THREE.MeshBasicMaterial({ map: texture });
                        var objectLoader = new THREE.ObjectLoader();
                        objectLoader.load( "blue-dog1.json", function( obj, texture ) {
                            obj.scale.set( .04, .04, .04);
                            scene.add( obj,texture );
                        });
                    });


                    // BEGIN Clara.io JSON loader code
                    //var objectLoader = new THREE.ObjectLoader();
                    //objectLoader.load("blue-dog.json", function ( obj ) {
                    //obj.scale.set( .045, .045, .045);
                     //scene.add( obj );
                    //});

                    //var loader = new THREE.TextureLoader();
                    //loader.load("dogtexture.jpg", function ( texture ) {
                    // do something with the texture
                    //var material = new THREE.MeshNormalMaterial( {    map:          //texture} );
                    //} );

                    // END Clara.io JSON loader code
                    renderer = new THREE.WebGLRenderer();
                    renderer.setPixelRatio( window.devicePixelRatio );
                    renderer.setSize( window.innerWidth, window.innerHeight );
                    container.appendChild( renderer.domElement );

                }

                //
                function animate() {
                    requestAnimationFrame( animate );
                    render();
                }
                function render() {
                    camera.position.x = 400;//vertical camera angle
                    camera.position.y = 300;
                    camera.position.z = 150;

                    camera.lookAt( scene.position );
                    renderer.render( scene, camera );
                }
            </script>

        </body>
    </html>

【问题讨论】:

    标签: javascript json three.js


    【解决方案1】:

    MeshNormalMaterial 可能不是您想要的材料类型。
    试试MeshBasicMaterialMeshPhongMaterial

    MeshNormalMaterial 的描述取自here

    一种将法线向量映射到 RGB 颜色的材质。

    向下滚动three.js docs 的左侧以查看可用的其他类型的材料。

    编辑..
    您永远不会将材料添加到对象上..

    编辑..
    添加一些示例代码。我将使用 ObjectLoader,因为您似乎可以使用它。

    // Create material for later use.
    var material = new THREE.MeshBasicMaterial();
    // Create ObjectLoader.
    var objectLoader = new THREE.ObjectLoader();
    // Initiate load of model
    objectLoader.load("blue-dog1.json", function(obj) {
      // The documentation for ObjectLoader says it can't load geometries.. confusing (I have never used the ObjectLoader class)
      // But since you say it is working, we'll run with it.
      // obj will be an Object3D, so we must traverse it's children and add the material (I think).
      obj.traverse(function(child) {
        if(child instanceof THREE.Mesh) {
          child.material = material;
        }
      }
      // Now the Object is loaded and the material is applied..
      // Add it to the scene (maybe do this after the texture is loaded?).
      scene.add(obj);
      // Load the texture.
      var loader = new THREE.TextureLoader();
      loader.load("dogtexture.jpg", function(texture) {
        // Apply texture to material.
        material.map = texture;
        // Maybe this is needed.
        material.needsUpdate = true;
      }
    }
    

    此代码未经测试,可能存在一些关闭问题,但它应该能让您走上正轨。
    如果您无法使其正常工作,请尝试使用 JSONLoader 类搜索示例。

    【讨论】:

    • 您好,感谢您的回复。我正在试验的材料。但是,我尝试了这两种方法,但对模型没有任何影响。
    • 因为模型有一个默认材质,所以你在纹理加载器回调中创建的那个永远不会添加到你的模型中
    • 我只是使用了textureloader文档中的示例,我该怎么做呢。
    • 也许可以考虑使用JSONLoader 类而不是ObjectLoader
    • 嗨,我已经尝试过其他加载程序,但这是我唯一能让它工作的方法
    猜你喜欢
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2015-03-12
    • 1970-01-01
    • 2014-07-11
    • 2014-12-30
    • 2015-02-27
    相关资源
    最近更新 更多