From af63d11f548ffb77045df7e46fdb49f9ccd6e33c Mon Sep 17 00:00:00 2001 From: martinRenou Date: Sat, 14 Dec 2019 00:25:40 +0100 Subject: [PATCH] Add Warp support --- ipygany/__init__.py | 2 +- ipygany/ipygany.py | 15 ++++++++++++ src/widget.ts | 57 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/ipygany/__init__.py b/ipygany/__init__.py index d67b1a6..fe6f2b5 100644 --- a/ipygany/__init__.py +++ b/ipygany/__init__.py @@ -8,7 +8,7 @@ PolyMesh, TetraMesh, PointCloud, Scene, Data, Component, - Alpha, IsoColor, Threshold, IsoSurface + Alpha, IsoColor, Threshold, IsoSurface, Warp ) from ._version import __version__, version_info # noqa diff --git a/ipygany/ipygany.py b/ipygany/ipygany.py index 9bdde35..4c0a49d 100644 --- a/ipygany/ipygany.py +++ b/ipygany/ipygany.py @@ -371,6 +371,21 @@ def __init__(self, parent, **kwargs): super(Effect, self).__init__(parent=parent, **kwargs) +class Warp(Effect): + """A warp effect to another block.""" + + _model_name = Unicode('WarpModel').tag(sync=True) + + input = Union((Tuple(trait=Unicode, minlen=2, maxlen=2), Unicode(), CFloat(0.))).tag(sync=True) + + offset = Union((Tuple(trait=Unicode, minlen=3, maxlen=3), CFloat(0.)), default_value=0.).tag(sync=True) + factor = Union((Tuple(trait=Unicode, minlen=3, maxlen=3), CFloat(0.)), default_value=1.).tag(sync=True) + + @default('input') + def _default_input(self): + return self.parent.data[0].name + + class Alpha(Effect): """An transparency effect to another block.""" diff --git a/src/widget.ts b/src/widget.ts index 495fed1..52b5593 100644 --- a/src/widget.ts +++ b/src/widget.ts @@ -23,7 +23,7 @@ import { Data, Component, Block, Effect, PolyMesh, TetraMesh, PointCloud, - Alpha, IsoColor, IsoSurface, Threshold + Warp, Alpha, IsoColor, IsoSurface, Threshold } from 'ganyjs'; @@ -347,6 +347,57 @@ abstract class EffectModel extends BlockModel { } +export +class WarpModel extends EffectModel { + + defaults() { + return {...super.defaults(), + _model_name: WarpModel.model_name, + }; + } + + get input () { + return this.get('input'); + } + + get offset () : THREE.Vector3 { + const offset = this.get('offset'); + + if (typeof offset == 'number') { + return new THREE.Vector3(offset, offset, offset); + } else { + return new THREE.Vector3(offset[0], offset[1], offset[2]); + } + } + + get factor () { + const factor = this.get('factor'); + + if (typeof factor == 'number') { + return new THREE.Vector3(factor, factor, factor); + } else { + return new THREE.Vector3(factor[0], factor[1], factor[2]);; + } + } + + createBlock () { + return new Warp(this.parent.block, this.input, this.factor, this.offset); + } + + initEventListeners () : void { + super.initEventListeners(); + + this.on('change:factor', () => { this.block.factor = this.factor; }); + this.on('change:offset', () => { this.block.offset = this.offset; }); + } + + block: Warp; + + static model_name = 'WarpModel'; + +} + + export class AlphaModel extends EffectModel { @@ -405,8 +456,8 @@ class IsoColorModel extends EffectModel { initEventListeners () : void { super.initEventListeners(); - this.on('change:min', () => { this.block.min = this.min }); - this.on('change:max', () => { this.block.max = this.max }); + this.on('change:min', () => { this.block.min = this.min; }); + this.on('change:max', () => { this.block.max = this.max; }); } block: IsoColor;