Skip to content

Commit

Permalink
Start lesson 22 raycaster and mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
luketurnbull committed Jun 7, 2024
1 parent 520d838 commit 5014877
Show file tree
Hide file tree
Showing 27 changed files with 1,263 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lessons/22-raycaster-mouse-events/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "threejs-journey-exercise",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"devDependencies": {
"vite": "^5.2.10",
"vite-plugin-restart": "^0.4.0"
},
"dependencies": {
"lil-gui": "^0.19.2",
"three": "^0.164.1"
}
}
16 changes: 16 additions & 0 deletions lessons/22-raycaster-mouse-events/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Three.js Journey

## Setup
Download [Node.js](https://nodejs.org/en/download/).
Run this followed commands:

``` bash
# Install dependencies (only the first time)
npm install

# Run the local server at localhost:8080
npm run dev

# Build for production in the dist/ directory
npm run build
```
13 changes: 13 additions & 0 deletions lessons/22-raycaster-mouse-events/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Raycaster and Mouse Events</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<canvas class="webgl"></canvas>
<script type="module" src="./script.js"></script>
</body>
</html>
102 changes: 102 additions & 0 deletions lessons/22-raycaster-mouse-events/src/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import GUI from 'lil-gui'

/**
* Base
*/
// Debug
const gui = new GUI()

// Canvas
const canvas = document.querySelector('canvas.webgl')

// Scene
const scene = new THREE.Scene()

/**
* Objects
*/
const object1 = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 16, 16),
new THREE.MeshBasicMaterial({ color: '#ff0000' })
)
object1.position.x = - 2

const object2 = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 16, 16),
new THREE.MeshBasicMaterial({ color: '#ff0000' })
)

const object3 = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 16, 16),
new THREE.MeshBasicMaterial({ color: '#ff0000' })
)
object3.position.x = 2

scene.add(object1, object2, object3)

/**
* Sizes
*/
const sizes = {
width: window.innerWidth,
height: window.innerHeight
}

window.addEventListener('resize', () =>
{
// Update sizes
sizes.width = window.innerWidth
sizes.height = window.innerHeight

// Update camera
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()

// Update renderer
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})

/**
* Camera
*/
// Base camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
camera.position.z = 3
scene.add(camera)

// Controls
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true

/**
* Renderer
*/
const renderer = new THREE.WebGLRenderer({
canvas: canvas
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

/**
* Animate
*/
const clock = new THREE.Clock()

const tick = () =>
{
const elapsedTime = clock.getElapsedTime()

// Update controls
controls.update()

// Render
renderer.render(scene, camera)

// Call tick again on the next frame
window.requestAnimationFrame(tick)
}

tick()
19 changes: 19 additions & 0 deletions lessons/22-raycaster-mouse-events/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*
{
margin: 0;
padding: 0;
}

html,
body
{
overflow: hidden;
}

.webgl
{
position: fixed;
top: 0;
left: 0;
outline: none;
}
Empty file.
32 changes: 32 additions & 0 deletions lessons/22-raycaster-mouse-events/static/draco/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Draco 3D Data Compression

Draco is an open-source library for compressing and decompressing 3D geometric meshes and point clouds. It is intended to improve the storage and transmission of 3D graphics.

[Website](https://google.github.io/draco/) | [GitHub](https://github.com/google/draco)

## Contents

This folder contains three utilities:

* `draco_decoder.js` — Emscripten-compiled decoder, compatible with any modern browser.
* `draco_decoder.wasm` — WebAssembly decoder, compatible with newer browsers and devices.
* `draco_wasm_wrapper.js` — JavaScript wrapper for the WASM decoder.

Each file is provided in two variations:

* **Default:** Latest stable builds, tracking the project's [master branch](https://github.com/google/draco).
* **glTF:** Builds targeted by the [glTF mesh compression extension](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression), tracking the [corresponding Draco branch](https://github.com/google/draco/tree/gltf_2.0_draco_extension).

Either variation may be used with `THREE.DRACOLoader`:

```js
var dracoLoader = new THREE.DRACOLoader();
dracoLoader.setDecoderPath('path/to/decoders/');
dracoLoader.setDecoderConfig({type: 'js'}); // (Optional) Override detection of WASM support.
```

Further [documentation on GitHub](https://github.com/google/draco/tree/master/javascript/example#static-loading-javascript-decoder).

## License

[Apache License 2.0](https://github.com/google/draco/blob/master/LICENSE)
34 changes: 34 additions & 0 deletions lessons/22-raycaster-mouse-events/static/draco/draco_decoder.js

Large diffs are not rendered by default.

Binary file not shown.
33 changes: 33 additions & 0 deletions lessons/22-raycaster-mouse-events/static/draco/draco_encoder.js

Large diffs are not rendered by default.

Loading

0 comments on commit 5014877

Please sign in to comment.