1

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
  • 1
    Each time your php loop is running, it is setting the $texid javascript variable, overriding the last value set by the loop, so there is only 1 $texid variable which is the last value. Personally, I probably would not even add them to the array, but instead put the needed values in a data attribute on the li dom element instead
    – 2pha
    Nov 9, 2015 at 22:53

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.