-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (156 loc) · 6.83 KB
/
Copy pathscript.js
File metadata and controls
181 lines (156 loc) · 6.83 KB
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
import { cssColor, babylonColor } from "./utility/colorUtils.mjs";
import { changeColor, resetSelectedFaces, resetMesh, undoMesh, rotateMesh, transformFace, nullifyExtrusionDetails } from "./utility/meshUtils.mjs";
import { setupLights, drawAxes } from "./utility/sceneUtils.mjs";
import { unproject } from "./utility/mathUtils.mjs";
const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
var resetGeometry = null;
var resetRotation = null;
var pressedDownOnFace = false;
var allowScaling = false;
var allowRotation = false;
var selectedFace = -1;
var extrusionDetails = {
allow: false,
mesh: null,
face: null,
position: null,
originalGeometry: null,
originalRotation: null,
centerVertex: null,
indicesList: null
};
const createScene = function () {
const scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color4(0.95, 0.95, 0.95, 1);
// initialize text that follows the cursor on scaling/extrusion
var cursorText = new BABYLON.GUI.TextBlock();
cursorText.text = "";
cursorText.color = "black";
cursorText.fontSize = 12;
var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
advancedTexture.addControl(cursorText);
// initialize guide line when the mesh is modified
var guideLine = BABYLON.Mesh.CreateLines("guideLine", [new BABYLON.Vector3(0, 0, 0), new BABYLON.Vector3(0, 0, 0)], scene);
// initializes box in the scene
var box = BABYLON.MeshBuilder.CreateBox("box", {size: 2}, scene);
box.material = new BABYLON.StandardMaterial("boxMaterial", scene);
box.material.backFaceCulling = false; // double-sided face
// stored for resetMesh function
resetGeometry = box.getVerticesData(BABYLON.VertexBuffer.PositionKind);
extrusionDetails.originalGeometry = resetGeometry;
resetRotation = BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 0), 0);
extrusionDetails.originalRotation = resetRotation;
box.isPickable = true;
box.enableEdgesRendering();
box.edgesWidth = 2;
box.edgesColor = new BABYLON.Color4(0, 0, 0, 1);
const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2.5, Math.PI / 2.5, 15, new BABYLON.Vector3(0, 0, 0));
camera.attachControl(canvas, true);
setupLights(scene);
drawAxes(100, scene);
// Register the onKeyboardObservable to listen for keyboard events
scene.onKeyboardObservable.add((kbInfo) => {
if (kbInfo.type === BABYLON.KeyboardEventTypes.KEYDOWN) {
if (kbInfo.event.key === 'd') {
undoMesh(box, extrusionDetails, cursorText);
extrusionDetails.originalGeometry = null;
extrusionDetails.originalRotation = null;
box.enableEdgesRendering();
guideLine.dispose();
}
else if(kbInfo.event.key === 'x') {
resetMesh(box, extrusionDetails, cursorText, resetGeometry, resetRotation);
guideLine.dispose();
}
else if(kbInfo.event.key === 's') {
// turn on scaling option
if(allowScaling == false) {
allowScaling = true;
allowRotation = false;
document.getElementById("scale").style.backgroundColor = cssColor.alertRed;
document.getElementById("rotate").style.backgroundColor = cssColor.transparentBlack;
}
// turn off scaling option
else {
allowScaling = false;
document.getElementById("scale").style.backgroundColor = cssColor.transparentBlack;
}
}
else if(kbInfo.event.key === 'r') {
// turn on rotation option
if(allowRotation == false) {
allowRotation = true;
allowScaling = false;
document.getElementById("scale").style.backgroundColor = cssColor.transparentBlack;
document.getElementById("rotate").style.backgroundColor = cssColor.alertRed;
}
// turn on rotation option
else {
allowRotation = false;
document.getElementById("rotate").style.backgroundColor = cssColor.transparentBlack;
}
}
}
});
scene.onPointerMove = function (event) {
var pickResult = scene.pick(scene.pointerX, scene.pointerY);
if(extrusionDetails.allow == true){
extrusionDetails.mesh.enableEdgesRendering();
const mousePosition = unproject({
x: scene.pointerX,
y: scene.pointerY,
engine, scene
});
guideLine.dispose();
guideLine = BABYLON.Mesh.CreateLines("guideLine", [mousePosition, extrusionDetails.position], scene);
guideLine.color = babylonColor.black;
if(!allowRotation)
transformFace(extrusionDetails, allowScaling, cursorText, engine, scene);
else
rotateMesh(extrusionDetails, cursorText, engine, scene);
}
// Check if a mesh was picked
if (pickResult.hit && pickResult.pickedMesh === box){
changeColor(pickResult, box, babylonColor.hoverBlue, selectedFace);
}
else {
if(box != null && selectedFace != null)
resetSelectedFaces(box, selectedFace);
}
};
scene.onPointerDown = function (event, pickResult) {
if (pickResult.hit && pickResult.pickedMesh === box) {
pressedDownOnFace = true;
}
}
scene.onPointerUp = function (event, pickResult) {
// Check if a mesh was picked
if(pressedDownOnFace == true){
if(extrusionDetails.allow == true) {
nullifyExtrusionDetails(extrusionDetails, cursorText);
guideLine.dispose();
pressedDownOnFace = false;
}
else {
if (pickResult.hit && pickResult.pickedMesh === box) {
extrusionDetails.allow = true;
extrusionDetails.mesh = box;
extrusionDetails.face = 2*Math.floor(pickResult.faceId/2);
extrusionDetails.position = unproject({x:scene.pointerX, y:scene.pointerY, engine, scene});
extrusionDetails.originalGeometry = box.getVerticesData(BABYLON.VertexBuffer.PositionKind);
extrusionDetails.originalRotation = box.rotationQuaternion;
}
}
}
};
return scene;
};
const scene = createScene();
// Register a render loop
engine.runRenderLoop(function () {
scene.render();
});
window.addEventListener("resize", function () {
engine.resize();
});