### SDF Operations (WGSL) Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Examples of common SDF operations like smooth union, intersection, and subtraction using WGSL. ```APIDOC ## sdf_op_smooth_union ### Description Smoothly blends two shapes together with adjustable smoothness. ### Method N/A (WGSL function) ### Endpoint N/A ### Parameters #### Query Parameters - **k** (f32) - Controls smoothness (larger = smoother blend) ### Request Example ```wgsl use sdf3d::* use sdf::op; fn sdf3d(p: vec3f) -> f32 { let sphere = sdf3d_sphere(p - vec3f(0.3, 0.0, 0.0), 0.4); let box_ = sdf3d_box(p + vec3f(0.3, 0.0, 0.0), vec3f(0.5, 0.5, 0.5)); // k=0.2 controls smoothness (larger = smoother blend) return sdf_op_smooth_union(sphere, box_, 0.2); } ``` ## sdf_op_smooth_intersection ### Description Smoothly intersects two shapes, keeping only the overlapping region. ### Method N/A (WGSL function) ### Endpoint N/A ### Parameters #### Query Parameters - **k** (f32) - Controls smoothness ### Request Example ```wgsl use sdf3d::* use sdf::op; fn sdf3d(p: vec3f) -> f32 { let sphere = sdf3d_sphere(p, 0.6); let box_ = sdf3d_box(p, vec3f(0.8, 0.8, 0.8)); // Smooth intersection with k=0.1 return sdf_op_smooth_intersection(sphere, box_, 0.1); } ``` ## sdf_op_smooth_subtraction ### Description Smoothly subtracts one shape from another with rounded edges. ### Method N/A (WGSL function) ### Endpoint N/A ### Parameters #### Query Parameters - **k** (f32) - Controls smoothness ### Request Example ```wgsl use sdf3d::* use sdf::op; fn sdf3d(p: vec3f) -> f32 { let box_ = sdf3d_box(p, vec3f(0.8, 0.8, 0.8)); let sphere = sdf3d_sphere(p, 0.5); // Subtract sphere from box with smooth edges (k=0.15) return sdf_op_smooth_subtraction(sphere, box_, 0.15); } ``` ``` -------------------------------- ### WGSL SDF Definition Example Source: https://github.com/wilstonoreo/sdf2mesh/blob/master/README.md This WGSL code defines a torus SDF. It imports predefined primitive functions and defines the main `sdf3d` function which takes a 3D point and returns the signed distance. ```wgsl use sdf3d::*; fn sdf3d(p: vec3f) -> f32 { return sdf3d_torus(p, vec2(0.5, 0.2)); } ``` -------------------------------- ### Mandelbulb Fractal GLSL Shader Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt A GLSL fragment shader example that defines a Mandelbulb fractal SDF. This can be used as input for sdf2mesh. ```glsl #version 450 core float sdf(vec3 p) { p.xyz = p.xzy; vec3 z = p; vec3 dz = vec3(0.0); float power = 8.0; float r, theta, phi; float dr = 1.0; float t0 = 1.0; for(int i = 0; i < 5; ++i) { r = length(z); if(r > 2.0) continue; theta = atan(z.y / z.x); phi = asin(z.z / r); dr = pow(r, power - 1.0) * dr * power + 1.0; r = pow(r, power); theta = theta * power; phi = phi * power; z = r * vec3(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi)) + p; t0 = min(t0, r); } return 0.5 * log(r) * r / dr - 0.003; } void main() {} ``` -------------------------------- ### WGSL Module Imports Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Import specific modules like `sdf3d::*` for all primitives and normal calculation, `sdf::op` for CSG operations, `sdf3d::primitives` for primitives only, or `sdf3d::normal` for normal calculation. ```wgsl // Import all SDF3D primitives and normal calculation use sdf3d::*; // Import only SDF operations (union, intersection, subtraction) use sdf::op; // Import only primitives use sdf3d::primitives; // Import normal calculation function use sdf3d::normal; fn sdf3d(p: vec3f) -> f32 { // Your SDF definition here return sdf3d_sphere(p, 0.5); } ``` -------------------------------- ### Download and Convert ShaderToy to Mesh Source: https://github.com/wilstonoreo/sdf2mesh/blob/master/README.md Use this command to download a ShaderToy shader, convert it from GLSL to WGSL, and generate an STL mesh. The `--shadertoy-sdf` flag allows specifying a custom SDF function name. ```shell cargo run -- --shadertoy-id XX3Xzl --resolution 512 --mesh shadertoy.stl --debug-wgsl test.wgsl --bounds 2 --shadertoy-sdf map ``` -------------------------------- ### Basic WGSL SDF File Structure Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt A basic WGSL SDF file defines a `sdf3d(p: vec3f) -> f32` function. Module imports are used to access built-in functionality. ```wgsl // examples/torus.sdf3d - Simple torus with radius 0.5 and tube width 0.2 use sdf3d::*; fn sdf3d(p: vec3f) -> f32 { return sdf3d_torus(p, vec2(0.5, 0.2)); } ``` -------------------------------- ### Generate Mandelbulb Mesh using sdf2mesh CLI Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Command-line instruction to generate a Mandelbulb mesh from a GLSL SDF file using sdf2mesh. Specify output file, resolution, and bounds. ```bash # Generate the Mandelbulb mesh cargo run -- --glsl examples/mandelmesh.frag --resolution 512 --mesh mandelbulb.stl --bounds 5 ``` -------------------------------- ### Generate Mesh from WGSL SDF File Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Use this command to generate a mesh from a WGSL SDF file. Specify the input SDF file, resolution, and output mesh file. Custom bounding box size and output format (PLY) are supported. ```bash cargo run -- --sdf examples/torus.sdf3d --resolution 128 --mesh torus.stl ``` ```bash cargo run -- --sdf examples/torus.sdf3d --resolution 256 --mesh torus.stl --bounds 3 ``` ```bash cargo run -- --sdf examples/torus.sdf3d --resolution 128 --mesh torus.ply ``` ```bash cargo run -- --sdf examples/torus.sdf3d --resolution 128 --mesh torus.stl --debug-wgsl debug.wgsl ``` ```bash cargo run -- --sdf examples/torus.sdf3d --resolution 128 --mesh torus.stl --debug-png slices/ ``` -------------------------------- ### Generate Mesh from SDF File Source: https://github.com/wilstonoreo/sdf2mesh/blob/master/README.md Use this command to generate a mesh from a WGSL SDF file. Specify the SDF file, desired resolution, and output mesh file name. ```shell cargo run -- --sdf examples/torus.sdf3d --resolution 128 --mesh torus.stl ``` -------------------------------- ### sdf3d_box Primitive Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Defines an axis-aligned box centered at the origin. Requires `use sdf3d::*;`. ```wgsl use sdf3d::*; fn sdf3d(p: vec3f) -> f32 { // Box with dimensions 1.0 x 0.5 x 0.3 return sdf3d_box(p, vec3f(1.0, 0.5, 0.3)); } ``` -------------------------------- ### sdf3d_sphere Primitive Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Defines a sphere centered at the origin. Requires `use sdf3d::*;`. ```wgsl use sdf3d::*; fn sdf3d(p: vec3f) -> f32 { // Sphere with radius 0.5 return sdf3d_sphere(p, 0.5); } ``` -------------------------------- ### Generate Mesh from ShaderToy Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Fetch shaders directly from ShaderToy and convert them to meshes. Requires public + API visibility. Specify the ShaderToy ID, resolution, output mesh, and bounds. Custom SDF function names are supported. ```bash cargo run -- --shadertoy-id DldfR7 --resolution 256 --mesh shadertoy.stl --bounds 5 ``` ```bash cargo run -- --shadertoy-id XX3Xzl --resolution 512 --mesh torusknot.stl --bounds 2 --shadertoy-sdf map ``` ```bash cargo run -- --shadertoy-id XX3Xzl --resolution 256 --mesh output.stl --debug-wgsl converted.wgsl --bounds 2 --shadertoy-sdf map ``` -------------------------------- ### sdf3d_torus Primitive Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Defines a torus centered at the origin, lying in the XZ plane. Requires `use sdf3d::*;`. ```wgsl use sdf3d::*; fn sdf3d(p: vec3f) -> f32 { // Torus with major radius 0.5 and minor radius (tube) 0.2 return sdf3d_torus(p, vec2(0.5, 0.2)); } ``` -------------------------------- ### Rust 3D Bounding Box Utility Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Defines 3D bounding boxes to limit SDF evaluation domains. Supports creation from size/center, explicit corners, and provides methods to access properties and check point containment. ```rust use sdf2mesh::{Bounds3D, Vec3D}; // Create a centered cube bounding box let bounds = Bounds3D::centered_cube(2.0); // 2x2x2 cube centered at origin // Create a bounding box with explicit size and center let bounds = Bounds3D::cube(3.0, &Vec3D::new(0.0, 0.5, 0.0)); // Create from min/max corners let bounds = Bounds3D::min_max( Vec3D::new(-1.0, -1.0, -1.0), Vec3D::new(1.0, 1.0, 1.0) ); // Access bounding box properties let min = bounds.min(); let max = bounds.max(); let size = bounds.size(); let center = bounds.center(); // Check if a point is inside the bounds let inside = bounds.contains(&Vec3D::new(0.5, 0.5, 0.5)); ``` -------------------------------- ### Generate Mesh from ShaderToy ID Source: https://github.com/wilstonoreo/sdf2mesh/blob/master/README.md Generate a mesh directly from a ShaderToy ID. Provide the ShaderToy ID, resolution, output mesh file, and bounds. ```shell cargo run -- --shadertoy-id DldfR7 --resolution 256 --mesh shadertoy.stl --bounds 5 ``` -------------------------------- ### sdf3d_cylinder Primitive Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Defines a cylinder centered at the origin, aligned with the Y-axis. Requires `use sdf3d::*;`. ```wgsl use sdf3d::*; fn sdf3d(p: vec3f) -> f32 { // Cylinder with height 1.0 and radius 0.3 return sdf3d_cylinder(p, 1.0, 0.3); } ``` -------------------------------- ### Rust Load SDF Shader Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Loads SDF shaders from various sources including files, GLSL fragments, and the ShaderToy API. Ensure the correct SDF function name is provided when loading from GLSL or ShaderToy. ```rust use sdf2mesh::shader::Sdf3DShader; // Load from a .sdf3d file let shader = Sdf3DShader::from_path("examples/torus.sdf3d"); // Load from a GLSL fragment shader let shader = Sdf3DShader::from_glsl_fragment_shader( "examples/mandelmesh.frag", "sdf" // name of the SDF function ).unwrap(); // Load from ShaderToy API (async) let shader = Sdf3DShader::from_shadertoy_api( "DldfR7", // ShaderToy shader ID "sdf" // name of the SDF function ).await.unwrap(); // Write compiled WGSL to file for debugging shader.write_to_file("debug.wgsl").unwrap(); // Create wgpu shader module let device: wgpu::Device = /* ... */; let shader_module = shader.create_shader_module(&device); ``` -------------------------------- ### Generate Mesh from GLSL Fragment Shader Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Convert GLSL fragment shaders to meshes. Specify the GLSL file, resolution, output mesh, and bounds. A custom SDF function name can also be provided. ```bash cargo run -- --glsl examples/mandelmesh.frag --resolution 512 --mesh mandelmesh.stl --bounds 5 ``` ```bash cargo run -- --glsl examples/mandelmesh.frag --resolution 256 --mesh output.stl --bounds 4 --glsl-sdf myDistanceFunction ``` -------------------------------- ### sdf3d_capsule Primitive Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Defines a capsule (line segment with rounded ends) between two points. Requires `use sdf3d::*;`. ```wgsl use sdf3d::*; fn sdf3d(p: vec3f) -> f32 { // Capsule from point A to point B with radius 0.2 let a = vec3f(0.0, -0.5, 0.0); let b = vec3f(0.0, 0.5, 0.0); return sdf3d_capsule(p, a, b, 0.2); } ``` -------------------------------- ### Bounds3D API Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Utility for defining 3D bounding boxes used to limit the SDF evaluation domain. ```APIDOC ## Bounds3D ### Description Utility for defining 3D bounding boxes used to limit the SDF evaluation domain. ### Methods - `centered_cube(size: f64)`: Create a centered cube bounding box. - `cube(size: f64, center: &Vec3D)`: Create a bounding box with explicit size and center. - `min_max(min: Vec3D, max: Vec3D)`: Create from min/max corners. - `min()`: Get the minimum corner. - `max()`: Get the maximum corner. - `size()`: Get the size of the bounding box. - `center()`: Get the center of the bounding box. - `contains(point: &Vec3D)`: Check if a point is inside the bounds. ### Request Example ```rust use sdf2mesh::{Bounds3D, Vec3D}; // Create a centered cube bounding box let bounds = Bounds3D::centered_cube(2.0); // 2x2x2 cube centered at origin // Create a bounding box with explicit size and center let bounds = Bounds3D::cube(3.0, &Vec3D::new(0.0, 0.5, 0.0)); // Create from min/max corners let bounds = Bounds3D::min_max( Vec3D::new(-1.0, -1.0, -1.0), Vec3D::new(1.0, 1.0, 1.0) ); // Access bounding box properties let min = bounds.min(); let max = bounds.max(); let size = bounds.size(); let center = bounds.center(); // Check if a point is inside the bounds let inside = bounds.contains(&Vec3D::new(0.5, 0.5, 0.5)); ``` ``` -------------------------------- ### Generate Mesh from GLSL Shader Source: https://github.com/wilstonoreo/sdf2mesh/blob/master/README.md Generate a mesh from a GLSL fragment shader. Ensure the shader includes a SDF function with the signature `float (vec3)`. Specify the GLSL file, resolution, output mesh file, and bounds. ```shell cargo run -- --glsl .\examples\mandelmesh.frag --resolution 512 --mesh mandelmesh.stl --bounds 5 ``` -------------------------------- ### Rust Triangle Mesh Creation and Export Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Represents a triangle mesh and handles its export to various file formats. VertexList is used to collect vertices during the dual contouring process. ```rust use sdf2mesh::mesh::{TriangleMesh, VertexList}; use sdf2mesh::Vertex; // VertexList collects vertices during dual contouring let mut vertex_list = VertexList::with_capacity(1024 * 1024); // Insert vertices with cell coordinates, sign changes, and vertex data vertex_list.insert( (x, y, z), // cell coordinates (u16, u16, u16) (sign_x, sign_y, sign_z, sign_w), // sign changes (bool, bool, bool, bool) Vertex { pos: Vec3D::new(px, py, pz), normal: Vec3D::new(nx, ny, nz), } ); // Convert to triangle mesh let mesh = TriangleMesh::from(vertex_list); // Export to file (format detected from extension) mesh.write_to_file("output.stl").unwrap(); // STL format mesh.write_to_file("output.ply").unwrap(); // PLY format ``` -------------------------------- ### Rust GPU Texture Storage for Compute Shaders Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Provides GPU texture storage for compute shader output, specifically for dual contouring. Includes methods for creating textures, accessing bind group information, copying data to CPU buffers, and saving as PNG. ```rust use sdf2mesh::texture::Rgba32FloatTextureStorage; // Create texture storage for compute shader output let device: wgpu::Device = /* ... */; let dims = (256, 256); // Width x Height let normal_texture = Rgba32FloatTextureStorage::new(&device, dims, 1); // binding 1 let position_texture = Rgba32FloatTextureStorage::new(&device, dims, 2); // binding 2 // Get bind group layout and entries for shader binding let layout_entry = normal_texture.bind_group_layout_entry(); let bind_entry = normal_texture.bind_group_entry(); // After compute shader execution, copy texture to CPU-readable buffer let mut encoder: wgpu::CommandEncoder = /* ... */; normal_texture.copy_texture_to_buffer(&mut encoder); // Map buffer and read data normal_texture.map_buffer(&device).await; let rgba = normal_texture.get_rgba(x, y); // Returns (f32, f32, f32, f32) // Save as PNG for debugging use sdf2mesh::png::ToPngFile; normal_texture.to_png_file("debug_normal.png"); ``` -------------------------------- ### Sdf3DShader API Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt API for loading and processing SDF shaders from various sources using the Sdf3DShader struct. ```APIDOC ## Sdf3DShader ### Description Main struct for loading and processing SDF shaders from various sources. ### Methods - `from_path(path: &str)`: Load from a .sdf3d file. - `from_glsl_fragment_shader(path: &str, function_name: &str)`: Load from a GLSL fragment shader. - `from_shadertoy_api(shader_id: &str, function_name: &str)`: Load from ShaderToy API (async). - `write_to_file(path: &str)`: Write compiled WGSL to file for debugging. - `create_shader_module(device: &wgpu::Device)`: Create wgpu shader module. ### Request Example ```rust use sdf2mesh::shader::Sdf3DShader; // Load from a .sdf3d file let shader = Sdf3DShader::from_path("examples/torus.sdf3d"); // Load from a GLSL fragment shader let shader = Sdf3DShader::from_glsl_fragment_shader( "examples/mandelmesh.frag", "sdf" // name of the SDF function ).unwrap(); // Load from ShaderToy API (async) let shader = Sdf3DShader::from_shadertoy_api( "DldfR7", // ShaderToy shader ID "sdf" // name of the SDF function ).await.unwrap(); // Write compiled WGSL to file for debugging shader.write_to_file("debug.wgsl").unwrap(); // Create wgpu shader module let device: wgpu::Device = /* ... */; let shader_module = shader.create_shader_module(&device); ``` ``` -------------------------------- ### TriangleMesh API Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt API for representing and exporting triangle meshes generated by the library. ```APIDOC ## TriangleMesh ### Description Represents the output triangle mesh and handles file export. ### Methods - `from(vertex_list: VertexList)`: Convert from VertexList to TriangleMesh. - `write_to_file(path: &str)`: Export to file (format detected from extension). ### VertexList #### Description Collects vertices during dual contouring. #### Methods - `with_capacity(capacity: usize)`: Create a new VertexList with specified capacity. - `insert(cell_coords: (u16, u16, u16), sign_changes: (bool, bool, bool, bool), vertex_data: Vertex)`: Insert vertices. ### Request Example ```rust use sdf2mesh::mesh::{TriangleMesh, VertexList}; use sdf2mesh::Vertex; // VertexList collects vertices during dual contouring let mut vertex_list = VertexList::with_capacity(1024 * 1024); // Insert vertices with cell coordinates, sign changes, and vertex data vertex_list.insert( (x, y, z), // cell coordinates (u16, u16, u16) (sign_x, sign_y, sign_z, sign_w), // sign changes (bool, bool, bool, bool) Vertex { pos: Vec3D::new(px, py, pz), normal: Vec3D::new(nx, ny, nz), } ); // Convert to triangle mesh let mesh = TriangleMesh::from(vertex_list); // Export to file (format detected from extension) mesh.write_to_file("output.stl").unwrap(); // STL format mesh.write_to_file("output.ply").unwrap(); // PLY format ``` ``` -------------------------------- ### Rgba32FloatTextureStorage API Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt GPU texture storage for compute shader output during dual contouring. ```APIDOC ## Rgba32FloatTextureStorage ### Description GPU texture storage for compute shader output during dual contouring. ### Methods - `new(device: &wgpu::Device, dims: (u32, u32), binding: u32)`: Create texture storage. - `bind_group_layout_entry()`: Get bind group layout entry. - `bind_group_entry()`: Get bind group entry. - `copy_texture_to_buffer(encoder: &mut wgpu::CommandEncoder)`: Copy texture to CPU-readable buffer. - `map_buffer(device: &wgpu::Device)`: Map buffer for reading (async). - `get_rgba(x: u32, y: u32)`: Get RGBA values at specified coordinates. - `to_png_file(path: &str)`: Save texture as PNG for debugging. ### Request Example ```rust use sdf2mesh::texture::Rgba32FloatTextureStorage; // Create texture storage for compute shader output let device: wgpu::Device = /* ... */; let dims = (256, 256); // Width x Height let normal_texture = Rgba32FloatTextureStorage::new(&device, dims, 1); // binding 1 let position_texture = Rgba32FloatTextureStorage::new(&device, dims, 2); // binding 2 // Get bind group layout and entries for shader binding let layout_entry = normal_texture.bind_group_layout_entry(); let bind_entry = normal_texture.bind_group_entry(); // After compute shader execution, copy texture to CPU-readable buffer let mut encoder: wgpu::CommandEncoder = /* ... */; normal_texture.copy_texture_to_buffer(&mut encoder); // Map buffer and read data normal_texture.map_buffer(&device).await; let rgba = normal_texture.get_rgba(x, y); // Returns (f32, f32, f32, f32) // Save as PNG for debugging use sdf2mesh::png::ToPngFile; normal_texture.to_png_file("debug_normal.png"); ``` ``` -------------------------------- ### WGSL Smooth Union SDF Operation Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Smoothly blends two shapes together. The 'k' parameter controls the smoothness of the blend; larger values result in a smoother transition. ```wgsl use sdf3d::* use sdf::op; fn sdf3d(p: vec3f) -> f32 { let sphere = sdf3d_sphere(p - vec3f(0.3, 0.0, 0.0), 0.4); let box_ = sdf3d_box(p + vec3f(0.3, 0.0, 0.0), vec3f(0.5, 0.5, 0.5)); // k=0.2 controls smoothness (larger = smoother blend) return sdf_op_smooth_union(sphere, box_, 0.2); } ``` -------------------------------- ### WGSL Smooth Subtraction SDF Operation Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Smoothly subtracts one shape from another, creating rounded edges. The 'k' value controls the edge rounding; a larger 'k' results in a smoother subtraction. ```wgsl use sdf3d::* use sdf::op; fn sdf3d(p: vec3f) -> f32 { let box_ = sdf3d_box(p, vec3f(0.8, 0.8, 0.8)); let sphere = sdf3d_sphere(p, 0.5); // Subtract sphere from box with smooth edges (k=0.15) return sdf_op_smooth_subtraction(sphere, box_, 0.15); } ``` -------------------------------- ### WGSL Smooth Intersection SDF Operation Source: https://context7.com/wilstonoreo/sdf2mesh/llms.txt Smoothly intersects two shapes, retaining only the overlapping region. The 'k' parameter determines the smoothness of the intersection boundary. ```wgsl use sdf3d::* use sdf::op; fn sdf3d(p: vec3f) -> f32 { let sphere = sdf3d_sphere(p, 0.6); let box_ = sdf3d_box(p, vec3f(0.8, 0.8, 0.8)); // Smooth intersection with k=0.1 return sdf_op_smooth_intersection(sphere, box_, 0.1); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.