缓冲几何自定义属性材料泄漏到其他对象中

时间:2015-01-08 18:15:41

标签: three.js

我正在进行可视化,并希望使用软边粒子来表示数据点。

我从各种示例中收集了代码,并且除了透明度问题之外还有其他工作。我尝试了新的Stack Overflow片段系统,并简单举例说明了我所看到的内容。

正如您所注意到的,球体从粒子系统中获取透明度。有没有办法解决这个问题 - 在我的应用程序中,这是一个必需的功能。我尝试将属性应用于我用于球体的材质,但这似乎没有帮助。

任何想法我做错了什么?



var camera, scene, renderer, controls;
var uniforms;

init();
animate();

function init() {
  renderer = new THREE.WebGLRenderer({
    antialias: true
  });
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  scene = new THREE.Scene();

  var ambient_light = new THREE.AmbientLight(0x333333);
  scene.add(ambient_light);

  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 10, 1000);
  camera.position.z = 80;

  var num_particles = 5000;
  var geometry = new THREE.BufferGeometry();
  var positions = new Float32Array(num_particles * 3);
  var colors = new Float32Array(num_particles * 3);
  var sizes = new Float32Array(num_particles);

  for (var i = 0; i < num_particles; i++) {
      positions[3 * i + 0] = Math.random() * 100 - 50;
      positions[3 * i + 1] = Math.random() * 100 - 50;
      positions[3 * i + 2] = Math.random() * 100 - 50;

      colors[3 * i + 0] = Math.random();
      colors[3 * i + 1] = Math.random();
      colors[3 * i + 2] = Math.random();

      sizes[i] = 2.5;
  }
  geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
  geometry.addAttribute('customColor', new THREE.BufferAttribute(colors, 3));
  geometry.addAttribute('size', new THREE.BufferAttribute(sizes, 1));

  var attributes = {
    size: {
      type: 'f',
      value: null
    },
    customColor: {
      type: 'c',
      value: null
    }
  };

  uniforms = {
    color: {
      type: "c",
      value: new THREE.Color(0xffffff)
    },
    texture: {
      type: "t",
      value: THREE.ImageUtils.loadTexture("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6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DRdL8F2ATCBFw8AkBwQMvhOtOEdBYd79RuMYS4Buk0uGxMDHexzirC1f9QRkNsOaAVsJ3a7Tio+ztHAHC7EJf2sftv42b/AXfX0RbECOkWWAo+gM7t42ZyDGFlxHbqTtet1r0/ozp6RpB+E/jVRwP8G3R7eXmZvRtYAAAAAElFTkSuQmCC")
    }
  };

  var shaderMaterial = new THREE.ShaderMaterial({

    uniforms: uniforms,
    attributes: attributes,
    vertexShader: document.getElementById('vertexshader').textContent,
    fragmentShader: document.getElementById('fragmentshader').textContent,

    blending: THREE.AdditiveBlending,
    depthTest: true,
    depthWrite: false,
    transparent: true
  });

  var pointcloud = new THREE.PointCloud(geometry, shaderMaterial);
  scene.add(pointcloud);

  scene.add(new THREE.Mesh(
    new THREE.SphereGeometry(50.0, 16, 16),
    new THREE.MeshNormalMaterial({ })
  ))

  controls = new THREE.TrackballControls(camera);
}

function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  controls.update();
}
&#13;
<script src="http://threejs.org/build/three.min.js"></script>
<script src="http://threejs.org/examples/js/controls/TrackballControls.js"></script>

<script type="x-shader/x-vertex" id="vertexshader">
  attribute float size;
  attribute vec3 customColor;

  varying vec3 vColor;

  void main() {
    vColor = customColor;
    vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
    gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
    gl_Position = projectionMatrix * mvPosition;
  }
</script>

<script type="x-shader/x-fragment" id="fragmentshader">
  uniform vec3 color;
  uniform sampler2D texture;

  varying vec3 vColor;

  void main() {
    gl_FragColor = vec4( color * vColor, 1.0 );
    gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
  }
</script>
&#13;
&#13;
&#13;

编辑:WestLangley在评论中提出深度测试结果的建议:(不知道如何在评论中添加图片,以便在此处添加)

depth test set to true

1 个答案:

答案 0 :(得分:0)

我缺少的点云材​​质属性是depthWrite:false,正如@WestLangley所说,depthTest:true;

我不知道如何对嵌入式代码进行版本设置,因此我对其进行了更新以使用这些值。