Removed translation, added axis arrows

This commit is contained in:
Lucas Dower 2022-03-13 00:36:51 +00:00
parent d039258ea5
commit b96226cc4c
7 changed files with 129 additions and 21 deletions

View File

@ -1,7 +1,7 @@
uniform mat4 u_worldViewProjection;
uniform sampler2D u_texture;
uniform float u_voxelSize;
uniform vec3 u_translate;
uniform vec3 u_gridOffset;
attribute vec3 position;
attribute vec3 normal;
@ -22,5 +22,5 @@ void main() {
v_blockTexcoord = blockTexcoord;
v_lighting = dot(light, abs(normal));
gl_Position = u_worldViewProjection * vec4((position.xyz + vec3(0.5)) * u_voxelSize + u_translate, 1.0);
gl_Position = u_worldViewProjection * vec4((position.xyz + u_gridOffset) * u_voxelSize, 1.0);
}

View File

@ -2,7 +2,6 @@ uniform vec3 u_lightWorldPos;
uniform mat4 u_worldViewProjection;
uniform mat4 u_worldInverseTranspose;
uniform vec3 u_fillColour;
uniform vec3 u_translate;
attribute vec3 position;
attribute vec2 texcoord;
@ -15,5 +14,5 @@ void main() {
lighting = (clamp(lighting, 0.0, 1.0) * 0.66) + 0.33;
v_lighting = lighting;
gl_Position = u_worldViewProjection * vec4(position + u_translate, 1.0);
gl_Position = u_worldViewProjection * vec4(position, 1.0);
}

View File

@ -1,7 +1,6 @@
uniform vec3 u_lightWorldPos;
uniform mat4 u_worldViewProjection;
uniform mat4 u_worldInverseTranspose;
uniform vec3 u_translate;
attribute vec3 position;
attribute vec2 texcoord;
@ -17,5 +16,5 @@ void main() {
lighting = (clamp(lighting, 0.0, 1.0) * 0.66) + 0.33;
v_lighting = lighting;
gl_Position = u_worldViewProjection * vec4(position + u_translate, 1.0);
gl_Position = u_worldViewProjection * vec4(position, 1.0);
}

View File

@ -1,6 +1,6 @@
uniform mat4 u_worldViewProjection;
uniform float u_voxelSize;
uniform vec3 u_translate;
uniform vec3 u_gridOffset;
attribute vec3 position;
attribute vec3 normal;
@ -21,5 +21,5 @@ void main() {
v_texcoord = texcoord;
v_colour = colour;
gl_Position = u_worldViewProjection * vec4((position.xyz + vec3(0.5)) * u_voxelSize + u_translate, 1.0);
gl_Position = u_worldViewProjection * vec4((position.xyz + u_gridOffset) * u_voxelSize, 1.0);
}

View File

