0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

plunkerでopenjscad その11

Posted at

概要

plunkerでopenjscadやってみる。
練習問題やってみた。

練習問題

OBJをパースして、描画せよ。

写真

image.png

サンプルコード

function main() {
	var obj0 = 
"g cube\n" +
"v 0 0 0\n" +
"v 10 0 0\n" +
"v 0 10 0\n" +
"v 10 10 0\n" +
"v 0 0 10\n" +
"v 10 0 10\n" +
"v 0 10 10\n" +
"v 10 10 10\n" +
"vt 0 0\n" +
"vt 0 1\n" +
"vt 1 0\n" +
"vt 1 1\n" +
"vn 0 0 -1\n" +
"vn -1 0 0\n" +
"vn 1 0 0\n" +
"vn 0 -1 0\n" +
"vn 0 1 0\n" +
"vn 0 0 1\n" +
"f 1/1/1 3/2/1 4/3/1 2/4/1\n" +
"f 1/1/2 5/2/2 7/3/2 3/4/2\n" +
"f 2/1/3 4/2/3 8/3/3 6/4/3\n" +
"f 1/1/4 2/2/4 6/3/4 5/4/4\n" +
"f 3/1/5 7/2/5 8/3/5 4/4/5\n" +
"f 5/1/6 6/2/6 8/3/6 7/4/6";
	var points = [];
	var faces = [];
  var t = 0;
  var textArray = obj0.split(/\r\n|\r|\n/);
	for (var i = 0; i < textArray.length; i++)
	{
		var line = textArray[i];
		if (line.indexOf('v ') === 0)
		{
			var tmp = line.split(' ');
			points.push([tmp[1], tmp[2], tmp[3]]);
		t = t + 3;
		}
		else if (line.indexOf('vn ') === 0)
		{
		}
		else if (line.indexOf('f ') === 0)
		{
			var tmp = line.split(' ');
			var p0 = tmp[1].split("/");
			var p1 = tmp[2].split("/");
			var p2 = tmp[3].split("/");
			var p3 = tmp[4].split("/");
			faces.push([p0[0] - 1, p1[0] - 1, p2[0] - 1]);
			faces.push([p0[0] - 1, p2[0] - 1, p3[0] - 1]);
		}
	}
	return CSG.polyhedron({
		points: points,
		faces: faces
	});
}

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?