import * as THREE from 'three'
import { WEBGL } from './webgl'
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"
if (WEBGL.isWebGLAvailable()) {
let markup = null;
const markupMouse = new THREE.Vector2();
const markupPoint = new THREE.Vector3();
//장면
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
//카메라
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.y = 1;
camera.position.z = 2;
//렌더러
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//OrbitControls 추가
const controls = new OrbitControls(camera,renderer.domElement);
controls.minDistance = 2;
controls.update();
//매쉬
const geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const material = new THREE.MeshStandardMaterial({
color: 0x999999
});
const cube = new THREE.Mesh(geometry, material);
cube.position.y = 0.5;
scene.add(cube);
//바닥
const planeGeoMetry = new THREE.PlaneGeometry(20,20,1,1);
const planeMaterial = new THREE.MeshStandardMaterial({color:0xffffff});
const plane = new THREE.Mesh(planeGeoMetry,planeMaterial);
plane.rotation.x = -0.5*Math.PI;
plane.position.y = -0.2;
scene.add(plane);
//빛
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
function animate() {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
}
animate();
function render(time) {
time *= 0.001; // convert time to seconds
cube.rotation.x = time;
cube.rotation.y = time;
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
/**
* 마우스 포인터
* @param event
* @returns {Promise<void>}
*/
async function getMousePointer({event}){
markupMouse.x = (event.clientX / window.innerWidth ) * 2 - 1;
markupMouse.y = -( event.clientY / window.innerHeight ) * 2 + 1;
markupPoint.set(markupMouse.x,markupMouse.y,0);
}
async function createRectangle({start,end,color}){
const distance = camera.position.distanceTo( new THREE.Vector3(0,0,0) )-1;
const points = [
new THREE.Vector3( start.x, start.y, distance ) ,
new THREE.Vector3( start.x, end.y, distance ) ,
new THREE.Vector3( end.x, end.y, distance ) ,
new THREE.Vector3( end.x, start.y, distance ),
new THREE.Vector3( start.x, start.y, distance )
];
const geometry = new THREE.BufferGeometry().setFromPoints( points );
const material = new THREE.LineBasicMaterial({
color: color?color:"rgb(255,0,0)"
});
const line = new THREE.Line( geometry, material );
line.quaternion.copy(camera.quaternion);
scene.add(line);
markup = line;
const corner = new THREE.Vector2();
const cornerPoint = new THREE.Vector3();
let planePosition = controls.target;
let normal = planePosition.clone().normalize()
normal.sub( camera.position )
const plane = new THREE.Plane().setFromNormalAndCoplanarPoint(normal, planePosition);
const raycaster = new THREE.Raycaster();
corner.set(0, 0); // NDC of the bottom-left corner
raycaster.setFromCamera(corner, camera);
raycaster.ray.intersectPlane(plane, cornerPoint);
line.position.copy(cornerPoint);//.add(new Vector3(0, 0, 1));
}
window.addEventListener("mousemove", async (event) => {
if(markup){
scene.remove(markup);
}
await getMousePointer({event})
let markupEndPoint = markupPoint.clone();
markupEndPoint.set(markupPoint.x+0.1,markupPoint.y+0.1,0);
await createRectangle({start:markupPoint,end:markupEndPoint});
});
} else {
var warning = WEBGL.getWebGLErrorMessage()
document.body.appendChild(warning)
}
As you move toward the edge of the screen, the red square moves away from the mouse.
I want the red square exist to right behind the cursor.