|
| 1 | +import {HtmlCanvasVisual, IRenderContext, NodeStyleBase, Point, Rect, SvgVisual} from 'yfiles' |
| 2 | +import {} from '../elements/raytrace/RaytraceFlowNodeViewModel'; |
| 3 | +import * as _ from 'lodash'; |
| 4 | +import {Material, RayTracer, roundRect, Scene, Sphere, Vector3} from './raytracer.js' |
| 5 | + |
| 6 | +const margin = { |
| 7 | + top: 30, |
| 8 | + right: 3, |
| 9 | + bottom: -20, |
| 10 | + left: 3 |
| 11 | +}; |
| 12 | + |
| 13 | +export class RaytraceVisual extends HtmlCanvasVisual { |
| 14 | + private rayTracer: RayTracer; |
| 15 | + |
| 16 | + private getRendering(scaling) { |
| 17 | + const buffer = this.rayTracer.render(Math.round(scaling*(105 - 2)), Math.round(scaling * (150 - 12))); |
| 18 | + return new Uint8ClampedArray(buffer); |
| 19 | + } |
| 20 | + |
| 21 | + public layout: Rect; |
| 22 | + |
| 23 | + constructor() { |
| 24 | + super(); |
| 25 | + const scene = new Scene(); |
| 26 | + |
| 27 | + // add background sphere |
| 28 | + scene.add( |
| 29 | + new Sphere(new Vector3(10.0, -10004, -20), 10000, |
| 30 | + new Material(new Vector3(0.2, 0.2, 0.2), 0.5, 0, new Vector3())) |
| 31 | + ); |
| 32 | + |
| 33 | + // add sphere |
| 34 | + scene.add( |
| 35 | + new Sphere(new Vector3(0, -1, -20), 3, |
| 36 | + new Material(new Vector3(0.100, 0.32, 0.936), 0.1, 0.05, new Vector3(0.8, 0, 0))) |
| 37 | + ); |
| 38 | + |
| 39 | + // add lights |
| 40 | + scene.add( |
| 41 | + new Sphere(new Vector3(0, 20, -30), 3, |
| 42 | + new Material(new Vector3(0, -20, 0), 0, 0, new Vector3(1.2, 1.2, 1.2))) |
| 43 | + ); |
| 44 | + scene.add( |
| 45 | + new Sphere(new Vector3(10, 20, 10), 3, |
| 46 | + new Material(new Vector3(), 0, 0, new Vector3(1, 1, 1))) |
| 47 | + ); |
| 48 | + |
| 49 | + const backgroundColor = new Vector3(0.7, 0.8, 0.9); |
| 50 | + |
| 51 | + // create ray tracer |
| 52 | + this.rayTracer = new RayTracer(backgroundColor, scene); |
| 53 | + } |
| 54 | + |
| 55 | + paint(renderContext: IRenderContext, ctx: CanvasRenderingContext2D): void { |
| 56 | + if (_.isNil(this.layout)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + const l = this.layout; |
| 60 | + const canvas = renderContext.canvasComponent; |
| 61 | + const viewPoint = canvas.viewPoint; |
| 62 | + const viewPort = canvas.viewport; |
| 63 | + const scaling = 1 / canvas.zoom; |
| 64 | + |
| 65 | + |
| 66 | + ctx.save(); |
| 67 | + roundRect(ctx, l.x, l.y, l.width, l.height, 6, false); |
| 68 | + ctx.clip(); |
| 69 | + |
| 70 | + // background |
| 71 | + ctx.fillStyle = '#6A5656'; |
| 72 | + ctx.fillRect(l.x, l.y, l.width, l.height); |
| 73 | + |
| 74 | + ctx.restore(); |
| 75 | + const factor = canvas.zoom; |
| 76 | + const imageData = ctx.getImageData(l.x, l.y, Math.round(factor*(105 - 2)), Math.round(factor * (150 - 12))); |
| 77 | + imageData.data.set(this.getRendering(factor)); |
| 78 | + // const p = canvas.toViewCoordinates(l) |
| 79 | + ctx.putImageData(imageData, canvas.zoom * (-viewPoint.x + l.x), canvas.zoom * (-viewPoint.y + l.y)); |
| 80 | + // ctx.putImageData(imageData, p.x, p.y); |
| 81 | + |
| 82 | + ctx.save(); |
| 83 | + roundRect(ctx, l.x, l.y, l.width, l.height, 6, false); |
| 84 | + ctx.clip(); |
| 85 | + |
| 86 | + // header |
| 87 | + ctx.fillStyle = 'green'; |
| 88 | + ctx.fillRect(l.x, l.y, l.width, 20); |
| 89 | + ctx.font = '10px sans-serif'; |
| 90 | + ctx.fillStyle = 'white'; |
| 91 | + ctx.fillText('Raytrace', l.x + 7, l.y + 13); |
| 92 | + ctx.restore(); |
| 93 | + |
| 94 | + |
| 95 | + // border |
| 96 | + roundRect(ctx, l.x, l.y, l.width, l.height, 6, false); |
| 97 | + ctx.strokeStyle = '#636363'; |
| 98 | + ctx.lineWidth = 2; |
| 99 | + ctx.stroke(); |
| 100 | + return; |
| 101 | + |
| 102 | + |
| 103 | + // ctx.translate(this.position.x, this.position.y); |
| 104 | + |
| 105 | + ctx.save() |
| 106 | + |
| 107 | + ctx.translate(viewPoint.x, viewPoint.y); |
| 108 | + ctx.scale(scaling, scaling); |
| 109 | + // create scene |
| 110 | + |
| 111 | + |
| 112 | + // get canvas |
| 113 | + const canvasWidth = 105; |
| 114 | + const canvasHeight = 150; |
| 115 | + |
| 116 | + // save start time |
| 117 | + const startTime = Date.now(); |
| 118 | + // ctx.save(); |
| 119 | + // ctx.rect(1, 1, 104, 147); |
| 120 | + // ctx.clip(); |
| 121 | + // ctx.fillStyle = '#6A5656'; |
| 122 | + // ctx.fillRect(0, 0, 105, 150); |
| 123 | + |
| 124 | + |
| 125 | + ctx.fillStyle = 'green'; |
| 126 | + ctx.fillRect(0, 0, 110, 20); |
| 127 | + |
| 128 | + ctx.font = '10px sans-serif'; |
| 129 | + ctx.fillStyle = 'white'; |
| 130 | + ctx.fillText('Raytrace', 7, 13); |
| 131 | + |
| 132 | + // ctx.restore(); |
| 133 | + roundRect(ctx, 0, 0, 105, 150, 6, false); |
| 134 | + ctx.strokeStyle = '#636363'; |
| 135 | + ctx.lineWidth = 2; |
| 136 | + ctx.stroke(); |
| 137 | + ctx.restore() |
| 138 | + const totalDuration = (Date.now() - startTime) / 1000; |
| 139 | + // console.log(`Raytraced in ${totalDuration}s.`) |
| 140 | + } |
| 141 | + |
| 142 | +} |
| 143 | + |
| 144 | +export default class RaytraceNodeStyle extends NodeStyleBase { |
| 145 | + constructor() { |
| 146 | + super(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Creates the visual for a node. |
| 151 | + * @see Overrides {@link NodeStyleBase#createVisual} |
| 152 | + * @return {SvgVisual} |
| 153 | + */ |
| 154 | + createVisual(renderContext, node) { |
| 155 | + const visual = new RaytraceVisual(); |
| 156 | + |
| 157 | + return visual |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Re-renders the node using the old visual for performance reasons. |
| 162 | + * @see Overrides {@link NodeStyleBase#updateVisual} |
| 163 | + * @return {SvgVisual} |
| 164 | + */ |
| 165 | + updateVisual(renderContext, oldVisual, node) { |
| 166 | + const visual = oldVisual as RaytraceVisual; |
| 167 | + visual.layout = node.layout; |
| 168 | + |
| 169 | + return visual; |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments