A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
import * as THREE from 'three';
export type Primitive3DType = 'cube' | 'sphere' | 'capsule' | 'cylinder' | 'pyramid';
export interface Primitive3DMeta {
type: Primitive3DType;
label: string;
description: string;
}
export const PRIMITIVE_3D_ITEMS: readonly Primitive3DMeta[] = [
{ type: 'cube', label: 'Cube', description: 'Create a 3D cube primitive' },
{ type: 'sphere', label: 'Sphere', description: 'Create a 3D sphere primitive' },
{ type: 'capsule', label: 'Capsule', description: 'Create a 3D capsule primitive' },
{ type: 'cylinder', label: 'Cylinder', description: 'Create a 3D cylinder primitive' },
{ type: 'pyramid', label: 'Pyramid', description: 'Create a 3D four-sided pyramid primitive' },
] as const;
export function getPrimitive3DMeta(type: Primitive3DType): Primitive3DMeta {
return PRIMITIVE_3D_ITEMS.find((item) => item.type === type) ?? PRIMITIVE_3D_ITEMS[0];
}
/**
* Builds clean, normalized BufferGeometry for a 3D reference primitive
* with its base resting flush on Y = 0 and centered on X/Z.
*/
export function createPrimitive3DGeometry(type: Primitive3DType): THREE.BufferGeometry {
let geom: THREE.BufferGeometry;
switch (type) {
case 'cube':
geom = new THREE.BoxGeometry(1.4, 1.4, 1.4);
break;
case 'sphere':
geom = new THREE.SphereGeometry(0.8, 36, 24);
break;
case 'capsule':
geom = new THREE.CapsuleGeometry(0.48, 0.84, 16, 32);
break;
case 'cylinder':
geom = new THREE.CylinderGeometry(0.65, 0.65, 1.6, 36, 1);
break;
case 'pyramid': {
const raw = new THREE.ConeGeometry(0.95, 1.5, 4, 1);
raw.rotateY(Math.PI / 4);
// Use flat per-face normals so pyramid facets and LT lineart creases stay crisp
geom = raw.toNonIndexed();
raw.dispose();
geom.computeVertexNormals();
break;
}
}
geom.computeBoundingBox();
if (geom.boundingBox) {
const center = new THREE.Vector3();
geom.boundingBox.getCenter(center);
geom.translate(-center.x, -geom.boundingBox.min.y, -center.z);
geom.computeBoundingBox();
geom.computeBoundingSphere();
}
return geom;
}
/**
* Creates a ready-to-attach Three.js Group containing a studio-shaded 3D primitive mesh.
*/
export function createPrimitive3DObject(type: Primitive3DType): {
group: THREE.Group;
mesh: THREE.Mesh;
displayName: string;
} {
const meta = getPrimitive3DMeta(type);
const geometry = createPrimitive3DGeometry(type);
const material = new THREE.MeshStandardMaterial({
color: 0xe2e8f0,
roughness: 0.4,
metalness: 0.08,
side: THREE.DoubleSide,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.name = `Primitive_${meta.label}`;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.origGeometry = geometry;
mesh.userData.origMaterial = material;
mesh.userData.hasCustomMaterial = false;
mesh.userData.primitiveType = type;
const group = new THREE.Group();
group.name = `PrimitiveGroup_${meta.label}`;
group.add(mesh);
return {
group,
mesh,
displayName: meta.label,
};
}