### Install and Import Sketchbook with NPM Source: https://github.com/swift502/sketchbook/blob/master/README.md Explains how to install Sketchbook using NPM and import it into a JavaScript project. ```bash npm i sketchbook ``` ```javascript import { World } from 'sketchbook'; ``` -------------------------------- ### Initialize Sketchbook World Source: https://context7.com/swift502/sketchbook/llms.txt Initializes the core Sketchbook engine, setting up the 3D renderer, physics world, camera, and input management. It can be initialized as an empty world or by loading a GLTF/GLB scene. ```javascript // Initialize an empty world const world = new Sketchbook.World(); // Load a world from a GLTF scene file const world = new Sketchbook.World('path/to/scene.glb'); // The world automatically sets up: // - THREE.js WebGL renderer with shadow mapping // - CANNON.js physics world with gravity // - Camera with 80° FOV // - FXAA post-processing // - Input management system // - Sky system with dynamic sun position ``` -------------------------------- ### Load GLB Scene with JavaScript Source: https://github.com/swift502/sketchbook/blob/master/README.md Demonstrates how to load a GLB scene defined in Blender using JavaScript within the Sketchbook library. ```javascript const world = new Sketchbook.World('scene.glb'); ``` ```javascript import { World } from 'sketchbook'; const world = new World('scene.glb'); ``` -------------------------------- ### Manage Input with KeyBinding System Source: https://context7.com/swift502/sketchbook/llms.txt Details the KeyBinding system for mapping inputs to character actions in Sketchbook. Shows how to define key bindings, check input states in update loops, and switch input receivers. Supports keyboard and mouse inputs with press/hold detection. ```typescript import { KeyBinding } from 'sketchbook'; // Define custom actions for a character character.actions = { 'forward': new KeyBinding('KeyW'), 'back': new KeyBinding('KeyS'), 'left': new KeyBinding('KeyA'), 'right': new KeyBinding('KeyD'), 'jump': new KeyBinding('Space'), 'run': new KeyBinding('ShiftLeft'), 'interact': new KeyBinding('KeyE'), 'enter_vehicle': new KeyBinding('KeyF'), 'primary': new KeyBinding('Mouse0'), 'secondary': new KeyBinding('Mouse1') }; // Check key states in update loop if (character.actions.jump.justPressed) { // Jump was just pressed this frame } if (character.actions.forward.isPressed) { // Forward is currently held down } // InputManager automatically routes input to current input receiver // Switch input receiver to change what is being controlled world.inputManager.setInputReceiver(vehicle); ``` -------------------------------- ### Import Sketchbook Script Tag Source: https://github.com/swift502/sketchbook/blob/master/README.md Shows how to import the Sketchbook script directly into an HTML file using a script tag. ```html ``` -------------------------------- ### Configure Camera Operator in TypeScript Source: https://context7.com/swift502/sketchbook/llms.txt Demonstrates how to control camera movement in the Sketchbook framework. Shows both character-following and free flight modes with adjustable sensitivity. The camera operator is automatically instantiated with the world instance. ```typescript // Camera operator is automatically created with the world const cameraOp = world.cameraOperator; // Follow a character cameraOp.setTarget(character); cameraOp.followMode = true; // Set camera distance cameraOp.setRadius(5.0); // Free camera mode (default) cameraOp.followMode = false; // In free mode, use: // - Mouse to look around // - WASD to move // - E/Q for up/down // - Shift for faster movement // Adjust sensitivity cameraOp.setSensitivity(0.5, 0.4); // (x, y) ``` -------------------------------- ### Manage Scenarios for Scene Composition Source: https://context7.com/swift502/sketchbook/llms.txt Utilizes Scenarios to define spawn points for characters and vehicles, enabling dynamic scene composition. Scenarios are defined in Blender using empty objects with specific userData and are automatically parsed when loading a scene. ```typescript // Scenarios are defined in Blender as empty objects with userData // and automatically parsed when loading a scene // Example GLTF structure: // - scenario_main (userData: {data: 'scenario', name: 'Main', default: 'true'}) // - spawn_player (userData: {data: 'spawn', type: 'player'}) // - spawn_car (userData: {data: 'spawn', type: 'car', driver: 'ai'}) // Launch a scenario programmatically world.launchScenario('scenario_main'); // Restart current scenario world.restartScenario(); // Scenarios automatically: // - Clear existing entities // - Spawn characters at player spawn points // - Spawn vehicles with optional AI drivers // - Set initial camera angles ``` -------------------------------- ### Implement AI Behaviors for Characters Source: https://context7.com/swift502/sketchbook/llms.txt Shows how to assign autonomous AI behaviors to characters in Sketchbook. Demonstrates three behavior types: path following, target tracking, and random wandering. Requires importing behavior classes and assigning them to the character's behaviour property. ```typescript import { FollowPath } from 'sketchbook'; import { FollowTarget } from 'sketchbook'; import { RandomBehaviour } from 'sketchbook'; // Create AI-controlled character const aiCharacter = new Character(gltf); world.add(aiCharacter); // Follow a predefined path const path = world.paths.find(p => p.name === 'patrol_route'); aiCharacter.behaviour = new FollowPath(aiCharacter, path); // Follow another character or object aiCharacter.behaviour = new FollowTarget(aiCharacter, targetCharacter); // Random wandering aiCharacter.behaviour = new RandomBehaviour(aiCharacter); // AI automatically updates each frame and controls character actions // Paths are defined in Blender as curves with userData: {data: 'path'} ``` -------------------------------- ### Create Custom Updatable Entities Source: https://context7.com/swift502/sketchbook/llms.txt Explains how to create custom entities that integrate with Sketchbook's update loop. Requires implementing IWorldEntity and IUpdatable interfaces with update, addToWorld, and removeToWorld methods. Entities receive both scaled and unscaled time steps for frame-rate independent updates. ```typescript import { IUpdatable } from 'sketchbook'; import { IWorldEntity } from 'sketchbook'; class CustomEntity implements IWorldEntity, IUpdatable { public updateOrder: number = 5; // Lower updates first public update(timeStep: number, unscaledTimeStep: number): void { // timeStep: scaled by world.params.Time_Scale // unscaledTimeStep: real time delta // Update logic here this.position.x += this.velocity.x * timeStep; } public addToWorld(world: World): void { // Add to scene world.graphicsWorld.add(this); // Add physics body if needed if (this.physicsBody) { world.physicsWorld.addBody(this.physicsBody); } } public removeFromWorld(world: World): void { world.graphicsWorld.remove(this); if (this.physicsBody) { world.physicsWorld.removeBody(this.physicsBody); } } } // Add custom entity const entity = new CustomEntity(); world.add(entity); // Calls addToWorld() and registers for updates ``` -------------------------------- ### Create and Control Vehicles Source: https://context7.com/swift502/sketchbook/llms.txt Integrates vehicle entities into the Sketchbook world, supporting physics-based driving with raycast wheels and character entry/exit. Vehicles are loaded from GLTF models with defined wheels and seats. ```typescript import { Car } from 'sketchbook'; // Load car model with wheels and seats defined loadingManager.loadGLTF('models/car.glb', (gltf) => { const car = new Car(gltf); // Position the vehicle car.collision.position.set(10, 2, 0); // Add to world world.add(car); // The car automatically handles: // - W/S for throttle/reverse // - A/D for steering // - Space for brake // - F to exit vehicle // - X to switch seats // Vehicle seats and wheels are parsed from GLTF userData // Example GLTF node structure: // - car (root) // - seat_driver (userData: {data: 'seat', type: 'driver'}) // - seat_passenger (userData: {data: 'seat'}) // - wheel_fl (userData: {data: 'wheel'}) // - wheel_fr (userData: {data: 'wheel'}) ``` -------------------------------- ### World Update Loop and Rendering Parameters (TypeScript) Source: https://context7.com/swift502/sketchbook/llms.txt This TypeScript code snippet details the automatic render loop managed by the World constructor in Swift502 Sketchbook. It outlines features like 60 FPS frame limiting, frame skipping, time-scaled physics updates, and automatic entity updates. It also shows how to access and modify world parameters such as time scale, debug rendering options, and graphical settings. ```typescript // The render loop is started automatically in the World constructor // It implements: // - 60 FPS frame limiting // - Frame skipping when performance drops below 30 FPS // - Time-scaled physics updates // - Automatic updatable entity updates // The update cycle: // 1. Calculate time step with min 30 FPS cap // 2. Apply time scale // 3. Step physics world // 4. Update all registered updatables (sorted by updateOrder) // 5. Check for out-of-bounds entities and respawn // 6. Render with FXAA or standard renderer // 7. Update stats (FPS, frame time) // Access world parameters world.params.Time_Scale; // Current time scale (0-1) world.params.Debug_Physics; // Show physics debug renderer world.params.Debug_FPS; // Show FPS counter world.params.FXAA; // Enable/disable anti-aliasing world.params.Shadows; // Enable/disable shadows world.params.Pointer_Lock; // Enable/disable pointer lock world.params.Mouse_Sensitivity; // Mouse sensitivity (0-1) world.params.Sun_Elevation; // Sun elevation angle (0-180) world.params.Sun_Rotation; // Sun rotation angle (0-360) ``` -------------------------------- ### Control Time Scale for Slow-Motion Effects Source: https://context7.com/swift502/sketchbook/llms.txt Explains how to adjust global time scaling for slow-motion effects in Sketchbook. Covers setting static time scales and dynamic adjustment via scroll wheel input. Affects physics simulation, animations, and all updatable entities while maintaining logic updates through frame-skipping. ```typescript // Set time scale (0.0 to 1.0) world.setTimeScale(0.5); // Half speed // Pause physics but keep rendering world.setTimeScale(0.0); // Use scroll wheel to adjust time scale dynamically world.scrollTheTimeScale(deltaY); // positive slows, negative speeds up // Time scale affects: // - Physics simulation speed // - Character animations // - Vehicle physics // - All updatable entities // Frame-skipping ensures logic updates continue even at low FPS ``` -------------------------------- ### Create Physics Colliders from Blender Source: https://context7.com/swift502/sketchbook/llms.txt Defines physics colliders within Blender using empty objects with specific userData properties. Supported types include 'box' and 'trimesh', which are automatically created and added to the physics world by Sketchbook. ```typescript // Blender setup for physics: // Create empty object with userData: // { // data: 'physics', // type: 'box' or 'trimesh' // } // Box collider example (userData: {data: 'physics', type: 'box'}) // - Scale defines box dimensions // - Position and rotation are applied to physics body // Trimesh collider example (userData: {data: 'physics', type: 'trimesh'}) // - Uses mesh geometry for collision shape // - Good for complex static geometry // The world.loadScene() method automatically: // - Finds all physics empties // - Creates CANNON.js bodies // - Adds to physics world // - Hides visual mesh ``` -------------------------------- ### Add and Control Characters Source: https://context7.com/swift502/sketchbook/llms.txt Adds player-controlled characters to the Sketchbook world. Characters include third-person controls, animation, and physics-based movement. They are loaded from GLTF models and can be positioned and controlled via input. ```typescript import { Character } from 'sketchbook'; import { LoadingManager } from 'sketchbook'; // Load character model const loadingManager = new LoadingManager(world); loadingManager.loadGLTF('models/character.glb', (gltf) => { const character = new Character(gltf); // Position the character character.characterCapsule.body.position.set(0, 5, 0); // Add to world (registers for updates and physics) world.add(character); // Set as player-controlled world.inputManager.setInputReceiver(character); // Character automatically handles: // - WASD movement // - Space for jumping // - Shift for sprinting // - E for interactions // - F/G for vehicle entry }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.