Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/tiny-creature. three.js journey #16 - tamagotchi flo-bit.dev/tiny-creature
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326import * as PIXI from 'pixi.js';import { AnimatedProperties } from './math-helper';
export type CurveOptions = { scale: number;
upperLipStroke: number; upperLipCurve: number;
upperLipCx1: number; upperLipCy1: number; upperLipCx2: number; upperLipCy2: number;
lowerLipStroke: number; lowerLipCurve: number;
lowerLipCx1: number; lowerLipCy1: number; lowerLipCx2: number; lowerLipCy2: number;
width: number;
colorRed: number; colorGreen: number; colorBlue: number;
x: number; y: number;
angle: number; sharpness: number;};
export default class Mouth { container: PIXI.Container;
graphics: PIXI.Graphics; lips: PIXI.Graphics; // teeth: PIXI.Graphics; // teethMask: PIXI.Graphics;
furMask: PIXI.Graphics;
options: AnimatedProperties<CurveOptions>;
constructor(opts: Partial<CurveOptions>) { this.options = new AnimatedProperties({ scale: opts?.scale ?? 1, upperLipStroke: opts?.upperLipStroke ?? 5, upperLipCurve: opts?.upperLipCurve ?? 10,
upperLipCx1: opts?.upperLipCx1 ?? 0, upperLipCy1: opts?.upperLipCy1 ?? 0, upperLipCx2: opts?.upperLipCx2 ?? 0, upperLipCy2: opts?.upperLipCy2 ?? 0,
lowerLipStroke: opts?.lowerLipStroke ?? 5, lowerLipCurve: opts?.lowerLipCurve ?? 10,
lowerLipCx1: opts?.lowerLipCx1 ?? 0, lowerLipCy1: opts?.lowerLipCy1 ?? 0, lowerLipCx2: opts?.lowerLipCx2 ?? 0, lowerLipCy2: opts?.lowerLipCy2 ?? 0,
width: opts?.width ?? 50,
x: opts?.x ?? 0, y: opts?.y ?? 0, angle: opts?.angle ?? 0, sharpness: opts?.sharpness ?? 0, colorRed: opts?.colorRed ?? 20, colorGreen: opts?.colorGreen ?? 20, colorBlue: opts?.colorBlue ?? 20 });
this.container = new PIXI.Container();
this.lips = new PIXI.Graphics(); this.container.addChild(this.lips);
this.graphics = new PIXI.Graphics(); this.container.addChild(this.graphics);
// this.teethMask = new PIXI.Graphics(); // this.teeth = new PIXI.Graphics(); // this.teeth.addChild(this.teethMask); // // this.teeth.mask = this.teethMask; // this.container.addChild(this.teeth); // this.container.addChild(this.teethMask);
this.furMask = new PIXI.Graphics(); this.furMask.blendMode = 'erase';
this.draw(); }
draw() { const upperLipStroke = this.options.get('upperLipStroke');
const upperLipCx1 = this.options.get('upperLipCx1'); const upperLipCy1 = this.options.get('upperLipCy1'); const upperLipCx2 = this.options.get('upperLipCx2'); const upperLipCy2 = this.options.get('upperLipCy2');
const lowerLipStroke = this.options.get('lowerLipStroke');
const lowerLipCx1 = this.options.get('lowerLipCx1'); const lowerLipCy1 = this.options.get('lowerLipCy1'); const lowerLipCx2 = this.options.get('lowerLipCx2'); const lowerLipCy2 = this.options.get('lowerLipCy2');
const mouthWidth = this.options.get('width'); const scale = this.options.get('scale'); const x = this.options.get('x'); const y = this.options.get('y'); const angle = this.options.get('angle'); const color = new PIXI.Color({ r: this.options.get('colorRed'), g: this.options.get('colorGreen'), b: this.options.get('colorBlue') });
this.graphics.clear(); this.graphics .moveTo((-mouthWidth / 2) * scale, 0) .bezierCurveTo( upperLipCx1 * scale, upperLipCy1 * scale, upperLipCx2 * scale, upperLipCy2 * scale, (mouthWidth / 2) * scale, 0 ) .bezierCurveTo( lowerLipCx1 * scale, lowerLipCy1 * scale, lowerLipCx2 * scale, lowerLipCy2 * scale, -(mouthWidth / 2) * scale, 0 ) .bezierCurveTo( upperLipCx1 * scale, upperLipCy1 * scale, upperLipCx2 * scale, upperLipCy2 * scale, (mouthWidth / 2) * scale, 0 ) .bezierCurveTo( lowerLipCx1 * scale, lowerLipCy1 * scale, lowerLipCx2 * scale, lowerLipCy2 * scale, -(mouthWidth / 2) * scale, 0 );
this.graphics.fill({ color: 0x220000 });
// this.teeth.clear(); // this.teeth // .moveTo((-mouthWidth / 2) * scale, 0) // .quadraticCurveTo(0, upperLipCurve * scale, (mouthWidth / 2) * scale, 0) // .bezierCurveTo( // mouthWidth / 2, // lowerLipCurve * scale * 0.5, // -mouthWidth / 2, // lowerLipCurve * scale * 0.5, // -(mouthWidth / 2) * scale, // 0 // ) // .quadraticCurveTo(0, upperLipCurve * scale, (mouthWidth / 2) * scale, 0);
// this.teethMask.clear(); // for (let i = 0; i < 11; i++) { // // how close to center // const distanceToCenter = 1 - Math.abs(i - 5.5) / 5.5;
// this.teethMask.rect( // (-mouthWidth / 2) * scale + (i * mouthWidth) / 10, // -50, // 15 * distanceToCenter, // 50 // ); // } // this.teethMask.fill({ // color: 0xffffff // });
// const colorStops = [0xa1a1a1, 0xffffff, 0xffffff, 0xa1a1a1];
// const gradient = new PIXI.FillGradient( // (-mouthWidth / 2) * scale, // 0, // (mouthWidth / 2) * scale, // 0 // );
// colorStops.forEach((number, index) => { // const ratio = index / colorStops.length;
// gradient.addColorStop(ratio, number); // }); // this.teeth.fill(gradient);
this.lips.clear(); this.lips .moveTo((-mouthWidth / 2) * scale, 0) .bezierCurveTo( upperLipCx1 * scale, upperLipCy1 * scale, upperLipCx2 * scale, upperLipCy2 * scale, (mouthWidth / 2) * scale, 0 ) .stroke({ color: color, width: upperLipStroke * scale, cap: 'round' });
this.lips .bezierCurveTo( lowerLipCx1 * scale, lowerLipCy1 * scale, lowerLipCx2 * scale, lowerLipCy2 * scale, -(mouthWidth / 2) * scale, 0 ) .stroke({ color: color, width: lowerLipStroke * scale, cap: 'round' });
this.furMask.clear(); this.furMask .moveTo((-mouthWidth / 2) * scale, 0) .bezierCurveTo( upperLipCx1 * scale, upperLipCy1 * scale, upperLipCx2 * scale, upperLipCy2 * scale, (mouthWidth / 2) * scale, 0 ) .bezierCurveTo( lowerLipCx1 * scale, lowerLipCy1 * scale, lowerLipCx2 * scale, lowerLipCy2 * scale, -(mouthWidth / 2) * scale, 0 ) .bezierCurveTo( upperLipCx1 * scale, upperLipCy1 * scale, upperLipCx2 * scale, upperLipCy2 * scale, (mouthWidth / 2) * scale, 0 ) .bezierCurveTo( lowerLipCx1 * scale, lowerLipCy1 * scale, lowerLipCx2 * scale, lowerLipCy2 * scale, -(mouthWidth / 2) * scale, 0 );
this.furMask.fill({ color: 0xffffff });
this.furMask .moveTo((-mouthWidth / 2) * scale, 0) .bezierCurveTo( upperLipCx1 * scale, upperLipCy1 * scale, upperLipCx2 * scale, upperLipCy2 * scale, (mouthWidth / 2) * scale, 0 ) .stroke({ color: 0xffffff, width: upperLipStroke * scale, cap: 'round' });
this.furMask .bezierCurveTo( lowerLipCx1 * scale, lowerLipCy1 * scale, lowerLipCx2 * scale, lowerLipCy2 * scale, -(mouthWidth / 2) * scale, 0 ) .stroke({ color: 0xffffff, width: lowerLipStroke * scale, cap: 'round' });
this.container.angle = angle; this.container.position.set(x * scale, y * scale);
this.furMask.angle = angle; this.furMask.position.set(x * scale, y * scale); }
update(deltaTime: number) { const updated = this.options.update(deltaTime); if (updated) this.draw(); }}