Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from JuliaPlots/sd/geometrybasics
Browse files Browse the repository at this point in the history
switch to GeometryBasics
  • Loading branch information
SimonDanisch authored Apr 15, 2020
2 parents 0f1b751 + 37e7bfe commit 578f3c8
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 103 deletions.
18 changes: 9 additions & 9 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
name = "WGLMakie"
uuid = "276b4fcb-3e11-5398-bf8b-a0c2d153d008"
authors = ["SimonDanisch <[email protected]>"]
version = "0.1.14"
version = "0.2.0"

[deps]
AbstractPlotting = "537997a7-5e4e-5d89-9595-2241ea00577e"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
GeometryTypes = "4d00f742-c7ba-57c2-abde-4428a4b178cb"
Hyperscript = "47d2ed2b-36de-50cf-bf87-49c2cf4b8b91"
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
JSServe = "824d6782-a2ef-11e9-3a09-e5662e0c26f9"
Expand All @@ -18,18 +17,19 @@ ShaderAbstractions = "65257c39-d410-5151-9873-9b3e5be5013e"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
AbstractPlotting = "0.9.27"
AbstractPlotting = "0.10.1"
Colors = "0.9, 0.10, 0.11, 0.12"
FileIO = "1.1"
GeometryBasics = "0.1"
GeometryTypes = "0.7, 0.8"
GeometryBasics = "0.2.3"
Hyperscript = "0.0.3"
ImageTransformations = "0.7, 0.8"
JSServe = "0.4, 0.5"
MakieGallery = "0.1.10"
Observables = "0.2, 0.3"
ShaderAbstractions = "0.1.1"
MakieGallery = "0.2"
MeshIO = "0.4"
JSServe = "0.6"
Observables = "0.3"
ShaderAbstractions = "0.2.1"
StaticArrays = "0.12, 0.1"
StatsMakie = "0.2"
julia = "1.0"

[extras]
Expand Down
4 changes: 2 additions & 2 deletions assets/line_segments.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ uniform mat4 modelViewMatrix;

#define AA_THICKNESS 2.0

vec2 screen_space(vec4 vertex)
vec2 screen_space(vec4 position)
{
return vec2(vertex.xy / vertex.w) * get_resolution();
return vec2(position.xy / position.w) * get_resolution();
}
vec3 tovec3(vec2 v){return vec3(v, 0.0);}
vec3 tovec3(vec3 v){return v;}
Expand Down
6 changes: 3 additions & 3 deletions assets/mesh.vert
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ vec4 tovec4(vec3 v){return vec4(v, 1.0);}
vec4 tovec4(vec4 v){return v;}

void main(){
// get_* gets the global inputs (uniform, sampler, vertex array)
// get_* gets the global inputs (uniform, sampler, position array)
// those functions will get inserted by the shader creation pipeline
vec3 vertex_position = tovec3(get_position());
vec3 lightpos = get_lightposition();
Expand All @@ -24,10 +24,10 @@ void main(){
frag_lightdir = normalize(lightpos - position_world.xyz);
// direction to camera
frag_position = -position_world.xyz;
frag_uv = get_texturecoordinates();
frag_uv = get_uv();
frag_uv = vec2(1.0 - frag_uv.y, frag_uv.x);
frag_color = tovec4(get_color());

// screen space coordinates of the vertex
// screen space coordinates of the position
gl_Position = projectionMatrix * viewMatrix * position_world;
}
9 changes: 6 additions & 3 deletions assets/particles.vert
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ void rotate(vec4 q, inout vec3 V, inout vec3 N){
vec4 to_vec4(vec3 v3){return vec4(v3, 1.0);}
vec4 to_vec4(vec4 v4){return v4;}

vec3 to_vec3(vec2 v3){return vec3(v3, 0.0);}
vec3 to_vec3(vec3 v4){return v4;}

void main(){
// get_* gets the global inputs (uniform, sampler, vertex array)
// get_* gets the global inputs (uniform, sampler, position array)
// those functions will get inserted by the shader creation pipeline
vec3 vertex_position = get_markersize() * get_position();
vec3 lightpos = vec3(20,20,20);
vec3 N = get_normals();
rotate(get_rotations(), vertex_position, N);
vertex_position = get_offset() + vertex_position;
vertex_position = to_vec3(get_offset()) + vertex_position;
vec4 position_world = modelMatrix * vec4(vertex_position, 1);
frag_normal = N;
frag_lightdir = normalize(lightpos - position_world.xyz);
frag_color = to_vec4(get_color());
// direction to camera
frag_position = -position_world.xyz;
// screen space coordinates of the vertex
// screen space coordinates of the position
gl_Position = projectionMatrix * viewMatrix * position_world;
}
9 changes: 6 additions & 3 deletions assets/simple.vert
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ float distancefield_scale(){
vec3 tovec3(vec2 v){return vec3(v, 0.0);}
vec3 tovec3(vec3 v){return v;}

vec4 tovec4(vec3 v){return vec4(v, 1.0);}
vec4 tovec4(vec4 v){return v;}

mat2 diagm(vec2 v){
return mat2(v.x, 0.0, 0.0, v.y);
}
Expand Down Expand Up @@ -102,10 +105,10 @@ void main(){
float sprite_from_u_scale = abs(get_markersize().x);
frag_uvscale = viewport_from_sprite_scale * sprite_from_u_scale;
frag_distancefield_scale = distancefield_scale();
frag_color = get_color();
frag_uv = get_texturecoordinates();
frag_color = tovec4(get_color());
frag_uv = get_uv();
frag_uv_offset_width = get_uv_offset_width();
// screen space coordinates of the vertex
// screen space coordinates of the position
vec4 quad_vertex = (trans * vec4(2.0 * bbox_signed_radius * get_position(), 0.0, 0.0));
gl_Position = vclip + quad_vertex;
}
2 changes: 1 addition & 1 deletion assets/volume.frag
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void main()
{
vec4 color;
vec3 eye_unit = vec3(modelinv * vec4(eyeposition, 1));
vec3 back_position = vec3(modelinv * vec4(frag_vert, 1));
vec3 back_position = frag_vert;
vec3 dir = normalize(eye_unit - back_position);
// solve back_position + distance * dir == 1
// solve back_position + distance * dir == 0
Expand Down
2 changes: 1 addition & 1 deletion assets/volume.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ uniform mat4 projectionMatrix, viewMatrix, modelMatrix;

void main()
{
frag_vert = position;
vec4 world_vert = modelMatrix * vec4(position, 1);
frag_vert = world_vert.xyz;
o_light_dir = vec3(modelinv * vec4(get_lightposition(), 1));
gl_Position = projectionMatrix * viewMatrix * world_vert;
}
8 changes: 4 additions & 4 deletions src/WGLMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module WGLMakie

using Hyperscript
using JSServe, Observables, AbstractPlotting
using GeometryTypes, Colors
using Colors, GeometryBasics
using ShaderAbstractions, LinearAlgebra
import GeometryBasics

Expand All @@ -14,8 +14,8 @@ using ShaderAbstractions: VertexArray, Buffer, Sampler, AbstractSampler
using ShaderAbstractions: InstancedProgram
import AbstractPlotting.FileIO
using StaticArrays
using GeometryBasics: decompose_uv

import GeometryTypes: GLNormalMesh, GLPlainMesh
using ImageTransformations

struct WebGL <: ShaderAbstractions.AbstractContext end
Expand Down Expand Up @@ -246,7 +246,8 @@ function three_display(session::Session, scene::Scene)
width, height = size(scene)
canvas = DOM.um("canvas", width = width, height = height)
comm = Observable(Dict{String, Any}())
threemod, renderer = JSObject(session, :THREE), JSObject(session, :renderer)
threemod = JSObject(session, THREE)
renderer = JSObject(session, :renderer)
window = JSObject(session, :window)
onload(session, canvas, js"""
function threejs_module(canvas){
Expand All @@ -267,7 +268,6 @@ function three_display(session::Session, scene::Scene)
renderer.setClearColor("#ff00ff");
renderer.setPixelRatio(ratio);
put_on_heap($(uuidstr(threemod)), $THREE);
put_on_heap($(uuidstr(renderer)), renderer);
put_on_heap($(uuidstr(window)), window);
Expand Down
47 changes: 27 additions & 20 deletions src/imagelike.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function draw_mesh(jsctx, jsscene, mscene::Scene, mesh, name, plot; uniforms...)
WebGL(),
lasset("mesh.vert"),
lasset("mesh.frag"),
VertexArray(mesh);
mesh;
uniforms...
)

Expand All @@ -46,28 +46,28 @@ function limits_to_uvmesh(plot)
rectangle = lift(px, py) do x, y
xmin, xmax = extrema(x)
ymin, ymax = extrema(y)
SimpleRectangle(xmin, ymin, xmax - xmin, ymax - ymin)
Rect2D(xmin, ymin, xmax - xmin, ymax - ymin)
end

positions = Buffer(lift(rectangle) do rect
ps = decompose(Point2f0, rect)
reinterpret(GeometryBasics.Point{2, Float32}, ps)
return decompose(Point2f0, rect)
end)

faces = Buffer(lift(rectangle) do rect
tris = decompose(GLTriangle, rect)
convert(Vector{GeometryBasics.TriangleFace{Cuint}}, tris)
end)
uv = Buffer(lift(rectangle) do rect
decompose(UV{Float32}, rect)
return decompose(GLTriangleFace, rect)
end)
vertices = GeometryBasics.meta(
positions; texturecoordinates = uv
)
mesh = GeometryBasics.Mesh(vertices, faces)

uv = Buffer(lift(decompose_uv, rectangle))

vertices = GeometryBasics.meta(positions; uv=uv)

return GeometryBasics.Mesh(vertices, faces)
end

function draw_js(jsctx, jsscene, mscene::Scene, plot::Surface)
# TODO OWN OPTIMIZED SHADER ... Or at least optimize this a bit more ...
px, py, pz = plot[1], plot[2], plot[3]

positions = Buffer(lift(px, py, pz) do x, y, z
vec(map(CartesianIndices(z)) do i
GeometryBasics.Point{3, Float32}(
Expand All @@ -77,26 +77,32 @@ function draw_js(jsctx, jsscene, mscene::Scene, plot::Surface)
)
end)
end)

faces = Buffer(lift(pz) do z
tris = decompose(GLTriangle, SimpleRectangle(0f0, 0f0, 1f0, 1f0), size(z))
convert(Vector{GeometryBasics.TriangleFace{Cuint}}, tris)
return decompose(GLTriangleFace, Rect2D(0f0, 0f0, 1f0, 1f0), size(z))
end)

uv = Buffer(lift(pz) do z
decompose(UV{Float32}, SimpleRectangle(0f0, 0f0, 1f0, 1f0), size(z))
decompose_uv(Rect2D(0f0, 0f0, 1f0, 1f0), size(z))
end)

pcolor = if haskey(plot, :color) && plot.color[] isa AbstractArray
plot.color
else
pz
end

color = Sampler(lift(
(args...)-> (array2color(args...)'),
pcolor, plot.colormap, plot.colorrange
))

normals = Buffer(lift(surface_normals, px, py, pz))

vertices = GeometryBasics.meta(
positions; texturecoordinates = uv, normals = normals
positions; uv=uv, normals=normals
)

mesh = GeometryBasics.Mesh(vertices, faces)

draw_mesh(jsctx, jsscene, mscene, mesh, "surface", plot;
Expand Down Expand Up @@ -138,7 +144,7 @@ end

function draw_js(jsctx, jsscene, mscene::Scene, plot::Volume)
x, y, z, vol = plot[1], plot[2], plot[3], plot[4]
box = ShaderAbstractions.VertexArray(GLPlainMesh(FRect3D(Vec3f0(0), Vec3f0(1))))
box = GeometryBasics.mesh(FRect3D(Vec3f0(0), Vec3f0(1)))
cam = cameracontrols(mscene)
model2 = lift(plot.model, x, y, z) do m, xyz...
mi = minimum.(xyz)
Expand All @@ -156,6 +162,7 @@ function draw_js(jsctx, jsscene, mscene::Scene, plot::Volume)
algorithm = lift(x-> Cuint(convert_attribute(x, key"algorithm"())), plot.algorithm)

eyepos = getfield(mscene.camera, :eyeposition)

lightposition = lift(plot.lightposition, eyepos, typ=Vec3f0) do pos, eyepos
ifelse(pos == :eyeposition, eyepos, pos)::Vec3f0
end
Expand All @@ -172,7 +179,7 @@ function draw_js(jsctx, jsscene, mscene::Scene, plot::Volume)
colorrange = lift(Vec2f0, plot.colorrange),
isovalue = lift(Float32, plot.isovalue),
isorange = lift(Float32, plot.isorange),
absorption = lift(Float32, plot.absorption),
absorption = lift(Float32, get(plot, :absorption, Observable(1f0))),

algorithm = algorithm,
eyeposition = eyepos,
Expand All @@ -191,6 +198,6 @@ function draw_js(jsctx, jsscene, mscene::Scene, plot::Volume)
on(model2) do model
three_geom.matrix.set((model')...)
end
three_geom.material.side = jsctx.FrontSide
three_geom.material.side = jsctx.BackSide
jsscene.add(three_geom)
end
17 changes: 11 additions & 6 deletions src/lines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ function create_shader(scene::Scene, plot::LineSegments)
end

uniforms[:resolution] = scene.camera.resolution
prim = GLUVMesh2D(
vertices = Vec2f0[(0, -1), (0, 1), (1, -1), (1, 1)],
texturecoordinates = UV{Float32}[(0,0), (0,0), (0,0), (0,0)],
faces = GLTriangle[(1, 2, 3), (2, 4, 3)]
instance = GeometryBasics.Mesh(
meta(Point2f0[(0, -1), (0, 1), (1, -1), (1, 1)], uv=Vec2f0[(0,0), (0,0), (0,0), (0,0)]),
GLTriangleFace[(1, 2, 3), (2, 4, 3)]
)
instance = VertexArray(prim)
return InstancedProgram(
WebGL(),
lasset("line_segments.vert"),
Expand Down Expand Up @@ -145,7 +143,14 @@ function jslines!(THREE, scene, plot, positions_nan, colors, linewidth, model, t
// position_buffer.needsUpdate = true;
}""")

geometry.computeBoundingSphere()
# ThreeJS does culling by calculating the boundingsphere
# But somehow it doesn't play well with our custom attributes.
# We just set it to a huge sphere, to not get them culled.
# Would be nice to just set them to null or so, but that's
# what triggers threejs to actually calculate the sphere
geometry.boundingSphere = THREE.new.Sphere()
geometry.boundingSphere.radius = 10000000000000f0

mesh = THREE.new.LineSegments(geometry, material)
mesh.matrixAutoUpdate = false;
mesh.matrix.set(model[]...)
Expand Down
Loading

2 comments on commit 578f3c8

@SimonDanisch
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/13021

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 578f3c8f0894821540ab29dd1ba7aff770393884
git push origin v0.2.0

Please sign in to comment.