Plane vertex manipulation experiment
How do I make an plane vertex manipulation experiment?
Playing around with input based vertex manipulation over a plane, using Three.jsTODO: add camera controls. What is a plane vertex manipulation experiment? How do you make a plane vertex manipulation experiment? This script and codes were developed by Ross B on 24 November 2022, Thursday.
Plane vertex manipulation experiment - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Plane vertex manipulation experiment</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
</head>
<body> <script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/6859/tween.min_1.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/r73/three.min.js'></script>
<script src='https://threejs.org/examples/js/controls/OrbitControls.js'></script> <script src="js/index.js"></script>
</body>
</html>
Plane vertex manipulation experiment - Script Codes JS Codes
'use strict';
var _THREE = THREE;
var Mesh = _THREE.Mesh;
var Scene = _THREE.Scene;
var Vector2 = _THREE.Vector2;
var SpotLight = _THREE.SpotLight;
var Raycaster = _THREE.Raycaster;
var WebGLRenderer = _THREE.WebGLRenderer;
var PlaneGeometry = _THREE.PlaneGeometry;
var HemisphereLight = _THREE.HemisphereLight;
var PerspectiveCamera = _THREE.PerspectiveCamera;
var MeshPhongMaterial = _THREE.MeshPhongMaterial;
var scene = new Scene();
var aspectRatio = window.innerWidth / window.innerHeight;
var camera = new PerspectiveCamera(75, aspectRatio, 0.1, 1000);
var renderer = new WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var geometry = new PlaneGeometry(2, 2, 8, 8);
var material = new MeshPhongMaterial({ color: 0xddddf4, specular: 0x009900, shininess: 30, shading: THREE.FlatShading, side: THREE.DoubleSide
});
var plane = new THREE.Mesh(geometry, material);
scene.add(plane);
var light = new HemisphereLight(0x0d0d0d, 0x59b3a1, 1);
scene.add(light);
var spotLight = new SpotLight(0x202230);
spotLight.position.set(0, 3, 5);
scene.add(spotLight);
camera.position.z = 1;
var raycaster = new Raycaster();
var mouse = new Vector2();
function onMouseMove(event) { // calculate mouse position in normalized device coordinates // (-1 to +1) for both components mouse.x = event.clientX / window.innerWidth * 2 - 1; mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; plane.rotation.y = mouse.x / 6; plane.rotation.x = -1 * mouse.y / 6;
}
window.addEventListener('mousemove', onMouseMove, false);
var vertexTransitions = [];
for (var i = 0; i < plane.geometry.vertices.length; i++) { vertexTransitions.push({ originalZ: Math.random() / 8, z: 0 });
}
// TODO: figure this out
function neareastVertexToPoint(p, vertices, indicesToTest) { scene.updateMatrixWorld(); var smallestDistance = null; var closestVertexIndex = null; var p1 = p.clone(); p1.applyMatrix3(plane.matrixWorld); indicesToTest.forEach(function (i) { var p2 = vertices[i]; var d = Math.abs(p1.distanceTo(p2)); if (!smallestDistance || d < smallestDistance) { smallestDistance = d; closestVertexIndex = i; } }); return closestVertexIndex;
}
camera.position.y -= 1.3;
camera.rotation.x += 0.8;
var render = function render() { requestAnimationFrame(render); // reset all others vertexTransitions.forEach(function (v) { v.z = v.originalZ; }); // bump nearby vertices raycaster.setFromCamera(mouse, camera); var intersects = raycaster.intersectObject(plane); if (intersects.length > 0) { var face = intersects[0].face; var a = face.a; var b = face.b; var c = face.c; vertexTransitions[a].z = 0.15; vertexTransitions[b].z = 0.15; vertexTransitions[c].z = 0.15; if (!window.once) { console.log(intersects[0].face); window.once = true; } } // Tween vertices z plane.geometry.vertices.forEach(function (v, i) { var targetZ = vertexTransitions[i].z; // Check if vertexes z is still changing if (v.z !== targetZ) { // Z needs increase if (v.z > targetZ) { v.z -= (v.z - targetZ) / 10; } else { // z needs to be reduced v.z += (targetZ - v.z) / 5; } } }); plane.geometry.verticesNeedUpdate = true; renderer.render(scene, camera);
};
render();
Developer | Ross B |
Username | rocbear |
Uploaded | November 24, 2022 |
Rating | 3.5 |
Size | 4,363 Kb |
Views | 16,192 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Spin button | 3,935 Kb |
Hovergram | 2,505 Kb |
Context menu experiment | 5,059 Kb |
Torus light track | 4,909 Kb |
Playful inner-page navigation | 4,689 Kb |
Unfolding circular menu | 7,466 Kb |
Spear checkboxes | 3,682 Kb |
Liquid gold | 6,470 Kb |
Babel React template | 5,357 Kb |
A Pen by Ross B | 3,715 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Importable Clearfix | Corysimmons | 1,411 Kb |
A Pen by Kevin | Kevinkenger | 2,642 Kb |
Pure CSS Tooltips | Mobius1 | 2,271 Kb |
Typefaces with descriptions | Megandoty | 2,944 Kb |
Parallax-ish Sliding Content | Jdsteinbach | 2,748 Kb |
Layout 11 | Altynai | 1,690 Kb |
Responsive Menu I | Rodericksandoval | 3,045 Kb |
Starfield old school style | Bolloxim | 5,214 Kb |
Drag in vanilla js using dotval math instead of translate | Paulq | 2,662 Kb |
Experiment - SCSS SVG Background Image with Variables | See8ch | 3,270 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!