Write three.js code that adds 2 lines on button click - a vertical line and a horizontal line, like crosshairs, in yellow - that extend across the width and height of the scene.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple example of three.js code that listens for a button click event and adds a vertical and horizontal line in yellow to the scene. Make sure to have Three.js and its dependencies installed before running this code. You can add this to a js file and include it in your HTML:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js Crosshairs</title>
<style>
#canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<button id="addCrosshairs">Add Crosshairs</button>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script>
// Initialize Three.js
const scene = new THREE.Scene();
const canvas = document.getElementById('canvas');
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(canvas.width, canvas.height);
document.body.appendChild(renderer.domElement);
// Set up a button click event listener
const button = document.getElementById('addCrosshairs');
button.addEventListener('click', addCrosshairs);
// Function to add crosshairs
function addCrosshairs() {
// Create a new yellow material
const yellowMaterial = new THREE.MeshBasicMaterial({ color: 0xFFFF00 });
// Create vertical and horizontal lines
const lineGeometry = new THREE.Geometry();
const line = new THREE.Line(lineGeometry, yellowMaterial);
// Create vertical line
const vertices = [
{ x: 0, y: 0, z: 0 },
{ x: canvas.width, y: 0, z: 0 }
];
lineGeometry.vertices.push.apply(lineGeometry.vertices, vertices);
line.position.set(0, 0, 0); // Set line position to be at the center
// Add the vertical line to the scene
scene.add(line);
// Create horizontal line
vertices = [
{ x: 0, y: 0, z: 0 },
{ x: 0, y: canvas.height, z: 0 }
];
lineGeometry.vertices.push.apply(lineGeometry.vertices, vertices);
line.position.set(0, canvas.height / 2, 0); // Set line position to be at the middle
// Add the horizontal line to the scene
scene.add(line);
// Update renderer
renderer.render(scene, camera);
}
// Set up camera and controls
const camera = new THREE.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
// Add a ground plane
const ground = new THREE.PlaneGeometry(100, 100, 1, 1);
const groundMaterial = new THREE.MeshBasicMaterial({ color: 0x000020 });
const groundMesh = new THREE.Mesh(ground, groundMaterial);
groundMesh.position.set(0, -0.5, 0);
scene.add(groundMesh);
// Update renderer on window resize
window.addEventListener('resize', () => {
renderer.setSize(canvas.width, canvas.height);
camera.aspect = canvas.width / canvas.height;
camera.updateProjectionMatrix();
controls.update();
});
// Render the scene
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
This code sets up a Three.js scene with a canvas, a camera, and a renderer. When you click the "Add Crosshairs" button, it adds a yellow line that extends across the width and height of the scene. The lines are positioned at the center and middle of the scene, respectively. The animate function is also included to continuously render the scene.
User
It says THREE.Geometry is not a constructor
Assistant
Apologies for the confusion. In Three.js, you should use THREE.LineGeometry instead of THREE.Geometry to create a line geometry. Here's the corrected code:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js Crosshairs</title>
<style>
#canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<button id="addCrosshairs">Add Crosshairs</button>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script>
// Initialize Three.js
const scene = new THREE.Scene();
const canvas = document.getElementById('canvas');
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(canvas.width, canvas.height);
document.body.appendChild(renderer.domElement);
// Set up a button click event listener
const button = document.getElementById('addCrosshairs');
button.addEventListener('click', addCrosshairs);
// Function to add crosshairs
function addCrosshairs() {
// Create a new yellow material
const yellowMaterial = new THREE.MeshBasicMaterial({ color: 0xFFFF00 });
// Create vertical and horizontal lines
const lineGeometry = new THREE.LineGeometry(); // Use LineGeometry instead of Geometry
const line1 = new THREE.Line(lineGeometry, yellowMaterial);
const line2 = new THREE.Line(lineGeometry, yellowMaterial); // Create two lines for both horizontal and vertical
// Set up positions for the lines
const vertices1 = [
{ x: 0, y: 0, z: 0 },
{ x: canvas.width, y: 0, z: 0 }
];
line1.geometry.vertices.push.apply(line1.geometry.vertices, vertices1);
line1.position.set(0, 0, 0); // Set line1 position to be at the center
const vertices2 = [
{ x: 0, y: 0, z: 0 },
{ x: 0, y: canvas.height, z: 0 }
];
line2.geometry.vertices.push.apply(line2.geometry.vertices, vertices2);
line2.position.set(0, canvas.height / 2, 0); // Set line2 position to be at the middle
// Add the lines to the scene
scene.add(line1);
scene.add(line2);
// Update renderer
renderer.render(scene, camera);
}
// Set up camera and controls
const camera = new THREE.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
// Add a ground plane
const ground = new THREE.PlaneGeometry(100, 100, 1, 1);
const groundMaterial = new THREE.MeshBasicMaterial({ color: 0x000020 });
const groundMesh = new THREE.Mesh(ground, groundMaterial);
groundMesh.position.set(0, -0.5, 0);
scene.add(groundMesh);
// Update renderer on window resize
window.addEventListener('resize', () => {
renderer.setSize(canvas.width, canvas.height);
camera.aspect = canvas.width / canvas.height;
camera.updateProjectionMatrix();
controls.update();
});
// Render the scene
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
This should work as expected, creating a vertical and horizontal line in yellow when the button is clicked.