|
1 | 1 |
|
2 | | -class polyview { |
| 2 | +class polybase { |
3 | 3 | protected __prop_upd: control.FrameCallback; __del: boolean; protected __unDel: boolean; |
4 | 4 |
|
5 | 5 | public init() { } |
@@ -233,7 +233,77 @@ class polyview { |
233 | 233 |
|
234 | 234 | } |
235 | 235 |
|
236 | | -class polymesh extends polyview { |
| 236 | +class polyview extends polybase { |
| 237 | + |
| 238 | + zBuffer: Buffer; |
| 239 | + cBuffer: Buffer; |
| 240 | + buf: Buffer; |
| 241 | + img: Image; |
| 242 | + near: number; |
| 243 | + width: number; |
| 244 | + height: number; |
| 245 | + far: number; |
| 246 | + |
| 247 | + private readonly pos2idx = (a: number, r: number, b: number) => (a * r) + b; |
| 248 | + |
| 249 | + setScene(img: Image) { |
| 250 | + if (!this.img) this.img = img, this.zBuffer = pins.createBuffer(this.img.width * this.img.height), this.cBuffer = pins.createBuffer(this.img.width * this.img.height), this.width = this.img.width, this.height = this.img.height, this.buf = pins.createBuffer(this.height); |
| 251 | + else if (this.img.width !== img.width || this.img.height !== img.height) this.img = img, this.zBuffer = pins.createBuffer(this.img.width * this.img.height), this.cBuffer = pins.createBuffer(this.img.width * this.img.height), this.width = this.img.width, this.height = this.img.height, this.buf = pins.createBuffer(this.height); |
| 252 | + } |
| 253 | + |
| 254 | + setRenderRange(near: number, far: number) { |
| 255 | + if (this.near !== near) this.near = near; |
| 256 | + if (this.far !== far) this.far = far; |
| 257 | + } |
| 258 | + |
| 259 | + private distToUint8(z: number) { |
| 260 | + if (z < this.near) return 0x00; |
| 261 | + if (z > this.far) return 0xff |
| 262 | + return Math.map(z, this.near, this.far, 0x00, 0xff) >> 0; |
| 263 | + } |
| 264 | + |
| 265 | + setDot(x: number, y: number, z: number, c: number, free?: boolean) { |
| 266 | + const i = this.pos2idx(x, this.height, y); |
| 267 | + const zUint8 = this.distToUint8(z); |
| 268 | + if (z <= 0x00 || z >= 0xff) return; |
| 269 | + if (!free && this.zBuffer[i] > zUint8) return; |
| 270 | + this.cBuffer[i] = c; |
| 271 | + this.zBuffer[i] = zUint8; |
| 272 | + } |
| 273 | + |
| 274 | + getPixel(x: number, y: number) { |
| 275 | + return this.cBuffer[this.pos2idx(x, this.height, y)] |
| 276 | + } |
| 277 | + |
| 278 | + getDepth(x: number, y: number) { |
| 279 | + return this.zBuffer[this.pos2idx(x, this.height, y)] |
| 280 | + } |
| 281 | + |
| 282 | + computeMsh(msh: polymesh) { |
| 283 | + |
| 284 | + } |
| 285 | + |
| 286 | + render() { |
| 287 | + for (let x = 0; x < this.width; x++) { |
| 288 | + this.buf.write(-(x * this.height), this.cBuffer); |
| 289 | + this.img.setRows(x, this.buf) |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + reset() { |
| 294 | + this.zBuffer.fill(0); |
| 295 | + this.cBuffer.fill(0); |
| 296 | + this.img.fill(0); |
| 297 | + } |
| 298 | + |
| 299 | + constructor(undel?: boolean) { |
| 300 | + super(undel); |
| 301 | + this.setScene(scene.backgroundImage()) |
| 302 | + } |
| 303 | + |
| 304 | +} |
| 305 | + |
| 306 | +class polymesh extends polybase { |
237 | 307 |
|
238 | 308 | faces: Polymesh.Face[]; points: Polymesh.Vector3[]; pivot: Polymesh.Vector3; |
239 | 309 | flag: { invisible: boolean, cull: boolean, lod: boolean, texStream: boolean }; |
|
0 commit comments