From fb8a697cece8fc9f3b07f314d0988b6e354664bf Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sun, 27 Jan 2019 16:48:47 +0100 Subject: splash page init, add obj2ply script --- megapixels/commands/misc/obj2ply.py | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 megapixels/commands/misc/obj2ply.py (limited to 'megapixels/commands') diff --git a/megapixels/commands/misc/obj2ply.py b/megapixels/commands/misc/obj2ply.py new file mode 100644 index 00000000..e3e18e54 --- /dev/null +++ b/megapixels/commands/misc/obj2ply.py @@ -0,0 +1,79 @@ +#!/usr/bin/python + +""" +Convert an OBJ 3D model (with vertex color) to a PLY file, which can be read by the draco_encoder. +""" + +import click + +@click.command() +@click.add_argument('--float_colors', action='store_true', help='pass if RGB colors are floats, not ints, in the obj') +@click.add_argument('--unwind', action='store_true', help='pass to reverse winding order on faces (if surface normals are upside down)') +@click.add_argument('--flip_y', action='store_true', help='flip Y axis') +@click.add_argument('-i', '--input_fn', required=True, help='input OBJ filename') +@click.add_argument('-o', '--output_fn', help='output PLY filename') +@click.pass_context +def cli(ctx, float_colors, unwind, flip_y, input_fn, output_fn): + """ + click command for converting OBJ to PLY + """ + + ply_header = """ply + format ascii 1.0 + element vertex {} + property float x + property float y + property float z + property uchar red + property uchar green + property uchar blue + element face {} + property list uchar int vertex_index + end_header + """ + + if output_fn is None: + output_fn = input_fn.replace('.obj', '.ply') + + with open(input_fn, 'r') as f: + i = 0 + vertexes = [] + faces = [] + for line in f.readlines(): + N = line.strip().split(' ') + if N[0] == 'v': + if flip_y: + N[2] = str(float(N[2]) * -1) + if float_colors: + vertexes.append([ + N[1], + N[2], + N[3], + str(int(255 * float(N[4]))), + str(int(255 * float(N[5]))), + str(int(255 * float(N[6]))), + ]) + else: + vertexes.append(N[1:]) + if N[0] == 'f': + if unwind: + faces.append([ + "3", + str(int(N[3]) - 1), + str(int(N[2]) - 1), + str(int(N[1]) - 1), + ]) + else: + faces.append([ + "3", + str(int(N[1]) - 1), + str(int(N[2]) - 1), + str(int(N[3]) - 1), + ]) + + with open(output_fn, 'w') as out_file: + out_file.write(ply_header.format(len(vertexes), len(faces))) + for v in vertexes: + out_file.write(" ".join(v) + "\n") + for f in faces: + out_file.write(" ".join(f) + "\n") -- cgit v1.2.3-70-g09d2 From 0c0c2ec2ecfd64b16ff6ca0e69142223615f0c36 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 28 Jan 2019 13:53:44 +0100 Subject: fix obj2ply, add may and xi --- client/splash/constants.js | 2 +- client/splash/face/markers.js | 4 +- client/splash/face/mesh.js | 153 +++++++++++++++++------------------- client/splash/renderer.js | 2 +- megapixels/commands/misc/obj2ply.py | 10 +-- site/assets/data/faces/may.drc | Bin 0 -> 175998 bytes site/assets/data/faces/xi.drc | Bin 0 -> 157325 bytes 7 files changed, 82 insertions(+), 89 deletions(-) create mode 100644 site/assets/data/faces/may.drc create mode 100644 site/assets/data/faces/xi.drc (limited to 'megapixels/commands') diff --git a/client/splash/constants.js b/client/splash/constants.js index c250ea4a..5b7309b4 100644 --- a/client/splash/constants.js +++ b/client/splash/constants.js @@ -19,7 +19,7 @@ export const CLOUD_TEXT_MAX_SIZE = 0.04 /* face */ -export const FACE_SCALE = 0.0008 +export const FACE_SCALE = 0.00038 /* face markers */ diff --git a/client/splash/face/markers.js b/client/splash/face/markers.js index 1832824c..1636349c 100644 --- a/client/splash/face/markers.js +++ b/client/splash/face/markers.js @@ -78,5 +78,5 @@ export function swap(face) { } export function update() { - group.rotation.y += 0.005 -} \ No newline at end of file + // group.rotation.y += 0.005 +} diff --git a/client/splash/face/mesh.js b/client/splash/face/mesh.js index 61c4e498..efa9530e 100644 --- a/client/splash/face/mesh.js +++ b/client/splash/face/mesh.js @@ -10,17 +10,21 @@ const dracoLoader = new DRACOLoader() DRACOLoader.getDecoderModule() export function load(name) { - dracoLoader.load('/assets/data/faces/' + name + '.drc', (geometry) => { - geometry.computeVertexNormals() - - const material = new MeshBasicMaterial({ vertexColors: VertexColors }) - const mesh = new Mesh(geometry, material) - scene.add(mesh) - console.log(name) - - // Release the cached decoder module. - DRACOLoader.releaseDecoderModule() - }) + dracoLoader.setVerbosity(1) + dracoLoader.setDrawMode(TrianglesDrawMode) + dracoLoader.setSkipDequantization('position', true) + dracoLoader.load('/assets/data/faces/' + name + '.drc', dracoDidLoad) + // dracoLoader.load('/assets/data/faces/' + name + '.drc', (geometry) => { + // geometry.computeVertexNormals() + + // const material = new MeshBasicMaterial({ vertexColors: VertexColors }) + // const mesh = new Mesh(geometry, material) + // scene.add(mesh) + // console.log(name) + + // // Release the cached decoder module. + // DRACOLoader.releaseDecoderModule() + // }) } export function update(name) { @@ -45,76 +49,65 @@ function setDequantizationForMaterial(material, bufferGeometry) { } } -export function loadFromFile(buf) { - // Enable logging to console output. - dracoLoader.setVerbosity(1) - - // To use triangle strips use: - // dracoLoader.setDrawMode(THREE.TriangleStripDrawMode) - dracoLoader.setDrawMode(TrianglesDrawMode) - - // Skip dequantization of the position attribute. It will be done on the GPU. - dracoLoader.setSkipDequantization('position', true) - dracoLoader.decodeDracoFile(buf, (bufferGeometry) => { - // if (dracoLoader.decode_time !== undefined) { - // fileDisplayArea.innerText = 'Decode time = ' + dracoLoader.decode_time + '\n' + - // 'Import time = ' + dracoLoader.import_time - // } - const material = new MeshBasicMaterial({ vertexColors: VertexColors }) - material.wireframe = true - // If the position attribute is quantized, modify the material to perform - // dequantization on the GPU. - if (bufferGeometry.attributes.position.isQuantized) { - setDequantizationForMaterial(material, bufferGeometry) - } - - let geometry - // Point cloud does not have face indices. - if (bufferGeometry.index === null) { - geometry = new Points(bufferGeometry, material) - } else { - if (bufferGeometry.attributes.normal === undefined) { - const geometryHelper = new GeometryHelper() - geometryHelper.computeVertexNormals(bufferGeometry) - } - geometry = new Mesh(bufferGeometry, material) - geometry.drawMode = dracoLoader.drawMode - } +export function dracoDidLoad(bufferGeometry) { + // if (dracoLoader.decode_time !== undefined) { + // fileDisplayArea.innerText = 'Decode time = ' + dracoLoader.decode_time + '\n' + + // 'Import time = ' + dracoLoader.import_time + // } + const material = new MeshBasicMaterial({ vertexColors: VertexColors }) + material.wireframe = true + // If the position attribute is quantized, modify the material to perform + // dequantization on the GPU. + if (bufferGeometry.attributes.position.isQuantized) { + setDequantizationForMaterial(material, bufferGeometry) + } - // Compute range of the geometry coordinates for proper rendering. - bufferGeometry.computeBoundingBox() - if (bufferGeometry.attributes.position.isQuantized) { - // If the geometry is quantized, transform the bounding box to the dequantized - // coordinates. - const posAttribute = bufferGeometry.attributes.position - const normConstant = posAttribute.maxRange / (1 << posAttribute.numQuantizationBits) - const minPos = posAttribute.minValues - bufferGeometry.boundingBox.max.x = minPos[0] + bufferGeometry.boundingBox.max.x * normConstant - bufferGeometry.boundingBox.max.y = minPos[1] + bufferGeometry.boundingBox.max.y * normConstant - bufferGeometry.boundingBox.max.z = minPos[2] + bufferGeometry.boundingBox.max.z * normConstant - bufferGeometry.boundingBox.min.x = minPos[0] + bufferGeometry.boundingBox.min.x * normConstant - bufferGeometry.boundingBox.min.y = minPos[1] + bufferGeometry.boundingBox.min.y * normConstant - bufferGeometry.boundingBox.min.z = minPos[2] + bufferGeometry.boundingBox.min.z * normConstant + let geometry + // Point cloud does not have face indices. + if (bufferGeometry.index === null) { + geometry = new Points(bufferGeometry, material) + } else { + if (bufferGeometry.attributes.normal === undefined) { + const geometryHelper = new GeometryHelper() + geometryHelper.computeVertexNormals(bufferGeometry) } - const sizeX = bufferGeometry.boundingBox.max.x - bufferGeometry.boundingBox.min.x - const sizeY = bufferGeometry.boundingBox.max.y - bufferGeometry.boundingBox.min.y - const sizeZ = bufferGeometry.boundingBox.max.z - bufferGeometry.boundingBox.min.z - const diagonalSize = Math.sqrt(sizeX * sizeX + sizeY * sizeY + sizeZ * sizeZ) - const scale = 1.0 / diagonalSize - const midX = (bufferGeometry.boundingBox.min.x + bufferGeometry.boundingBox.max.x) / 2 - const midY = (bufferGeometry.boundingBox.min.y + bufferGeometry.boundingBox.max.y) / 2 - const midZ = (bufferGeometry.boundingBox.min.z + bufferGeometry.boundingBox.max.z) / 2 - - geometry.scale.multiplyScalar(scale) - geometry.position.x = -midX * scale - geometry.position.y = -midY * scale - geometry.position.z = -midZ * scale - // geometry.castShadow = true - // geometry.receiveShadow = true + geometry = new Mesh(bufferGeometry, material) + geometry.drawMode = dracoLoader.drawMode + } - const selectedObject = scene.getObjectByName("my_mesh") - scene.remove(selectedObject) - geometry.name = "my_mesh" - scene.add(geometry) - }) + // Compute range of the geometry coordinates for proper rendering. + bufferGeometry.computeBoundingBox() + if (bufferGeometry.attributes.position.isQuantized) { + // If the geometry is quantized, transform the bounding box to the dequantized + // coordinates. + const posAttribute = bufferGeometry.attributes.position + const normConstant = posAttribute.maxRange / (1 << posAttribute.numQuantizationBits) + const minPos = posAttribute.minValues + bufferGeometry.boundingBox.max.x = minPos[0] + bufferGeometry.boundingBox.max.x * normConstant + bufferGeometry.boundingBox.max.y = minPos[1] + bufferGeometry.boundingBox.max.y * normConstant + bufferGeometry.boundingBox.max.z = minPos[2] + bufferGeometry.boundingBox.max.z * normConstant + bufferGeometry.boundingBox.min.x = minPos[0] + bufferGeometry.boundingBox.min.x * normConstant + bufferGeometry.boundingBox.min.y = minPos[1] + bufferGeometry.boundingBox.min.y * normConstant + bufferGeometry.boundingBox.min.z = minPos[2] + bufferGeometry.boundingBox.min.z * normConstant + } + const sizeX = bufferGeometry.boundingBox.max.x - bufferGeometry.boundingBox.min.x + const sizeY = bufferGeometry.boundingBox.max.y - bufferGeometry.boundingBox.min.y + const sizeZ = bufferGeometry.boundingBox.max.z - bufferGeometry.boundingBox.min.z + const diagonalSize = Math.sqrt(sizeX * sizeX + sizeY * sizeY + sizeZ * sizeZ) + const scale = 1.0 / diagonalSize + const midX = (bufferGeometry.boundingBox.min.x + bufferGeometry.boundingBox.max.x) / 2 + const midY = (bufferGeometry.boundingBox.min.y + bufferGeometry.boundingBox.max.y) / 2 + const midZ = (bufferGeometry.boundingBox.min.z + bufferGeometry.boundingBox.max.z) / 2 + + geometry.scale.multiplyScalar(scale) + geometry.position.x = -midX * scale + geometry.position.y = -midY * scale + geometry.position.z = -midZ * scale + // geometry.castShadow = true + // geometry.receiveShadow = true + + const selectedObject = scene.getObjectByName("my_mesh") + scene.remove(selectedObject) + geometry.name = "my_mesh" + scene.add(geometry) } diff --git a/client/splash/renderer.js b/client/splash/renderer.js index 369d02fa..e71a4526 100644 --- a/client/splash/renderer.js +++ b/client/splash/renderer.js @@ -5,7 +5,7 @@ import { FOG_COLOR } from './constants' export const scene = new THREE.Scene() scene.fog = new THREE.Fog(FOG_COLOR, 2, 15) -export const camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.01, 15) +export const camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.001, 15) camera.position.set(3, 0.15, 3) // export const camera = new THREE.PerspectiveCamera(70, w / h, 1, 10000) diff --git a/megapixels/commands/misc/obj2ply.py b/megapixels/commands/misc/obj2ply.py index e3e18e54..8d0cec60 100644 --- a/megapixels/commands/misc/obj2ply.py +++ b/megapixels/commands/misc/obj2ply.py @@ -7,11 +7,11 @@ Convert an OBJ 3D model (with vertex color) to a PLY file, which can be read by import click @click.command() -@click.add_argument('--float_colors', action='store_true', help='pass if RGB colors are floats, not ints, in the obj') -@click.add_argument('--unwind', action='store_true', help='pass to reverse winding order on faces (if surface normals are upside down)') -@click.add_argument('--flip_y', action='store_true', help='flip Y axis') -@click.add_argument('-i', '--input_fn', required=True, help='input OBJ filename') -@click.add_argument('-o', '--output_fn', help='output PLY filename') +@click.option('-c', '--float_colors/--int_colors', 'float_colors', default=False, help='pass if RGB colors are floats, not ints, in the obj') +@click.option('-u', '--unwind/--wind', 'unwind', default=False, help='pass to reverse winding order on faces (if surface normals are upside down)') +@click.option('-y', '--flip_y/--no_flip_y', 'flip_y', default=False, help='flip Y axis') +@click.option('-i', '--input_fn', required=True, help='input OBJ filename') +@click.option('-o', '--output_fn', help='output PLY filename') @click.pass_context def cli(ctx, float_colors, unwind, flip_y, input_fn, output_fn): """ diff --git a/site/assets/data/faces/may.drc b/site/assets/data/faces/may.drc new file mode 100644 index 00000000..819ebbf6 Binary files /dev/null and b/site/assets/data/faces/may.drc differ diff --git a/site/assets/data/faces/xi.drc b/site/assets/data/faces/xi.drc new file mode 100644 index 00000000..ef90c1c7 Binary files /dev/null and b/site/assets/data/faces/xi.drc differ -- cgit v1.2.3-70-g09d2