Skip to content

Commit 88e5171

Browse files
committed
fix tree dragging
1 parent 2fd3372 commit 88e5171

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

src/components/tree/TreeCanvas.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export default function TreeCanvas(props: PanCanvasOpts) {
4141
ctx: gl,
4242
nauting: nauting,
4343
scale,
44-
offset
44+
offset,
45+
resol: [props.w, props.h]
4546
})
4647
}
4748

@@ -54,10 +55,13 @@ export default function TreeCanvas(props: PanCanvasOpts) {
5455
}
5556

5657
function onDrag(delta: Point) {
57-
delta.x /= props.w
58-
delta.y /= -props.h
59-
const scaledDelta = mulPoint(delta, Math.exp(scale-1))
60-
setOffset(diffPoints(offset, scaledDelta))
58+
// delta.x /= props.w
59+
// delta.y /= -props.h
60+
// const scaledDelta = mulPoint(delta, Math.exp(scale-1))
61+
// setOffset(diffPoints(offset, scaledDelta))
62+
const escale = 1 / Math.exp(scale-1)
63+
// const escale = 1
64+
setOffset({x: offset.x + delta.x * escale, y: offset.y - delta.y * escale})
6165
}
6266

6367
useEffect(() => {

src/logic/tree/generator.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ interface SquareProps {
7878
depth: number,
7979
branchLong: number
8080
}
81+
8182
export const squareByCoordinates = (props: SquareProps): Square => {
82-
const {x, y, size, branchLong } = props
83-
const p1 = {x: x - size / 2, y: y - (size*branchLong) / 2}
84-
const p2 = {x: x + size / 2, y: y - (size*branchLong) / 2}
85-
const p3 = {x: x + size / 2, y: y + (size*branchLong) / 2}
86-
const p4 = {x: x - size / 2, y: y + (size*branchLong) / 2}
83+
const {x, y, size, branchLong} = props
84+
const p1 = {x: x - size / 2, y: y - (size * branchLong) / 2}
85+
const p2 = {x: x + size / 2, y: y - (size * branchLong) / 2}
86+
const p3 = {x: x + size / 2, y: y + (size * branchLong) / 2}
87+
const p4 = {x: x - size / 2, y: y + (size * branchLong) / 2}
8788

8889
return {points: [p1, p2, p3, p4], number: 1}
8990
}
@@ -99,6 +100,7 @@ interface DrawTreeProps {
99100
nauting: number,
100101
scale: number,
101102
offset: Point
103+
resol: [number, number]
102104
}
103105

104106
function prepareBuffers(gl: WebGLRenderingContext, program: WebGLProgram) {
@@ -135,15 +137,15 @@ export function drawTree(props: DrawTreeProps) {
135137
if (!program) return
136138

137139
prepareBuffers(gl, program);
138-
putVertexUniform(Math.exp(props.scale-1), props.offset, gl, program)
140+
putVertexUniform(Math.exp(props.scale - 1), props.offset, props.resol, gl, program)
139141

140142
function drawSquares(gl: WebGLRenderingContext, blob: PolygonBlob) {
141143
gl.bufferData(gl.ARRAY_BUFFER, blob.vertexBuffer, gl.STATIC_DRAW)
142144

143145
for (let i = 0; i < blob.last; i++) {
144146
const c = color(blob.buffer[i], props.n)
145147
putColor(gl, program as WebGLProgram, c)
146-
gl.drawArrays(gl.TRIANGLE_FAN, i*4, 4)
148+
gl.drawArrays(gl.TRIANGLE_FAN, i * 4, 4)
147149
}
148150

149151
}
@@ -168,7 +170,7 @@ export function drawTree(props: DrawTreeProps) {
168170
function prepareProgram(gl: WebGLRenderingContext): WebGLProgram | null {
169171
const vertexShader = gl.createShader(gl.VERTEX_SHADER)
170172
if (!vertexShader) return null
171-
const fragmentShader= gl.createShader(gl.FRAGMENT_SHADER)
173+
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
172174
if (!fragmentShader) return null;
173175

174176
gl.shaderSource(vertexShader, TreeVert)
@@ -208,16 +210,38 @@ function putColor(gl: WebGLRenderingContext, program: WebGLProgram, color: RGB)
208210
if (!colorLoc) return
209211
gl.uniform3fv(colorLoc, new Float32Array(color.map(c => c / 255)))
210212
}
211-
function putVertexUniform(scale: number, offset: Point, gl: WebGLRenderingContext, program: WebGLProgram) {
213+
214+
function putVertexUniform(scale: number, offset: Point, resol: [number, number], gl: WebGLRenderingContext, program: WebGLProgram) {
212215
const tLoc = gl.getUniformLocation(program, "u_transform")
213216
if (!tLoc) {
214217
console.error("unable to find transform matrix uniform")
215218
return
216219
}
217220
gl.uniformMatrix3fv(tLoc, false, new Float32Array([
218-
scale, 0, offset.x,
219-
0, scale, offset.y,
220-
0, 0, 0
221+
scale, 0, 0,
222+
0, scale, 0,
223+
0, 0, 1
221224
]))
225+
// const scaleId = gl.getUniformLocation(program, "scale")
226+
// if (!scaleId) {
227+
// console.error("unable to find scale uniform")
228+
// return
229+
// }
230+
// console.log(scale)
231+
// gl.uniform1f(scaleId, scale)
232+
233+
const positionId = gl.getUniformLocation(program, "position")
234+
if (!positionId) {
235+
console.error("unable to find offset uniform")
236+
return
237+
}
238+
gl.uniform2f(positionId, offset.x, offset.y)
222239

240+
const resolId = gl.getUniformLocation(program, "resol")
241+
if (!resolId) {
242+
console.error("unable to find offset uniform")
243+
return
244+
}
245+
console.log(resol)
246+
gl.uniform2f(resolId, resol[0], resol[1])
223247
}

src/logic/tree/shader.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ void main() {
99
export const TreeVert = `
1010
attribute vec2 a_position;
1111
uniform mat3 u_transform;
12+
uniform vec2 resol;
13+
uniform vec2 position;
1214
void main() {
13-
vec3 ext = vec3(a_position, 1.0);
15+
vec2 pos = a_position - position / resol.xy - vec2(-0.5);
16+
vec3 ext = vec3(pos, 1.0);
1417
ext = ext * u_transform;
1518
gl_Position = vec4(ext.x, ext.y, 0.0, 1.0);
1619
}

0 commit comments

Comments
 (0)