Compare commits

..

2 Commits

Author SHA1 Message Date
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 148 additions and 110 deletions

View File

@ -19,122 +19,103 @@
</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, inCenterInd, ...inPointsInd) =>
const scene = new T.Scene(); {
const camera = new T.PerspectiveCamera(80, 2, 0.1, 10); let output = [0, 0, 0];
const light = new T.DirectionalLight(0xFFFFFF, 1); let centerInd = inCenterInd*3;
const geometry = new T.BoxGeometry(0.8, 0.8, 0.8);
const shader = new T.MeshPhongMaterial({color:0xFFCC00});
const shaderBlue = new T.MeshPhongMaterial({color:0x0033FF});
const object = new T.Mesh(geometry, shader);
const controls = new OrbitControls(camera, renderer.domElement); for(let i=0; i<inPointsInd.length; i++)
controls.update(); {
let pointInd = inPointsInd[i]*3;
output[0] += inArray[pointInd+0];
output[1] += inArray[pointInd+1];
output[2] += inArray[pointInd+2];
}
scene.add(light); let scalar = inPointsInd.length;
scene.add(object); output[0] /= scalar;
scene.add(new T.GridHelper(10, 10)); output[1] /= scalar;
output[2] /= scalar;
let limit = (inPoint, inCenter) =>
{
let limit = inCenter-0.5;
if(inPoint < limit)
{
return limit;
}
limit = inCenter+0.5;
if(inPoint > limit)
{
return limit;
}
return inPoint;
}
output[0] = limit(output[0], inArray[centerInd+0]);
output[1] = limit(output[1], inArray[centerInd+1]);
output[2] = limit(output[2], inArray[centerInd+2]);
const geoBuff = new T.BufferGeometry(); return output;
geoBuff.setAttribute("position", new T.BufferAttribute(new Float32Array( };
let smooth = (inArray) =>
{
let output = [inArray[0], inArray[1], inArray[2] ];
for(let i=1; i < (inArray.length/3)-1; i++)
{
output.push( ...average(inArray, i, i-1, i, i+1) );
}
output.push(inArray[inArray.length-3], inArray[inArray.length-2], inArray[inArray.length-1]);
return output;
}
let path = (inPoints, inColor) =>
{
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute( inPoints, 3 ));
const material = new THREE.LineBasicMaterial( { color: inColor, linewidth: 3 } );
return new THREE.Line( geometry, material );
}
const points =
[ [
0, 0, 0, 0, 0, 0,
1, 0, 0,
1, 1, 0,
0, 1, 0, 0, 1, 0,
2, 0, 1, 1, 1, 0,
2, 1, 1 2, 1, 0,
] 2, 0, 0,
), 3)); 3, 0, 0,
4, 0, 0,
4, 1, 0
];
geoBuff.setIndex( console.log( smooth(points) );
[
0, 1, 2, let path0 = path(points, 0xFF0000);
2, 3, 0, let path1 = path(smooth(points), 0x009900);
1, 4, 5, let path2 = path(smooth(smooth(points)), 0x00AA00);
5, 2, 1 let path3 = path(smooth(smooth(smooth(points))), 0x00FF00);
]
Scene(
{
DOM:"canvas",
Init(inScene)
{
inScene.add( path0 );
inScene.add( path1 );
inScene.add( path2 );
inScene.add( path3 );
},
Update(scene, delta)
{
}
}
); );
geoBuff.computeVertexNormals();
const geoObj = new T.Mesh(geoBuff, shader);
shader.shading = T.FlatShading;
scene.add(geoObj);
/*
let r = Region.Create();
let filler = (x, y, z) =>
{
r.Voxels[Region.CoordsToIndex(x, y, z)] = true;
};
filler(1, 1, 1);
Kernel.Loop(Kernel.Corners, [1, 1, 1], filler);
Kernel.Loop(Kernel.Cardinal, [1, 1, 1], filler);
Region.Update(r);
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);
camera.position.set(0, 0, 2);
let dirty = false;
const update = inTime =>
{
let timeNew = new Date();
let timeDelta = timeNew - inTime;
let delta = timeDelta * 0.001;
object.rotation.x += delta;
controls.update();
dirty = true;
setTimeout(()=>update(timeNew), 100);
}
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);
};
window.addEventListener("resize", resize);
resize();
render(0);
update(new Date());
</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());
};