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.
Now, create a JavaScript file called script.js and add the following code:
javascript
document.getElementById('addLines').addEventListener('click', addCrosshairs);
function addCrosshairs() {
if (typeof scene === 'undefined' || typeof camera === 'undefined' || typeof renderer === 'undefined') {
alert('Three.js scene, camera, and renderer not initialized. Please reload the page.');
return;
}
const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffff00 });
// Vertical line
const geometryV = new THREE.Geometry();
geometryV.vertices.push(new THREE.Vector3(-500, 0, 0));
geometryV.vertices.push(new THREE.Vector3(500, 0, 0));
const verticalLine = new THREE.Line(geometryV, lineMaterial);
scene.add(verticalLine);
// Horizontal line
const geometryH = new THREE.Geometry();
geometryH.vertices.push(new THREE.Vector3(0, -500, 0));
geometryH.vertices.push(new THREE.Vector3(0, 500, 0));
const horizontalLine = new THREE.Line(geometryH, lineMaterial);
scene.add(horizontalLine);
// Update the renderer
renderer.render(scene, camera);
}
// Initialize Three.js scene, camera, and renderer
init();
animate();
function init() {
const container = document.createElement('div');
document.body.appendChild(container);
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const fieldOfView = 75;
const near = 0.1;
const far = 10000;
camera = new THREE.PerspectiveCamera(fieldOfView, aspect, near, far);
camera.position.z = 1000;
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
This code sets up a basic Three.js scene with a button. When you click the "Add Crosshairs" button, it will add a vertical and a horizontal yellow line that extend across the width and height of the scene. The addCrosshairs function is called on button click, and it creates the lines using THREE.Geometry, THREE.LineBasicMaterial, and THREE.Line. The init function initializes the scene, camera, and renderer, and the animate function renders the scene.
User
It says THREE.Geometry is not a constructor
Assistant
Apologies for that. The THREE.Geometry class has been deprecated in recent versions of Three.js and replaced with THREE.BufferGeometry. Here's the updated code:
javascript
document.getElementById('addLines').addEventListener('click', addCrosshairs);
function addCrosshairs() {
if (typeof scene === 'undefined' || typeof camera === 'undefined' || typeof renderer === 'undefined') {
alert('Three.js scene, camera, and renderer not initialized. Please reload the page.');
return;
}
const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffff00 });
// Vertical line
const geometryV = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(-500, 0, 0), new THREE.Vector3(500, 0, 0)]);
const verticalLine = new THREE.Line(geometryV, lineMaterial);
scene.add(verticalLine);
// Horizontal line
const geometryH = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(0, -500, 0), new THREE.Vector3(0, 500, 0)]);
const horizontalLine = new THREE.Line(geometryH, lineMaterial);
scene.add(horizontalLine);
// Update the renderer
renderer.render(scene, camera);
}
// Initialize Three.js scene, camera, and renderer
init();
animate();
function init() {
const container = document.createElement('div');
document.body.appendChild(container);
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const fieldOfView = 75;
const near = 0.1;
const far = 10000;
camera = new THREE.PerspectiveCamera(fieldOfView, aspect, near, far);
camera.position.z = 1000;
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
This code uses THREE.BufferGeometry instead of THREE.Geometry to create the lines. The setFromPoints method is used to define the line vertices.