From fc9833d830b919274c6b1a512e99d01b1635be53 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Tue, 16 Jul 2019 10:59:50 +0300 Subject: [PATCH] idk if i like the shadows --- .../simulation/classes/SimulationRenderer.ts | 7 +- .../simulation/helpers/renderGateShadow.ts | 90 ++++++++++--------- src/modules/vector2/helpers/basic.ts | 9 ++ src/modules/vector2/helpers/minmaxVector.ts | 34 +++++++ 4 files changed, 90 insertions(+), 50 deletions(-) create mode 100644 src/modules/vector2/helpers/minmaxVector.ts diff --git a/src/modules/simulation/classes/SimulationRenderer.ts b/src/modules/simulation/classes/SimulationRenderer.ts index c45ff76..36972a4 100644 --- a/src/modules/simulation/classes/SimulationRenderer.ts +++ b/src/modules/simulation/classes/SimulationRenderer.ts @@ -128,7 +128,6 @@ export class SimulationRenderer { // render gates for (const gate of this.simulation.gates) { - renderGate(ctx, gate) if (this.options.shadows.enabled) { renderGateShadow( ctx, @@ -139,7 +138,7 @@ export class SimulationRenderer { ) } - // renderGate(ctx, gate) + renderGate(ctx, gate) } } @@ -162,10 +161,6 @@ export class SimulationRenderer { } else { this.mouseManager.update() } - - // for (const gate of this.simulation.gates) { - // gate.transform.rotation += 0.01 - // } } public getSelected() { diff --git a/src/modules/simulation/helpers/renderGateShadow.ts b/src/modules/simulation/helpers/renderGateShadow.ts index 7a46844..3cd83ea 100644 --- a/src/modules/simulation/helpers/renderGateShadow.ts +++ b/src/modules/simulation/helpers/renderGateShadow.ts @@ -4,13 +4,13 @@ import { drawPolygon } from './drawPolygon' import { vector3, vector2, vector4, vector8 } from '../classes/Transform' import { checkIntersection } from 'line-intersect' import { reverseArray } from './reverseArray' -import { length, add, invert } from '../../vector2/helpers/basic' +import { minVector, maxVector } from '../../vector2/helpers/minmaxVector' +import { relativeTo } from '../../vector2/helpers/basic' export const pointRecivesLight = ( points: vector2[], //this needs to have an even length light: vector3, - index: number, - ctx: CanvasRenderingContext2D + index: number ) => { const point = points[index] const oposittePoint = points[(index + points.length / 2) % points.length] @@ -48,63 +48,65 @@ export const renderGateShadow = ( const points = gate.transform.getPoints() const exposedPoints = points.filter((point, index) => - pointRecivesLight(points, light, index, ctx) + pointRecivesLight(points, light, index) ) let includedPoints = [...points] + if (exposedPoints.length === 4) { + return + } + if (exposedPoints.length === 3) { - let min = Infinity - let current: null | vector2 = null - - for (const point of exposedPoints) { - const size = length( - add(point, invert(light.slice(0, 2) as vector2)) + const minimum = minVector( + ...exposedPoints.map(point => + relativeTo(point, light.slice(0, 2) as vector2) ) + ) - if (size < min) { - min = size - current = point - } - } + includedPoints.splice(includedPoints.indexOf(exposedPoints[minimum]), 1) + } - if (current) { - includedPoints.splice(includedPoints.indexOf(current), 1) - } + if (includedPoints.length === 3) { + const maximum = maxVector( + ...includedPoints.map(point => + relativeTo(point, light.slice(0, 2) as vector2) + ) + ) - if ( - includedPoints[0][1] < light[1] && - includedPoints[1][1] < light[1] && - !(includedPoints[2][1] > light[1]) - ) { + const otherIndices = [(maximum + 1) % 3, (maximum + 2) % 3] + const newIndices = [otherIndices[0], maximum, otherIndices[1]] + + includedPoints = newIndices.map(index => includedPoints[index]) + } + + let projections = includedPoints.map(point => + // ts doesnt let me do [...point, gateHeight] + projectPointOnPlane([point[0], point[1], gateHeight], light) + ) + + if (exposedPoints.length === 2) { + const toProject = includedPoints.filter( + point => exposedPoints.indexOf(point) === -1 + ) + + projections = toProject.map(point => + projectPointOnPlane([point[0], point[1], gateHeight], light) + ) + + includedPoints = reverseArray(exposedPoints) + + const firstIncludedIndex = points.indexOf(toProject[0]) + const firstExposedPointIndex = points.indexOf(includedPoints[0]) + + if ((firstIncludedIndex + 2) % 4 === firstExposedPointIndex % 4) { const temporary = includedPoints[0] includedPoints[0] = includedPoints[1] includedPoints[1] = temporary } } - if (exposedPoints.length === 2) { - includedPoints = points.filter( - point => exposedPoints.indexOf(point) === -1 - ) - } - - const projections = includedPoints.map(point => - // ts doesnt let me do [...point, gateHeight] - projectPointOnPlane([point[0], point[1], gateHeight], light) - ) - const polygon = [includedPoints, reverseArray(projections)].flat() drawPolygon(ctx, polygon) - - ctx.fillStyle = 'red' - for (const point of [...includedPoints, ...projections, light]) { - ctx.beginPath() - ctx.ellipse(point[0], point[1], 10, 10, 0, 0, Math.PI * 2) - ctx.fill() - } - - ctx.strokeStyle = 'yellow' - drawPolygon(ctx, points, false, true) } diff --git a/src/modules/vector2/helpers/basic.ts b/src/modules/vector2/helpers/basic.ts index c3cf2e9..ba49c11 100644 --- a/src/modules/vector2/helpers/basic.ts +++ b/src/modules/vector2/helpers/basic.ts @@ -12,20 +12,29 @@ export const add = (...vectors: vector2[]): vector2 => { return first.map((value, index) => value + othersSum[index]) as vector2 } +// This just vhanges the direction of the vector export const invert = (vector: vector2) => vector.map(val => -val) as vector2 +// This gets the length of a vector export const length = (vector: vector2) => Math.sqrt(vector[0] ** 2 + vector[1] ** 2) +// This multiplies a vector with a scalaer export const multiply = (vector: vector2, scalar: number) => vector.map(val => val * scalar) as vector2 +// This makese the length of the vector 1 export const normalise = (vector: vector2) => { const size = length(vector) return vector.map(val => val / size) as vector2 } +// This changes the length of the vector to some valie export const ofLength = (vector: vector2, l: number) => { return multiply(vector, l / length(vector)) } + +// This returns a vector relative to the other +export const relativeTo = (vector: vector2, other: vector2) => + add(other, invert(vector)) diff --git a/src/modules/vector2/helpers/minmaxVector.ts b/src/modules/vector2/helpers/minmaxVector.ts new file mode 100644 index 0000000..bf9933a --- /dev/null +++ b/src/modules/vector2/helpers/minmaxVector.ts @@ -0,0 +1,34 @@ +import { vector2 } from '../../simulation/classes/Transform' +import { length } from './basic' + +export const minVector = (...vectors: vector2[]) => { + let min = length(vectors[0]) + let current = 0 + + for (let index = 1; index < vectors.length; index++) { + const size = length(vectors[index]) + + if (size < min) { + min = size + current = index + } + } + + return current +} + +export const maxVector = (...vectors: vector2[]) => { + let max = length(vectors[0]) + let current = 0 + + for (let index = 1; index < vectors.length; index++) { + const size = length(vectors[index]) + + if (size > max) { + max = size + current = index + } + } + + return current +}