Skip to content

Commit

Permalink
Add thick lines example
Browse files Browse the repository at this point in the history
Vendor the thick lines code from three.js (r97), and expose to kernel.
  • Loading branch information
vidartf committed Oct 1, 2018
1 parent a1579f9 commit d77a75b
Show file tree
Hide file tree
Showing 17 changed files with 1,485 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/Geometries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.6"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
368 changes: 368 additions & 0 deletions examples/LineGeometry.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,368 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Line Geometry"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Three.js has some example code for thick lines via an instance-based geometry. Since WebGL does not guarantee support for line thickness greater than 1 for GL lines, pytheejs includes these objects."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pythreejs import *\n",
"from IPython.display import display\n",
"from ipywidgets import VBox, HBox, Checkbox, jslink\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g1 = BufferGeometry(\n",
" attributes={\n",
" 'position': BufferAttribute(np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [4, 4, 4]], dtype=np.float32), normalized=False),\n",
" 'color': BufferAttribute(np.array([[1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float32), normalized=False),\n",
" },\n",
")\n",
"m1 = LineBasicMaterial(vertexColors='VertexColors', linewidth=10)\n",
"line1 = LineSegments(g1, m1);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g2 = LineSegmentsGeometry(\n",
" positions=[\n",
" [[0, 0, 0], [1, 1, 1]],\n",
" [[2, 2, 2], [4, 4, 4]]\n",
" ],\n",
" colors=[\n",
" [[1, 0, 0], [1, 0, 0]],\n",
" [[0, 1, 0], [0, 0, 1]]\n",
" ],\n",
")\n",
"m2 = LineMaterial(linewidth=10, vertexColors='VertexColors')\n",
"line2 = LineSegments2(g2, m2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"view_width = 600\n",
"view_height = 400\n",
"camera = PerspectiveCamera(position=[10, 0, 0], aspect=view_width/view_height)\n",
"key_light = DirectionalLight(position=[0, 10, 10])\n",
"ambient_light = AmbientLight()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scene = Scene(children=[line1, line2, camera, key_light, ambient_light])\n",
"controller = OrbitControls(controlling=camera, screenSpacePanning=False)\n",
"renderer = Renderer(camera=camera, scene=scene, controls=[controller],\n",
" width=view_width, height=view_height)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chks = [Checkbox(True, description='GL line'), Checkbox(True, description='Fat line')]\n",
"jslink((chks[0], 'value'), (line1, 'visible'))\n",
"jslink((chks[1], 'value'), (line2, 'visible'))\n",
"VBox([renderer, HBox(chks)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"posInstBuffer = InstancedInterleavedBuffer( np.array([[0, 0, 0, 1, 1, 1], [2, 2, 2, 4, 4, 4]], dtype=np.float32))\n",
"colInstBuffer = InstancedInterleavedBuffer( np.array([[1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1]], dtype=np.float32))\n",
"dbgG = InstancedBufferGeometry(attributes={\n",
" 'position': BufferAttribute(np.array([ [- 1, 2, 0], [1, 2, 0], [- 1, 1, 0], [1, 1, 0], [- 1, 0, 0], [1, 0, 0], [- 1, - 1, 0], [1, - 1, 0] ], dtype=np.float32)),\n",
" 'uv': BufferAttribute(np.array([ [- 1, 2], [1, 2], [- 1, 1], [1, 1], [- 1, - 1], [1, - 1], [- 1, - 2], [1, - 2] ], dtype=np.float32)),\n",
" 'index': BufferAttribute(np.array([ 0, 2, 1, 2, 3, 1, 2, 4, 3, 4, 5, 3, 4, 6, 5, 6, 7, 5 ], dtype=np.uint8)),\n",
" 'instanceStart': InterleavedBufferAttribute(posInstBuffer, 3, 0),\n",
" 'instanceEnd': InterleavedBufferAttribute(posInstBuffer, 3, 3),\n",
" 'instanceColorStart': InterleavedBufferAttribute(colInstBuffer, 3, 0),\n",
" 'instanceColorEnd': InterleavedBufferAttribute(colInstBuffer, 3, 3),\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = ShaderMaterial(\n",
" vertexShader='''\n",
"#include <common>\n",
"#include <color_pars_vertex>\n",
"#include <fog_pars_vertex>\n",
"#include <logdepthbuf_pars_vertex>\n",
"#include <clipping_planes_pars_vertex>\n",
"\n",
"uniform float linewidth;\n",
"uniform vec2 resolution;\n",
"\n",
"attribute vec3 instanceStart;\n",
"attribute vec3 instanceEnd;\n",
"\n",
"attribute vec3 instanceColorStart;\n",
"attribute vec3 instanceColorEnd;\n",
"\n",
"varying vec2 vUv;\n",
"\n",
"void trimSegment( const in vec4 start, inout vec4 end ) {\n",
"\n",
" // trim end segment so it terminates between the camera plane and the near plane\n",
"\n",
" // conservative estimate of the near plane\n",
" float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column\n",
" float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column\n",
" float nearEstimate = - 0.5 * b / a;\n",
"\n",
" float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );\n",
"\n",
" end.xyz = mix( start.xyz, end.xyz, alpha );\n",
"\n",
"}\n",
"\n",
"void main() {\n",
"\n",
" #ifdef USE_COLOR\n",
"\n",
" vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;\n",
"\n",
" #endif\n",
" \n",
" float aspect = resolution.x / resolution.y;\n",
"\n",
" vUv = uv;\n",
" \n",
" // camera space\n",
" vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );\n",
" vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );\n",
"\n",
" // special case for perspective projection, and segments that terminate either in, or behind, the camera plane\n",
" // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space\n",
" // but we need to perform ndc-space calculations in the shader, so we must address this issue directly\n",
" // perhaps there is a more elegant solution -- WestLangley\n",
"\n",
" bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column\n",
"\n",
" if ( perspective ) {\n",
"\n",
" if ( start.z < 0.0 && end.z >= 0.0 ) {\n",
"\n",
" trimSegment( start, end );\n",
"\n",
" } else if ( end.z < 0.0 && start.z >= 0.0 ) {\n",
"\n",
" trimSegment( end, start );\n",
"\n",
" }\n",
"\n",
" }\n",
"\n",
" // clip space\n",
" vec4 clipStart = projectionMatrix * start;\n",
" vec4 clipEnd = projectionMatrix * end;\n",
"\n",
" // ndc space\n",
" vec2 ndcStart = clipStart.xy / clipStart.w;\n",
" vec2 ndcEnd = clipEnd.xy / clipEnd.w;\n",
"\n",
" // direction\n",
" vec2 dir = ndcEnd - ndcStart;\n",
"\n",
" // account for clip-space aspect ratio\n",
" dir.x *= aspect;\n",
" dir = normalize( dir );\n",
"\n",
" // perpendicular to dir\n",
" vec2 offset = vec2( dir.y, - dir.x );\n",
"\n",
" // undo aspect ratio adjustment\n",
" dir.x /= aspect;\n",
" offset.x /= aspect;\n",
"\n",
" // sign flip\n",
" if ( position.x < 0.0 ) offset *= - 1.0;\n",
"\n",
" // endcaps\n",
" if ( position.y < 0.0 ) {\n",
"\n",
" offset += - dir;\n",
"\n",
" } else if ( position.y > 1.0 ) {\n",
"\n",
" offset += dir;\n",
"\n",
" }\n",
"\n",
" // adjust for linewidth\n",
" offset *= linewidth;\n",
" \n",
" // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...\n",
" offset /= resolution.y;\n",
"\n",
" // select end\n",
" vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;\n",
"\n",
" // back to clip space\n",
" offset *= clip.w;\n",
"\n",
" clip.xy += offset;\n",
"\n",
" gl_Position = clip;\n",
" \n",
" //gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n",
" \n",
" //if ( instanceStart.x > 1.5) {\n",
" // gl_Position.x += 2.0;\n",
" //}\n",
" \n",
" vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation\n",
"\n",
" #include <logdepthbuf_vertex>\n",
" #include <clipping_planes_vertex>\n",
" #include <fog_vertex>\n",
"}\n",
"''',\n",
" fragmentShader='''\n",
"uniform vec3 diffuse;\n",
"uniform float opacity;\n",
"\n",
"varying float vLineDistance;\n",
"\n",
"#include <common>\n",
"#include <color_pars_fragment>\n",
"#include <fog_pars_fragment>\n",
"#include <logdepthbuf_pars_fragment>\n",
"#include <clipping_planes_pars_fragment>\n",
"\n",
"varying vec2 vUv;\n",
"\n",
"void main() {\n",
"\n",
" #include <clipping_planes_fragment>\n",
"\n",
"\n",
" if ( abs( vUv.y ) > 1.0 ) {\n",
"\n",
" float a = vUv.x;\n",
" float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;\n",
" float len2 = a * a + b * b;\n",
"\n",
" if ( len2 > 1.0 ) discard;\n",
"\n",
" }\n",
"\n",
" vec4 diffuseColor = vec4( diffuse, opacity );\n",
"\n",
" #include <logdepthbuf_fragment>\n",
" #include <color_fragment>\n",
"\n",
" gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );\n",
"\n",
" #include <premultiplied_alpha_fragment>\n",
" #include <tonemapping_fragment>\n",
" #include <encodings_fragment>\n",
" #include <fog_fragment>\n",
"\n",
"}\n",
"''',\n",
" vertexColors='VertexColors',\n",
" uniforms=dict(\n",
" **UniformsLib['common'],\n",
" linewidth={'value': 10.0},\n",
" resolution={'value': (100., 100.)},\n",
" )\n",
")\n",
"Mesh(g2, m)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(repr(line2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
5 changes: 5 additions & 0 deletions js/scripts/generate-wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const CUSTOM_CLASSES = [
'core/BaseBufferGeometry.js',
'objects/CloneArray.js',
'objects/Blackbox.js',
'objects/Line2.js',
'objects/LineSegments2.js',
'geometries/LineGeometry.js',
'geometries/LineSegmentsGeometry.js',
'materials/LineMaterial.js',
];

const IGNORE_FILES = [
Expand Down
Loading

0 comments on commit d77a75b

Please sign in to comment.