gpt4 book ai didi

three.js - 将粒子推离glsl和三个js中的mouseposition

转载 作者:行者123 更新时间:2023-12-01 23:32:08 29 4
gpt4 key购买 nike

我的 THREE.Points 对象有以下设置:

        this.particleGeometry = new THREE.BufferGeometry()
this.particleMaterial = new THREE.ShaderMaterial(
{
vertexShader: vshader,
fragmentShader: fshader,
blending: THREE.AdditiveBlending,
depthWrite: false,
uniforms: {
uTime: new THREE.Uniform(0),
uMousePosition: this.mousePosition
}
}
)

然后是一些代码,用于在球体上的 BufferGeometry 中放置点。一切正常。

Particlesphere

我还设置了一个 Raycaster 来跟踪与隐藏平面相交的鼠标位置,然后相应地更新统一的 uMousePosition。这也很好,我将鼠标位置发送到我的顶点着色器。

现在我试图让距离 d 与鼠标有一定距离的粒子从它推开,当然最接近的粒子被推开,并且还向它们施加重力时间后恢复一切的原位。

所以这是我的顶点着色器中的内容:

void main() {
float lerp(float a, float b, float amount) {
return a + (b - a) * amount;
}

void main() {
vec3 p = position;

float dist = min(distance(p, mousePosition), 1.);

float lerpFactor = .2;

p.x = lerp(p.x, position.x * dist, lerpFactor);
p.y = lerp(p.y, position.y * dist, lerpFactor);
p.z = lerp(p.z, position.z * dist, lerpFactor);//Mouse is always in z=0

vec4 mvPosition = modelViewMatrix * vec4(p, 1.);
gl_PointSize = 30. * (1. / -mvPosition.z );
gl_Position = projectionMatrix * mvPosition;
}
}

下面是当鼠标在球体外面时的样子(添加了一个随鼠标位置移动的小球体以指示鼠标位置)

enter image description here

当鼠标在里面时:

enter image description here

外面看起来已经有点正确了,但是鼠标在里面只会让粒子更靠近它们原来的位置,它应该把它们推到更远的外面。我想我必须以某种方式确定距离的方向。

另外, lerp 方法不进行 lerp,粒子直接跳到它们的位置。

所以我想知道我如何获得与鼠标的正确距离以始终在特定区域移动粒子以及如何为 lerp/重力效果设置动画。

最佳答案

这就是你如何做的第一个近似值:

body{
overflow: hidden;
margin: 0;
}
<script type="module">
import * as THREE from "https://cdn.skypack.dev/three@0.136.0";
import {OrbitControls} from "https://cdn.skypack.dev/three@0.136.0/examples/jsm/controls/OrbitControls.js";
import * as BufferGeometryUtils from "https://cdn.skypack.dev/three@0.136.0/examples/jsm/utils/BufferGeometryUtils.js";

let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 1, 100);
camera.position.set(0, 0, 10);
let renderer = new THREE.WebGLRenderer();
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);

let controls = new OrbitControls(camera, renderer.domElement);

let marker = new THREE.Mesh(new THREE.SphereGeometry(0.5, 16, 8), new THREE.MeshBasicMaterial({color: "red", wireframe: true}));
scene.add(marker);

let g = new THREE.IcosahedronGeometry(4, 20);
g = BufferGeometryUtils.mergeVertices(g);
let uniforms = {
mousePos: {value: new THREE.Vector3()}
}
let m = new THREE.PointsMaterial({
size: 0.1,
onBeforeCompile: shader => {
shader.uniforms.mousePos = uniforms.mousePos;
shader.vertexShader = `
uniform vec3 mousePos;
${shader.vertexShader}
`.replace(
`#include <begin_vertex>`,
`#include <begin_vertex>

vec3 seg = position - mousePos;
vec3 dir = normalize(seg);
float dist = length(seg);
if (dist < 2.){
float force = clamp(1. / (dist * dist), 0., 1.);
transformed += dir * force;
}

`
);
console.log(shader.vertexShader);
}
});
let p = new THREE.Points(g, m);
scene.add(p);

let clock = new THREE.Clock();

renderer.setAnimationLoop( _ => {
let t = clock.getElapsedTime();
marker.position.x = Math.sin(t * 0.5) * 5;
marker.position.y = Math.cos(t * 0.3) * 5;
uniforms.mousePos.value.copy(marker.position);
renderer.render(scene, camera);
})
</script>

关于three.js - 将粒子推离glsl和三个js中的mouseposition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66136122/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com