From 6e21320f0963a3fe05909be066ec9534daba9d87 Mon Sep 17 00:00:00 2001 From: Coopus <120044030+CoopusDev@users.noreply.github.com> Date: Sat, 23 Mar 2024 20:06:17 +0800 Subject: [PATCH] fix z-fighting in an example --- site/pages/gallery/Axes.astro | 2 -- src/mobjects/Axes.js | 11 +++++++++-- src/mobjects/FunctionGraph2D.js | 7 ++++--- types.d.ts | 10 ++++++++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/site/pages/gallery/Axes.astro b/site/pages/gallery/Axes.astro index 5e9da920..6438d346 100644 --- a/site/pages/gallery/Axes.astro +++ b/site/pages/gallery/Axes.astro @@ -12,8 +12,6 @@ import Example from "../../layouts/Example.astro"; const axes = new mp.Axes({ xRange: [-10, 10, 1], // You can set the length of every axis like this }); - axes.material = new mp.MobjectMaterial(); - axes.material.depthTest = false; // Draw function figures const figure0 = axes.drawFunction2D((x) => x); diff --git a/src/mobjects/Axes.js b/src/mobjects/Axes.js index 5392c4b6..39ea9c7a 100644 --- a/src/mobjects/Axes.js +++ b/src/mobjects/Axes.js @@ -25,6 +25,8 @@ export default class Axes extends Graph2D { this.yRange = yRange; this.zRange = zRange; + this.graphs = []; + this.addChild(this.xAxis); this.addChild(this.yAxis); this.addChild(this.zAxis); @@ -44,12 +46,17 @@ export default class Axes extends Graph2D { } } - drawFunction2D(func, step = 0.1) { + drawFunction2D(func, { step = 0.1, autoStack = true } = {}) { const range = this.xRange; range[2] = step; - const graph = new FunctionGraph2D(func, { xRange: range }); + const last = this.graphs[this.graphs.length - 1]; + const z = autoStack && last ? last.z + 0.001 : 0.01; + + const graph = new FunctionGraph2D(func, { xRange: range, z: z }); graph.update(); + this.addChild(graph); + this.graphs.push(graph); return graph; } diff --git a/src/mobjects/FunctionGraph2D.js b/src/mobjects/FunctionGraph2D.js index 9a48ad6b..a739a573 100644 --- a/src/mobjects/FunctionGraph2D.js +++ b/src/mobjects/FunctionGraph2D.js @@ -1,10 +1,11 @@ import Graph2D from "./Graph2D.js"; export default class FunctionGraph2D extends Graph2D { - constructor(func = (x) => x, { xRange = [-8, 8, 0.1] } = {}) { + constructor(func = (x) => x, { xRange = [-8, 8, 0.1], z = 0 } = {}) { super(); this.xRange = xRange; this.func = func; + this.z = z; } update() { @@ -14,9 +15,9 @@ export default class FunctionGraph2D extends Graph2D { const func = this.func; this.clear(); - this.move([from, func(from), 0]); + this.move([from, func(from), this.z]); for (let i = from + step; i <= to; i += step) { - this.line([i, func(i), 0]); + this.line([i, func(i), this.z]); } this.stroke(); return this; diff --git a/types.d.ts b/types.d.ts index fc6f0367..f6a7d688 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1445,11 +1445,13 @@ declare module "mobjects/Axis" { } declare module "mobjects/FunctionGraph2D" { export default class FunctionGraph2D extends Graph2D { - constructor(func?: (x: any) => any, { xRange }?: { + constructor(func?: (x: any) => any, { xRange, z }?: { xRange?: number[]; + z?: number; }); xRange: number[]; func: (x: any) => any; + z: number; update(): this; redraw(): this; } @@ -1471,8 +1473,12 @@ declare module "mobjects/Axes" { xRange: number[]; yRange: number[]; zRange: number[]; + graphs: any[]; addTip(): void; - drawFunction2D(func: any, step?: number): FunctionGraph2D; + drawFunction2D(func: any, { step, autoStack }?: { + step?: number; + autoStack?: boolean; + }): FunctionGraph2D; set tickLength(val: number); get tickLength(): number; }