// constant gravity\r
let g = new THREE.Vector3(0, -9.8, 0);\r
// stiffness\r
- let k = 500;\r
+ let k = 1000;\r
\r
// Wind vector\r
let fWind = new THREE.Vector3(\r
Math.cos(vertex.z * this.time),\r
Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
);\r
- fWind.set(0, 0, 0);\r
\r
/**\r
* constant determined by the properties of the surrounding fluids (air)\r
* achievement of cloth effects through try out\r
* */\r
- let a = 0.01;\r
+ let a = 0.1;\r
\r
let velocity = new THREE.Vector3(\r
(this.previousPositions[vertexIndex].x - vertex.x) / dt,\r
// next position = 2 * current Position - previous position + acceleration * (passed time)^2\r
// acceleration (dv/dt) = F(net)\r
// Dependency for one vertex: gravity, fluids/air, springs\r
-\r
+ const DRAG = 0.97;\r
let nextPosition = new THREE.Vector3(\r
- (2 * currentPosition.x) - previousPosition.x + acceleration.x * (passedTime * passedTime),\r
- (2 * currentPosition.y) - previousPosition.y + acceleration.y * (passedTime * passedTime),\r
- (2 * currentPosition.z) - previousPosition.z + acceleration.z * (passedTime * passedTime),\r
+ (currentPosition.x - previousPosition.x) * DRAG + currentPosition.x + acceleration.x * (passedTime * passedTime),\r
+ (currentPosition.y - previousPosition.y) * DRAG + currentPosition.y + acceleration.y * (passedTime * passedTime),\r
+ (currentPosition.z - previousPosition.z) * DRAG + currentPosition.z + acceleration.z * (passedTime * passedTime),\r
);\r
\r
+ // let nextPosition = new THREE.Vector3(\r
+ // (2 * currentPosition.x) - previousPosition.x + acceleration.x * (passedTime * passedTime),\r
+ // (2 * currentPosition.y) - previousPosition.y + acceleration.y * (passedTime * passedTime),\r
+ // (2 * currentPosition.z) - previousPosition.z + acceleration.z * (passedTime * passedTime),\r
+ // );\r
+\r
return nextPosition;\r
}\r
\r
return nextPosition;\r
}\r
\r
+wind(intersects) {\r
+ let intersect = intersects[0];\r
+ this.geometry.vertices[intersect.face.a].z -= 0.05;\r
+ this.geometry.vertices[intersect.face.b].z -= 0.05;\r
+ this.geometry.vertices[intersect.face.c].z -= 0.05;\r
+}\r
+\r
}\r
\r
* evtl. an Stoff ziehen\r
*/\r
\r
-\r
-class Point {\r
- constructor(x, y) {\r
- this.x = x;\r
- this.y = y;\r
- }\r
-\r
- add(that) {\r
- return new Point(\r
- this.x + that.x,\r
- this.y + that.y\r
- );\r
- }\r
-\r
- sub(that) {\r
- return new Point(\r
- this.x - that.x,\r
- this.y - that.y\r
- );\r
- }\r
-\r
- dist(that) {\r
- let a = this.x - that.x;\r
- let b = this.y - that.y;\r
- return Math.sqrt(a * a + b * b)\r
- }\r
-}\r
-\r
-\r
/**\r
* setup THREE JS Scene, Camera and Renderer\r
*/\r
document.body.onload = init;\r
\r
function init() {\r
- let mousePos = new Point();\r
+ let mousePos = new THREE.Vector2();\r
let previousClothSimulation;\r
\r
/**\r
const light = new THREE.DirectionalLight( 0xffffff, 0.5 );\r
light.position.set( 0, 1, 0.5 );\r
scene.add( light );\r
+ \r
+ let raycaster = new THREE.Raycaster();\r
\r
/**\r
* function called every frame\r
function animate(dt) {\r
cloth.simulate(dt/1000);\r
\r
+ raycaster.setFromCamera( new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera );\r
+\r
+ const intersects = raycaster.intersectObject( mesh );\r
+\r
+ if ( intersects.length > 0 ) {\r
+ cloth.wind(intersects);\r
+ }\r
setTimeout(() => {\r
animate(frameTime);\r
}, frameTime);\r
renderer.render(scene, camera);\r
}\r
\r
+\r
/** add callback for window resize */\r
let canvas = document.getElementsByTagName("canvas")[0];\r
+ let w = window.innerWidth;\r
+ let h = window.innerHeight - canvasSpace;\r
let resize = function () {\r
- let w = window.innerWidth;\r
- let h = window.innerHeight - 200;\r
+ w = window.innerWidth;\r
+ h = window.innerHeight - canvasSpace;\r
canvas.width = w;\r
canvas.height = h;\r
}\r