Skip to content

Commit

Permalink
initial take on slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
mduft committed Jan 26, 2023
1 parent 86a23ea commit b5328d0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
5 changes: 5 additions & 0 deletions res/shaders/block_fragment.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ varying vec4 v_occlusion;
varying vec2 v_texcoord;
varying vec2 v_blockTexcoord;
varying float v_blockLighting;
varying float v_sliced;

float dither8x8(vec2 position, float alpha) {
int x = int(mod(position.x, 8.0));
Expand Down Expand Up @@ -105,5 +106,9 @@ void main() {
discard;
}

if(v_sliced > 0.5) {
discard;
}

gl_FragColor = vec4(diffuse.rgb * v_lighting * g * (u_nightVision ? 1.0 : v_blockLighting), 1.0);
}
11 changes: 11 additions & 0 deletions res/shaders/block_vertex.vs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ uniform sampler2D u_texture;
uniform float u_voxelSize;
uniform vec3 u_gridOffset;
uniform bool u_nightVision;
uniform float u_sliceHeight;
uniform vec3 u_boundsMin;
uniform vec3 u_boundsMax;

attribute vec3 position;
attribute vec3 normal;
Expand All @@ -18,6 +21,7 @@ varying vec4 v_occlusion;
varying vec2 v_texcoord;
varying vec2 v_blockTexcoord;
varying float v_blockLighting;
varying float v_sliced;

vec3 light = vec3(0.78, 0.98, 0.59);

Expand All @@ -28,5 +32,12 @@ void main() {
v_lighting = dot(light, abs(normal));
v_blockLighting = lighting;

if(u_sliceHeight > 0.0 && (position.y + u_gridOffset.y) >= (u_sliceHeight + u_boundsMin.y))
{
v_sliced = 1.0;
} else {
v_sliced = 0.0;
}

gl_Position = u_worldViewProjection * vec4((position.xyz + u_gridOffset) * u_voxelSize, 1.0);
}
10 changes: 10 additions & 0 deletions src/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class ArcballCamera {
private _zNear: number;
private _zFar: number;
private _aspect: number;
private _sliceHeight: number;

private _distance: SmoothVariable;// = new SmoothVariable(this._defaultDistance, 0.025);
private _azimuth: SmoothVariable;// = new SmoothVariable(this._defaultAzimuth, 0.025);
Expand Down Expand Up @@ -47,6 +48,7 @@ export class ArcballCamera {
this._azimuth = new SmoothVariable(AppConfig.Get.CAMERA_DEFAULT_AZIMUTH_RADIANS, AppConfig.Get.CAMERA_SMOOTHING);
this._elevation = new SmoothVariable(AppConfig.Get.CAMERA_DEFAULT_ELEVATION_RADIANS, AppConfig.Get.CAMERA_SMOOTHING);
this._target = new SmoothVectorVariable(new Vector3(0, 0, 0), AppConfig.Get.CAMERA_SMOOTHING);
this._sliceHeight = 0;

this._elevation.setClamp(0.001, Math.PI - 0.001);
this._distance.setClamp(1.0, 100.0);
Expand Down Expand Up @@ -80,6 +82,14 @@ export class ArcballCamera {
this._isPerspective = mode === 'perspective';
}

public setSliceHeight(sliceHeight: number) {
this._sliceHeight = sliceHeight;
}

public getSliceHeight(): number {
return this._sliceHeight;
}

private _angleSnap = false;
public toggleAngleSnap() {
this._angleSnap = !this._angleSnap;
Expand Down
14 changes: 14 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Bounds } from './bounds';
import * as twgl from 'twgl.js';

import { ArcballCamera } from './camera';
Expand All @@ -9,6 +10,7 @@ import { RenderBuffer } from './render_buffer';
import { ShaderManager } from './shaders';
import { EImageChannel, Texture } from './texture';
import { ASSERT } from './util/error_util';
import { LOG } from './util/log_util';
import { Vector3 } from './vector';
import { RenderMeshParams, RenderNextBlockMeshChunkParams, RenderNextVoxelMeshChunkParams } from './worker_types';

Expand Down Expand Up @@ -73,6 +75,7 @@ export class Renderer {
}>;
public _voxelBuffer?: twgl.BufferInfo[];
private _blockBuffer?: twgl.BufferInfo[];
private _blockBounds: Bounds = new Bounds(new Vector3(0, 0, 0), new Vector3(0, 0, 0));
private _debugBuffers: { [meshType: string]: { [bufferComponent: string]: RenderBuffer } };
private _axisBuffer: RenderBuffer;

Expand Down Expand Up @@ -351,6 +354,14 @@ export class Renderer {
public useBlockMeshChunk(params: RenderNextBlockMeshChunkParams.Output) {
if (params.isFirstChunk) {
this._blockBuffer = [];

const min = new Vector3(0, 0, 0);
min.setFrom(params.bounds['_min']);

const max = new Vector3(0, 0, 0);
max.setFrom(params.bounds['_max']);

this._blockBounds = new Bounds(min, max);
}

this._blockBuffer?.push(twgl.createBufferInfoFromArrays(this._gl, params.buffer.buffer));
Expand Down Expand Up @@ -485,6 +496,9 @@ export class Renderer {
u_atlasSize: this._atlasSize,
u_gridOffset: this._gridOffset.toArray(),
u_nightVision: this.isNightVisionEnabled(),
u_sliceHeight: ArcballCamera.Get.getSliceHeight(),
u_boundsMin: this._blockBounds.min.toArray(),
u_boundsMax: this._blockBounds.max.toArray(),
};
this._blockBuffer?.forEach((buffer) => {
this._gl.useProgram(shader.program);
Expand Down
18 changes: 16 additions & 2 deletions src/ui/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,23 @@ export class UI {
},
elementsOrder: ['grid', 'axes', 'night-vision'],
},

'sliceHeight': {
elements: {
'plus': new ToolbarItemElement({icon: 'plus'})
.onClick(() => {
// FIXME: check current value and limits
ArcballCamera.Get.setSliceHeight(ArcballCamera.Get.getSliceHeight() + 1);
}),
'minus': new ToolbarItemElement({icon: 'minus'})
.onClick(() => {
// FIXME: check current value and limits
ArcballCamera.Get.setSliceHeight(ArcballCamera.Get.getSliceHeight() - 1);
}),
},
elementsOrder: ['plus', 'minus'],
},
},
groupsOrder: ['viewmode', 'debug'],
groupsOrder: ['viewmode', 'debug', 'sliceHeight'],
};

private _toolbarRight = {
Expand Down
2 changes: 1 addition & 1 deletion src/worker_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class WorkerClient {

return {
buffer: buffer,
dimensions: this._loadedBlockMesh.getVoxelMesh().getBounds().getDimensions(),
bounds: this._loadedBlockMesh.getVoxelMesh().getBounds(),
atlasTexturePath: atlas.getAtlasTexturePath(),
atlasSize: atlas.getAtlasSize(),
moreBlocksToBuffer: buffer.moreBlocksToBuffer,
Expand Down
3 changes: 2 additions & 1 deletion src/worker_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TDithering } from './util/type_util';
import { Vector3 } from './vector';
import { TVoxelOverlapRule } from './voxel_mesh';
import { TVoxelisers } from './voxelisers/voxelisers';
import { Bounds } from './bounds';

export namespace InitParams {
export type Input = {
Expand Down Expand Up @@ -129,7 +130,7 @@ export namespace RenderNextBlockMeshChunkParams {

export type Output = {
buffer: TBlockMeshBufferDescription,
dimensions: Vector3,
bounds: Bounds,
atlasTexturePath: string,
atlasSize: number,
moreBlocksToBuffer: boolean,
Expand Down

0 comments on commit b5328d0

Please sign in to comment.