mirror of
https://github.com/kou029w/pucchinglgl.git
synced 2025-01-19 00:27:59 +00:00
feat: パフォーマンスの改善
This commit is contained in:
parent
24b6516d39
commit
e3be64d253
1 changed files with 35 additions and 44 deletions
79
src/main.ts
79
src/main.ts
|
@ -20,10 +20,7 @@ const grid = { colum: 14, row: 4 } as const;
|
||||||
const mouse = new Vector2(NaN, NaN);
|
const mouse = new Vector2(NaN, NaN);
|
||||||
const panels = Array.from({ length: grid.colum * grid.row }, () => {
|
const panels = Array.from({ length: grid.colum * grid.row }, () => {
|
||||||
const geometry = new PlaneGeometry();
|
const geometry = new PlaneGeometry();
|
||||||
const material = new MeshBasicMaterial({
|
const material = new MeshBasicMaterial({ opacity: 0 });
|
||||||
color: new Color("white"),
|
|
||||||
transparent: true,
|
|
||||||
});
|
|
||||||
const mesh = new Mesh(geometry, material);
|
const mesh = new Mesh(geometry, material);
|
||||||
return mesh;
|
return mesh;
|
||||||
});
|
});
|
||||||
|
@ -31,48 +28,29 @@ const renderer = new WebGLRenderer();
|
||||||
const canvas = renderer.domElement;
|
const canvas = renderer.domElement;
|
||||||
const raycaster = new Raycaster();
|
const raycaster = new Raycaster();
|
||||||
|
|
||||||
|
function render() {
|
||||||
|
renderer.render(scene, camera);
|
||||||
|
}
|
||||||
|
|
||||||
function drawRect() {
|
function drawRect() {
|
||||||
initPanels(panels);
|
|
||||||
raycaster.setFromCamera(mouse, camera);
|
raycaster.setFromCamera(mouse, camera);
|
||||||
const [intersect] = raycaster.intersectObjects(panels);
|
const [intersect] = raycaster.intersectObjects(panels);
|
||||||
if (!intersect) return;
|
if (!intersect) return;
|
||||||
|
|
||||||
(intersect.object as Mesh<
|
|
||||||
PlaneGeometry,
|
|
||||||
MeshBasicMaterial
|
|
||||||
>).material.opacity = 0.25;
|
|
||||||
const c16s = ["red", "green", "blue", "cyan", "magenta", "yellow"];
|
const c16s = ["red", "green", "blue", "cyan", "magenta", "yellow"];
|
||||||
const c16 = c16s[randomInt(c16s.length - 1)];
|
const c16 = c16s[randomInt(c16s.length - 1)];
|
||||||
const color = new Color(c16);
|
const color = new Color(c16);
|
||||||
const geometry = new PlaneGeometry(1, 1);
|
const geometry = new PlaneGeometry(1, 1);
|
||||||
const material = new MeshBasicMaterial({
|
const material = new MeshBasicMaterial({ color });
|
||||||
color,
|
|
||||||
});
|
|
||||||
const mesh = new Mesh(geometry, material);
|
const mesh = new Mesh(geometry, material);
|
||||||
mesh.scale.set(0.1, 0.1, 1);
|
mesh.scale.set(0.1, 0.1, 1);
|
||||||
Object.assign(mesh.position, intersect.point);
|
Object.assign(mesh.position, intersect.point);
|
||||||
scene.add(mesh);
|
scene.add(mesh);
|
||||||
}
|
render();
|
||||||
|
|
||||||
function initPanels(panels: Mesh<PlaneGeometry, MeshBasicMaterial>[]) {
|
|
||||||
const width = camera.aspect / grid.colum;
|
|
||||||
const height = 1 / grid.row;
|
|
||||||
for (const [i, panel] of panels.entries()) {
|
|
||||||
panel.material.opacity = 0;
|
|
||||||
panel.scale.set(width, height, 1);
|
|
||||||
Object.assign(panel.position, {
|
|
||||||
x: width * ((i % grid.colum) - (grid.colum - 1) / 2),
|
|
||||||
y: height * (-Math.floor(i / grid.colum) + (grid.row - 1) / 2),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleKeydown({ key }: KeyboardEvent) {
|
function handleKeydown({ key }: KeyboardEvent) {
|
||||||
if (key === " ") {
|
if (key === " ") return setup();
|
||||||
scene.clear();
|
|
||||||
setup();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [x = randomInt(grid.colum - 1), y = randomInt(grid.row - 1)] = [
|
const [x = randomInt(grid.colum - 1), y = randomInt(grid.row - 1)] = [
|
||||||
...keyPosition(keyLayouts.default, key),
|
...keyPosition(keyLayouts.default, key),
|
||||||
|
@ -101,11 +79,36 @@ function handleMouseMoveEnd() {
|
||||||
mouse.set(NaN, NaN);
|
mouse.set(NaN, NaN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function adjustPanelSize() {
|
||||||
|
const width = camera.aspect / grid.colum;
|
||||||
|
const height = 1 / grid.row;
|
||||||
|
for (const [i, panel] of panels.entries()) {
|
||||||
|
panel.scale.set(width, height, 1);
|
||||||
|
Object.assign(panel.position, {
|
||||||
|
x: width * ((i % grid.colum) - (grid.colum - 1) / 2),
|
||||||
|
y: height * (-Math.floor(i / grid.colum) + (grid.row - 1) / 2),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function adjustCanvasSize() {
|
||||||
|
camera.aspect = canvas.clientWidth / canvas.clientHeight;
|
||||||
|
camera.updateProjectionMatrix();
|
||||||
|
adjustPanelSize();
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
function setup() {
|
function setup() {
|
||||||
scene.background = new Color();
|
scene.background = new Color();
|
||||||
initPanels(panels);
|
scene.clear();
|
||||||
scene.add(...panels);
|
scene.add(...panels);
|
||||||
camera.position.z = 1;
|
camera.position.z = 1;
|
||||||
|
adjustCanvasSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
renderer.setSize(window.innerWidth, window.innerHeight);
|
||||||
|
Object.assign(canvas.style, { width: "100vw", height: "100vh" });
|
||||||
root.addEventListener("keydown", handleKeydown);
|
root.addEventListener("keydown", handleKeydown);
|
||||||
root.addEventListener("touchmove", handleTouchmove);
|
root.addEventListener("touchmove", handleTouchmove);
|
||||||
root.addEventListener("keyup", handleMouseMoveEnd);
|
root.addEventListener("keyup", handleMouseMoveEnd);
|
||||||
|
@ -119,21 +122,9 @@ function setup() {
|
||||||
handleMouseMoveEnd();
|
handleMouseMoveEnd();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function tick() {
|
|
||||||
window.requestAnimationFrame(tick);
|
|
||||||
camera.aspect = canvas.clientWidth / canvas.clientHeight;
|
|
||||||
camera.updateProjectionMatrix();
|
|
||||||
renderer.render(scene, camera);
|
|
||||||
}
|
|
||||||
|
|
||||||
function main() {
|
|
||||||
renderer.setSize(window.innerWidth, window.innerHeight);
|
|
||||||
Object.assign(canvas.style, { width: "100vw", height: "100vh" });
|
|
||||||
root.appendChild(canvas);
|
root.appendChild(canvas);
|
||||||
|
window.addEventListener("resize", adjustCanvasSize);
|
||||||
setup();
|
setup();
|
||||||
tick();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
Loading…
Add table
Reference in a new issue