idk if i like the shadows

This commit is contained in:
Matei Adriel 2019-07-16 10:59:50 +03:00
parent 2872b742fc
commit fc9833d830
4 changed files with 90 additions and 50 deletions

View file

@ -128,7 +128,6 @@ export class SimulationRenderer {
// render gates // render gates
for (const gate of this.simulation.gates) { for (const gate of this.simulation.gates) {
renderGate(ctx, gate)
if (this.options.shadows.enabled) { if (this.options.shadows.enabled) {
renderGateShadow( renderGateShadow(
ctx, ctx,
@ -139,7 +138,7 @@ export class SimulationRenderer {
) )
} }
// renderGate(ctx, gate) renderGate(ctx, gate)
} }
} }
@ -162,10 +161,6 @@ export class SimulationRenderer {
} else { } else {
this.mouseManager.update() this.mouseManager.update()
} }
// for (const gate of this.simulation.gates) {
// gate.transform.rotation += 0.01
// }
} }
public getSelected() { public getSelected() {

View file

@ -4,13 +4,13 @@ import { drawPolygon } from './drawPolygon'
import { vector3, vector2, vector4, vector8 } from '../classes/Transform' import { vector3, vector2, vector4, vector8 } from '../classes/Transform'
import { checkIntersection } from 'line-intersect' import { checkIntersection } from 'line-intersect'
import { reverseArray } from './reverseArray' 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 = ( export const pointRecivesLight = (
points: vector2[], //this needs to have an even length points: vector2[], //this needs to have an even length
light: vector3, light: vector3,
index: number, index: number
ctx: CanvasRenderingContext2D
) => { ) => {
const point = points[index] const point = points[index]
const oposittePoint = points[(index + points.length / 2) % points.length] const oposittePoint = points[(index + points.length / 2) % points.length]
@ -48,63 +48,65 @@ export const renderGateShadow = (
const points = gate.transform.getPoints() const points = gate.transform.getPoints()
const exposedPoints = points.filter((point, index) => const exposedPoints = points.filter((point, index) =>
pointRecivesLight(points, light, index, ctx) pointRecivesLight(points, light, index)
) )
let includedPoints = [...points] let includedPoints = [...points]
if (exposedPoints.length === 4) {
return
}
if (exposedPoints.length === 3) { if (exposedPoints.length === 3) {
let min = Infinity const minimum = minVector(
let current: null | vector2 = null ...exposedPoints.map(point =>
relativeTo(point, light.slice(0, 2) as vector2)
for (const point of exposedPoints) {
const size = length(
add(point, invert(light.slice(0, 2) as vector2))
) )
)
if (size < min) { includedPoints.splice(includedPoints.indexOf(exposedPoints[minimum]), 1)
min = size }
current = point
}
}
if (current) { if (includedPoints.length === 3) {
includedPoints.splice(includedPoints.indexOf(current), 1) const maximum = maxVector(
} ...includedPoints.map(point =>
relativeTo(point, light.slice(0, 2) as vector2)
)
)
if ( const otherIndices = [(maximum + 1) % 3, (maximum + 2) % 3]
includedPoints[0][1] < light[1] && const newIndices = [otherIndices[0], maximum, otherIndices[1]]
includedPoints[1][1] < light[1] &&
!(includedPoints[2][1] > light[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] const temporary = includedPoints[0]
includedPoints[0] = includedPoints[1] includedPoints[0] = includedPoints[1]
includedPoints[1] = temporary 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() const polygon = [includedPoints, reverseArray(projections)].flat()
drawPolygon(ctx, polygon) 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)
} }

View file

@ -12,20 +12,29 @@ export const add = (...vectors: vector2[]): vector2 => {
return first.map((value, index) => value + othersSum[index]) as 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 export const invert = (vector: vector2) => vector.map(val => -val) as vector2
// This gets the length of a vector
export const length = (vector: vector2) => export const length = (vector: vector2) =>
Math.sqrt(vector[0] ** 2 + vector[1] ** 2) Math.sqrt(vector[0] ** 2 + vector[1] ** 2)
// This multiplies a vector with a scalaer
export const multiply = (vector: vector2, scalar: number) => export const multiply = (vector: vector2, scalar: number) =>
vector.map(val => val * scalar) as vector2 vector.map(val => val * scalar) as vector2
// This makese the length of the vector 1
export const normalise = (vector: vector2) => { export const normalise = (vector: vector2) => {
const size = length(vector) const size = length(vector)
return vector.map(val => val / size) as vector2 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) => { export const ofLength = (vector: vector2, l: number) => {
return multiply(vector, l / length(vector)) 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))

View file

@ -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
}