@ -2,6 +2,7 @@ import { m4, v3 } from 'twgl.js';
import { MouseManager } from './mouse';
import { degreesToRadians, clamp } from './math';
import { Renderer } from './renderer';
import { Vector3 } from './vector';
export class ArcballCamera {
public isUserRotating = false;

View File

@ -1,7 +1,7 @@
import * as twgl from 'twgl.js';
import { UVTriangle } from './triangle';
import { Vector3 } from './vector';
import { AttributeData } from './buffer';
import { AttributeData, RenderBuffer } from './buffer';
import { Bounds, RGB } from './util';
export class GeometryTemplates {
@ -60,7 +60,7 @@ export class GeometryTemplates {
}
export class DebugGeometryTemplates {
public static cross(centre: Vector3, radius: number, colour: RGB) {
public static cross(centre: Vector3, radius: number, colour: RGB): AttributeData {
return {
indices: new Uint32Array([0, 1, 2, 3, 4, 5]),
custom: {
@ -84,7 +84,7 @@ export class DebugGeometryTemplates {
};
}
public static line(start: Vector3, end: Vector3, colour: RGB) {
public static line(start: Vector3, end: Vector3, colour: RGB): AttributeData {
return {
indices: new Uint32Array([0, 1]),
custom: {
@ -100,7 +100,7 @@ export class DebugGeometryTemplates {
};
}
public static bounds(bounds: Bounds, colour: RGB, translate: Vector3 = new Vector3(0, 0, 0)) {
public static bounds(bounds: Bounds, colour: RGB, translate: Vector3 = new Vector3(0, 0, 0)): AttributeData {
return {
indices: new Uint32Array([
0, 1,
@ -140,4 +140,93 @@ export class DebugGeometryTemplates {
},
};
}
public static circle(centre: Vector3, normal: Vector3, radius: number, colour: RGB, steps: number = 8): AttributeData {
const indices = [];
const positions = [];
const colours = [];
const circlePoints = DebugGeometryTemplates._generateCirclePoints(centre, normal, radius, steps);
for (let i = 0; i < steps; ++i) {
const point = circlePoints[i];
positions.push(point.x, point.y, point.z);
indices.push(i, (i+1) % steps);
colours.push(colour.r, colour.g, colour.b);
}
return {
indices: new Uint32Array(indices),
custom: {
position: positions,
colour: colours,
},
};
}
public static cone(tipCentre: Vector3, tipHeight: number, normal: Vector3, radius: number, colour: RGB, quarterSteps: number) {
const indices = [];
const positions = [];
const colours = [];
const steps = quarterSteps * 4;
const circleCentre = Vector3.add(tipCentre, Vector3.mulScalar(normal.copy().normalise(), -tipHeight));
const circlePoints = DebugGeometryTemplates._generateCirclePoints(circleCentre, normal, radius, steps);
// Add circle data
for (let i = 0; i < steps; ++i) {
const point = circlePoints[i];
positions.push(point.x, point.y, point.z);
indices.push(i, (i+1) % steps);
colours.push(colour.r, colour.g, colour.b);
}
// Add cone tip
positions.push(tipCentre.x, tipCentre.y, tipCentre.z);
colours.push(colour.r, colour.g, colour.b);
const tipIndex = steps;
// Add cone lines
for (let i = 0; i < 4; ++i) {
const coneIndex = i * quarterSteps;
indices.push(tipIndex, coneIndex);
}
return {
indices: new Uint32Array(indices),
custom: {
position: positions,
colour: colours,
},
};
}
static _generateCirclePoints(centre: Vector3, normal: Vector3, radius: number, steps: number): Vector3[] {
normal = normal.copy().normalise();
const c = [{ i: 0, v: normal.x }, { i: 1, v: normal.y }, { i: 2, v: normal.z }];
{
let comps = c.sort((a, b) => {
return b.v - a.v;
}); // largest -> smallest
comps[2].v = 0;
const temp = comps[0].v;
comps[0].v = comps[1].v;
comps[1].v = temp;
comps = c.sort((a, b) => {
return a.i - b.i;
});
}
const aVec = new Vector3(c[0].v, c[1].v, c[2].v);
const bVec = Vector3.cross(normal, aVec);
aVec.normalise();
bVec.normalise();
const circlePoints: Vector3[] = [];
for (let i = 0; i < steps; ++i) {
const t = i / steps * Math.PI * 2;
const point = centre.copy()
.add(Vector3.mulScalar(aVec, radius * Math.cos(t)))
.add(Vector3.mulScalar(bVec, radius * Math.sin(t)));
circlePoints.push(point);
}
return circlePoints;
}
}

View File

@ -41,6 +41,8 @@ export class Renderer {
private _meshToUse: MeshType = MeshType.None;
private _voxelSize: number = 1.0;
private _translate: Vector3;
private _gridOffset: Vector3 = new Vector3(0, 0, 0);
private _modelsAvailable: number;
private _materialBuffers: Array<{
@ -168,7 +170,6 @@ export class Renderer {
}
this._translate = new Vector3(0, mesh.getBounds().getDimensions().y/2, 0);
ArcballCamera.Get.targetHeight = this._translate.y;
this._debugBuffer = this._setupDebugBuffer({
axis: true,
@ -194,9 +195,14 @@ export class Renderer {
this._voxelBuffer = voxelMesh.createBuffer();
this._voxelSize = voxelMesh?.getVoxelSize();
this._translate = new Vector3(0, voxelMesh.getBounds().getDimensions().y/2 * voxelMesh.getVoxelSize(), 0);
ArcballCamera.Get.targetHeight = this._translate.y;
// this._translate = new Vector3(0, voxelMesh.getBounds().getDimensions().y/2 * voxelMesh.getVoxelSize(), 0);
const dimensions = voxelMesh.getBounds().getDimensions();
this._gridOffset = new Vector3(
dimensions.x % 2 === 0 ? 0.5 : 0,
dimensions.y % 2 === 0 ? 0.5 : 0,
dimensions.z % 2 === 0 ? 0.5 : 0,
);
this._debugBuffer = this._setupDebugBuffer({
axis: true,
bounds: true,
@ -256,7 +262,6 @@ export class Renderer {
u_worldViewProjection: ArcballCamera.Get.getWorldViewProjection(),
u_worldInverseTranspose: ArcballCamera.Get.getWorldInverseTranspose(),
u_texture: materialBuffer.material.texture,
u_translate: this._translate.toArray(),
});
} else {
this._drawRegister(materialBuffer.buffer, ShaderManager.Get.solidTriProgram, {
@ -264,7 +269,6 @@ export class Renderer {
u_worldViewProjection: ArcballCamera.Get.getWorldViewProjection(),
u_worldInverseTranspose: ArcballCamera.Get.getWorldInverseTranspose(),
u_fillColour: materialBuffer.material.colour.toArray(),
u_translate: this._translate.toArray(),
});
}
}
@ -274,7 +278,7 @@ export class Renderer {
this._drawRegister(this._voxelBuffer, ShaderManager.Get.voxelProgram, {
u_worldViewProjection: ArcballCamera.Get.getWorldViewProjection(),
u_voxelSize: this._voxelSize,
u_translate: this._translate.toArray(),
u_gridOffset: this._gridOffset.toArray(),
});
}
@ -284,7 +288,7 @@ export class Renderer {
u_texture: this._atlasTexture,
u_voxelSize: this._voxelSize,
u_atlasSize: BlockAtlas.Get.getAtlasSize(),
u_translate: this._translate.toArray(),
u_gridOffset: this._gridOffset.toArray(),
});
}
@ -403,13 +407,29 @@ export class Renderer {
buffer.add(DebugGeometryTemplates.line(
new Vector3(-gridRadius, 0, 0),
new Vector3(gridRadius, 0, 0),
(new RGB(0.44, 0.64, 0.11)),
new RGB(0.44, 0.64, 0.11),
));
buffer.add(DebugGeometryTemplates.cone(
new Vector3(gridRadius, 0, 0),
0.5,
new Vector3(1, 0, 0),
0.1,
new RGB(0.44, 0.64, 0.11),
8,
));
buffer.add(DebugGeometryTemplates.line(
new Vector3(0, 0, -gridRadius),
new Vector3(0, 0, gridRadius),
new RGB(0.96, 0.21, 0.32)),
);
buffer.add(DebugGeometryTemplates.cone(
new Vector3(0, 0, gridRadius),
0.5,
new Vector3(0, 0, 1),
0.1,
new RGB(0.96, 0.21, 0.32),
8,
));
}
if (settings.bounds) {