Add minimal dungeon drawing code
This commit is contained in:
parent
00bde7a283
commit
29c6b14440
11 changed files with 352 additions and 111 deletions
|
|
@ -2,134 +2,137 @@
|
|||
|
||||
precision mediump float;
|
||||
|
||||
const float smoothing = 10.0;
|
||||
const float inf = 1000000.0;
|
||||
const float pi = atan(1.0) * 4.0;
|
||||
|
||||
#define INFINITY 1.0 / 0.0
|
||||
#define WORLD_SIZE 4
|
||||
#define N (20)
|
||||
float interpolate(float from, float to, float quotient) {
|
||||
float steppedQ = sin(quotient * pi - pi * 0.5) * 0.5 + 0.5;
|
||||
return from + (to - from) * clamp(steppedQ, 0.0, 1.0);
|
||||
}
|
||||
|
||||
#define CIRCLE 1
|
||||
#define LINE 2
|
||||
#define TRIANGLE 3
|
||||
|
||||
out vec4 fragmentColor;
|
||||
uniform vec2 resolution;
|
||||
uniform float time;
|
||||
|
||||
|
||||
struct Material {
|
||||
vec3 color;
|
||||
};
|
||||
|
||||
struct Primitive {
|
||||
uint type;
|
||||
uint index;
|
||||
};
|
||||
|
||||
struct Object {
|
||||
Material material;
|
||||
Primitive[10] primitives;
|
||||
};
|
||||
|
||||
struct Circle {
|
||||
uint parent;
|
||||
vec2 center;
|
||||
float radius;
|
||||
};
|
||||
vec2 rotate90deg(in vec2 vector) {
|
||||
return vec2(-vector.y, vector.x);
|
||||
}
|
||||
|
||||
struct Line {
|
||||
uint parent;
|
||||
vec2 a;
|
||||
vec2 b;
|
||||
};
|
||||
vec2 normal;
|
||||
bool isLineEnd;
|
||||
}[16] lines;
|
||||
|
||||
struct Triangle {
|
||||
uint parent;
|
||||
vec2 a;
|
||||
vec2 b;
|
||||
vec2 c;
|
||||
};
|
||||
float noise(float x){
|
||||
return fract(sin(x) * 43758.5453123);
|
||||
}
|
||||
|
||||
// uniform Object[100] objects;
|
||||
float terrain(float x) {
|
||||
float result = 0.0;
|
||||
|
||||
Circle[3] circles;
|
||||
Line[1] lines;
|
||||
Triangle[1] triangles;
|
||||
float frequency = 0.01;
|
||||
float amplitude = 1.0;
|
||||
|
||||
float triangleDistance(in vec2 position, in Triangle triangle)
|
||||
{
|
||||
vec2 e0 = triangle.b - triangle.a, e1 = triangle.c - triangle.b, e2 = triangle.a - triangle.c;
|
||||
vec2 v0 = position - triangle.a, v1 = position - triangle.b, v2 = position - triangle.c;
|
||||
vec2 pq0 = v0 - e0 * clamp(dot(v0, e0) / dot(e0, e0), 0.0, 1.0);
|
||||
vec2 pq1 = v1 - e1 * clamp(dot(v1, e1) / dot(e1, e1), 0.0, 1.0);
|
||||
vec2 pq2 = v2 - e2 * clamp(dot(v2, e2) / dot(e2, e2), 0.0, 1.0);
|
||||
float s = sign(e0.x*e2.y - e0.y*e2.x);
|
||||
vec2 d = min(min(vec2(dot(pq0,pq0), s*(v0.x*e0.y-v0.y*e0.x)),
|
||||
vec2(dot(pq1,pq1), s*(v1.x*e1.y-v1.y*e1.x))),
|
||||
vec2(dot(pq2,pq2), s*(v2.x*e2.y-v2.y*e2.x)));
|
||||
return -sqrt(d.x)*sign(d.y);
|
||||
}
|
||||
for (int i = 0; i < 8; i++) {
|
||||
result += sin(2.0 * pi * x * frequency - 2.0 * pi * noise(float(i) * 200.0)) * amplitude;
|
||||
frequency *= 1.5;
|
||||
amplitude /= 1.2;
|
||||
}
|
||||
|
||||
float lineDistance(in vec2 position, in Line line)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
float lineDistance(in vec2 position, in Line line, out float h) {
|
||||
vec2 pa = position - line.a, ba = line.b - line.a;
|
||||
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
|
||||
return length(pa - ba*h);
|
||||
h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
|
||||
vec2 delta = pa - ba*h;
|
||||
// sign can return 0, double sign prevents this
|
||||
float side = sign(sign(dot(delta, line.normal)) - 0.5);
|
||||
return length(delta) * side + terrain(length(ba * h));
|
||||
}
|
||||
|
||||
float circleDistance(in vec2 position, in Circle circle)
|
||||
{
|
||||
return length(position - circle.center) - circle.radius;
|
||||
Line endDummyLineFromLine(Line line) {
|
||||
return Line(line.b, line.b + rotate90deg(line.normal), line.normal, false);
|
||||
}
|
||||
|
||||
float getDistance(in vec2 target, out uint nearestParentIndex) {
|
||||
float distance = INFINITY;
|
||||
|
||||
for (int i = 0; i < circles.length(); i++) {
|
||||
float distanceToCurrent = circleDistance(target, circles[i]);
|
||||
if (distanceToCurrent < distance) {
|
||||
distance = distanceToCurrent;
|
||||
nearestParentIndex = circles[i].parent;
|
||||
}
|
||||
}
|
||||
float getDistance(in vec2 target) {
|
||||
float minDistance = inf;
|
||||
|
||||
float leftJoinAcuteness = 0.0;
|
||||
vec2 splitterLineNormalStart = vec2(-1.0, 0.0);
|
||||
bool skipDistanceToPrevious = true;
|
||||
for (int i = 0; i < lines.length(); i++) {
|
||||
float distanceToCurrent = lineDistance(target, lines[i]);
|
||||
if (distanceToCurrent < distance) {
|
||||
distance = distanceToCurrent;
|
||||
nearestParentIndex = lines[i].parent;
|
||||
Line current = lines[i];
|
||||
|
||||
Line next;
|
||||
if (current.isLineEnd || i + 1 == lines.length()) {
|
||||
next = endDummyLineFromLine(current);
|
||||
} else {
|
||||
next = lines[i + 1];
|
||||
}
|
||||
|
||||
vec2 splitterLineNormalEnd = rotate90deg(normalize(current.normal + next.normal));
|
||||
|
||||
float h;
|
||||
float distanceToCurrent = lineDistance(target, current, h);
|
||||
float rightJoinAcuteness = dot(next.b - current.a, next.normal - current.normal);
|
||||
distanceToCurrent -= interpolate(
|
||||
sign(leftJoinAcuteness) * smoothing,
|
||||
sign(rightJoinAcuteness) * smoothing, h
|
||||
);
|
||||
leftJoinAcuteness = rightJoinAcuteness;
|
||||
|
||||
if (
|
||||
!(
|
||||
dot(target - current.a, splitterLineNormalStart * -sign(dot(current.b - current.a, splitterLineNormalStart))) > 0.0
|
||||
|| dot(target - current.b, splitterLineNormalEnd * sign(dot(current.a - current.b, splitterLineNormalEnd))) <= 0.0
|
||||
) && abs(distanceToCurrent) < abs(minDistance)
|
||||
) {
|
||||
minDistance = distanceToCurrent;
|
||||
}
|
||||
splitterLineNormalStart = splitterLineNormalEnd;
|
||||
}
|
||||
|
||||
for (int i = 0; i < triangles.length(); i++) {
|
||||
float distanceToCurrent = triangleDistance(target, triangles[i]);
|
||||
if (distanceToCurrent < distance) {
|
||||
distance = distanceToCurrent;
|
||||
nearestParentIndex = triangles[i].parent;
|
||||
}
|
||||
}
|
||||
|
||||
return distance;
|
||||
return minDistance;
|
||||
}
|
||||
|
||||
void createWorld() {
|
||||
circles[0] = Circle(0u, vec2(250.0, 100.0), 12.5);
|
||||
circles[1] = Circle(0u, vec2(150.0, 50.0), 32.5);
|
||||
circles[2] = Circle(0u, vec2(300.0, 350.0), 52.5);
|
||||
lines[0] = Line(vec2(0.0, 300.0), vec2(550.0, 140.0), vec2(1.0), false);
|
||||
lines[1] = Line(vec2(550.0, 140.0), vec2(750.0, 130.0), vec2(1.0), false);
|
||||
lines[2] = Line(vec2(750.0, 130.0), vec2(650.0, 230.0), vec2(1.0), false);
|
||||
lines[3] = Line(vec2(650.0, 230.0), vec2(850.0, 230.0), vec2(1.0), false);
|
||||
lines[4] = Line(vec2(850.0, 230.0), vec2(800.0, 150.0), vec2(1.0), false);
|
||||
lines[5] = Line(vec2(800.0, 150.0), vec2(1000.0, 120.0), vec2(1.0), false);
|
||||
lines[6] = Line(vec2(1000.0, 120.0), vec2(1150, 120.0), vec2(1.0), false);
|
||||
lines[7] = Line(vec2(1150, 120.0), vec2(10200, 350.0), vec2(1.0), true);
|
||||
lines[8] = Line(vec2(0.0, 600.0), vec2(550.0, 440.0), vec2(-1.0), false);
|
||||
lines[9] = Line(vec2(550.0, 440.0), vec2(750.0, 430.0), vec2(-1.0), false);
|
||||
lines[10] = Line(vec2(750.0, 430.0), vec2(650.0, 530.0), vec2(-1.0), false);
|
||||
lines[11] = Line(vec2(650.0, 530.0), vec2(850.0, 530.0), vec2(-1.0), false);
|
||||
lines[12] = Line(vec2(850.0, 530.0), vec2(820.0, 450.0), vec2(-1.0), false);
|
||||
lines[13] = Line(vec2(820.0, 450.0), vec2(1000.0, 420.0), vec2(-1.0), false);
|
||||
lines[14] = Line(vec2(1000.0, 420.0), vec2(1150, 420.0), vec2(-1.0), false);
|
||||
lines[15] = Line(vec2(1150, 420.0), vec2(10200, 650.0), vec2(-1.0), true);
|
||||
|
||||
lines[0] = Line(0u, vec2(100.0, 300.0), vec2(550.0, 140.0));
|
||||
|
||||
triangles[0] = Triangle(0u, vec2(400.0, 100.0), vec2(200.0, 240.0), vec2(600.0, 340.0));
|
||||
for (int i = 0; i < lines.length(); i++) {
|
||||
vec2 tangent = lines[i].b - lines[i].a;
|
||||
lines[i].normal = normalize(
|
||||
vec2(-lines[i].normal.x * tangent.y, lines[i].normal.x * tangent.x)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
float linearstep(float a, float b, float q) {
|
||||
return a + clamp((b - a) * q, 0.0, 1.0);
|
||||
}
|
||||
|
||||
uniform vec2 cameraPosition;
|
||||
uniform vec2 viewBoxSize;
|
||||
uniform vec2 resolution;
|
||||
|
||||
out vec4 fragmentColor;
|
||||
|
||||
void main() {
|
||||
createWorld();
|
||||
|
||||
vec2 position = gl_FragCoord.xy + vec2(0.5);
|
||||
uint index;
|
||||
vec3 color = vec3(1.0) * linearstep(0.0, 1.0, getDistance(position, index));
|
||||
fragmentColor = vec4(color, 1.0);
|
||||
|
||||
vec2 pixelPosition = gl_FragCoord.xy + vec2(0.5);
|
||||
vec2 position = pixelPosition / resolution * viewBoxSize + cameraPosition;
|
||||
|
||||
fragmentColor = vec4(vec3(1.0) * clamp(0.0, 1.0, getDistance(position)), 1.0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue