-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (156 loc) · 4.91 KB
/
index.js
File metadata and controls
186 lines (156 loc) · 4.91 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
182
183
184
185
186
import './style.css'
// LIBS
import {
Scene,
Color,
Mesh, Vector2,
// MATERIALS
MeshBasicMaterial,
// LIGHTS
AmbientLight, DirectionalLight,
// GEOMETRIES
PlaneBufferGeometry,
PerspectiveCamera,
WebGLRenderer,
// HELPERS
GridHelper, AxesHelper
} from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import TWEEN from '@tweenjs/tween.js'
// APP
import Debug, { parameters } from './debug'
import KeyboardEvents from './keyboard'
import CommandsPanel from './commands-panels'
import Cursor from './cursor'
// COMMANDS
import CreateCommand from './commands/create'
import DeleteCommand from './commands/delete'
import EditCommand from './commands/edit'
import FillCommand from './commands/fill'
import RotateCommand from './commands/rotate'
import MoveCommand from './commands/move'
class WebEditor3D {
constructor() {
this.animate = this.animate.bind(this)
this.objects = []
this.init()
}
init () {
// CAMERA
this.aspect = window.innerWidth / window.innerHeight
this.camera = new PerspectiveCamera(50, this.aspect, 1, 500)
this.camera.position.set(60, 60, 60)
this.camera.lookAt(0, 0, 0);
// SCENE
this.scene = new Scene()
this.scene.background = new Color(parameters.background)
// GRID
this.gridSize = 128
this.gridDiv = 16
this.createGrid()
// PLANE
const geoPlane = new PlaneBufferGeometry(128, 128);
geoPlane.rotateX(-Math.PI / 2);
const matPlane = new MeshBasicMaterial({
color: 0x4d4444, /* , side: THREE.DoubleSide */
opacity: 0.5,
transparent: true
});
const plane = new Mesh(geoPlane, matPlane);
this.scene.add(plane);
plane.position.y = -0.001;
plane.name = "hidden_plane";
plane.visible = false;
this.objects.push(plane);
// lights
this.ambientLight = new AmbientLight(parameters.ambientLight);
this.scene.add(this.ambientLight);
this.directionalLight = new DirectionalLight(parameters.directionalLight);
this.directionalLight.position.set(100, 75, 50);
this.scene.add(this.directionalLight);
// RENDERER
this.renderer = new WebGLRenderer({
powerPreference: 'high-performance',
antialias: true
})
this.renderer.setPixelRatio(window.devicePixelRatio)
this.renderer.setSize(window.innerWidth, window.innerHeight)
this.renderer.render(this.scene, this.camera)
document.body.appendChild(this.renderer.domElement)
// CONTROLS
this.controls = new OrbitControls(this.camera, this.renderer.domElement)
this.mouse = new Vector2();
this.KeyboardEvents = new KeyboardEvents(this)
this.colorsPanel = new CommandsPanel(this)
this.cursor = new Cursor()
// Commands
this.commands = {
'CREATE': new CreateCommand(this),
'DELETE': new DeleteCommand(this),
'EDIT': new EditCommand(this),
'FILL': new FillCommand(this),
'ROTATE': new RotateCommand(this),
'MOVE': new MoveCommand(this),
}
// STATS + DAT.GUI
this.debug = new Debug(this)
// EVENTS
this.startListening()
// LOOP
this.startLoop()
}
createGrid (div = 16) {
if (this.gridHelper) {
this.gridHelper.material.dispose()
this.gridHelper.geometry.dispose()
this.scene.remove(this.gridHelper)
}
this.gridDiv = div;
this.gridHelper = new GridHelper(this.gridSize, this.gridDiv, 0x888888, 0x444444);
this.gridHelper.name = 'grid'
const axesHelper = new AxesHelper( this.gridSize / 2 );
axesHelper.position.y = 0.01
this.gridHelper.add( axesHelper );
this.scene.add(this.gridHelper);
}
startListening () {
window.addEventListener('resize', this.onWindowResize.bind(this), false)
window.oncontextmenu = () => false;
this.renderer.domElement.addEventListener('mousemove', this.globalHandler.bind(this), false);
this.renderer.domElement.addEventListener('click', this.globalHandler.bind(this), false);
// document.addEventListener('pointerup', this.globalHandler.bind(this), false);
}
globalHandler (ev) {
ev.preventDefault();
ev.stopPropagation();
this.mouse.set(
(ev.clientX / window.innerWidth) * 2 - 1,
-(ev.clientY / window.innerHeight) * 2 + 1
);
ev._x = this.mouse.x;
ev._y = this.mouse.y;
var func = this.commands[this.KeyboardEvents.currentCommand][ev.type].bind(this.commands[this.KeyboardEvents.currentCommand]);
if (func) {
func(ev, this);
}
}
startLoop () {
this.renderer.setAnimationLoop(this.animate)
}
stopLoop () {
this.renderer.setAnimationLoop()
}
animate () {
this.debug.stats.begin()
TWEEN.update()
this.controls.update()
this.renderer.render(this.scene, this.camera)
this.debug.stats.end()
}
onWindowResize () {
this.camera.aspect = window.innerWidth / window.innerHeight
this.camera.updateProjectionMatrix()
this.renderer.setSize(window.innerWidth, window.innerHeight)
}
}
new WebEditor3D()