Skip to content

Commit

Permalink
Using Phong shading as material default (like in the first version) w…
Browse files Browse the repository at this point in the history
…ith added custom shininess parameter
  • Loading branch information
Lucandia committed Dec 28, 2024
1 parent 9d26beb commit 870f15b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ success = stl_from_file(
material='material', # Material of the STL file ('material', 'flat', or 'wireframe')
auto_rotate=True, # Enable auto-rotation of the STL model
opacity=1, # Opacity of the STL model (0 to 1)
shininess=100, # How shiny the specular highlight is, when using the 'material' style.
cam_v_angle=60, # Vertical angle (in degrees) of the camera
cam_h_angle=-90, # Horizontal angle (in degrees) of the camera
cam_distance=None, # Distance of the camera from the object (defaults to 3x bounding box size)
Expand All @@ -58,6 +59,7 @@ if file_input is not None:
material='material', # Material of the STL file ('material', 'flat', or 'wireframe')
auto_rotate=True, # Enable auto-rotation of the STL model
opacity=1, # Opacity of the STL model (0 to 1)
shininess=100, # How shiny the specular highlight is, when using the 'material' style.
cam_v_angle=60, # Vertical angle (in degrees) of the camera
cam_h_angle=-90, # Horizontal angle (in degrees) of the camera
cam_distance=None, # Distance of the camera from the object (defaults to 3x bounding box size)
Expand All @@ -69,6 +71,8 @@ if file_input is not None:

The functions return a boolean value indicating if the program was able to write and read the files.

The 'material' style is the default style, it uses the [Phong shading](https://threejs.org/docs/api/en/materials/MeshPhongMaterial.html) model from Three.js.

## License

Code is licensed under the GNU General Public License v3.0 ([GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.en.html))
Expand Down
1 change: 1 addition & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
auto_rotate=auto_rotate,
opacity=opacity,
height=height,
shininess=100,
cam_v_angle=cam_v_angle,
cam_h_angle=cam_h_angle,
cam_distance=cam_distance,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='streamlit_stl',
version='0.0.4',
version='0.0.5',
author='Luca Monari',
author_email='Luca.Monari@mr.mpg.de',
url="https://github.com/Lucandia/streamlit_stl",
Expand Down
8 changes: 8 additions & 0 deletions streamlit_stl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def stl_from_text(self,
material: Literal['material', 'flat', 'wireframe'] = 'material',
auto_rotate: bool = False,
opacity: int = 1,
shininess: int = 100,
cam_v_angle: int = 60,
cam_h_angle: int = -90,
cam_distance: int = 0,
Expand All @@ -68,6 +69,8 @@ def stl_from_text(self,
Whether to enable auto-rotation of the 3D object. Default is False.
opacity : int, optional
Opacity of the 3D object, ranging from 0 (fully transparent) to 1 (fully opaque). Default is 1.
shininess : int, optional
How shiny the specular highlight is, when using the 'material' material style. Default is 100.
cam_v_angle : int, optional
Vertical angle (in degrees) for the camera view. Default is 60.
cam_h_angle : int, optional
Expand Down Expand Up @@ -122,6 +125,7 @@ def stl_from_text(self,
material=material,
auto_rotate=bool(auto_rotate),
opacity=opacity,
shininess=shininess,
cam_v_angle=cam_v_angle,
cam_h_angle=cam_h_angle,
cam_distance=cam_distance,
Expand All @@ -136,6 +140,7 @@ def stl_from_file(self,
material: Literal['material', 'flat', 'wireframe'] = 'material',
auto_rotate: bool = False,
opacity: int = 1,
shininess: int = 100,
cam_v_angle: int = 60,
cam_h_angle: int = -90,
cam_distance: int = 0,
Expand All @@ -161,6 +166,8 @@ def stl_from_file(self,
Whether to enable auto-rotation of the 3D object. Default is False.
opacity : int, optional
Opacity of the 3D object, ranging from 0 (fully transparent) to 1 (fully opaque). Default is 1.
shininess : int, optional
How shiny the specular highlight is, when using the 'material' material style. Default is 100.
cam_v_angle : int, optional
Vertical angle (in degrees) for the camera view. Default is 60.
cam_h_angle : int, optional
Expand Down Expand Up @@ -193,6 +200,7 @@ def stl_from_file(self,
material=material,
auto_rotate=auto_rotate,
opacity=opacity,
shininess=shininess,
height=height,
cam_v_angle=cam_v_angle,
cam_h_angle=cam_h_angle,
Expand Down
2 changes: 2 additions & 0 deletions streamlit_stl/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
let material = event.data.args.material;
let auto_rotate = event.data.args.auto_rotate;
let opacity = event.data.args.opacity;
let shininess = event.data.args.shininess;
let cam_v_angle = event.data.args.cam_v_angle;
let cam_h_angle = event.data.args.cam_h_angle;
let cam_distance = event.data.args.cam_distance;
Expand All @@ -63,6 +64,7 @@
stlViewer.setAttribute('auto_rotate', auto_rotate);
stlViewer.setAttribute('materialType', material);
stlViewer.setAttribute('opacity', opacity);
stlViewer.setAttribute('shininess', shininess);
stlViewer.setAttribute('cam_v_angle', cam_v_angle);
stlViewer.setAttribute('cam_h_angle', cam_h_angle);
stlViewer.setAttribute('cam_distance', cam_distance);
Expand Down
9 changes: 5 additions & 4 deletions streamlit_stl/three_js_scripts/stl-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ class STLViewer extends HTMLElement {
const color = parseInt(this.getAttribute('color').replace("#","0x"), 16);
const auto_rotate = this.getAttribute('auto_rotate');
const opacity = this.getAttribute('opacity');
const shininess = Number(this.getAttribute('shininess'));
let materialType = this.getAttribute('materialType');
const cam_v_angle = Number(this.getAttribute('cam_v_angle'));
const cam_h_angle = Number(this.getAttribute('cam_h_angle'));
let cam_distance = Number(this.getAttribute('cam_distance'));
const max_view_distance = Number(this.getAttribute('max_view_distance'));


let camera = new THREE.PerspectiveCamera(50, container.clientWidth / container.clientHeight, 1, max_view_distance);
let renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
Expand All @@ -46,12 +47,12 @@ class STLViewer extends HTMLElement {
let dirLight = new THREE.DirectionalLight(0xffffff,1.5);
dirLight.position.set(-1,0,-1);
scene.add(dirLight);


new THREE.STLLoader().load(model, (geometry) => {
let material = new THREE.MeshPhysicalMaterial({
let material = new THREE.MeshPhongMaterial({
color: color,
// specular: "0x000000",
shininess: shininess,
opacity: opacity,
transparent: true,
});
Expand Down

0 comments on commit 870f15b

Please sign in to comment.