f_dot 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +1,249 @@
1
1
  //
2
2
  // canvas.js
3
- // f_dot webgl.
3
+ // f_dot with threejs.
4
4
  //
5
5
  // created by i2097i
6
6
  //
7
7
 
8
- const canvas = document.querySelector("#gl-canvas");
8
+ import * as THREE from "three";
9
+ import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
10
+ import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js';
9
11
 
10
- export function configureCanvas() {
11
- console.info("configureCanvas");
12
+ let camera, controls, scene, renderer;
13
+ let openSystemSpace, projectedFromData;
14
+ let updateCameraTimeout;
12
15
 
13
- // initialize webgl context
14
- const gl = canvas.getContext("webgl");
16
+ export const MOVE_STEP = 11;
15
17
 
16
- if (gl === null) {
17
- console.error(
18
- "unable to initialize webgl. your browser or machine may not support it.",
19
- );
20
- return;
18
+ export function clear() {
19
+ // clear the canvas
20
+ camera = undefined;
21
+ controls = undefined;
22
+ scene = undefined;
23
+ renderer = undefined;
24
+ document.getElementById('canvas').innerHTML = `<div id="canvas_empty">
25
+ the projector is empty.
26
+ there are no glyphs loaded.
27
+ navigate to open_system to load a glyph.
28
+ </div>`;
29
+ }
30
+
31
+ export function isLoaded() {
32
+ return [camera, controls, scene, renderer].filter((i) => typeof i !== 'undefined').length > 0;
33
+ }
34
+
35
+ function width() {
36
+ return parseInt(document.getElementById('canvas').offsetWidth);
37
+ }
38
+
39
+ function height() {
40
+ return parseInt(document.getElementById('canvas').offsetHeight);
41
+ }
42
+
43
+ function init(data, onChangeCallback) {
44
+ clear();
45
+ scene = new THREE.Scene();
46
+ scene.background = new THREE.Color(0xcccccc);
47
+ scene.fog = new THREE.FogExp2(0xcccccc, 0.002);
48
+ renderer = new CSS3DRenderer();
49
+ // renderer.setPixelRatio(window.devicePixelRatio);
50
+ renderer.setSize(width(), height());
51
+ document.getElementById('canvas').innerHTML = "";
52
+ document.getElementById('canvas').appendChild(renderer.domElement);
53
+ camera = new THREE.PerspectiveCamera(60, width() / height(), 1, 1000);
54
+ // camera.position.set(0, 200, 200);
55
+
56
+ // controls
57
+ controls = new OrbitControls(camera, renderer.domElement);
58
+ controls.addEventListener('change', function() {
59
+ render();
60
+ if (typeof updateCameraTimeout !== 'undefined') {
61
+ clearTimeout(updateCameraTimeout);
62
+ }
63
+ updateCameraTimeout = setTimeout(function() {
64
+ onChangeCallback(camera.position, camera.rotation);
65
+ onWindowResize();
66
+ }, 242);
67
+ });
68
+ controls.screenSpacePanning = true;
69
+ controls.cursorStyle = 'grab';
70
+ // controls.zoomToCursor = true;
71
+ controls.minDistance = 0;
72
+ controls.maxDistance = Infinity;
73
+ controls.maxPolarAngle = Math.PI;
74
+ controls.keyRotateSpeed = MOVE_STEP * 2;
75
+ controls.panSpeed = MOVE_STEP * 2;
76
+ controls.keyPanSpeed = MOVE_STEP * 2;
77
+ controls.zoomSpeed = MOVE_STEP * 2;
78
+ // world
79
+
80
+ generateOpenSystemSpace(data);
81
+ // lights
82
+
83
+ const dirLight1 = new THREE.DirectionalLight(0xffffff, 3);
84
+ dirLight1.position.set(1, 1, 1);
85
+ scene.add(dirLight1);
86
+ const dirLight2 = new THREE.DirectionalLight(0x002288, 3);
87
+ dirLight2.position.set(- 1, - 1, - 1);
88
+ scene.add(dirLight2);
89
+ const ambientLight = new THREE.AmbientLight(0x555555);
90
+ scene.add(ambientLight);
91
+ //
92
+
93
+ window.addEventListener('resize', onWindowResize);
94
+ }
95
+
96
+ export function projectedFrom() {
97
+ return projectedFromData;
98
+ }
99
+
100
+ function Perspecticus(localizedName, x, y, z, ry) {
101
+ // todo: add camera for each perspecticus
102
+ const div = document.createElement('div');
103
+ div.textContent = localizedName;
104
+ div.className = "glyph";
105
+ const button = document.createElement('button');
106
+ button.textContent = "click!";
107
+ // button.addEventListener('click', function(event) {
108
+ // console.error("perspecticus clicked!", event.target);
109
+ // });
110
+ div.appendChild(button);
111
+
112
+ const object = new CSS3DObject(div);
113
+ object.position.set(x, y, z);
114
+ object.rotation.y = ry;
115
+
116
+ return object;
117
+ }
118
+
119
+ function Objecticus(localizedName, x, y, z, ry) {
120
+ // todo: add camera for each objecticus
121
+ const div = document.createElement('div');
122
+ div.textContent = localizedName;
123
+ div.className = "glyph objecticus";
124
+ const button = document.createElement('button');
125
+ button.textContent = "click!";
126
+ button.addEventListener('click', function(event) {
127
+ console.error("perspecticus clicked!", event.target);
128
+ });
129
+ div.appendChild(button);
130
+
131
+ const object = new CSS3DObject(div);
132
+ object.position.set(x, y, z);
133
+ object.rotation.y = ry;
134
+
135
+ return object;
136
+ }
137
+
138
+ function generateOpenSystemSpace(data) {
139
+ openSystemSpace = data["open_system_space"];
140
+ projectedFromData = data["projected_from_data"];
141
+ camera.position.set(
142
+ openSystemSpace.camera_position.x,
143
+ openSystemSpace.camera_position.y,
144
+ openSystemSpace.camera_position.z
145
+ )
146
+ camera.rotation.set(
147
+ openSystemSpace.camera_rotation.x,
148
+ openSystemSpace.camera_rotation.y,
149
+ openSystemSpace.camera_rotation.z
150
+ )
151
+ for (var pIndex = 0; pIndex < openSystemSpace.perspecticus.length; pIndex ++) {
152
+ const perspecticusData = openSystemSpace.perspecticus[pIndex];
153
+ const perspecticusInstance = Perspecticus(
154
+ perspecticusData.localized_name,
155
+ perspecticusData.position.x,
156
+ perspecticusData.position.y,
157
+ perspecticusData.position.z,
158
+ pIndex == 0 ? 0 : Math.PI / pIndex
159
+ )
160
+ scene.add(perspecticusInstance);
161
+ }
162
+ for (var oIndex = 0; oIndex < openSystemSpace.objecticus.length; oIndex ++) {
163
+ const objecticusData = openSystemSpace.objecticus[oIndex];
164
+ const objecticusInstance = Objecticus(
165
+ objecticusData.localized_name,
166
+ objecticusData.position.x,
167
+ objecticusData.position.y,
168
+ objecticusData.position.z,
169
+ oIndex == 0 ? 0 : Math.PI / oIndex
170
+ )
171
+ scene.add(objecticusInstance);
172
+ }
173
+
174
+ // const perspecticusGeometry = new THREE.ConeGeometry( 10, 30, 4, 1 );
175
+ // const perspecticusMaterial = new THREE.MeshPhongMaterial({ color: 0xeeeeee, flatShading: true });
176
+ // const perspecticusMesh = new THREE.InstancedMesh(
177
+ // perspecticusGeometry,
178
+ // perspecticusMaterial,
179
+ // 500
180
+ // );
181
+
182
+ // const objecticusGeometry = new THREE.BoxGeometry();
183
+ // objecticusGeometry.translate(0, 0.5, 0);
184
+ // const objecticusMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
185
+ // const objecticusMesh = new THREE.InstancedMesh(
186
+ // objecticusGeometry,
187
+ // objecticusMaterial,
188
+ // 500
189
+ // );
190
+
191
+ // const translator = new THREE.Object3D();
192
+ // translator.scale.x = 1;
193
+ // translator.scale.y = 1;
194
+ // translator.scale.z = 1;
195
+ // for (var pIndex = 0; pIndex < openSystemSpace.perspecticus.length; pIndex ++) {
196
+ // const perspecticusData = openSystemSpace.perspecticus[pIndex];
197
+ // translator.position.x = perspecticusData.x;
198
+ // translator.position.y = perspecticusData.y;
199
+ // translator.position.z = perspecticusData.z;
200
+ // translator.updateMatrix();
201
+ // perspecticusMesh.setMatrixAt(pIndex, translator.matrix);
202
+ // }
203
+ // for (var oIndex = 0; oIndex < openSystemSpace.objecticus.length; oIndex ++) {
204
+ // const objecticusData = openSystemSpace.objecticus[oIndex];
205
+ // translator.position.x = objecticusData.x;
206
+ // translator.position.y = objecticusData.y;
207
+ // translator.position.z = objecticusData.z;
208
+ // translator.updateMatrix();
209
+ // perspecticusMesh.setMatrixAt(oIndex, translator.matrix);
210
+ // }
211
+
212
+ // scene.add(perspecticusMesh);
213
+ // scene.add(objecticusMesh);
214
+ }
215
+
216
+ export function handleKeyPress(event) {
217
+ if (typeof controls === 'undefined') {
218
+ return false;
21
219
  }
22
220
 
23
- gl.clearColor(0.1, 0.2, 0.3, 0.77);
24
- gl.clear(gl.COLOR_BUFFER_BIT);
221
+ controls._onKeyDown(event);
222
+
223
+ return event.defaultPrevented;
224
+ }
225
+
226
+ function onWindowResize() {
227
+ camera.aspect = width() / height();
228
+ camera.updateProjectionMatrix();
229
+ renderer.setSize(width(), height());
230
+ render();
25
231
  }
26
232
 
27
- export function toggleVisible() {
28
- if (canvas.style.display == "none") {
29
- canvas.style.display = "block";
30
- } else {
31
- canvas.style.display = "none";
233
+ function animate() {
234
+ controls.update(); // only required if controls.enableDamping = true, or if controls.autoRotate = true
235
+ render();
236
+ }
237
+
238
+ export function load(openSystemSpace, onChangeCallback) {
239
+ init(openSystemSpace, onChangeCallback);
240
+ render();
241
+ }
242
+
243
+ export function render() {
244
+ if (!renderer) {
245
+ console.error("not initialized!");
246
+ return;
32
247
  }
248
+ renderer.render(scene, camera);
33
249
  }
@@ -1,8 +1,4 @@
1
1
  import * as f_dot_go from "./f_dot_go.js"
2
- import * as f_dot_canvas from "./canvas.js"
3
2
  console.info("starting f_dot_go.js", f_dot_go);
4
3
  f_dot_go.connect();
5
4
  f_dot_go.configureDom();
6
-
7
- // todo: webgl
8
- f_dot_canvas.configureCanvas();