doujing1967 2015-11-09 11:10
浏览 278
已采纳

Three.js:在运行时更改纹理

I'm creating an UI in which the user will be able to change the texture of the selected object by clicking on the desired textures picture.

The problem is that I can only use the last texture added in the array.

Here is my php which lists the textures in my specified folder:

<ul id="textureH">
    <script type="text/javascript">
      texArray = [];
    </script>
    <?php
    for($index=0; $index < $indexCount; $index++) {
        $extension = substr($dirArray[$index], -3);

        if ($extension == 'jpg'){
            $texName = $dirArray[$index];
            $texId = "texture". $index;
            ?>

        <script type="text/javascript">
            var texName = '<?php echo $texName ?>';
            var texId = '<?php echo $texId ?>';

            texArray.push(texId);
        </script>
            <?php
            echo "<li id='".$texId."'><table><tr><td><img class='texture-image-list' src='img/" . $texName . "' alt='Image' /></td><td><span id='texture-item-name'>" . $texName . "</span></td></tr></table></li>";
        }   
    }
    ?>
</ul>

And here's my function:

var uTexture = document.getElementById(texId);
uTexture.addEventListener("click", updateTexture, false);
function updateTexture(){
  var texMap = "./img/" + texName;

  for (var i in texArray) {
    if ((texArray[i] == texId) && (SELECTED instanceof THREE.Mesh)) {
      SELECTED.material.map = THREE.ImageUtils.loadTexture(texMap);
      SELECTED.material.needsUpdate = true;
    }
  }
}

I think the problem comes from the array.

  • 写回答

1条回答

  • dongle3217 2015-11-10 08:51
    关注

    Thanks to 2pha I could achieve what I wanted to do.

    Here's my new code: (The php/html)

    <div class="right-panel-textures">
    <h3 id="cat-hierarchy">Textures</h3>
    <?php
        $myDirectory = opendir('img/textures');
    
        while($entryName = readdir($myDirectory)) {
            $dirArray[] = $entryName;
        }
        sort($dirArray);
        closedir($myDirectory);
        $indexCount = count($dirArray);
    ?>
    
    <ul id="textureH">              
        <?php
        for($index=0; $index < $indexCount; $index++) {
            $extension = substr($dirArray[$index], -3);
            $texName = $dirArray[$index];
            $texId = "texture". $index;
            if ($extension == 'jpg'){
                ?>
    
            <script type="text/javascript">
                var texName = '<?php echo $texName ?>';
                var texId = '<?php echo $texId ?>';
            </script>
    
            <?php
                echo "<li class='texture-single-item' data-texture-name='".$texName."' data-texture-id='".$texId."' id='texture-single-item'><table><tr><td><img class='texture-image-list' src='img/textures/" . $texName . "' alt='Image' /></td><td><span id='texture-item-name'>" . $texName . "</span></td></tr></table></li>";
            }   
        }
        ?>
    </ul>
    </div>
    

    (And the JavaScript)

    var uTexture = document.getElementById("texture-single-item");
    uTexture.addEventListener("click", updateTexture, false);
    function updateTexture(){
        $(".texture-single-item").bind("click", function(event) {
    
          var nameT = $(this).attr("data-texture-name");
            if (SELECTED instanceof THREE.Mesh) {
              var texMap = "./img/textures/" + nameT;
    
              SELECTED.material.map = THREE.ImageUtils.loadTexture(texMap);
              SELECTED.material.needsUpdate = true;
            }
        });
    }
    

    Thank you :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable