THREE.JS:所有轴上的粒子排斥力[XYZ]

时间:2019-05-30 00:05:19

标签: three.js mouse interaction particles

var blob = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3wgaExY5fZXYlgAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAASFUlEQVR42u1bS48bR5KOR2ZV8dHsh97WeD2Y82IxEI996YMAw/9jftb8j8ECPVgBgz74QP+AgS0YMNAtW61+kyxWZkbsIbPIYrHYLcmaxQK7BFJksyiy4osvIiPjAfD/j//bD/ySX/aXv/wFW9+LD6zmQxvP9y3461//qv+rAGgJXi96YDVBaAoo96z6OnwpMPALCl4Lxem1Sa9N6zWn1WZBLWQAAN94br6ur0sTtN8DAn4hjVNDMNtYWWvZw/HYQrHDvZ5h8IBgADx4BQ/inPNlWfrJZOIAwAFA1XiuXzdBCb8XCPwdwjfpbJLAeVoFABRjgN7O0VGR93qFZVswY27IZMhoAMEg4NIEFDSogBcJVQihEucWpXPl5eW8nExO5gBQprVIqwYptEzkk8wCP0N4bFDdNDTdS2tweHg4GI6GwyIrBsbagWHuE1GPiQogyhAxIwRGQEp3KqoaVNWpqhORUkTKEPxcJEzdwk1nZTm9PD6eTgCmADADgHkComowogbio0HAzxCeGlSvBe8DwPDo8Gg03B+OcpuPTGZGhs0OMw+JaYCEPSIuECAnjAyAyAAEAFHQAAJeQSsRXajKXCTMQpBp8OHOeXfjnLspp+XNf16e38JkcgsrMGog1sziY0DAzxDeJMHzJPjOeHw4evFitJ/n/b3M2n02Zt8Ys8tEO0g8JMI+EfUQMUdECwAGERkBUREAVFUVBEB9YkGlqqWqzIPoVEK4CyFce++vnXNXVbW4nE5nV8fHx1cAcAsAd4kRVcMsPgoE/AThuSF8DwAGALD7+vXr/cFg56Ao8kfWmsfGmANm3mPmXSIaEtEAEQs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tJ4ley97hi99iFc+ODfe+/fOefOqqo6nc9nZ+fXH347+cdJl93LtmbJTgDuMYUahPD1118HIoonK8SAAEEV/CoLo6KgITnvsGp+jMK2/pZ0kAki6lOkWYnIIm1xdyGEW+9Tl6j3773z76qqOquq8nQ+L88+fPjw/uQfJxct4f026t8LQAcI7ZGW8NNPP4WXL196IvCq6ADUQUxAxCXqFMCpqgMAL7LMMHmNmg0i6uISJyIuneIWjZPcNIRwE728XIQQzr1zv4bgzypXnS0W5dl8Xr47PT09//777y86+oVlG/U/p1m6ORhRpGbpncPDw939/f2DoigOsix7ZIzZZ+Z9Zt5l4h2i1CofS1Sx0oRoGp1dAOt9wjGTszzP60yC3AXxNyHItfPuwrtwUVbzD9Pb6UXqGL/u6Bj/qNmBz2mX58Z0SN01Pnr97etRvxjs5Vm2Z43dY2t2mXnERENC7CNRj5ByJMgA0CAgA9aHsUZzU90uL1pKnBm4C0HuvA/Xwftr593VbDG7ujn77epkMrlJgk9bwxMfPTjxOQMT7db5BhDjnW+/fbxT5MWOzezIGDNk5iETD4gpltkJcwSyiMtOjWU6btkhrroIIQ5MSJBp8O6u8v7WufLm8u7m9uTNST0jMGuM0bTniOCLDUzcMy/UHJIq0uoDQP/o9dFgWAz7bO3AMPUMmx4SFYSUEYFFRKOAGCvfdYs8+NQZvvAhlEFkHhZutnCL2fn53XQyOZk1xmXK1mzAZ80N/Z6hKdwCRNYcnIIxFEc7R3me57m1NqfU6EwUu8viEVQEBEQk+BDABecqF9zicj5fTE5OmsNSZWuCbKMh+lMnxz57brBlEtiouzeHqWpAmqN0BgDMeDymoiioLMtYkZqAAEzqhIy7Z7X7fz9rWuxfMTiJHazgjoFJbk2PbhZmNld7aHKtxPU/Pjj5kUBsG6HFlvDQAYJC98jsF58h/lcNT0PH4ei+wek2CLBl6uOLD0//NxKXqwa3BaHgAAAAAElFTkSuQmCC";

var parentDiv = "logo";
var renderer, scene, camera, controls, origins, helper;
var mouse = new THREE.Vector2(0.0, 0.0);

var OBJECT_SCALE = 128.0;
var OBJECT_SUBDIVISION = 2;
var PARTICLE_SIZE = 16.0;

var particles = new THREE.Geometry();

var loader = new THREE.OBJLoader();

loader.load(

	"https://raw.githubusercontent.com/OpenGLInsights/OpenGLInsightsCode/master/Chapter%2026%20Indexing%20Multiple%20Vertex%20Arrays/article/suzanne.obj",
    
	function ( object_ ) {

        var vertices = [];
        
        //subdivider
        var geometry = new THREE.Geometry().fromBufferGeometry( object_.children[0].geometry );
        var modifier = new THREE.SubdivisionModifier(OBJECT_SUBDIVISION);
        geometry = modifier.modify(geometry);
        
        //scale
        geometry.scale(OBJECT_SCALE, OBJECT_SCALE, OBJECT_SCALE);
        
        inits(geometry.vertices);

	}
);

function inits(vertices_){
    
    renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize( window.innerWidth, window.innerHeight );
    var div = document.getElementById(parentDiv);
    div.appendChild( renderer.domElement );

    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );

    camera.position.y = -450;
    camera.position.z = 450;

    var light = new THREE.AmbientLight( 0xFFFFFF, 1 );
    scene.add(light);

    controls = new THREE.OrbitControls( camera );
    controls.update();

    //helper
    var geometry = new THREE.SphereGeometry( 5, 32, 32 );
    var material = new THREE.MeshBasicMaterial( {color: 0xffff00} );
    helper = new THREE.Mesh( geometry, material );
    scene.add( helper );
    
    var vertices = vertices_;
    var positions = new Float32Array( vertices.length * 3 );
    var colors = new Float32Array( vertices.length * 3 );
    var sizes = new Float32Array( vertices.length );
    var vertex;
    var color = new THREE.Color();
    for ( var i = 0, l = vertices.length; i < l; i ++ ) {
        
        vertex = vertices[ i ];
        vertex.toArray( positions, i * 3 );
        color.setHSL( 0.01 + 0.1 * ( i / l ), 1.0, 0.5 );
        color.toArray( colors, i * 3 );
        sizes[ i ] = PARTICLE_SIZE * 0.5;
        
    }
    
    var geometry = new THREE.BufferGeometry();
    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 material = new THREE.ShaderMaterial( {
        
    uniforms: {
        
        color: { value: new THREE.Color( 0xFFFFFF ) },
        texture: { value: new THREE.TextureLoader().load( blob ) }
        
    },
        
    vertexShader: document.getElementById( "vertexshader" ).textContent,
    fragmentShader: document.getElementById( "fragmentshader" ).textContent,
    alphaTest: 0.9
        
    } );
 
    particles = new THREE.Points( geometry, material );
    particles.geometry.dynamic = true;
    scene.add(particles);

    window.addEventListener("resize", resizeWindow, false)
    renderer.domElement.addEventListener("mousemove", onMouseMove, false);
    
    origins = particles.geometry.attributes.position.array.slice(0);
    
    animate();

}

function animate() {

    controls.update();
    
	window.requestAnimationFrame( animate );
	renderer.render( scene, camera );
    
}

function onMouseMove(event) {
            
    var threshold = 32.0;

    camera.clearViewOffset();

    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

    //mouse screen to world
      vector = new THREE.Vector3(),
      vector.set(mouse.x, mouse.y, 0.5);
      vector.unproject(camera);
  
    var direction = vector.sub(camera.position).normalize(),
        distance = - camera.position.z / direction.z,
        scaled = direction.multiplyScalar(distance),
        mouseXYZ = camera.position.clone().add(scaled);

    for(var i = 0, l = particles.geometry.attributes.position.count; i < l; i++){
        
        var x = particles.geometry.attributes.position.array[i * 3];
        var y = particles.geometry.attributes.position.array[i * 3 + 1];
        var z = particles.geometry.attributes.position.array[i * 3 + 2];
        
        x += ((Math.cos(angleTo(mouseXYZ, new THREE.Vector3(x, y, z))) * (Math.pow(threshold, 2.0) / mouseXYZ.distanceTo(new THREE.Vector3(x, y, 0.0)))) + (origins[i * 3] - x)) * 0.1;
        
        //y += ((Math.sin(angleTo(mouseXYZ, new THREE.Vector3(x, y, z))) * (Math.pow(threshold, 2.0) / mouseXYZ.distanceTo(new THREE.Vector3(x, y, 0.0)))) + (origins[i * 3 + 1] - y)) * 0.1;
        
        z += ((Math.sin(angleTo(mouseXYZ, new THREE.Vector3(x, y, z))) * (Math.pow(threshold, 2.0) / mouseXYZ.distanceTo(new THREE.Vector3(x, y, 0.0)))) + (origins[i * 3 + 2] - z)) * 0.1;
        
        particles.geometry.attributes.position.array[i * 3] = x;
        //particles.geometry.attributes.position.array[i * 3 + 1] = y;
        particles.geometry.attributes.position.array[i * 3 + 2] = z;
        
    }
    
    helper.position.set(mouseXYZ.x, mouseXYZ.y, 0.0);
    
    particles.geometry.attributes.position.needsUpdate = true;

}

function angleTo(a_, b_){ return Math.atan2(b_.y - a_.y, b_.x - a_.x); }
function distance(a_, b_){ return a_.distanceTo(b_); }

function resizeWindow(){
    
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize( window.innerWidth, window.innerHeight );
    
}

function screenToWorld(_screenPos)
{
    var worldPos = _screenPos.clone();
    worldPos.x = worldPos.x / window.innerWidth/2 - 1;
    worldPos.y = - worldPos.y / window.innerWidth/2 + 1;
    worldPos.unproject( camera );
    return worldPos;                    
}
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/104/three.min.js"></script>
<script src="https://unpkg.com/three@0.104.0/examples/js/controls/OrbitControls.js"></script>
<script src="https://unpkg.com/three@0.104.0/examples/js/loaders/OBJLoader.js"></script>
<script src="https://unpkg.com/three@0.104.0/examples/js/modifiers/SubdivisionModifier.js"></script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html lang="en"> 
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<title>BlockPros Logo 3D Demo β</title> 
    
<style> body { margin: 0px; } </style>
    
</head> 
    
<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 / -mvPosition.z );
        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 );
        if ( gl_FragColor.a < ALPHATEST ) discard;
        
    }
    
</script>
    
<body> 

<div id="logo"></div>

    
</body> 
</html>

有一个通过OBJLoader加载的模型,并呈现为点[THREE.Points]。

我正在尝试达到与这个简单的2D示例相似的视觉美感 https://youtu.be/vIxX23ozQ7c

到现在,我已经有了 https://youtu.be/ZTxi3iyiVSY

从顶部查看模型后,它正常工作,但从其他方面来看,该问题就很麻烦。

非常确定我需要调整此代码:

x += ((Math.cos(angleTo(mouseXYZ, new THREE.Vector3(x, y, z))) * (Math.pow(threshold, 2.0) / mouseXYZ.distanceTo(new THREE.Vector3(x, y, 0.0)))) + (origins[i * 3] - x)) * 0.1;

y += ((Math.sin(angleTo(mouseXYZ, new THREE.Vector3(x, y, z))) * (Math.pow(threshold, 2.0) / mouseXYZ.distanceTo(new THREE.Vector3(x, y, 0.0)))) + (origins[i * 3 + 1] - y)) * 0.1;

//z += ((Math.sin(angleTo(mouseXYZ, new THREE.Vector3(x, y, z))) * (Math.pow(threshold, 2.0) / mouseXYZ.distanceTo(new THREE.Vector3(x, y, 0.0)))) + (origins[i * 3 + 2] - z)) * 0.1;

并尝试使用极坐标中的phi和theta角,但尚未找到合适的解决方案。

有什么建议吗?

0 个答案:

没有答案