Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better pointer lock #8

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/template/public/acm.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Symphonia</title>
</head>
<body>
<canvas class="webgl"></canvas>

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/template/public/symphonia.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Symphonia</title>
<link type="text/css" rel="stylesheet" href="/template/style.css">
<style>
#blocker {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}

#instructions {
width: 100%;
height: 100%;

display: flex;
flex-direction: column;
justify-content: center;
align-items: center;

text-align: center;
font-size: 14px;
cursor: pointer;
}
</style>
</head>


<body>

<canvas class="webgl">
<script type="module" src="/js/main.js"></script>
</body>
</html>
</canvas>

<div id="blocker">
<div id="instructions">
<p style="font-size:36px">
Click to play
</p>
</p>
</div>
</div>

</body>

</html>
9 changes: 4 additions & 5 deletions js/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ class FirstPersonControls {
this.moveRight = true;
break;

case "KeyR":
case "Space":
this.moveUp = true;
break;
case "KeyF":
case "ShiftLeft":
this.moveDown = true;
break;
}
Expand Down Expand Up @@ -205,11 +205,10 @@ class FirstPersonControls {
this.moveRight = false;
break;

case "KeyR":
case 87:
case "Space":
this.moveUp = false;
break;
case "KeyF":
case "ShiftLeft":
this.moveDown = false;
break;
}
Expand Down
95 changes: 49 additions & 46 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { FirstPersonControls } from "./controller";
import { fragmentShader } from "./shader.js";
import { PointerLockControls } from "./pointerlock";

import Stats from "stats.js";

Expand All @@ -26,9 +27,8 @@ void main() {
gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
}`;


let model, clock, controls, shader_mat, renderer;
let sun, sky;
let clock, controls, shader_mat, raycaster, renderer;
let prevTime = performance.now();

// Scene, Camera and Renderer
clock = new THREE.Clock();
Expand All @@ -43,26 +43,25 @@ document.body.appendChild(stats.domElement);
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.01,
0.1,
10000
);
camera.position.set(0, 5, 0);
camera.position.set(0, 10, 0);

function loadSky() {

const hemiLight = new THREE.HemisphereLight(0xffffff, 0xfffffff, 0.6);
hemiLight.color.setHSL(0.6, 1, 0.6);
hemiLight.color.setHSL(0.5, 1, 0.75);
hemiLight.groundColor.setHSL(0.095, 1, 0.75);
hemiLight.position.set(0, 50, 0);
scene.add(hemiLight);

const uniforms = {
topColor: { value: new THREE.Color(0x0077ff) },
topColor: { value: new THREE.Color(0x217eff) },
bottomColor: { value: new THREE.Color(0xffffff) },
offset: { value: 33 },
exponent: { value: 0.6 },
};
// scene.fog.color.copy(uniforms["bottomColor"].value);
scene.fog.color.copy(uniforms["bottomColor"].value);

const skyGeo = new THREE.SphereGeometry(1000, 32, 15);
const skyMat = new THREE.ShaderMaterial({
Expand All @@ -77,23 +76,39 @@ function loadSky() {
}

function main() {
scene.fog = new THREE.Fog( 0xa0a0a0, 1, 5000);
scene.fog = new THREE.Fog(0xa0a0a0, 1, 500);

renderer = new THREE.WebGLRenderer({
canvas: document.querySelector(".webgl"),
antialias: true,
});

controls = new PointerLockControls(camera, document.body);

const blocker = document.getElementById("blocker");
const instructions = document.getElementById("instructions");

blocker.addEventListener("click", function () {
controls.lock();
});

controls.addEventListener("lock", function () {
instructions.style.display = "none";
blocker.style.display = "none";
});

controls = new FirstPersonControls(camera, renderer.domElement);
controls.movementSpeed = 20;
controls.lookSpeed = 0.8;
controls.activeLook = false;
controls.heightMin = -10;
controls.mouseDragOn = true;
// controls.autoForward = true;
controls.mouseDragOn = true;
controls.addEventListener("unlock", function () {
blocker.style.display = "block";
instructions.style.display = "";
});
scene.add(controls.getObject());

const orbit = new OrbitControls(camera, renderer.domElement);
orbit.update();
raycaster = new THREE.Raycaster(
new THREE.Vector3(),
new THREE.Vector3(0, -1, 0),
0,
10
);

renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
Expand All @@ -103,50 +118,38 @@ function main() {
renderer.render(scene, camera);

// Adding objects here
const geom = new THREE.BoxGeometry(5, 5, 5);
const material = new THREE.MeshStandardMaterial({ color: "#ffffff" });
const box = new THREE.Mesh(geom, material);
const geom = new THREE.BoxGeometry(1, 1, 1);
const mat1 = new THREE.MeshStandardMaterial({ color: "#4287f5" });
const mat2 = new THREE.MeshStandardMaterial({ color: "#f542d4" });
const box = new THREE.Mesh(geom, mat1);
const box2 = new THREE.Mesh(geom, mat2);
box.castShadow = true;
box.position.set(0, 10, 0);
box.position.set(-10, 10, 0);
box2.position.set(10, 10, 0);
scene.add(box);
scene.add(box2);

// todo
shader_mat = new THREE.ShaderMaterial({
uniforms: controls.uniforms,
fragmentShader: fragmentShader(),
});
const floor_mat = new THREE.MeshBasicMaterial({ color: "#76e3a1" });
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(1000, 1000, 256, 256),
shader_mat,
new THREE.PlaneGeometry(1000, 1000, 100, 100),
floor_mat
);
mesh.rotation.x = -Math.PI / 2;
mesh.receiveShadow = true;
scene.add(mesh);

// cont planeMat = new THREE.

// light
// const light = new THREE.DirectionalLight(0xffffff, 1, 100);
// light.position.set(0, 10, 30);
// light.castShadow = true;
// scene.add(light);

// const lightHelper = new THREE.DirectionalLightHelper(light, 1);
// scene.add(lightHelper);

camera.position.z = 25;
camera.position.z = 10;

loadSky();

// updating on window size
// window.addEventListener("resize", onWindowResize);
window.addEventListener("resize", onWindowResize);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize(window.innerWidth, window.innerHeight);
controls.handleResize();
}

// updating the scene
Expand All @@ -158,7 +161,7 @@ function animate() {
}

function render() {
controls.update(clock.getDelta(), clock.elapsedTime);
prevTime = controls.update(prevTime);
renderer.render(scene, camera);
}

Expand Down
Loading