|
| 1 | +<script> |
| 2 | + import * as THREE from 'three'; |
| 3 | +
|
| 4 | + import { all, create, xor } from 'mathjs'; |
| 5 | + import M from '../M.svelte'; |
| 6 | + import { |
| 7 | + ShardsEdgesGeometry, |
| 8 | + ShardsGeometry, |
| 9 | + filterBang, |
| 10 | + gaussLegendre, |
| 11 | + sampleImplicitSurface, |
| 12 | + } from '../utils'; |
| 13 | + import { mathToJSFunction } from '../objects/mathutils'; |
| 14 | + import { onMount, onDestroy, untrack } from 'svelte'; |
| 15 | + import { demoObjects } from '../states.svelte'; |
| 16 | +
|
| 17 | + let { scene, render } = $props(); |
| 18 | +
|
| 19 | + const config = {}; |
| 20 | + const math = create(all, config); |
| 21 | +
|
| 22 | + let sampleParam = $state(0); |
| 23 | +
|
| 24 | + let backupObjects = $state( |
| 25 | + demoObjects.map((obj) => { |
| 26 | + obj.selected = false; |
| 27 | + return obj; |
| 28 | + }), |
| 29 | + ); |
| 30 | +
|
| 31 | + let standardExamples = $state([ |
| 32 | + { |
| 33 | + uuid: 'lag-story-example-001-', |
| 34 | + kind: 'level', |
| 35 | + title: 'Sphere', |
| 36 | + params: { |
| 37 | + a: '-2', |
| 38 | + b: '2', |
| 39 | + c: '-2', |
| 40 | + d: '2', |
| 41 | + e: '-2', |
| 42 | + f: '2', |
| 43 | + g: 'x^2 + y^2 - 1', |
| 44 | + }, |
| 45 | + color: '#44CB44', |
| 46 | + }, |
| 47 | + ]); |
| 48 | +
|
| 49 | + const whiteLineMaterial = new THREE.LineBasicMaterial({ |
| 50 | + color: 0xffffff, |
| 51 | + linewidth: 2, |
| 52 | + depthTest: true, |
| 53 | + }); |
| 54 | +
|
| 55 | + let currentSurface = $state( |
| 56 | + backupObjects.find((o) => o.kind === 'level') ?? standardExamples[0], |
| 57 | + ); |
| 58 | +
|
| 59 | + // $inspect(currentSurface); |
| 60 | +
|
| 61 | + onMount(() => { |
| 62 | + render(); |
| 63 | + }); |
| 64 | +
|
| 65 | + onDestroy(() => { |
| 66 | + for (let index = 0; index < boxes.children.length; index++) { |
| 67 | + const element = boxes.children[index]; |
| 68 | + element.geometry?.dispose(); |
| 69 | + element.material?.dispose(); |
| 70 | + } |
| 71 | + scene.remove(boxes); |
| 72 | + demoObjects.length = 0; |
| 73 | + demoObjects.push(...backupObjects); |
| 74 | + render(); |
| 75 | + }); |
| 76 | +
|
| 77 | + let geo = $state(); |
| 78 | + let edgesUp = $state(); |
| 79 | +
|
| 80 | + const plusMaterial = new THREE.MeshStandardMaterial({ |
| 81 | + color: 0xccddff, |
| 82 | + side: THREE.DoubleSide, |
| 83 | + roughness: 0.4, |
| 84 | + metalness: 0.8, |
| 85 | + }); |
| 86 | +
|
| 87 | + const boxes = new THREE.Object3D(); |
| 88 | + // boxes.add(new THREE.Mesh(undefined, plusMaterial)); // pos boxes |
| 89 | + // boxes.add(new THREE.LineSegments(undefined, whiteLineMaterial)); // pos edges |
| 90 | + // boxes.add(new THREE.Mesh(undefined, minusMaterial)); // neg boxes |
| 91 | + // boxes.add(new THREE.LineSegments(undefined, whiteLineMaterial)); // neg edges |
| 92 | +
|
| 93 | + scene.add(boxes); |
| 94 | +
|
| 95 | + let boxesVisible = $state(true); |
| 96 | + $effect(() => { |
| 97 | + boxes.visible = boxesVisible; |
| 98 | + }); |
| 99 | +
|
| 100 | + let nBoxes = $state(10); |
| 101 | + let boxing = false; |
| 102 | +
|
| 103 | + let x0 = $derived(math.parse(currentSurface.params.a).evaluate()); |
| 104 | + let x1 = $derived(math.parse(currentSurface.params.b).evaluate()); |
| 105 | + let y0 = $derived(math.parse(currentSurface.params.c).evaluate()); |
| 106 | + let y1 = $derived(math.parse(currentSurface.params.d).evaluate()); |
| 107 | + let z0 = $derived(math.parse(currentSurface.params.e).evaluate()); |
| 108 | + let z1 = $derived(math.parse(currentSurface.params.f).evaluate()); |
| 109 | + let g = $derived( |
| 110 | + mathToJSFunction(currentSurface.params.g, ['x', 'y', 'z']), |
| 111 | + ); |
| 112 | +
|
| 113 | + let pts = $derived( |
| 114 | + sampleImplicitSurface(g, [ |
| 115 | + [x0, x1], |
| 116 | + [y0, y1], |
| 117 | + [z0, z1], |
| 118 | + nBoxes, |
| 119 | + nBoxes, |
| 120 | + nBoxes, |
| 121 | + ]), |
| 122 | + ); |
| 123 | +
|
| 124 | + $inspect(pts); |
| 125 | +
|
| 126 | + const sphereGeo = new THREE.SphereGeometry(0.01, 14, 14); |
| 127 | +
|
| 128 | + $effect(() => { |
| 129 | + untrack(() => { |
| 130 | + for (let index = boxes.children.length - 1; index >= 0; index--) { |
| 131 | + const element = boxes.children[index]; |
| 132 | + // element.geometry?.dispose(); |
| 133 | + // element.material?.dispose(); |
| 134 | + boxes.remove(element); |
| 135 | + } |
| 136 | + }); |
| 137 | + for (const p of pts) { |
| 138 | + const box = new THREE.Mesh(sphereGeo, plusMaterial); |
| 139 | + box.position.set(...p); |
| 140 | + boxes.add(box); |
| 141 | + } |
| 142 | + console.log('effecttt', boxes); |
| 143 | + render(); |
| 144 | + }); |
| 145 | +
|
| 146 | + $effect(() => { |
| 147 | + // console.log('demobj maintenance'); |
| 148 | + if (currentSurface) |
| 149 | + untrack(() => { |
| 150 | + demoObjects[0] = currentSurface; |
| 151 | + demoObjects.length = 1; |
| 152 | + // filterBang( |
| 153 | + // (o) => o === currentField || o === currentSurface, |
| 154 | + // demoObjects, |
| 155 | + // ); |
| 156 | + // if (demoObjects.findIndex((o) => o === currentField) < 0) |
| 157 | + // demoObjects.push(currentField); |
| 158 | + // if (demoObjects.findIndex((o) => o === currentSurface) < 0) |
| 159 | + // demoObjects.push(currentSurface); |
| 160 | + }); |
| 161 | + }); |
| 162 | +</script> |
| 163 | +
|
| 164 | +<div> |
| 165 | + <p>Hello</p> |
| 166 | +
|
| 167 | + <input |
| 168 | + type="range" |
| 169 | + name="" |
| 170 | + id="" |
| 171 | + min="1" |
| 172 | + bind:value={nBoxes} |
| 173 | + step="1" |
| 174 | + max="20" |
| 175 | + /> |
| 176 | +</div> |
| 177 | +
|
| 178 | +<style> |
| 179 | + /* .demos-obj-select { |
| 180 | + border-bottom-right-radius: 0; |
| 181 | + border-top-right-radius: 0; |
| 182 | + } */ |
| 183 | + /* .blue-button { |
| 184 | + background-color: red; |
| 185 | + } */ |
| 186 | +</style> |
0 commit comments