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

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 :)

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名