UNPKG

@6edesign/svelte-three

Version:

[Demo](https://6edesign.github.io/svelte-three-mograph/)

346 lines (315 loc) 10.2 kB
/* src/components/mograph/Emitter.svelte generated by Svelte v3.31.2 */ import { SvelteComponent, check_outros, create_component, create_slot, destroy_component, detach, empty, group_outros, init, insert, mount_component, outro_and_destroy_block, safe_not_equal, space, transition_in, transition_out, update_keyed_each, update_slot } from "../../../web_modules/svelte/internal.js"; import { getContext } from "../../../web_modules/svelte.js"; import { Vector3 } from "../../../web_modules/three.js"; import Particle from "./Particle.js"; import { addVariance } from "../../utils/index.js"; function get_each_context(ctx, list, i) { const child_ctx = ctx.slice(); child_ctx[25] = list[i]; return child_ctx; } const get_default_slot_changes = dirty => ({ position: dirty & /*position*/ 4, rotation: dirty & /*rotation*/ 8 }); const get_default_slot_context = ctx => ({ position: /*position*/ ctx[2], rotation: /*rotation*/ ctx[3] }); // (78:2) <Particle {forces} {rotationalForces} position={particle.position} velocity={particle.velocity} rotationalVelocity={particle.rotationalVelocity} let:position let:rotation > function create_default_slot(ctx) { let t; let current; const default_slot_template = /*#slots*/ ctx[16].default; const default_slot = create_slot(default_slot_template, ctx, /*$$scope*/ ctx[17], get_default_slot_context); return { c() { if (default_slot) default_slot.c(); t = space(); }, m(target, anchor) { if (default_slot) { default_slot.m(target, anchor); } insert(target, t, anchor); current = true; }, p(ctx, dirty) { if (default_slot) { if (default_slot.p && dirty & /*$$scope, position, rotation*/ 131084) { update_slot(default_slot, default_slot_template, ctx, /*$$scope*/ ctx[17], dirty, get_default_slot_changes, get_default_slot_context); } } }, i(local) { if (current) return; transition_in(default_slot, local); current = true; }, o(local) { transition_out(default_slot, local); current = false; }, d(detaching) { if (default_slot) default_slot.d(detaching); if (detaching) detach(t); } }; } // (77:0) {#each particles as particle (particle.id)} function create_each_block(key_1, ctx) { let first; let particle; let current; particle = new Particle({ props: { forces: /*forces*/ ctx[0], rotationalForces: /*rotationalForces*/ ctx[1], position: /*particle*/ ctx[25].position, velocity: /*particle*/ ctx[25].velocity, rotationalVelocity: /*particle*/ ctx[25].rotationalVelocity, $$slots: { default: [ create_default_slot, ({ position, rotation }) => ({ 2: position, 3: rotation }), ({ position, rotation }) => (position ? 4 : 0) | (rotation ? 8 : 0) ] }, $$scope: { ctx } } }); return { key: key_1, first: null, c() { first = empty(); create_component(particle.$$.fragment); this.first = first; }, m(target, anchor) { insert(target, first, anchor); mount_component(particle, target, anchor); current = true; }, p(new_ctx, dirty) { ctx = new_ctx; const particle_changes = {}; if (dirty & /*forces*/ 1) particle_changes.forces = /*forces*/ ctx[0]; if (dirty & /*rotationalForces*/ 2) particle_changes.rotationalForces = /*rotationalForces*/ ctx[1]; if (dirty & /*particles*/ 16) particle_changes.position = /*particle*/ ctx[25].position; if (dirty & /*particles*/ 16) particle_changes.velocity = /*particle*/ ctx[25].velocity; if (dirty & /*particles*/ 16) particle_changes.rotationalVelocity = /*particle*/ ctx[25].rotationalVelocity; if (dirty & /*$$scope, position, rotation*/ 131084) { particle_changes.$$scope = { dirty, ctx }; } particle.$set(particle_changes); }, i(local) { if (current) return; transition_in(particle.$$.fragment, local); current = true; }, o(local) { transition_out(particle.$$.fragment, local); current = false; }, d(detaching) { if (detaching) detach(first); destroy_component(particle, detaching); } }; } function create_fragment(ctx) { let each_blocks = []; let each_1_lookup = new Map(); let each_1_anchor; let current; let each_value = /*particles*/ ctx[4]; const get_key = ctx => /*particle*/ ctx[25].id; for (let i = 0; i < each_value.length; i += 1) { let child_ctx = get_each_context(ctx, each_value, i); let key = get_key(child_ctx); each_1_lookup.set(key, each_blocks[i] = create_each_block(key, child_ctx)); } return { c() { for (let i = 0; i < each_blocks.length; i += 1) { each_blocks[i].c(); } each_1_anchor = empty(); }, m(target, anchor) { for (let i = 0; i < each_blocks.length; i += 1) { each_blocks[i].m(target, anchor); } insert(target, each_1_anchor, anchor); current = true; }, p(ctx, [dirty]) { if (dirty & /*forces, rotationalForces, particles, $$scope, position, rotation*/ 131103) { each_value = /*particles*/ ctx[4]; group_outros(); each_blocks = update_keyed_each(each_blocks, dirty, get_key, 1, ctx, each_value, each_1_lookup, each_1_anchor.parentNode, outro_and_destroy_block, create_each_block, each_1_anchor, get_each_context); check_outros(); } }, i(local) { if (current) return; for (let i = 0; i < each_value.length; i += 1) { transition_in(each_blocks[i]); } current = true; }, o(local) { for (let i = 0; i < each_blocks.length; i += 1) { transition_out(each_blocks[i]); } current = false; }, d(detaching) { for (let i = 0; i < each_blocks.length; i += 1) { each_blocks[i].d(detaching); } if (detaching) detach(each_1_anchor); } }; } function instance($$self, $$props, $$invalidate) { let { $$slots: slots = {}, $$scope } = $$props; let { position = [0, 0, 0] } = $$props; let { rotation = [0, 0, 0] } = $$props; let { velocity = 0.04 } = $$props; let { velocityVariance = 0.03 } = $$props; let { rotationVelocity = 0.02 } = $$props; let { rotationVelocityVariance = 0.008 } = $$props; let { size = [0, 0, 0] } = $$props; let { direction = [0, 0, 1] } = $$props; let { directionVariance = 0.5 } = $$props; let { rotationDirection = [0, 0, 0] } = $$props; let { rotationDirectionVariance = 2 } = $$props; let { lifespan = 15000 } = $$props; let { particlesPerSecond = 5 } = $$props; let { forces = [] } = $$props; let { rotationalForces = [] } = $$props; const varyVelocity = addVariance(velocityVariance); const varyDirection = addVariance(directionVariance); const varyRotationVelocity = addVariance(rotationVelocityVariance); const varyRotationDirection = addVariance(rotationDirectionVariance); let particles = []; let particleCount = 0; const addParticle = () => { const now = Date.now(); const particleDirection = new Vector3(...direction.map(varyDirection)); const particleVelocity = new Vector3().addScaledVector(particleDirection, varyVelocity(velocity)); const particleRotationalDirection = new Vector3(...rotationDirection.map(varyRotationDirection)); const particleRotationalVelocity = new Vector3().addScaledVector(particleRotationalDirection, varyRotationVelocity(rotationVelocity)); $$invalidate(4, particles = [ ...particles.filter(({ dies }) => now < dies), { id: particleCount, dies: now + lifespan, position: [ addVariance(size[0] / 2)(position[0]), addVariance(size[1] / 2)(position[1]), addVariance(size[2] / 2)(position[2]) ], rotation, force: { direction: new Vector3(), rotation: new Vector3() }, velocity: [particleVelocity.x, particleVelocity.y, particleVelocity.z], rotationalVelocity: [ particleRotationalVelocity.x, particleRotationalVelocity.y, particleRotationalVelocity.z ] } ]); ++particleCount; setTimeout(addParticle, 1000 / particlesPerSecond); }; addParticle(); const sceneCtx = getContext("sceneCtx"); $$self.$$set = $$props => { if ("position" in $$props) $$invalidate(2, position = $$props.position); if ("rotation" in $$props) $$invalidate(3, rotation = $$props.rotation); if ("velocity" in $$props) $$invalidate(5, velocity = $$props.velocity); if ("velocityVariance" in $$props) $$invalidate(6, velocityVariance = $$props.velocityVariance); if ("rotationVelocity" in $$props) $$invalidate(7, rotationVelocity = $$props.rotationVelocity); if ("rotationVelocityVariance" in $$props) $$invalidate(8, rotationVelocityVariance = $$props.rotationVelocityVariance); if ("size" in $$props) $$invalidate(9, size = $$props.size); if ("direction" in $$props) $$invalidate(10, direction = $$props.direction); if ("directionVariance" in $$props) $$invalidate(11, directionVariance = $$props.directionVariance); if ("rotationDirection" in $$props) $$invalidate(12, rotationDirection = $$props.rotationDirection); if ("rotationDirectionVariance" in $$props) $$invalidate(13, rotationDirectionVariance = $$props.rotationDirectionVariance); if ("lifespan" in $$props) $$invalidate(14, lifespan = $$props.lifespan); if ("particlesPerSecond" in $$props) $$invalidate(15, particlesPerSecond = $$props.particlesPerSecond); if ("forces" in $$props) $$invalidate(0, forces = $$props.forces); if ("rotationalForces" in $$props) $$invalidate(1, rotationalForces = $$props.rotationalForces); if ("$$scope" in $$props) $$invalidate(17, $$scope = $$props.$$scope); }; return [ forces, rotationalForces, position, rotation, particles, velocity, velocityVariance, rotationVelocity, rotationVelocityVariance, size, direction, directionVariance, rotationDirection, rotationDirectionVariance, lifespan, particlesPerSecond, slots, $$scope ]; } class Emitter extends SvelteComponent { constructor(options) { super(); init(this, options, instance, create_fragment, safe_not_equal, { position: 2, rotation: 3, velocity: 5, velocityVariance: 6, rotationVelocity: 7, rotationVelocityVariance: 8, size: 9, direction: 10, directionVariance: 11, rotationDirection: 12, rotationDirectionVariance: 13, lifespan: 14, particlesPerSecond: 15, forces: 0, rotationalForces: 1 }); } } export default Emitter;