- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在理解我尝试使用 Three.js 和 yomboprime 的 GPUComputationRenderer 实现的逻辑时遇到了问题。
( https://github.com/yomboprime/GPGPU-threejs-demos/blob/gh-pages/js/GPUComputationRenderer.js )
我想做一个简单的 Verlet-Cloth-Simulation。这是我已经能够实现的逻辑(简短版):
1) Position-Fragment-Shader:此着色器采用旧位置纹理和当前位置纹理并计算新位置,如下所示:
vec3 position = texture2D( texturePosition, uv ).xyz;
vec3 oldPosition = texture2D( textureOldPosition, uv ).xyz;
position = (position * 2.0 - oldPosition + acceleration * delta *delta )
t = checkConstraints(position);
position += t;
gl_FragColor = vec4(position,1);
2) Old-Position-Shader 这个着色器只是保存当前位置,并为下一步保存。
vec3 position = texture2D( texturePosition, uv ).xyz;
gl_FragColor = vec4(position,1);
这工作正常,但使用该模式,不可能在一个步骤中多次计算约束,因为每个顶点都是单独观察的,并且看不到其他像素在第一次迭代中所做的位置变化。
我想做的是将约束与 verlet 分开。目前它看起来像这样:
1)位置着色器(texturePosition)
vec3 position = texture2D( textureConstraints, uv ).xyz;
vec3 oldPosition = texture2D( textureOldPosition, uv ).xyz;
position = (position * 2.0 - oldPosition + acceleration * delta *delta );
gl_FragColor = vec4(position, 1 );
2)约束着色器(textureConstraints)
vec3 position = texture2D( texturePosition, uv ).xyz;
t = checkConstraints(position);
position += t;
gl_FragColor = vec4(position,1);
3) 旧位置着色器 (textureOldPosition)
vec3 position = texture2D( textureConstraints, uv ).xyz;
gl_FragColor = vec4(position,1);
这个逻辑不起作用,即使我根本不计算约束,只是像以前一样传递值。一旦在位置着色器中添加了一些加速度,位置值就会爆炸到无处。
我做错了什么?
最佳答案
这个例子不是verlet cloth,但我认为基本前提可能对你有帮助。我有一个 fiddle它使用 GPUComputationRender
在点云上完成一些 Spring 物理。我认为您可以根据自己的需要对其进行调整。
您需要的是更多信息。你需要固定引用布料的原始形状(就像它是一 block 平板)以及当前施加在任何这些点上的力(通过重力+风+结构完整性或其他任何方式),然后给你该点的当前位置。那些对其原始形状的点引用与力相结合,将为您的布料留下内存,而不是像过去那样四散开来。
例如,这是我的 Spring 物理着色器,GPUComputationRenderer
使用它来计算可视化中的点位置。在这种情况下,tOffsets
是使云永久内存其原始形状的坐标 - 它们永远不会改变。它是我在程序开始时添加到制服中的DataTexture。 mass
、springConstant
、gravity
和 damping
等各种力也保持一致并存在于着色器中。 tPositions
是变化的 vec4 坐标(其中两个坐标记录当前位置,另外两个记录当前速度):
<script id="position_fragment_shader" type="x-shader/x-fragment">
// This shader handles only the math to move the various points. Adding the sprites and point opacity comes in the following shader.
uniform sampler2D tOffsets;
uniform float uTime;
varying vec2 vUv;
float hash(float n) { return fract(sin(n) * 1e4); }
float noise(float x) {
float i = floor(x);
float f = fract(x);
float u = f * f * (3.0 - 2.0 * f);
return mix(hash(i), hash(i + 1.0), u);
}
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
float damping = 0.98;
vec4 nowPos = texture2D( tPositions, uv ).xyzw;
vec4 offsets = texture2D( tOffsets, uv ).xyzw;
vec2 velocity = vec2(nowPos.z, nowPos.w);
float anchorHeight = 100.0;
float yAnchor = anchorHeight;
vec2 anchor = vec2( -(uTime * 50.0) + offsets.x, yAnchor + (noise(uTime) * 30.0) );
// Newton's law: F = M * A
float mass = 24.0;
vec2 acceleration = vec2(0.0, 0.0);
// 1. apply gravity's force:
vec2 gravity = vec2(0.0, 2.0);
gravity /= mass;
acceleration += gravity;
// 2. apply the spring force
float restLength = yAnchor - offsets.y;
float springConstant = 0.2;
// Vector pointing from anchor to point position
vec2 springForce = vec2(nowPos.x - anchor.x, nowPos.y - anchor.y);
// length of the vector
float distance = length( springForce );
// stretch is the difference between the current distance and restLength
float stretch = distance - restLength;
// Calculate springForce according to Hooke's Law
springForce = normalize(springForce);
springForce *= (springConstant * stretch);
springForce /= mass;
acceleration += springForce;
velocity += acceleration;
velocity *= damping;
vec2 newPosition = vec2(nowPos.x - velocity.x, nowPos.y - velocity.y);
// Write new position out
gl_FragColor = vec4(newPosition.x, newPosition.y, velocity.x, velocity.y);
}
</script>
关于javascript - GPU 上的 Three.js Verlet 布料模拟 : Can't follow my logic for finding bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623830/
我正试图在使用 webvr-boilerplate 的项目中使镜像效果正常工作当 VREffect 处于事件状态时。 我尝试(使用 r72dev 和 r72)使用 THREE.Mirror:
我正在尝试为 localClipping 配置 THREE.plane 的位置,但我发现它有点不直观。我有一个想法,是否可以创建一些planeGeometry,操纵它,并将其位置和方向转换为在其位置创
我有 8 个从 Object3D 到不同方向的光线转换器,用于碰撞检测。我想根据对象旋转旋转他们指向的方向。我遵循了 here 中的解决方案 光线转换器开始旋转,但方式很奇怪。它开始检测来自不同方向的
我在微信上用three.js开发3D游戏,但是有个问题,three.js怎么设置渐变的背景色。我在文档上看到了背景色,但是没有渐变的背景色。请说明我是中国人英语不好。 最佳答案 对于高级效果,请尝试使
我试图通过使用Three.js挤压形状来给SVG路径(lineto和moveto)3D感觉,但是该过程会导致一些我无法删除的瑕疵。 什么会导致渲染的3D形状出现奇异的伪像?有没有办法删除它们? 这些伪
我试图在THREE.js中学习有关性能几何的更多信息,并且已经了解索引BufferGeometry和InstancedBufferGeometry是两种性能最高的几何类型。 到目前为止,我的理解是,在
three.js带有许多有用的控件,这些控件会导致相机响应键盘和鼠标输入而移动。 它们都位于https://github.com/mrdoob/three.js/blob/master/example
几个小时以来,我尝试了数十种不同的方法,但都没有效果,如下所示: document.body.addEventListener("keydown", function() { THREEx.Ful
几个小时以来,我尝试了数十种不同的方法,但都没有效果,如下所示: document.body.addEventListener("keydown", function() { THREEx.Ful
当我创建一个 JSON 模型并使用 THREE.JSONLoader.load 加载它时,它的加载没有任何问题,但是当我尝试从中创建一个变量并将数据包含在主脚本中时,THREE.JSONLoader.
我正在尝试添加一个系统粒子,一个三点到场景,但我有这个错误: “THREE.Object3D.add:对象不是 THREE.Object3D 的实例。未定义” 代码: var backCount =
请参阅以下 fiddle : https://jsfiddle.net/1jmws2bp/ 如果将鼠标移动到线条或圆圈上,它应该将颜色更改为白色(对我来说,本地有效,在 jsfiddle 中有时会有一
我正在努力处理我正在处理的涉及重复图像流的可视化。我让它与一个带有 ParticleSystem 的 Sprite 一起工作,但我只能将一种 Material 应用于系统。因为我想在纹理之间进行选择,
我想移动和旋转摄像头,但要保持PointLight相对于摄像头的位置相同。我读过一堆线程说,您可以将灯光对象添加到摄影机而不是场景。像这样: pointLight = new THREE.PointL
我是 three.js 的新手。我创建了多个网格并使用 JSONLoader 进行了分组。现在我的问题是将动画应用于该组或多个网格。是否可以将动画应用于 JSONLoader 中的该组。 提前致谢。
我想了解如何three.js在内部工作。我不明白的一件事是如何three.js编译并运行着色器程序。如果我要重新实现 three.js我自己我会有一个顶点着色器归因于 Geometry以及归因于 Ma
我以前设法显示了框,但在这里,我已经剥离了所有内容,以便通过定位 collada 模型来试验扩展框,但框不会显示。 function loadObjects(){ cobj = new THREE
我是3D编程的新手,我确实开始使用Three.JS从WebGL探索3D世界。 我想在更改camera.position.z和对象的“Z”位置时预先确定对象的大小。 例如: 我有一个大小为 100x10
在 BufferGeometry 中,有没有一种方法可以访问面索引和法线而不转换为 Geometry? 手头的几何体是由 Threejs 编辑器创建的 SphereBufferGeometry。 我只
我遵循了这个 stackoverflow 示例: ThreeJS geometry flipping 我成功镜像了我的几何体。然而现在我的几何图形是黑色的。我可以在翻转几何体的同时翻转法线来纠正这个问
我是一名优秀的程序员,十分优秀!