Certainly! Below is a step-by-step guide to implementing a 3D rendering widget with PBR (Physically Based Rendering) materials inside a Marimo notebook using Three.js. Marimo notebooks support custom widgets, so we can create a custom widget to render 3D scenes.
Step 1: Set Up the Marimo Notebook
First, ensure you have Marimo installed. If not, you can install it using pip:
pip install marimo
Create a new Marimo notebook:
marimo new 3d_rendering_pbr
cd 3d_rendering_pbr
Step 2: Create the Three.js Widget
We will create a custom widget using HTML and JavaScript to embed a Three.js scene. Marimo allows you to create custom components using the mo.html function.
Open the app.py file in the 3d_rendering_pbr directory and replace its contents with the following code:
import marimo as mo
# Custom Three.js Widget
three_js_widget = mo.ui.html(
"""
<div id="three-js-container" style="width: 100%; height: 500px; border: 1px solid #ccc;"></div>
<script>
// Three.js code to create a 3D scene with PBR materials
let container = document.getElementById('three-js-container');
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
let renderer = new THREE.WebGLRenderer();
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
// Add a light to the scene
let light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5).normalize();
scene.add(light);
// Add a PBR material
let loader = new THREE.TextureLoader();
let albedoMap = loader.load('https://threejs.org/examples/textures/materials/fabric_red_albedo.jpg');
let normalMap = loader.load('https://threejs.org/examples/textures/materials/fabric_red_normal.jpg');
let aoMap = loader.load('https://threejs.org/examples/textures/materials/fabric_red_ao.jpg');
let roughnessMap = loader.load('https://threejs.org/examples/textures/materials/fabric_red_roughness.jpg');
let metalnessMap = loader.load('https://threejs.org/examples/textures/materials/fabric_red_metalness.jpg');
let material = new THREE.MeshStandardMaterial({
map: albedoMap,
normalMap: normalMap,
aoMap: aoMap,
roughnessMap: roughnessMap,
metalnessMap: metalnessMap,
});
// Create a mesh with the PBR material
let geometry = new THREE.BoxGeometry();
let cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
// Handle resize
window.addEventListener('resize', () => {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
""",
height=500,
)
# Display the widget
three_js_widget
Step 3: Run the Marimo Notebook
Save the app.py file and run the Marimo notebook:
marimo run app.py
Explanation
- Container Div: We create a
div with the ID three-js-container to hold the Three.js renderer.
- Three.js Setup:
- Scene, Camera, Renderer: We initialise a new Three.js scene, camera, and WebGL renderer.
- Light: A directional light is added to illuminate the scene.
- PBR Material:
- Textures for albedo, normal, ambient occlusion, roughness, and metalness maps are loaded.
- A
MeshStandardMaterial is created using these textures to achieve PBR.
- Mesh: A box geometry is created and assigned the PBR material.
- Animation: The cube is rotated continuously, and the scene is rendered in an animation loop.
- Resize Handler: An event listener is added to handle window resizing, ensuring the camera aspect ratio and renderer size are updated accordingly.
- Three.js Library: The Three.js library is included via a CDN.
This widget will display a 3D box with PBR materials inside your Marimo notebook. You can further customise the scene, materials, and objects as needed.