-
Notifications
You must be signed in to change notification settings - Fork 12
/
BaseGroundPlugin.ts
304 lines (265 loc) · 11.4 KB
/
BaseGroundPlugin.ts
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import {AViewerPluginSync, ThreeViewer} from '../../viewer'
import {IGeometry, iGeometryCommons, IMaterial, ISceneEvent, Mesh2, PhysicalMaterial, UnlitMaterial} from '../../core'
import {BufferAttribute, Euler, InterleavedBufferAttribute, PlaneGeometry, Vector3} from 'three'
import {onChange, onChange2, serialize} from 'ts-browser-helpers'
import {OrbitControls3} from '../../three'
import {uiConfig, uiFolderContainer, uiNumber, uiToggle} from 'uiconfig.js'
@uiFolderContainer('Ground')
export class BaseGroundPlugin<TEvent extends string = ''> extends AViewerPluginSync<TEvent> {
public static readonly PluginType: string = 'BaseGroundPlugin'
get enabled() {
return this.visible
}
set enabled(value) {
this.visible = value
}
protected _geometry: IGeometry&PlaneGeometry
protected _mesh: Mesh2<IGeometry&PlaneGeometry, IMaterial>
private _transformNeedRefresh = true
constructor() {
super()
this._refreshMaterial = this._refreshMaterial.bind(this)
this._refreshTransform = this._refreshTransform.bind(this)
this._refreshCameraLimits = this._refreshCameraLimits.bind(this)
this.refresh = this.refresh.bind(this)
this._refresh2 = this._refresh2.bind(this)
this._onSceneUpdate = this._onSceneUpdate.bind(this)
this._preRender = this._preRender.bind(this)
this._postFrame = this._postFrame.bind(this)
this._geometry = iGeometryCommons.upgradeGeometry.call(new PlaneGeometry(1, 1, 1, 1))
this._geometry.attributes.uv2 = (this._geometry.attributes.uv as any as BufferAttribute | InterleavedBufferAttribute).clone()
this._geometry.attributes.uv2.needsUpdate = true
this._mesh = this._createMesh()
this.refresh()
}
@uiToggle('Visible')
@onChange(BaseGroundPlugin.prototype.refreshTransform)
@serialize() visible = true
@uiNumber('Size')
@onChange2(BaseGroundPlugin.prototype._onSceneUpdate)
@serialize() size = 8
@uiNumber('Height (yOffset)')
@onChange2(BaseGroundPlugin.prototype._onSceneUpdate)
@serialize() yOffset = 0
@uiToggle('Render to Depth')
@onChange(BaseGroundPlugin.prototype._refresh2)
@serialize() renderToDepth = true
/**
* If false, the ground will not be tonemapped in post processing.
* note: this will only work when {@link GBufferPlugin} is being used. Also needs {@link renderToDepth} to be true.
*/
@uiToggle('Tonemap Ground')
@onChange(BaseGroundPlugin.prototype._refresh2)
@serialize() tonemapGround = true
/**
* If true, the camera will be limited to not go below the ground.
* note: this will only work when {@link OrbitControls3} or three.js OrbitControls are being used.
*/
@uiToggle('Limit Camera Above Ground')
@onChange(BaseGroundPlugin.prototype._refreshCameraLimits)
@serialize() limitCameraAboveGround = false
@uiToggle('Auto Adjust Transform')
@onChange(BaseGroundPlugin.prototype.refreshTransform)
@serialize() autoAdjustTransform = true
@serialize('material')
@uiConfig()
protected _material?: PhysicalMaterial
onAdded(viewer: ThreeViewer): void {
super.onAdded(viewer)
// if (viewer.getPlugin('TweakpaneUi')) console.error('TweakpaneUiPlugin must be added after Ground Plugin')
viewer.scene.addObject(this._mesh, {addToRoot: true})
viewer.scene.addEventListener('sceneUpdate', this._onSceneUpdate) // todo: refresh when update...
viewer.scene.addEventListener('addSceneObject', this._onSceneUpdate)
viewer.addEventListener('preRender', this._preRender)
viewer.addEventListener('postFrame', this._postFrame)
this.refresh()
}
onRemove(viewer: ThreeViewer): void {
this._mesh?.dispose(true)
this._removeMaterial()
viewer.scene.removeEventListener('sceneUpdate', this._onSceneUpdate)
viewer.scene.removeEventListener('addSceneObject', this._onSceneUpdate)
viewer.removeEventListener('postFrame', this._postFrame)
viewer.removeEventListener('preRender', this._preRender)
return super.onRemove(viewer)
}
protected _postFrame() {
if (this._transformNeedRefresh) this._refreshTransform()
if (!this._viewer) return
}
protected _preRender() {
if (!this._viewer) return
}
dispose(): void {
this._removeMaterial()
this._geometry.dispose()
this._material?.dispose() // todo
this._mesh?.dispose?.()
super.dispose()
}
protected _removeMaterial() {
if (!this._material) return
// this._manager?.materials?.unregisterMaterial(this._material)
this._material.userData.renderToDepth = this._material.userData.__renderToDepth
this._material.userData.__renderToDepth = undefined
// todo reset gBufferData.tonemapEnabled also
this._material = undefined
}
protected _onSceneUpdate(event?: ISceneEvent) {
if (event?.geometryChanged === false) return
if (event?.updateGround !== false)
this.refreshTransform()
}
/**
* Extra flag for plugins to disable transform refresh like when animating or dragging
*/
enableRefreshTransform = true
refreshTransform(): void {
if (!this.enableRefreshTransform) return
this._transformNeedRefresh = true
}
public refresh(): void {
if (!this._viewer) return
this._refreshMaterial()
this.refreshTransform()
this._refreshCameraLimits()
}
// because of inheritance breaks onChange
private _refresh2(): void {
this.refresh()
}
private _cameraLimitsSet = false
private _cameraLastMaxPolarAngle = Math.PI
private _refreshCameraLimits() {
const orbit = this._viewer?.scene.mainCamera.controls as OrbitControls3
if (!orbit) return
if (orbit.maxPolarAngle === undefined) {
console.warn('refreshCameraLimits only available with orbit controls.')
return
}
if (this.limitCameraAboveGround) {
if (!this._cameraLimitsSet) this._cameraLastMaxPolarAngle = orbit.maxPolarAngle
orbit.maxPolarAngle = Math.PI / 2
this._cameraLimitsSet = true
} else if (this._cameraLimitsSet) {
orbit.maxPolarAngle = this._cameraLastMaxPolarAngle
this._cameraLimitsSet = false
}
}
protected _refreshTransform() {
if (!this._mesh) return
if (!this._viewer) return
let updated = false
if (this.visible !== this._mesh.visible) {
this._mesh.visible = this.visible
updated = true
}
if (this.isDisabled()) {
if (updated) this._viewer?.scene.setDirty()
return
}
if (this.autoAdjustTransform) {
this._mesh.userData.bboxVisible = false
const bbox = this._viewer.scene.getBounds(true)
this._mesh.userData.bboxVisible = true
const v = bbox.getCenter(
new Vector3()).sub(new Vector3(0,
bbox.getSize(new Vector3()).y / 2 + this.yOffset,
0))
updated = updated || v.clone().sub(this._mesh.position).length() > 0.0001
if (updated) {
this._mesh.position.copy(v)
}
}
updated = updated || Math.abs(this._mesh.scale.x - this.size) > 0.0001
// todo: check rotation, someone could externally change it
if (updated) {
this._mesh.scale.setScalar(this.size)
// this._mesh.lookAt(new Vector3().fromArray(this._options.up))
this._mesh.setRotationFromEuler(new Euler(-Math.PI / 2., 0, this._mesh.rotation.z))
this._mesh.matrixWorldNeedsUpdate = true
this._mesh.setDirty()
// this._viewer.scene.setDirty()
}
this._transformNeedRefresh = false
}
protected _createMesh(mesh?: Mesh2<IGeometry&PlaneGeometry, IMaterial>): Mesh2<IGeometry&PlaneGeometry, IMaterial> {
if (!mesh) mesh = new Mesh2(this._geometry, new UnlitMaterial())
else mesh.geometry = this._geometry
if (mesh) {
mesh.userData.physicsMass = 0
mesh.userData.userSelectable = false
mesh.castShadow = true
mesh.receiveShadow = true
mesh.name = 'Ground Plane'
}
return mesh
}
protected _createMaterial(material?: PhysicalMaterial): PhysicalMaterial {
if (!material) material = new PhysicalMaterial({
name: 'BaseGroundMaterial',
color: 0xffffff,
})
material.userData.runtimeMaterial = true
return material
}
protected _refreshMaterial(): boolean {
if (!this._viewer) return false
if (this.isDisabled()) return false
const mat = this._material ?? this._createMaterial()
const isNewMaterial = mat !== this._material
if (isNewMaterial) { // new material
this._removeMaterial()
this._material = mat
const id = this._material?.uuid
if (!id) console.warn('No material found for ground')
this._viewer.scene.setDirty()
if (this._mesh && this._material) {
this._material.roughness = 0.2
this._material.metalness = 0.5
this._mesh.material = this._material // for update event handlers.
}
}
if (this._material) {
if (this._material.userData.__renderToDepth === undefined) {
this._material.userData.__renderToDepth = this._material.userData.renderToDepth
}
if (this._material.userData.renderToDepth !== this.renderToDepth) {
this._material.userData.renderToDepth = this.renderToDepth // required to work with SSR, SSAO etc when the ground is transparent / transmissive
}
if (!this._material.userData.gBufferData) this._material.userData.gBufferData = {}
if (this._material.userData.gBufferData.__tonemapEnabled === undefined) {
this._material.userData.gBufferData.__tonemapEnabled = this._material.userData.gBufferData.tonemapEnabled
}
if (this._material.userData.gBufferData.tonemapEnabled !== this.tonemapGround) {
this._material.userData.gBufferData.tonemapEnabled = this.tonemapGround
}
this._material.userData.ssaoDisabled = true
this._material.userData.sscsDisabled = true
// if (this._material.userData.__postTonemap === undefined) {
// this._material.userData.__postTonemap = this._material.userData.postTonemap
// }
// if (this._material.userData.postTonemap !== this.tonemapGround) {
// this._material.userData.postTonemap = this.tonemapGround
// }
}
this._viewer.setDirty(this) // todo: something else also?
return isNewMaterial
}
get material() {
return this._material
}
get mesh() {
return this._mesh
}
fromJSON(data: any, meta?: any): this | null {
if (data.options) {
console.error('todo: support old webgi v0 file')
}
if (!super.fromJSON(data, meta)) return null
// if (this._material && this._material.transmission >= 0.01) this._material.transparent = true
this.refresh()
// Note: baked shadow reset is done in ShadowMapBaker.fromJSON
return this
}
}