UNPKG

@6edesign/svelte-three

Version:

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

544 lines (487 loc) 13.8 kB
/* src/OrbitExample.svelte generated by Svelte v3.31.2 */ import { SvelteComponent, check_outros, create_component, create_slot, destroy_component, destroy_each, detach, empty, group_outros, init, insert, mount_component, safe_not_equal, space, transition_in, transition_out, update_slot } from "../web_modules/svelte/internal.js"; import { Scene, Material, numbers, lights, mograph, primitives } from "./components/index.js"; import { Vector3 } from "../web_modules/three.js"; import { random, addVariance } from "./utils/index.js"; function get_each_context(ctx, list, i) { const child_ctx = ctx.slice(); child_ctx[26] = list[i]; return child_ctx; } const get_default_slot_changes = dirty => ({ color: dirty & /*color*/ 1073741824 }); const get_default_slot_context = ctx => ({ color: /*color*/ ctx[30] }); // (85:8) <Material metalness={0} roughness={1} {color}> function create_default_slot_2(ctx) { let icosahedron; let current; icosahedron = new /*Icosahedron*/ ctx[6]({ props: { size: /*size*/ ctx[0] + Math.random() * 0.05, position: /*position*/ ctx[2], rotation: /*rotation*/ ctx[29] } }); return { c() { create_component(icosahedron.$$.fragment); }, m(target, anchor) { mount_component(icosahedron, target, anchor); current = true; }, p(ctx, dirty) { const icosahedron_changes = {}; if (dirty & /*size*/ 1) icosahedron_changes.size = /*size*/ ctx[0] + Math.random() * 0.05; if (dirty & /*position*/ 4) icosahedron_changes.position = /*position*/ ctx[2]; if (dirty & /*rotation*/ 536870912) icosahedron_changes.rotation = /*rotation*/ ctx[29]; icosahedron.$set(icosahedron_changes); }, i(local) { if (current) return; transition_in(icosahedron.$$.fragment, local); current = true; }, o(local) { transition_out(icosahedron.$$.fragment, local); current = false; }, d(detaching) { destroy_component(icosahedron, detaching); } }; } // (92:8) {#if addPointLights} function create_if_block(ctx) { let pointlight; let current; pointlight = new /*PointLight*/ ctx[5]({ props: { position: /*position*/ ctx[2], intensity: 1, distance: 2.8, color: /*color*/ ctx[30] } }); return { c() { create_component(pointlight.$$.fragment); }, m(target, anchor) { mount_component(pointlight, target, anchor); current = true; }, p(ctx, dirty) { const pointlight_changes = {}; if (dirty & /*position*/ 4) pointlight_changes.position = /*position*/ ctx[2]; if (dirty & /*color*/ 1073741824) pointlight_changes.color = /*color*/ ctx[30]; pointlight.$set(pointlight_changes); }, i(local) { if (current) return; transition_in(pointlight.$$.fragment, local); current = true; }, o(local) { transition_out(pointlight.$$.fragment, local); current = false; }, d(detaching) { destroy_component(pointlight, detaching); } }; } // (84:20) function fallback_block(ctx) { let material; let t; let if_block_anchor; let current; material = new Material({ props: { metalness: 0, roughness: 1, color: /*color*/ ctx[30], $$slots: { default: [create_default_slot_2] }, $$scope: { ctx } } }); let if_block = /*addPointLights*/ ctx[1] && create_if_block(ctx); return { c() { create_component(material.$$.fragment); t = space(); if (if_block) if_block.c(); if_block_anchor = empty(); }, m(target, anchor) { mount_component(material, target, anchor); insert(target, t, anchor); if (if_block) if_block.m(target, anchor); insert(target, if_block_anchor, anchor); current = true; }, p(ctx, dirty) { const material_changes = {}; if (dirty & /*color*/ 1073741824) material_changes.color = /*color*/ ctx[30]; if (dirty & /*$$scope, size, position, rotation*/ 536903685) { material_changes.$$scope = { dirty, ctx }; } material.$set(material_changes); if (/*addPointLights*/ ctx[1]) { if (if_block) { if_block.p(ctx, dirty); if (dirty & /*addPointLights*/ 2) { transition_in(if_block, 1); } } else { if_block = create_if_block(ctx); if_block.c(); transition_in(if_block, 1); if_block.m(if_block_anchor.parentNode, if_block_anchor); } } else if (if_block) { group_outros(); transition_out(if_block, 1, 1, () => { if_block = null; }); check_outros(); } }, i(local) { if (current) return; transition_in(material.$$.fragment, local); transition_in(if_block); current = true; }, o(local) { transition_out(material.$$.fragment, local); transition_out(if_block); current = false; }, d(detaching) { destroy_component(material, detaching); if (detaching) detach(t); if (if_block) if_block.d(detaching); if (detaching) detach(if_block_anchor); } }; } // (83:4) <RandomColor let:color> function create_default_slot_1(ctx) { let current; const default_slot_template = /*#slots*/ ctx[14].default; const default_slot = create_slot(default_slot_template, ctx, /*$$scope*/ ctx[15], get_default_slot_context); const default_slot_or_fallback = default_slot || fallback_block(ctx); return { c() { if (default_slot_or_fallback) default_slot_or_fallback.c(); }, m(target, anchor) { if (default_slot_or_fallback) { default_slot_or_fallback.m(target, anchor); } current = true; }, p(ctx, dirty) { if (default_slot) { if (default_slot.p && dirty & /*$$scope, color*/ 1073774592) { update_slot(default_slot, default_slot_template, ctx, /*$$scope*/ ctx[15], dirty, get_default_slot_changes, get_default_slot_context); } } else { if (default_slot_or_fallback && default_slot_or_fallback.p && dirty & /*position, color, addPointLights, size, rotation*/ 1610612743) { default_slot_or_fallback.p(ctx, dirty); } } }, i(local) { if (current) return; transition_in(default_slot_or_fallback, local); current = true; }, o(local) { transition_out(default_slot_or_fallback, local); current = false; }, d(detaching) { if (default_slot_or_fallback) default_slot_or_fallback.d(detaching); } }; } // (75:2) <Particle position={[orbiter.position.x, orbiter.position.y, orbiter.position.z]} velocity={[orbiter.velocity.x, orbiter.velocity.y, orbiter.velocity.z]} rotationalVelocity={[0.02, 0.02, 0.02]} forces={[orbit(orbiter.radius, [0, 0, 0])]} let:position let:rotation > function create_default_slot(ctx) { let randomcolor; let t; let current; randomcolor = new /*RandomColor*/ ctx[3]({ props: { $$slots: { default: [ create_default_slot_1, ({ color }) => ({ 30: color }), ({ color }) => color ? 1073741824 : 0 ] }, $$scope: { ctx } } }); return { c() { create_component(randomcolor.$$.fragment); t = space(); }, m(target, anchor) { mount_component(randomcolor, target, anchor); insert(target, t, anchor); current = true; }, p(ctx, dirty) { const randomcolor_changes = {}; if (dirty & /*$$scope, position, color, addPointLights, size, rotation*/ 1610645511) { randomcolor_changes.$$scope = { dirty, ctx }; } randomcolor.$set(randomcolor_changes); }, i(local) { if (current) return; transition_in(randomcolor.$$.fragment, local); current = true; }, o(local) { transition_out(randomcolor.$$.fragment, local); current = false; }, d(detaching) { destroy_component(randomcolor, detaching); if (detaching) detach(t); } }; } // (74:0) {#each orbiters as orbiter} function create_each_block(ctx) { let particle; let current; particle = new /*Particle*/ ctx[4]({ props: { position: [ /*orbiter*/ ctx[26].position.x, /*orbiter*/ ctx[26].position.y, /*orbiter*/ ctx[26].position.z ], velocity: [ /*orbiter*/ ctx[26].velocity.x, /*orbiter*/ ctx[26].velocity.y, /*orbiter*/ ctx[26].velocity.z ], rotationalVelocity: [0.02, 0.02, 0.02], forces: [/*orbit*/ ctx[8](/*orbiter*/ ctx[26].radius, [0, 0, 0])], $$slots: { default: [ create_default_slot, ({ position, rotation }) => ({ 2: position, 29: rotation }), ({ position, rotation }) => (position ? 4 : 0) | (rotation ? 536870912 : 0) ] }, $$scope: { ctx } } }); return { c() { create_component(particle.$$.fragment); }, m(target, anchor) { mount_component(particle, target, anchor); current = true; }, p(ctx, dirty) { const particle_changes = {}; if (dirty & /*$$scope, position, addPointLights, size, rotation*/ 536903687) { 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) { destroy_component(particle, detaching); } }; } function create_fragment(ctx) { let each_1_anchor; let current; let each_value = /*orbiters*/ ctx[7]; let each_blocks = []; for (let i = 0; i < each_value.length; i += 1) { each_blocks[i] = create_each_block(get_each_context(ctx, each_value, i)); } const out = i => transition_out(each_blocks[i], 1, 1, () => { each_blocks[i] = null; }); 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 & /*orbiters, orbit, position, color, addPointLights, size, Math, rotation, $$scope*/ 1610645895) { each_value = /*orbiters*/ ctx[7]; let i; for (i = 0; i < each_value.length; i += 1) { const child_ctx = get_each_context(ctx, each_value, i); if (each_blocks[i]) { each_blocks[i].p(child_ctx, dirty); transition_in(each_blocks[i], 1); } else { each_blocks[i] = create_each_block(child_ctx); each_blocks[i].c(); transition_in(each_blocks[i], 1); each_blocks[i].m(each_1_anchor.parentNode, each_1_anchor); } } group_outros(); for (i = each_value.length; i < each_blocks.length; i += 1) { out(i); } 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) { each_blocks = each_blocks.filter(Boolean); for (let i = 0; i < each_blocks.length; i += 1) { transition_out(each_blocks[i]); } current = false; }, d(detaching) { destroy_each(each_blocks, detaching); if (detaching) detach(each_1_anchor); } }; } function instance($$self, $$props, $$invalidate) { let { $$slots: slots = {}, $$scope } = $$props; const { RandomColor } = numbers; const { Emitter, Particle } = mograph; const { PointLight } = lights; const { Cube, Icosahedron, Octahedron, Sphere, Tetrahedron, Torus, TorusKnot } = primitives; let { position = [0, 0, 0] } = $$props; let { radius = 1.2 } = $$props; let { orbitingCount = 13 } = $$props; let { velocity = 0.025 } = $$props; let { velocityVariance = 0.14 } = $$props; let { radiusVariance = 0.5 } = $$props; let { size = 0.01 } = $$props; let { addPointLights = true } = $$props; const varyRadius = r => Math.abs(addVariance(radiusVariance)(r)); const varyVelocity = addVariance(velocityVariance); const randomUnitVector = () => new Vector3(random(1), random(1), random(1)).normalize(); const orbiters = new Array(orbitingCount).fill(0).map(() => { const center = new Vector3(...position); const orbitingRadius = varyRadius(radius); const orbitingVelocityScalar = varyVelocity(velocity); const orbitingPosition = center.clone().add(randomUnitVector().multiplyScalar(orbitingRadius)); const orbitingVelocity = randomUnitVector().cross(center.clone().sub(orbitingPosition)).normalize().multiplyScalar(orbitingVelocityScalar); return { position: orbitingPosition, velocity: orbitingVelocity, radius: orbitingRadius }; }); const orbit = (radius, center) => particle => { const direction = particle.getVector(...center).sub(particle.position).normalize(); return direction.multiplyScalar(Math.pow(particle.velocity.length(), 2) / radius); }; $$self.$$set = $$props => { if ("position" in $$props) $$invalidate(2, position = $$props.position); if ("radius" in $$props) $$invalidate(9, radius = $$props.radius); if ("orbitingCount" in $$props) $$invalidate(10, orbitingCount = $$props.orbitingCount); if ("velocity" in $$props) $$invalidate(11, velocity = $$props.velocity); if ("velocityVariance" in $$props) $$invalidate(12, velocityVariance = $$props.velocityVariance); if ("radiusVariance" in $$props) $$invalidate(13, radiusVariance = $$props.radiusVariance); if ("size" in $$props) $$invalidate(0, size = $$props.size); if ("addPointLights" in $$props) $$invalidate(1, addPointLights = $$props.addPointLights); if ("$$scope" in $$props) $$invalidate(15, $$scope = $$props.$$scope); }; return [ size, addPointLights, position, RandomColor, Particle, PointLight, Icosahedron, orbiters, orbit, radius, orbitingCount, velocity, velocityVariance, radiusVariance, slots, $$scope ]; } class OrbitExample extends SvelteComponent { constructor(options) { super(); init(this, options, instance, create_fragment, safe_not_equal, { position: 2, radius: 9, orbitingCount: 10, velocity: 11, velocityVariance: 12, radiusVariance: 13, size: 0, addPointLights: 1 }); } } export default OrbitExample;