Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
canvas { display: block; width: 100%; height: 100vh; position: fixed; left: 0; top: 0; background: #222; }
JavaScript
// Compute anchor xy function getClothAnchorPoint({ faceId, subdivisions }) { const col = (faceId % (subdivisions * 2) / 2 | 0) const row = faceId / (subdivisions * 2) | 0; const o = 0.5 / subdivisions; // offset half grid const s = 1 - 1 / subdivisions; // scale const x = o + (col / (subdivisions - 1)) * s; const y = o + (1 - row / (subdivisions - 1)) * s; return [x, y]; } function createMeshGround({ scene, width = 1e3, depth = 1e3, height = 1e1 }) { const mesh = BABYLON.MeshBuilder.CreateBox('', { width, depth, height }); mesh.position.y = -0.5 * height; mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0.1 }); const mat = new BABYLON.PBRMaterial('', scene); mat.roughness = 1; mat.specularIntensity = 0.2; mat.albedoColor = new BABYLON.Color3(1, 1, 1); mesh.material = mat; return mesh; } function createMeshCloth({ scene, width = 7, height = 10, subdivisions }) { const mesh = BABYLON.MeshBuilder.CreateGround('meshCloth', { width, height, subdivisions }); mesh.position.y = 2; mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.ClothImpostor, { margin: 0.1, damping: 0.1, mass: 0.1, stiffness: 1, friction: 0.1, positionIterations: 10, velocityIterations: 10 }); const mat = new BABYLON.PBRMaterial('', scene); mat.albedoTexture = new BABYLON.Texture('__stub__', scene); mat.specularIntensity = 0.8; mat.roughness = 0.8; mat.backFaceCulling = false; mat.twoSidedLighting = true; mesh.material = mat; return mesh; } function createMeshClothAnchor({ scene }) { const mesh = BABYLON.MeshBuilder.CreateBox('meshClothAnchor', { size: 0.1 }); mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }); const mat = new BABYLON.PBRMaterial('', scene); mat.albedoColor = new BABYLON.Color3(0, 1, 0); mat.roughness = 1; mesh.material = mat; return mesh; } function updateClothAnchor({ meshClothAnchor, meshCloth, x, y, pickedPoint, scene }) { removeAllClothAnchors({ meshCloth }); // ... 1 meshClothAnchor.position = pickedPoint; // ... 2 recreateClothAnchorImpostor({ scene, meshClothAnchor }); // ... 3 meshCloth.physicsImpostor.addAnchor( meshClothAnchor.physicsImpostor, x, y, 1, true ); } // Remove clothImpostor anchors function removeAllClothAnchors({ meshCloth }) { meshCloth.physicsImpostor.physicsBody.get_m_anchors().clear(); } // Re-create cloth anchor impostor function recreateClothAnchorImpostor({ scene, meshClothAnchor }) { scene.getPhysicsEngine().removeImpostor(meshClothAnchor.physicsImpostor); meshClothAnchor.physicsImpostor = new BABYLON.PhysicsImpostor(meshClothAnchor, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }); } function createScene(engine) { const scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color4(0, 0, 0, 1); const camera0 = new BABYLON.ArcRotateCamera('', BABYLON.Tools.ToRadians(-50), BABYLON.Tools.ToRadians(65), 15, null, scene ); //camera0.attachControl(elCanvas); camera0.wheelPrecision = 100; const light0 = new BABYLON.HemisphericLight('', new BABYLON.Vector3(0, 1, 0), scene ); light0.intensity = 2; const light1 = new BABYLON.SpotLight('', new BABYLON.Vector3(0, 10, 0), new BABYLON.Vector3(0, -1, 0), Math.PI / 180 * 120, 10, scene); light1.intensity = 1000; const gravity = new BABYLON.Vector3(0, -10, 0); scene.enablePhysics(gravity, new BABYLON.AmmoJSPlugin()); const meshGround = createMeshGround({ scene }); const subdivisions = 40; const meshCloth = createMeshCloth({ scene, subdivisions }); const meshClothAnchor = createMeshClothAnchor({ scene }); // shadow const sg = new BABYLON.ShadowGenerator(2048, light1); sg.addShadowCaster(meshCloth); sg.useCloseExponentialShadowMap = true; light1.shadowMinZ = 8; light1.shadowMaxZ = 11; meshGround.receiveShadows = true; meshCloth.receiveShadows = true; // postprocess const rp = new BABYLON.DefaultRenderingPipeline('', true, scene, [camera0]); rp.bloomEnabled = true; rp.bloomWeight = 0.2; rp.bloomThreshold = 0.9; rp.bloomKernel = 32; rp.bloomScale = 1; rp.samples = 4; rp.fxaaEnabled = true; return { scene, meshCloth, meshClothAnchor, subdivisions }; } function onPointerDown({ scene, meshCloth, meshClothAnchor, subdivisions }) { return (ev, info) => { if (info.hit) { scene.metadata.pickedMesh = info.pickedMesh; if (info.pickedMesh === meshCloth) { const { faceId, pickedPoint } = info; const [x, y] = getClothAnchorPoint({ faceId, subdivisions }); updateClothAnchor({ meshCloth, meshClothAnchor, x, y, pickedPoint, scene }); } } else { scene.metadata.pickedMesh = null; } }; } function onPointerUp({ scene, meshCloth }) { return (ev, info) => { scene.metadata.pickedMesh = null; removeAllClothAnchors({ meshCloth }); }; } function onPointerMove({ scene, meshCloth, meshClothAnchor }) { return (ev, info) => { if (scene.metadata.pickedMesh === meshCloth) { const { hit, pickedPoint } = scene.pick(scene.pointerX, scene.pointerY); if (hit) { meshClothAnchor.position = pickedPoint; } } } } function init() { const engine = new BABYLON.Engine(elCanvas, true); const { scene, meshCloth, meshClothAnchor, subdivisions } = createScene(engine); engine.runRenderLoop(() => { scene.render(); }); addEventListener('resize', () => { engine.resize(); }); scene.metadata = { pickedMesh: null }; scene.onPointerDown = onPointerDown({ scene, meshCloth, meshClothAnchor, subdivisions }); scene.onPointerUp = onPointerUp({ scene, meshCloth }); scene.onPointerMove = onPointerMove({ scene, meshCloth, meshClothAnchor }); } init();
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>canvas模拟布拖动-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号