使用图像作为地图创建粒子

时间:2012-06-19 10:24:58

标签: javascript three.js

我在使用图像作为纹理上的贴图创建粒子时遇到了问题。以下是我的代码:

var camera, scene, renderer, material, img, texture;

init();
animate();

function init() {
  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000);
  camera.position.z = 8000;
  scene.add(camera);

  img = new Image();
  texture = new THREE.Texture(img);

  img.onload = function() {
    texture.needsUpdate = true;
    makeParticle();
  };
  img.src = "http://www.aerotwist.com/tutorials/creating-particles-with-three-js/images/particle.png";

  renderer = new THREE.CanvasRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);
}

function makeParticle() {
  material = new THREE.ParticleBasicMaterial({
    color: 0xE60000,
    size: 20,
    map: texture,
    blending: THREE.AdditiveBlending,
    transparent: true
  });
  // make the particle
  particle = new THREE.Particle(material);
  particle.scale.x = particle.scale.y = 1;
  scene.add(particle);
}


function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
}

此代码的小提琴在这里:jsfiddle

我知道我可以使用Three Image Helper,如下所示:

map: THREE.ImageUtils.loadTexture(
  "FILE PATH"
),

但我正在实施自己的资产加载器,因此不希望单独使用它。目前我的代码显示没有错误,但没有显示粒子。

1 个答案:

答案 0 :(得分:2)

正在显示粒子,但相机距离很远。

更改此行:

camera.position.z = 8000;

对于这样的事情:

camera.position.z = 50;