UNPKG

xtorcga

Version:

Xtor Compute Geometry Algorithm Libary 计算几何算法库

242 lines (241 loc) 11.9 kB
"use strict"; // // import { Vec3, v3, Quat, Segment } from "./index" // import { createProgram } from './render/gl'; // // var v = new Vec3(); // // v.add(v3(10, 1230, 12)); // // document.write(v.toString()); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); // // var quat = new Quat(); // // document.write(quat.x + "") // // var segment = new Segment(v3(10, 20, 0), v3(100, 100, 0)); // // const distance = v.distanceSegment(segment); // // document.write(JSON.stringify(distance)) // const vs = ` // uniform mat4 u,worldViewProjection; // uniform vec3 u,lightWorldPos; // uniform mat4 u,world; // uniform mat4 u,viewInverse; // uniform mat4 u,worldInverseTranspose; // attribute vec4 position; // attribute vec3 normal; // attribute vec2 texcoord; // varying vec4 v,position; // varying vec2 v,texCoord; // varying vec3 v,normal; // varying vec3 v,surfaceToLight; // varying vec3 v,surfaceToView; // void main() { // v,texCoord = texcoord; // v,position = u,worldViewProjection * position; // v,normal = (u,worldInverseTranspose * vec4(normal, 0)).xyz; // v,surfaceToLight = u,lightWorldPos - (u,world * position).xyz; // v,surfaceToView = (u,viewInverse[3] - (u,world * position)).xyz; // gl,Position = v,position; // } // ` // const fs = ` // varying vec4 v,position; // varying vec2 v,texCoord; // varying vec3 v,normal; // varying vec3 v,surfaceToLight; // varying vec3 v,surfaceToView; // uniform vec4 u,lightColor; // uniform vec4 u,ambient; // uniform sampler2D u,diffuse; // uniform vec4 u,specular; // uniform float u,shininess; // uniform float u,specularFactor; // vec4 lit(float l, float h, float m) { // return vec4(1.0, // max(l, 0.0), // (l > 0.0) ? pow(max(0.0, h), m) : 0.0, // 1.0); // } // void main() { // vec4 diffuseColor = texture2D(u,diffuse, v,texCoord); // vec3 a,normal = normalize(v,normal); // vec3 surfaceToLight = normalize(v,surfaceToLight); // vec3 surfaceToView = normalize(v,surfaceToView); // vec3 halfVector = normalize(surfaceToLight + surfaceToView); // vec4 litR = lit(dot(a,normal, surfaceToLight), // dot(a,normal, halfVector), u,shininess); // vec4 outColor = vec4(( // u,lightColor * (diffuseColor * litR.y + diffuseColor * u,ambient + // u,specular * litR.z * u,specularFactor)).rgb, // diffuseColor.a); // gl,FragColor = outColor; // } // ` // const canvas = document.createElement('canvas'); // const renderer: WebGL2RenderingContext | WebGLRenderingContext = canvas.getContext("webgl2") || canvas.getContext("webgl")!; // const program = createProgram(renderer, { vertexShader: vs, fragmentShader: fs }) // const arrays = { // position: [1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1], // normal: [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1], // texcoord: [1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1], // indices: [0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23], // }; // function render(time: number) { // requestAnimationFrame(render); // } // requestAnimationFrame(render) var cga = __importStar(require("./index")); var Vec3_1 = require("./math/Vec3"); var glview_1 = require("./glview"); var three_1 = require("three"); var extrude_1 = require("./alg/extrude"); // var a = Vec3.fromDegrees(-75.62898254394531, 40.02804946899414, 0.0); // console.log(a); var glv = new glview_1.GLView({ container: document.body }); glv.run(); // var delaunay = new cga.Delaunay() // var vs = [] // var data = [] // for (let i = 0; i < 10000; i++) { // var x = Math.random() * 1000 - 500 // var y = Math.random() * 1000 - 500 // vs.push(new Vec3(x, y, 0)); // data.push(x, y); // } // // // var index = delaunay.triangulation(vs) // // var delaunator = Delaunay.from(data); // // // const delaunay1 = Delaunay.from(data); // // var index = delaunator.triangles; // // const voronoi = delaunator.voronoi([-520, -520, 520, 520]); // // var k = -1; // // var geometry = new Geometry(); // // while (k++ < 10000) { // // var vvs: any = voronoi._clip(k); // // debugger // // for (let i = 0; i < vvs.length; i++) { // // const e0 = vvs[i]; // // const e1 = vvs[(i + 1) % vvs.length]; // // geometry.vertices.push(new Vector3(e0[0], e0[1], 0)); // // geometry.vertices.push(new Vector3(e1[0], e1[1], 0)); // // } // // } // // var geo = toGeometryBuffer(vs, index) // // glv.add(new Mesh(geo, new MeshBasicMaterial({ wireframe: true, side: DoubleSide }))); // // glv.add(new LineSegments(geometry, new LineBasicMaterial({ color: 0xff0000 }))); // // var section = [-1, -1, -1, 1, 1, 1, 1, -1]; // // extrudeNext(section, path, { sectionClosed: true, pathClosed: false, vecdim: 2 }) // var pathx = [v3(-20, 0, 0), v3(-20, 0, 20), v3(20, 0, 20), v3(20, 0, 0)] // var polyline = new Polyline(pathx); // polyline.offset(1) var shape1 = [new cga.Vec3(-10, -10, 0), new cga.Vec3(10, -10, 0), new cga.Vec3(10, 10, 0), new cga.Vec3(-10, 10, 0)]; var path1 = [new cga.Vec3(), new cga.Vec3(0, 0, 100)]; extrude_1.extrude({ shape: shape1, path: path1, enableSmooth: true }); var dizhu = function (bottomR, topR, bh, gh, th) { var bq = []; var tq = []; for (var i = 0; i < 33; i++) { var x = Math.cos(cga.PI_TWO / 32 * i); var z = Math.sin(cga.PI_TWO / 32 * i); bq.push(cga.v3(x, 0, z)); } tq = cga.clone(bq); cga.scale(bq, cga.v3(bottomR, 1, bottomR)); var bq1 = cga.clone(bq); cga.translate(bq1, cga.v3(0, bh, 0)); cga.scale(tq, cga.v3(topR, 1, topR)); var tq1 = cga.clone(tq); cga.translate(tq, cga.v3(0, bh + gh, 0)); cga.translate(tq1, cga.v3(0, bh + gh + th, 0)); var sides = [bq, bq1, cga.clone(bq1), tq, cga.clone(tq), tq1]; var index = { index: 0 }; var geo = cga.linkSides({ shapes: sides, index: index, orgShape: bq }); var geometry = cga.toGeometryBuffer(geo); return geometry; }; var shape = [Vec3_1.v3(-5, 0, 0), Vec3_1.v3(5, 0, 0), Vec3_1.v3(5, 0, 0), Vec3_1.v3(5, 10, 0), Vec3_1.v3(5, 10, 0), Vec3_1.v3(-5, 10, 0), Vec3_1.v3(-5, 10, 0)]; var hole3 = [Vec3_1.v3(-4, 5, 0), Vec3_1.v3(-1, 5, 0), Vec3_1.v3(-1, 5, 0), Vec3_1.v3(-4, 9, 0), Vec3_1.v3(-4, 9, 0), Vec3_1.v3(-4, 5, 0)]; var hole1 = [Vec3_1.v3(1, 5, 0), Vec3_1.v3(4, 5, 0), Vec3_1.v3(4, 5, 0), Vec3_1.v3(4, 9, 0), Vec3_1.v3(4, 9, 0), Vec3_1.v3(1, 9, 0), Vec3_1.v3(1, 9, 0), Vec3_1.v3(1, 5, 0)]; var hole2 = [Vec3_1.v3(-4, 1, 0), Vec3_1.v3(4, 1, 0), Vec3_1.v3(4, 1, 0), Vec3_1.v3(4, 4, 0), Vec3_1.v3(4, 4, 0), Vec3_1.v3(-4, 4, 0), Vec3_1.v3(-4, 4, 0), Vec3_1.v3(-4, 1, 0)]; var holes = [hole1, hole2, hole3]; var path = []; var ia = Math.PI / 100; for (var i = 0; i <= 100; i++) { var x = Math.cos(ia * i) * 50 + 50; var y = Math.sin(ia * i) * 50; path.push(Vec3_1.v3(x, y, 0)); } var geo = extrude_1.extrude({ shape: shape, path: path, right: Vec3_1.Vec3.UnitZ, sealStart: true }); // var geo = dizhu(1.8, 0.9, 0.3, 0.5, 10); var geometry = cga.toGeometryBuffer(geo); geometry.computeVertexNormals(); var THREE = __importStar(require("three")); var breath_light_1 = require("./effect/breath-light"); var Mat4_1 = require("./math/Mat4"); var tgeo = new THREE.BufferGeometry(); tgeo.setAttribute('position', new THREE.Float32BufferAttribute(geometry.getAttribute('position').array, 3)); tgeo.setAttribute('normal', new THREE.Float32BufferAttribute(geometry.getAttribute('normal').array, 3)); tgeo.setAttribute('uv', new THREE.Float32BufferAttribute(geometry.getAttribute('uv').array, 2)); tgeo.setIndex(new THREE.Uint16BufferAttribute(geometry.getIndex().array, 1)); var map = new THREE.TextureLoader().load("./assets/color.jpg"); map.repeat.set(0.4, 0.4); map.wrapT = map.wrapS = THREE.MirroredRepeatWrapping; var renderTarget = new three_1.WebGLRenderTarget(window.innerWidth, window.innerHeight, { depthTexture: new three_1.DepthTexture(window.innerWidth, window.innerHeight) }); var normalMaterial = new three_1.MeshNormalMaterial(); var mesh = new three_1.Mesh(tgeo, new three_1.MeshPhongMaterial({ map: map, side: three_1.FrontSide })); glv.add(mesh); // var mesh1 = new Mesh(new PlaneBufferGeometry(100, 100), new MeshBasicMaterial({ map: renderTarget.depthTexture, side: DoubleSide })); // glv.add(mesh1); // glv.addUpdates(() => { // glv.renderer.setRenderTarget(renderTarget); // glv.render(); // glv.renderer.setRenderTarget(null); // }) var LabelTexture = /** @class */ (function () { function LabelTexture(width, height, sideColor, areaColor) { if (width === void 0) { width = 1; } if (height === void 0) { height = 1; } if (sideColor === void 0) { sideColor = "#1f9ccf"; } if (areaColor === void 0) { areaColor = "#1f9cff1f"; } this.canvas = document.createElement('canvas'); this.ctx = this.canvas.getContext('2d'); this.fontsize = 100; this.width = width; this.height = height; this.sideColor = sideColor; this.areaColor = areaColor; } return LabelTexture; }()); glv.add(new breath_light_1.BreathLight()); var label = new LabelTexture(); var mesh1 = new three_1.Mesh(new three_1.PlaneBufferGeometry(100, 10), new three_1.MeshBasicMaterial({ map: new three_1.CanvasTexture(label.canvas), side: three_1.DoubleSide, transparent: true })); glv.add(mesh1); var shapeMaterial = new three_1.ShaderMaterial({ transparent: true, side: three_1.DoubleSide }); var planeGeo = new three_1.PlaneBufferGeometry(10, 10); planeGeo.translate(0, 5, -1); var mesh = new three_1.Mesh(planeGeo, shapeMaterial); glv.add(mesh); var m = new Mat4_1.Mat4(); var geo = extrude_1.extrude({ shape: shape, path: path, right: Vec3_1.Vec3.UnitZ, sealStart: true }); var geometry = cga.toGeometryBuffer(geo); geometry.computeVertexNormals(); var tgeo = new THREE.BufferGeometry(); tgeo.setAttribute('position', new THREE.Float32BufferAttribute(geometry.getAttribute('position').array, 3)); tgeo.setAttribute('normal', new THREE.Float32BufferAttribute(geometry.getAttribute('normal').array, 3)); tgeo.setAttribute('uv', new THREE.Float32BufferAttribute(geometry.getAttribute('uv').array, 2)); tgeo.setIndex(new THREE.Uint16BufferAttribute(geometry.getIndex().array, 1)); var mesh = new three_1.Mesh(tgeo, new three_1.MeshPhongMaterial({}));