LoginSignup
12
15

More than 5 years have passed since last update.

three.jsでテクスチャ画像をロードする

Last updated at Posted at 2016-04-25

three.jsでテクスチャ画像をロードするサンプルコードです。

REVISION:76時点では、THREE.ImageUtils.loadTexture(url)よりTextureLoaderを使うことが推奨されているので、こちらを使ったサンプルです。

const texLoader = new THREE.TextureLoader();
texLoader.crossOrigin = '*';
texLoader.load('http://server/path/to/image.jpg', 
  texture => { // onLoad
    const geometry = new THREE.BoxGeometry(1, 1, 1);
    const material = new MeshBasicMaterial({ map: texture });
    const cube = new THREE.Mesh(geometry, material);
    scene.add(cube);
    ...
  },
  xhr => { // onProgress
    ...
  },
  xhr => { // onError
    ...
  }
);

SecurityError: DOM Exception 18のようなクロスドメイン系のエラーが出る場合は、texLoader.crossOrigin = '*';を忘れずに付けることで解消できます。

12
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
15