From 729aaa09c24bf1ea17bb5140fb239c82650b7944 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Wed, 8 Apr 2015 12:26:38 +0200 Subject: [PATCH] Smooth shading on perspective spheres --- js/BufferGeometryToGeometry.js | 30 ++++++++++++++++++++++++++++++ scene/index.html | 1 + scene/js/main.js | 5 ++++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 js/BufferGeometryToGeometry.js diff --git a/js/BufferGeometryToGeometry.js b/js/BufferGeometryToGeometry.js new file mode 100644 index 0000000..46303c0 --- /dev/null +++ b/js/BufferGeometryToGeometry.js @@ -0,0 +1,30 @@ +var toGeometry = function (bg) { + // The following snipped is from Mattatz - Masatatsu Nakamura http://mattatz.org + var attrib = bg.getAttribute('position'); + if(attrib === undefined) { + throw new Error('a given BufferGeometry object must have a position attribute.'); + } + + var positions = attrib.array; + + var vertices = []; + + for(var i = 0, n = positions.length; i < n; i += 3) { + var x = positions[i]; + var y = positions[i + 1]; + var z = positions[i + 2]; + vertices.push(new THREE.Vector3(x, y, z)); + } + + var faces = []; + + for(var i = 0, n = vertices.length; i < n; i += 3) { + faces.push(new THREE.Face3(i, i + 1, i + 2)); + } + + var geometry = new THREE.Geometry(); + geometry.vertices = vertices; + geometry.faces = faces; + geometry.computeFaceNormals(); + return geometry; +} diff --git a/scene/index.html b/scene/index.html index afda122..a7b9d56 100644 --- a/scene/index.html +++ b/scene/index.html @@ -20,6 +20,7 @@ + diff --git a/scene/js/main.js b/scene/js/main.js index 57f9b1a..ffe6ecb 100644 --- a/scene/js/main.js +++ b/scene/js/main.js @@ -135,11 +135,14 @@ function loadScene() { if (child instanceof THREE.Mesh ) { child.material.color.setHex(colors[i]); child.up = new THREE.Vector3(0,0,1); - child.geometry.computeVertexNormals(); child.translateX(positions[i].x); child.translateY(positions[i].y); child.translateZ(positions[i].z); new_id = child.id; + child.geometry = toGeometry(child.geometry); + child.geometry.mergeVertices(); + child.geometry.computeFaceNormals(); + child.geometry.computeVertexNormals(); } }); spheres[i] = object;