/
index.html
236 lines (175 loc) · 6.14 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - interactive cubes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
color: #444;
margin: 0px;
}
a {
color: #08f;
}
.loading {
position: fixed;
margin: auto;
width: 100%;
text-align: center;
}
</style>
</head>
<body>
<div class="loading">LOADING...</div>
<script type="module">
// Adapted from https://threejs.org/examples/#webgl_interactive_cubes to use olta
/**
This example renders 100 cuboids with random rotation and position.
The colors of the cuboids are derived from an Olta smart contract.
A cuboids color can be "cycled" between colors (white, red, green, blue) by clicking the mouse.
*/
import * as THREE from "../../lib/three.module.min.js"
import { OrbitControls } from "../../lib/three.orbit-controls.js"
import { Olta } from "../../lib/olta.module.js"
let cuboids = []
const palette = [
0xffffff,
0xff0000,
0x00ff00,
0x0000ff
]
let camera, scene, raycaster, renderer, controls
let INTERSECTED
let theta = 0
const pointer = new THREE.Vector2()
const radius = 5
const loading = document.querySelector(".loading")
// create a seeded random number generator with "cuboids" as the seed
const rand = seededRandom("cuboids")
// initialize olta instance
const olta = Olta()
// setup what to do on an update
olta.onUpdate(() => {
loading.hidden = true
const colors = olta.getAll("colors")
if (!colors || colors.length === 0) {
return
}
colors.forEach((color, i) => {
const cuboid = cuboids[i]
// update color
cuboid.material.color.setHex(palette[color.value])
// store reference to color document
cuboid.name = color._id
})
})
const updateCuboidColor = (event) => {
// force a render to update the intersected object
// without this the update will happen on the previous intersected object
onPointerMove(event)
render()
// make sure the mouse is hovering a cuboid
if (!INTERSECTED || INTERSECTED.name === "") {
return
}
// retrieve the document id from intersected object
const id = INTERSECTED.name
// cycle over palette indexes
const currentIndex = palette.indexOf(INTERSECTED.material.color.getHex())
const value = currentIndex < palette.length - 1 ? currentIndex + 1 : 0
// create color document to update
const doc = { id, value }
// send the update
olta.update("colors", doc)
}
// listen for mouse and touch events
window.addEventListener("click", updateCuboidColor)
window.addEventListener("touchstart", updateCuboidColor)
init()
animate()
function init() {
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100)
camera.position.z = 20
scene = new THREE.Scene()
scene.background = new THREE.Color(0xf0f0f0)
const light = new THREE.AmbientLight(0xffffff, 1)
const light2 = new THREE.DirectionalLight(0xffffff, 1)
light.position.set(1, 1, 1).normalize()
scene.add(light, light2)
const geometry = new THREE.BoxGeometry()
const size = 15
for (let i = 0; i < 100; i++) {
const object = new THREE.Mesh(
geometry,
new THREE.MeshLambertMaterial({ color: palette[0] })
)
object.position.x = rand() * size - size/2
object.position.y = rand() * size - size/2
object.position.z = rand() * size - size/2
object.scale.x = 1 + rand()
object.scale.y = 1 + rand()
object.scale.z = 1 + rand()
cuboids.push(object)
scene.add(object)
}
raycaster = new THREE.Raycaster()
renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
controls = new OrbitControls( camera, renderer.domElement );
document.body.appendChild(renderer.domElement)
document.addEventListener('mousemove', onPointerMove)
window.addEventListener('resize', onWindowResize)
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
}
function onPointerMove(event) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1
pointer.y = - (event.clientY / window.innerHeight) * 2 + 1
}
function animate() {
requestAnimationFrame(animate)
render()
}
function render() {
// find intersections
raycaster.setFromCamera(pointer, camera)
const intersects = raycaster.intersectObjects(scene.children, false)
if (intersects.length > 0) {
if (INTERSECTED != intersects[0].object) {
if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex)
INTERSECTED = intersects[0].object
INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex()
INTERSECTED.material.emissive.setHex(0x999999)
}
} else {
if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex)
INTERSECTED = null
}
renderer.render(scene, camera)
}
// for seeded number generation
// see https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript
function seededRandom(seed = 1337 ^ 0xDEADBEEF) {
return sfc32(0x9E3779B9, 0x243F6A88, 0xB7E15162, seed)
}
function sfc32(a, b, c, d) {
return function() {
a |= 0; b |= 0; c |= 0; d |= 0;
var t = (a + b | 0) + d | 0;
d = d + 1 | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = (c << 21 | c >>> 11);
c = c + t | 0;
return (t >>> 0) / 4294967296;
}
}
</script>
</body>
</html>