怀表 发表于 2022-5-12 18:41:52

魔方模拟器twisty-sphere

本帖最后由 怀表 于 2022-5-12 23:32 编辑

https://github.com/worldmaker18349276/twisty-sphere
没有文档,我也搞不清楚是怎么自定义的。从例子看很炫酷。不过没边框很难看清块。

切割好像有两个方法sliceBySphere和sliceByPlane。不过球面切割生成魔方很慢,把resolution参数调小可提高速度。

调了半天弄出了个斜转,等明天再来试一下球面切割。cube = CSG.cube({center: , radius: })
  .intersect(CSG.cube({center: [ 0,-1, 0], radius: }).setColor(1,1,1))
  .intersect(CSG.cube({center: [ 0, 1, 0], radius: }).setColor(1,1,0))
  .intersect(CSG.cube({center: [-1, 0, 0], radius: }).setColor(0,0,1))
  .intersect(CSG.cube({center: [ 1, 0, 0], radius: }).setColor(0,1,0))
  .intersect(CSG.cube({center: [ 0, 0,-1], radius: }).setColor(1,0,0))
  .intersect(CSG.cube({center: [ 0, 0, 1], radius: }).setColor(1,0.6,0.2));
var puzzle = new ModeledSphPuzzle(cube);

puzzle.sliceByPlane(,0)
puzzle.sliceByPlane([-1,1,1],0)
puzzle.sliceByPlane([-1,-1,1],0)
puzzle.sliceByPlane([-1,-1,-1],0)
puzzle.sliceByPlane([-1,1,-1],0)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane(,0)
puzzle.clean();
puzzle.prepare();

var world = new ModeledSphPuzzleWorld(puzzle, "display");cube = CSG.cube({center: , radius: })
  .intersect(CSG.cube({center: [ 0,-1, 0], radius: }).setColor(1,1,1))
  .intersect(CSG.cube({center: [ 0, 1, 0], radius: }).setColor(1,1,0))
  .intersect(CSG.cube({center: [-1, 0, 0], radius: }).setColor(0,0,1))
  .intersect(CSG.cube({center: [ 1, 0, 0], radius: }).setColor(0,1,0))
  .intersect(CSG.cube({center: [ 0, 0,-1], radius: }).setColor(1,0,0))
  .intersect(CSG.cube({center: [ 0, 0, 1], radius: }).setColor(1,0.6,0.2));
var puzzle = new ModeledSphPuzzle(cube);


puzzle.sliceByPlane(,0)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane([-1,0,1],0)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane(,0)

puzzle.clean();
puzzle.prepare();

var world = new ModeledSphPuzzleWorld(puzzle, "display")var phi = (Math.sqrt(5) + 1)/2;

function fzy_add(set, elem) {
  set.add(fzy_snap(elem, set));
}
function csort(arr0) {
  var arr_min = arr0.slice();
  var arr_ = arr0.slice();

  for ( let i=0,len=arr0.length; i<len; i++ ) {
    if ( fzy_cmp(arr_, arr_min) < 0 )
      arr_min = arr_.slice();
    arr_.push(arr_.shift());
  }
  return arr_min;
}

var radius = (phi + 1)/Math.sqrt(phi + 2);
var offset = 1;


var faces = new Set();
var vertices = [];
for ( let i=0; i<4; i++ )
  vertices.push(rotate(vertices, ));
faces.add(csort(vertices));
for ( let face of faces ) {
  fzy_add(faces, csort(face.map(v => rotate(v, ))));
  fzy_add(faces, csort(face.map(v => rotate(v, ))));
}

var polygons = [];
for ( let face of faces ) {
  let polygon = new CSG.Polygon(face.map(v => new CSG.Vertex(new CSG.Vector3D(...v))));
  let {x, y, z} = polygon.plane.normal;
  let = .map(v => Math.sign(v) * Math.ceil(Math.abs(v) - 0.01)).map(v => (v+1)/2);
  polygon.setColor(r, g, b);
  polygons.push(polygon);
}
var dodeca = CSG.fromPolygons(polygons);

var puzzle = new ModeledSphPuzzle(dodeca, radius);

puzzle.sliceByPlane(,0)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane([-1,0,1],0)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane(,0)

puzzle.clean(1);
puzzle.prepare();

var world = new ModeledSphPuzzleWorld(puzzle, "display")
感觉跟胡波的正四面体切割群魔方java助手的用法有些像,不过定义多面体得手写,切割的面的法向量也得手写,调配色也很麻烦。

mf06 居然没有打乱功能。

怀表 发表于 2022-5-16 18:17:20

cube = CSG.cube({center: , radius: })
  .intersect(CSG.cube({center: [ 0,-1, 0], radius: }).setColor(1,1,1))
  .intersect(CSG.cube({center: [ 0, 1, 0], radius: }).setColor(1,1,0))
  .intersect(CSG.cube({center: [-1, 0, 0], radius: }).setColor(0,0,1))
  .intersect(CSG.cube({center: [ 1, 0, 0], radius: }).setColor(0,1,0))
  .intersect(CSG.cube({center: [ 0, 0,-1], radius: }).setColor(1,0,0))
  .intersect(CSG.cube({center: [ 0, 0, 1], radius: }).setColor(1,0.6,0.2));
var puzzle = new ModeledSphPuzzle(cube);

var g = 5/3
var radius = Math.sqrt(29)/3
puzzle.sliceBySphere([ g, 0, g], radius)
puzzle.sliceBySphere([ -g,0, g], radius)
puzzle.sliceBySphere([ g,0,-g], radius)
puzzle.sliceBySphere([ -g,0,-g], radius)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane(,0)
puzzle.sliceByPlane([-1,0,1],0)

puzzle.clean();
puzzle.prepare();

var world = new ModeledSphPuzzleWorld(puzzle, "display");
页: [1]
查看完整版本: 魔方模拟器twisty-sphere