|
| 1 | +<head> |
| 2 | +<meta charset=utf-8> |
| 3 | +<style> |
| 4 | +body { margin: 0px; } |
| 5 | +canvas { width:100%; height:100%; overflow: hidden; } |
| 6 | +</style> |
| 7 | +<script type="text/javascript" src="../glutil_min.js"></script> |
| 8 | +<script type="text/javascript" src="../extras/camera.js"></script> |
| 9 | +<script type="text/javascript" src="../extras/path.js"></script> |
| 10 | +<script type="text/javascript" src="../extras/curvetube.js"></script> |
| 11 | +<script type="text/javascript" src="../extras/frame.js"></script> |
| 12 | +<script type="text/javascript" src="demoutil.js"></script> |
| 13 | +<script src="https://peteroupc.github.io/colorpicker/cbox.js"></script> |
| 14 | +</head> |
| 15 | +<body> |
| 16 | +<p style="position:absolute;left:0;top:1em"> |
| 17 | +<a href="javascript:sampleShape(0)">Plane</a>, |
| 18 | +<a href="javascript:sampleShape(1)">Extruded</a>, |
| 19 | +<a href="javascript:sampleShape(2)">Closed figure</a>, |
| 20 | +<a href="javascript:sampleShape(3)">Tube</a> |
| 21 | +<br> |
| 22 | +Color: <input type=color value="#E0E0E0" id=colorsetting> |
| 23 | +</p> |
| 24 | +<canvas id=canvas></canvas> |
| 25 | +<script id="demo"> |
| 26 | +//<!-- |
| 27 | +/* |
| 28 | +Written by Peter O. in 2015. |
| 29 | +
|
| 30 | +Any copyright is dedicated to the Public Domain. |
| 31 | +http://creativecommons.org/publicdomain/zero/1.0/ |
| 32 | +If you like this, you should donate to Peter O. |
| 33 | +at: http://upokecenter.dreamhosters.com/articles/donate-now-2/ |
| 34 | +*/ |
| 35 | + |
| 36 | +var path=new GraphicsPath() |
| 37 | + .moveTo(-1,1,0) |
| 38 | + .lineTo(0,1) |
| 39 | + .arcTo(1,1,1,0,1) |
| 40 | + .lineTo(1,-1) |
| 41 | + .lineTo(0,-1) |
| 42 | + .arcTo(-1,-1,-1,0,1) |
| 43 | + .closePath() |
| 44 | + |
| 45 | +function pathClosedFigure(path, zBottom, zTop, flatness){ |
| 46 | + var mesh=new Mesh() |
| 47 | + mesh.merge(extrudePath(path,zBottom,zTop,flatness)) |
| 48 | + mesh.merge(pathFloor(path,zTop,flatness)) |
| 49 | + mesh.merge(pathFloor(path,zBottom,flatness).reverseWinding().reverseNormals()) |
| 50 | + return mesh |
| 51 | +} |
| 52 | + |
| 53 | +function makeTubeFromPath(path,flatness,thickness,pathSection){ |
| 54 | + var mesh=new Mesh() |
| 55 | + var curves=path.getCurves(flatness) |
| 56 | + var resolution=Math.ceil((curves.getLength()/flatness)/10) |
| 57 | + var curveSection=(pathSection) ? pathSection.getCurves(flatness) : null |
| 58 | + new SurfaceEval() |
| 59 | + .vertex(new CurveTube(curves,thickness,curveSection)) |
| 60 | + .setAutoNormal(true) |
| 61 | + .evalSurface(mesh,Mesh.TRIANGLES,resolution, |
| 62 | + Math.ceil(2*thickness/flatness)); |
| 63 | + return mesh |
| 64 | +} |
| 65 | + |
| 66 | +function pathFloor(path, z, flatness){ |
| 67 | + if(z==null)z=0 |
| 68 | + var tris=path.getTriangles(flatness); |
| 69 | + var mesh=new Mesh().mode(Mesh.TRIANGLES) |
| 70 | + .normal3(0,0,1); |
| 71 | + for(var i=0;i<tris.length;i++){ |
| 72 | + var tri=tris[i] |
| 73 | + mesh.vertex3(tri[0],tri[1],z) |
| 74 | + .vertex3(tri[2],tri[3],z) |
| 75 | + .vertex3(tri[4],tri[5],z) |
| 76 | + } |
| 77 | + return mesh |
| 78 | +} |
| 79 | +function extrudePath(path, zStart, zEnd, flatness){ |
| 80 | + var lines=path.getLines(flatness) |
| 81 | + var mesh=new Mesh().mode(Mesh.TRIANGLES) |
| 82 | + var z1=Math.min(zStart,zEnd) |
| 83 | + var z2=Math.max(zStart,zEnd) |
| 84 | + for(var i=0;i<lines.length;i++){ |
| 85 | + var line=lines[i] |
| 86 | + var dx=line[2]-line[0] |
| 87 | + var dy=line[3]-line[1] |
| 88 | + var dot=dx*dx+dy*dy |
| 89 | + if(dot==0)continue; |
| 90 | + mesh.vertex3(line[0],line[1],z1) |
| 91 | + .vertex3(line[0],line[1],z2) |
| 92 | + .vertex3(line[2],line[3],z1) |
| 93 | + .vertex3(line[2],line[3],z1) |
| 94 | + .vertex3(line[0],line[1],z2) |
| 95 | + .vertex3(line[2],line[3],z2) |
| 96 | + } |
| 97 | + mesh.recalcNormals() |
| 98 | + return mesh |
| 99 | +} |
| 100 | +function createSampleShape(index){ |
| 101 | + switch(index){ |
| 102 | + case 0: |
| 103 | + return pathFloor(path,0,0.01); |
| 104 | + case 1: |
| 105 | + return extrudePath(path,-0.2,0.2,0.01); |
| 106 | + case 2: |
| 107 | + return pathClosedFigure(path,-0.25,0.25,0.01); |
| 108 | + case 3: |
| 109 | + return makeTubeFromPath(path,0.01,0.1); |
| 110 | + default: |
| 111 | + return new Mesh(); |
| 112 | + } |
| 113 | +} |
| 114 | +function sampleShape(num){ |
| 115 | + if(shape)scene.removeShape(shape); |
| 116 | + var cs=document.getElementById("colorsetting").value |
| 117 | + scene.addShape(shape=scene.makeShape(createSampleShape(num)).setColor(cs)) |
| 118 | +} |
| 119 | + // Create the 3D scene; find the HTML canvas and pass it |
| 120 | + // to Scene3D. |
| 121 | + var scene=new Scene3D(document.getElementById("canvas")); |
| 122 | + scene.setClearColor("white"); |
| 123 | + var camera=new Camera(scene,45,1,1000).setDistance(5) |
| 124 | + .turnVertical(-90) |
| 125 | + var xyz=new ShapeGroup(); |
| 126 | + var pc=new FrameCounterDiv(scene) |
| 127 | + var shape=null; |
| 128 | + var axisline=scene.makeShape(Meshes.createCapsule(0.005,10,6,4)); |
| 129 | + var zaxis=axisline.copy().setColor("blue") |
| 130 | + var yaxis=axisline.copy().setColor("green"); |
| 131 | + yaxis.getTransform().setOrientation(90,1,0,0); |
| 132 | + var xaxis=axisline.copy().setColor("red"); |
| 133 | + xaxis.getTransform().setOrientation(90,0,1,0); |
| 134 | + document.getElementById("colorsetting").addEventListener("change", |
| 135 | + function(e){ if(shape)shape.setColor(e.target.value) }); |
| 136 | + xyz.addShape(xaxis).addShape(yaxis).addShape(zaxis); |
| 137 | + scene.addShape(xyz); |
| 138 | + sampleShape(0); |
| 139 | + GLUtil.renderLoop(function(){ |
| 140 | + pc.update(); |
| 141 | + camera.update(); |
| 142 | + scene.render(); |
| 143 | + }); |
| 144 | +//--> |
| 145 | +</script> |
| 146 | +</body> |
0 commit comments