Compare commits

...

5 Commits

Author SHA1 Message Date
5351ec84cb only neighbors 2021-07-07 15:12:39 -04:00
7611192683 direct modification 2021-07-07 15:01:21 -04:00
c419dca58f constraints working 2021-07-07 12:09:52 -04:00
9e5ea8098c smoothing works 2021-07-07 07:26:21 -04:00
dc2f826688 averaging started 2021-07-07 07:00:50 -04:00
2 changed files with 182 additions and 110 deletions

View File

@ -19,122 +19,137 @@
</head> </head>
<body> <body>
<canvas></canvas> <canvas></canvas>
<script type="module"> <script type="module">
import * as T from 'http://esm.sh/three'; import { Scene, THREE } from './scene.js';
import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js';
import { Region, Kernel } from "./region.js";
const renderer = new T.WebGLRenderer({canvas:document.querySelector("canvas")}); let average = (inArray, inPointInd, ...inOthersInd) =>
const scene = new T.Scene(); {
const camera = new T.PerspectiveCamera(80, 2, 0.1, 10); let sum = [0, 0, 0];
const light = new T.DirectionalLight(0xFFFFFF, 1); for(let i=0; i<inOthersInd.length; i++)
const geometry = new T.BoxGeometry(0.8, 0.8, 0.8); {
const shader = new T.MeshPhongMaterial({color:0xFFCC00}); let otherInd = inOthersInd[i]*3;
const shaderBlue = new T.MeshPhongMaterial({color:0x0033FF}); sum[0] += inArray[otherInd+0];
const object = new T.Mesh(geometry, shader); sum[1] += inArray[otherInd+1];
sum[2] += inArray[otherInd+2];
}
const controls = new OrbitControls(camera, renderer.domElement); let pointX = inPointInd*3 + 0;
controls.update(); let pointY = inPointInd*3 + 1;
let pointZ = inPointInd*3 + 2;
let reducer = inOthersInd.length;
let limit = (inCenter, inPoint) =>
{
var limit;
limit = inCenter-0.49;
if(inPoint <= limit)
{
return limit;
}
limit = inCenter + 0.49;
if(inPoint >= limit)
{
return limit;
}
scene.add(light); return inPoint;
scene.add(object); }
scene.add(new T.GridHelper(10, 10)); inArray[pointX] = limit(Math.round(inArray[pointX]), sum[0]/reducer);
inArray[pointY] = limit(Math.round(inArray[pointY]), sum[1]/reducer);
inArray[pointZ] = limit(Math.round(inArray[pointZ]), sum[2]/reducer);
const geoBuff = new T.BufferGeometry(); };
geoBuff.setAttribute("position", new T.BufferAttribute(new Float32Array( let smooth = (inArray, inTimes) =>
[ {
0, 0, 0, let count = inArray.length/3;
1, 0, 0, for(let t=0; t<inTimes; t++)
1, 1, 0, {
0, 1, 0, for(let i=0; i<count; i++)
2, 0, 1, {
2, 1, 1 if(i==0)
] {
), 3)); average(inArray, i, count-1, i+1);
}
geoBuff.setIndex( else
[ {
0, 1, 2, if(i==count-1)
2, 3, 0, {
1, 4, 5, average(inArray, i, i-1, 0);
5, 2, 1 }
] else
); {
geoBuff.computeVertexNormals(); average(inArray, i, i-1, i+1);
const geoObj = new T.Mesh(geoBuff, shader); }
shader.shading = T.FlatShading; }
scene.add(geoObj); }
}
}
/* /*
let r = Region.Create(); let testPoints =
let filler = (x, y, z) => [
{ 0, 0, 0,
r.Voxels[Region.CoordsToIndex(x, y, z)] = true; 0, 1, 0,
}; 1, 1, 0
filler(1, 1, 1); ];
Kernel.Loop(Kernel.Corners, [1, 1, 1], filler); console.log(testPoints);
Kernel.Loop(Kernel.Cardinal, [1, 1, 1], filler); average(testPoints, 1, 0, 2);
Region.Update(r); console.log(testPoints);
r.Filled.forEach((inIndexMap) =>
{
let coords = Region.IndexToCoords(inIndexMap);
let cube = new T.Mesh(geometry, shader);
cube.position.set(...coords);
scene.add(cube);
});
r.Surface.forEach((inIndexMap) =>
{
let coords = Region.IndexToCoords(inIndexMap);
let cube = new T.Mesh(geometry, shaderBlue);
cube.position.set(...coords);
scene.add(cube);
});
*/ */
light.position.set(-1, 2, 4); let path = (inPoints, inSmoothing, inColor) =>
camera.position.set(0, 0, 2);
let dirty = false;
const update = inTime =>
{ {
let timeNew = new Date(); let clone = [...inPoints];
let timeDelta = timeNew - inTime; smooth(clone, inSmoothing);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute( clone, 3 ));
const materialLine = new THREE.LineBasicMaterial( { color: inColor, linewidth: 3 } );
const materialPoint = new THREE.PointsMaterial( { color:inColor, size: 0.05 } );
const output = new THREE.Group();
let delta = timeDelta * 0.001; output.add( new THREE.Line( geometry, materialLine ) );
object.rotation.x += delta; output.add( new THREE.Points( geometry, materialPoint ) );
return output;
controls.update();
dirty = true;
setTimeout(()=>update(timeNew), 100);
} }
const render = inTime => const points =
{ [
if(dirty) 4, 0, 0,
{ 3, 0, 0,
renderer.render(scene, camera); 2, 0, 0,
dirty = false; 1, 0, 0,
} //
requestAnimationFrame(render); 0, 0, 0,
} 0, 1, 0,
1, 1, 0,
2, 1, 0,
2, 2, 0,
3, 2, 0,
4, 2, 0,
4, 3, 0,
//
5, 3, 0,
5, 2, 0,
5, 1, 0,
5, 0, 0,
];
const resize = () =>
{
let canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
};
window.addEventListener("resize", resize);
resize(); Scene(
render(0); {
update(new Date()); DOM:"canvas",
Init(inScene)
{
inScene.add(path(points, 0, 0xFF0000));
inScene.add(path(points, 10, 0x00ff00));
},
Update(scene, delta)
{
}
}
);
</script> </script>
</body> </body>

57
scene.js Normal file
View File

@ -0,0 +1,57 @@
import * as T from 'http://esm.sh/three';
import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js';
export { T as THREE };
export function Scene(inSettings)
{
const renderer = new T.WebGLRenderer({canvas:document.querySelector(inSettings.DOM)});
const scene = new T.Scene();
const camera = new T.PerspectiveCamera(80, 2, 0.1, 10);
const light = new T.DirectionalLight(0xFFFFFF, 1);
const controls = new OrbitControls(camera, renderer.domElement);
let dirty = false;
const update = inTime =>
{
let timeNew = new Date();
let timeDelta = timeNew - inTime;
let delta = timeDelta * 0.001;
inSettings.Update(scene, delta);
controls.update();
dirty = true;
setTimeout(()=>update(timeNew), 50);
}
const render = inTime =>
{
if(dirty)
{
renderer.render(scene, camera);
dirty = false;
}
requestAnimationFrame(render);
}
const resize = () =>
{
let canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
};
scene.add(light);
//scene.add(new T.GridHelper(10, 10));
light.position.set(-1, 2, 4);
camera.position.set(0, 0, 2);
controls.update();
inSettings.Init(scene);
window.addEventListener("resize", resize);
resize();
render(0);
update(new Date());
};