materials, brick and nodes structure, preparing for world generation
This commit is contained in:
Binary file not shown.
@@ -1,28 +1,9 @@
|
||||
|
||||
/*
|
||||
point: Hit point
|
||||
view: View vector (camera ray)
|
||||
normal: Normal vector
|
||||
*/
|
||||
vec3 solve(vec3 hitPoint, vec3 view, vec3 normal)
|
||||
{
|
||||
// Lighting
|
||||
vec3 light_color = vec3(1.0, 1.0, 1.0);
|
||||
vec3 light_dir = normalize(hitPoint-vec3(0.0, 1.5, 4.0));
|
||||
|
||||
vec3 ambient = light_color * 0.1;
|
||||
vec3 diffuse = light_color * max(1.0-dot(normal, light_dir), 0.0);
|
||||
vec3 specular = light_color * pow(max(dot(view, reflect(-light_dir, normal)), 0.0), 32);
|
||||
|
||||
// Combine colors
|
||||
vec3 object_color = vec3(0.0, 0.1, 0.5);
|
||||
return (ambient + diffuse + specular) * object_color;
|
||||
}
|
||||
|
||||
/*
|
||||
* origin: Point hit on the node
|
||||
* dir: Camera view direction (ray direction)
|
||||
* addr: Number given by node, afaik brick offset in brick array
|
||||
* incAxis: Last axis that was used to increment position
|
||||
* addr: Number given by node, brick offset in brick array
|
||||
*/
|
||||
vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
|
||||
{
|
||||
@@ -38,14 +19,12 @@ vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
|
||||
vec3 rayInv = 1.0/dir;
|
||||
vec3 raySign = sign(dir);
|
||||
vec3 dist = (pos-origin+0.5 + raySign*0.5) * rayInv;
|
||||
|
||||
ivec3 offset = ivec3(0, 0, (addr-1)*32);
|
||||
|
||||
vec3 tmp = abs(raySign);
|
||||
for(int i=0;i<110;i++)
|
||||
{
|
||||
// Get brick
|
||||
uint brick = texelFetch(bricksTexture, offset+ivec3(pos), 0).r;
|
||||
uint brick = texelFetch(_BricksTexture, offset+ivec3(pos), 0).r;
|
||||
if(brick != 0)
|
||||
{
|
||||
// Hit point
|
||||
@@ -55,7 +34,7 @@ vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
|
||||
|
||||
// Normal vector (negate previous increment axis and mult by sign)
|
||||
vec3 normal = -incAxis * raySign;
|
||||
return vec4(solve(hitPoint, dir, normal), 1.0);
|
||||
return vec4(solve(hitPoint, dir, normal, brick-1), 1.0);
|
||||
}
|
||||
|
||||
// Get new closest axis to increment
|
||||
@@ -68,7 +47,7 @@ vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
|
||||
if(pos.x > 31 || pos.y > 31 || pos.z > 31) { break; }
|
||||
}
|
||||
|
||||
return vec4(0.5,0.5,0.5, 0.0);
|
||||
return vec4(1.0,0.0,0.5,0.0);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -93,16 +72,13 @@ vec3 castNodes(vec3 origin, vec3 dir)
|
||||
for(int i=0;i<110;i++)
|
||||
{
|
||||
// Get node
|
||||
uint node = texelFetch(nodesTexture, ivec3(pos), 0).r;
|
||||
uint node = texelFetch(_NodesTexture, ivec3(pos), 0).r;
|
||||
if(node != 0)
|
||||
{
|
||||
// Hit point
|
||||
vec3 mini = (pos-origin+0.5 - raySign*0.5) * rayInv;
|
||||
float len = max(0.0, max(mini.x, max(mini.y, mini.z)));
|
||||
vec3 hitPoint = origin + dir * len;
|
||||
// Normal vector (negate previous increment axis and mult by sign)
|
||||
//vec3 normal = -incAxis * raySign;
|
||||
//return solve(hitPoint, dir, normal);
|
||||
|
||||
// Cast brick
|
||||
vec4 result = castBricks(pos, hitPoint, dir, incAxis, node);
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
|
||||
// Returns nearest distance to and object id from given point
|
||||
vec2 map(in vec3 pos)
|
||||
{
|
||||
vec2 d1 = vec2(sdPlane(pos), 1);
|
||||
vec2 d2 = vec2(sdSphere(pos - vec3(0.0, 2.0, 5.0)), 2);
|
||||
vec2 d3 = vec2(sdSphere(pos - vec3(0.0, 3.0, 3.0)), 3);
|
||||
return sdUnion(sdUnion(d1, d2), d3);
|
||||
}
|
||||
|
||||
// Returns distance to and object id that has been intersected by ray
|
||||
vec2 intersect(in vec3 ro, in vec3 rd)
|
||||
{
|
||||
float depth = NEAR;
|
||||
for(int i=0;i<MAX_STEPS;i++)
|
||||
{
|
||||
// Travel through map
|
||||
vec2 dist = map(ro + rd * depth);
|
||||
if(dist.x < EPSILON)
|
||||
{
|
||||
// Hit something
|
||||
return vec2(depth, dist.y);
|
||||
}
|
||||
|
||||
// Move further
|
||||
depth += dist.x;
|
||||
}
|
||||
|
||||
// Nothing found
|
||||
return vec2(0);
|
||||
}
|
||||
|
||||
vec3 estimateNormal(vec3 p)
|
||||
{
|
||||
return normalize(vec3(
|
||||
map(vec3(p.x + EPSILON, p.y, p.z)).x - map(vec3(p.x - EPSILON, p.y, p.z)).x,
|
||||
map(vec3(p.x, p.y + EPSILON, p.z)).x - map(vec3(p.x, p.y - EPSILON, p.z)).x,
|
||||
map(vec3(p.x, p.y, p.z + EPSILON)).x - map(vec3(p.x, p.y, p.z - EPSILON)).x
|
||||
));
|
||||
}
|
||||
|
||||
vec3 raymarch(in vec3 ro, in vec3 rd)
|
||||
{
|
||||
// Light dir
|
||||
vec3 ld = normalize(vec3(-0.5, 2.0, -0.2));
|
||||
|
||||
vec2 result = intersect(ro, rd);
|
||||
if(result.y > 0.0)
|
||||
{
|
||||
// Calculate intersection point, normal vector and reflection vector
|
||||
vec3 pos = ro + rd * result.x;
|
||||
vec3 nor = estimateNormal(pos);
|
||||
vec3 ref = reflect(rd, nor);
|
||||
|
||||
// Light
|
||||
vec3 light = vec3(1);
|
||||
|
||||
float dif = clamp( dot( nor, ld ), 0.0, 1.0 );
|
||||
light += 2.20*dif*vec3(1.30,1.00,0.70);
|
||||
|
||||
vec3 hal = normalize( ld-rd );
|
||||
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0);
|
||||
spe *= dif;
|
||||
spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,ld),0.0,1.0),5.0);
|
||||
light += 5.00*spe*vec3(1.30,1.00,0.70);
|
||||
|
||||
//float diffuse = max(dot(norm, -ld), 0);
|
||||
//color *= diffuse;
|
||||
|
||||
//vec3 ldr = normalize(reflect(-ld, norm));
|
||||
//float specular = max(dot(ldr, rd), 0.0);
|
||||
//specular = pow(specular, 3);
|
||||
//color += vec3(0.2) * specular;
|
||||
|
||||
//float scatter = pow(1.0-dot(rd, -norm), 2);
|
||||
|
||||
// Calculate color
|
||||
vec3 color = vec3(0);
|
||||
if(result.y == 1) color = vec3(0.0, 0.1, 0.01);
|
||||
else if(result.y == 2) color = vec3(0.5, 0.7, 0.05);
|
||||
else if(result.y == 3) color = vec3(0.0, 0.2, 0.7);
|
||||
|
||||
// Return
|
||||
color *= light;
|
||||
return color;
|
||||
}
|
||||
else
|
||||
{
|
||||
return vec3(0);
|
||||
}
|
||||
}
|
||||
+19
-16
@@ -1,38 +1,41 @@
|
||||
// shader.frag
|
||||
#version 450
|
||||
|
||||
struct Material {
|
||||
vec4 albedo;
|
||||
};
|
||||
|
||||
// Variables
|
||||
layout(location=0) in vec2 vert_uv;
|
||||
layout(location=0) out vec4 out_color;
|
||||
|
||||
layout(set=0, binding=0) uniform Uniforms
|
||||
{
|
||||
mat4 _viewMatrix;
|
||||
mat4 _projMatrixInv;
|
||||
layout(set=0,binding=0) uniform Uniforms {
|
||||
mat4 _ViewMatrix;
|
||||
mat4 _ProjMatrixInv;
|
||||
vec3 _CamPos;
|
||||
float _Time;
|
||||
};
|
||||
|
||||
//layout(set = 1, binding = 0) uniform texture3D node_tex;
|
||||
//layout(set = 1, binding = 1) uniform usampler3D node_sampl;
|
||||
layout(set=1,binding=0) uniform usampler3D nodesTexture;
|
||||
layout(set=2,binding=0) uniform usampler3D bricksTexture;
|
||||
layout(set=1,binding=0) uniform usampler3D _NodesTexture;
|
||||
layout(set=2,binding=0) uniform usampler3D _BricksTexture;
|
||||
layout(set=3, binding=0) uniform Materials {
|
||||
Material _Materials[256];
|
||||
};
|
||||
|
||||
// Defines
|
||||
#define PI 3.141592
|
||||
#define EPSILON 0.01
|
||||
#define NEAR 0.01
|
||||
#define MAX_STEPS 200
|
||||
// #define PI 3.141592
|
||||
// #define EPSILON 0.01
|
||||
// #define NEAR 0.01
|
||||
|
||||
// Include
|
||||
#include "shapes.cginc"
|
||||
#include "shading.cginc"
|
||||
#include "raycasting.cginc"
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate ray direction
|
||||
vec3 view_dir = (_projMatrixInv * vec4(vert_uv, 0, 1)).xyz;
|
||||
vec3 ray_dir = normalize(_viewMatrix * vec4(view_dir,0)).xyz;
|
||||
vec3 view_dir = (_ProjMatrixInv * vec4(vert_uv, 0, 1)).xyz;
|
||||
vec3 ray_dir = normalize(_ViewMatrix * vec4(view_dir,0)).xyz;
|
||||
|
||||
// Raycast
|
||||
out_color = vec4(castNodes(_CamPos, ray_dir), 1.0);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
point: Hit point
|
||||
view: View vector (camera ray)
|
||||
normal: Normal vector
|
||||
*/
|
||||
vec3 solve(vec3 hitPoint, vec3 view, vec3 normal, uint brick)
|
||||
{
|
||||
// Lighting
|
||||
vec3 light_color = vec3(1.0, 1.0, 1.0);
|
||||
vec3 light_dir = normalize(hitPoint-vec3(0.0, 1.5, 4.0));
|
||||
|
||||
vec3 ambient = light_color * 0.1;
|
||||
vec3 diffuse = light_color * max(1.0-dot(normal, light_dir), 0.0);
|
||||
vec3 specular = light_color * pow(max(dot(view, reflect(-light_dir, normal)), 0.0), 32);
|
||||
|
||||
// Combine colors
|
||||
vec3 object_color = _Materials[brick].albedo.rgb;//vec3(0.0, 0.1, 0.5);
|
||||
return (ambient + diffuse + specular) * object_color;
|
||||
}
|
||||
Reference in New Issue
Block a user