|
| 1 | +import * as Nodes from 'three/examples/jsm/nodes/Nodes'; |
| 2 | + |
| 3 | +import { |
| 4 | + Effect, Input, InputDimension |
| 5 | +} from '../EffectBlock'; |
| 6 | + |
| 7 | +import { |
| 8 | + Block |
| 9 | +} from '../Block'; |
| 10 | + |
| 11 | +import { |
| 12 | + NodeOperation |
| 13 | +} from '../NodeMesh'; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Transform the vertices positions. The new vertice position is equal to `factor * input * normal + initialPosition` |
| 18 | + **/ |
| 19 | +export |
| 20 | +class WarpByScalar extends Effect { |
| 21 | + |
| 22 | + constructor (parent: Block, input: Input, factor: number) { |
| 23 | + super(parent, input); |
| 24 | + |
| 25 | + this.factorNode = new Nodes.FloatNode(factor); |
| 26 | + |
| 27 | + this.intermediateTransformNode = new Nodes.OperatorNode( |
| 28 | + this.factorNode, |
| 29 | + this.inputNode, |
| 30 | + Nodes.OperatorNode.MUL |
| 31 | + ); |
| 32 | + |
| 33 | + this.transformNode = new Nodes.OperatorNode( |
| 34 | + this.intermediateTransformNode, |
| 35 | + new Nodes.NormalNode(Nodes.NormalNode.WORLD), |
| 36 | + Nodes.OperatorNode.MUL |
| 37 | + ); |
| 38 | + |
| 39 | + this.addTransformNode(NodeOperation.ADD, this.transformNode); |
| 40 | + |
| 41 | + this.buildMaterial(); |
| 42 | + |
| 43 | + this.initialized = true; |
| 44 | + |
| 45 | + // There is no new geometry specific to this effect, we forward the parent event |
| 46 | + this.parent.on('change:geometry', () => { this.trigger('change:geometry'); }); |
| 47 | + |
| 48 | + this.updateMatrix(); |
| 49 | + } |
| 50 | + |
| 51 | + setInput(input?: Input) : void { |
| 52 | + super.setInput(input); |
| 53 | + |
| 54 | + if (this.initialized) { |
| 55 | + this.intermediateTransformNode.b = this.inputNode; |
| 56 | + |
| 57 | + this.buildMaterial(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + set factor (value: number) { |
| 62 | + this.factorNode.value = value; |
| 63 | + } |
| 64 | + |
| 65 | + get factor () { |
| 66 | + return this.factorNode.value; |
| 67 | + } |
| 68 | + |
| 69 | + get inputDimension () : InputDimension { |
| 70 | + return 1; |
| 71 | + } |
| 72 | + |
| 73 | + private initialized: boolean = false; |
| 74 | + |
| 75 | + private factorNode: Nodes.FloatNode; |
| 76 | + private intermediateTransformNode: Nodes.OperatorNode; |
| 77 | + private transformNode: Nodes.OperatorNode; |
| 78 | + |
| 79 | + protected inputNode: Nodes.Node; |
| 80 | + |
| 81 | +} |
0 commit comments