ios - Custom Geometry Mesh -- How to Triangulate in Order to Look Smooth -
edit: altering subdivision count seems have smoothened surface. however, surface seems have shrunk... can't seem it's normal size. why happen , how can fix effect?
i'm hoping draw hundreds of smooth-looking planes sequentially in 3d space model data. these planes different, each 1 given 4 corners of plane -- plus arbitrary point in middle of plane give surface slight curvature aesthetic appeal.
thanks site, i've triangulated surface somewhat, looks pyramid. here's have far:
var vertices = [scnvector3(x: -5, y: 5, z: 0), scnvector3(x: 0, y: 5, z: -5), scnvector3(x: 5, y: 5, z: 0), scnvector3(x: 0, y: 5, z: 5), scnvector3(x: 0, y: 7, z: 0)] var indices : [cint] = [4,0,1, 4,1,2, 4,2,3, 4,3,0] var normals = [scnvector3]() var normalmap = [int : scnvector3]() (var = 0; < vertices.count; i++) { var origvec = scnvector3(x: 0.0, y: 0.0, z: 0.0) normalmap.updatevalue(origvec, forkey: i) } //calculate face normals --> (var = 0; < vertices.count - 2; i++) { var p1 = vertices[i] //b var p2 = vertices[i + 1] //a var p3 = vertices[vertices.count - 1] //c var vector1 = scnvector3(x: 0, y: 0, z: 0) var vector2 = scnvector3(x: 0, y: 0, z: 0) vector1.x = p1.x - p2.x vector1.y = p1.y - p2.y vector1.z = p1.z - p2.z vector2.x = p3.x - p2.x vector2.y = p3.y - p2.y vector2.z = p3.z - p2.z var facenormal = crossproduct(vector1, v2: vector2) ((normalmap[i]!).x) += facenormal.x ((normalmap[i]!).y) += facenormal.y ((normalmap[i]!).z) += facenormal.z ((normalmap[i + 1]!).x) += facenormal.x ((normalmap[i + 1]!).y) += facenormal.y ((normalmap[i + 1]!).z) += facenormal.z ((normalmap[vertices.count - 1]!).x) += facenormal.x ((normalmap[vertices.count - 1]!).y) += facenormal.y ((normalmap[vertices.count - 1]!).z) += facenormal.z } (var = 0; < vertices.count; i++) { normals.append(normalize(normalmap[i]!)) } //vertex source --> var vertexsource = scngeometrysource(vertices: vertices, count: 5) //normal source --> var normalsource = scngeometrysource(normals: normals, count: 5) //indices data --> var data = nsdata(bytes: indices, length: countelements(indices) * sizeof(int)) //geometry element --> var geometryelement = scngeometryelement(data: data, primitivetype: .triangles, primitivecount: 4, bytesperindex: sizeof(cint)) //geometry --> var geometry = scngeometry(sources: [vertexsource, normalsource], elements: [geometryelement]) geometry.firstmaterial?.doublesided = true let node = scnnode(geometry: geometry) self.rootnode.addchildnode(node)
since pyramid-ish shape smooth, need hardwire many more triangles? if so, what's "magic number?"
or, there way automate triangulation process?
or, perhaps barking wrong tree , merely need improve of model lighting or shading or of nature. altering subdivision count offer sort of help?
thanks , help; have never built custom geometries before.
the usual way achieve smooth results without subdivision average normals of vertices touching adjacent planes. ie, average of normals in same location. not clear question whether acceptable solution or whether each plane needs distinguishable.
here link describing process (look @ bottom): http://www.lighthouse3d.com/opengl/terrain/index.php3?normals
in conclusion, each vertex should have average normal vector of surrounding polygons.
Comments
Post a Comment