import { Vector3D } from '@awayjs/core'; import { ShaderRegisterCache, ShaderRegisterElement } from '@awayjs/stage'; import { ShaderBase, AnimationRegisterData } from '@awayjs/renderer'; import { ParticleProperties } from '../data/ParticleProperties'; import { ParticlePropertiesMode } from '../data/ParticlePropertiesMode'; import { ParticleOscillatorState } from '../states/ParticleOscillatorState'; import { ParticleAnimationSet } from '../ParticleAnimationSet'; import { AnimatorBase } from '../AnimatorBase'; import { ParticleNodeBase } from './ParticleNodeBase'; /** * A particle animation node used to control the position of a particle over time using simple harmonic motion. */ export class ParticleOscillatorNode extends ParticleNodeBase { /** @private */ public _iOscillator: Vector3D; /** * Reference for ocsillator node properties on a single particle (when in local property mode). * Expects a Vector3D object representing the axis (x,y,z) and cycle speed (w) of the motion on the particle. */ public static OSCILLATOR_VECTOR3D: string = 'OscillatorVector3D'; /** * Creates a new ParticleOscillatorNode * * @param mode Defines whether the mode of operation acts on local properties of a particle or global properties of the node. * @param [optional] oscillator Defines the default oscillator axis (x, y, z) and cycleDuration (w) of the node, used when in global mode. */ constructor(mode: number, oscillator: Vector3D = null) { super('ParticleOscillator', mode, 4); this._pStateClass = ParticleOscillatorState; this._iOscillator = oscillator || new Vector3D(); } /** * @inheritDoc */ public getAGALVertexCode(shader: ShaderBase, animationSet: ParticleAnimationSet, registerCache: ShaderRegisterCache, animationRegisterData: AnimationRegisterData): string { const oscillatorRegister: ShaderRegisterElement = (this._pMode == ParticlePropertiesMode.GLOBAL) ? registerCache.getFreeVertexConstant() : registerCache.getFreeVertexAttribute(); animationRegisterData.setRegisterIndex(this, ParticleOscillatorState.OSCILLATOR_INDEX, oscillatorRegister.index); const temp: ShaderRegisterElement = registerCache.getFreeVertexVectorTemp(); const dgree: ShaderRegisterElement = new ShaderRegisterElement(temp.regName, temp.index, 0); const sin: ShaderRegisterElement = new ShaderRegisterElement(temp.regName, temp.index, 1); const cos: ShaderRegisterElement = new ShaderRegisterElement(temp.regName, temp.index, 2); registerCache.addVertexTempUsages(temp, 1); const temp2: ShaderRegisterElement = registerCache.getFreeVertexVectorTemp(); const distance: ShaderRegisterElement = new ShaderRegisterElement(temp2.regName, temp2.index); registerCache.removeVertexTempUsage(temp); let code: string = ''; code += 'mul ' + dgree + ',' + animationRegisterData.vertexTime + ',' + oscillatorRegister + '.w\n'; code += 'sin ' + sin + ',' + dgree + '\n'; code += 'mul ' + distance + '.xyz,' + sin + ',' + oscillatorRegister + '.xyz\n'; code += 'add ' + animationRegisterData.positionTarget + '.xyz,' + distance + '.xyz,' + animationRegisterData.positionTarget + '.xyz\n'; if (animationSet.needVelocity) { code += 'cos ' + cos + ',' + dgree + '\n'; code += 'mul ' + distance + '.xyz,' + cos + ',' + oscillatorRegister + '.xyz\n'; code += 'add ' + animationRegisterData.velocityTarget + '.xyz,' + distance + '.xyz,' + animationRegisterData.velocityTarget + '.xyz\n'; } return code; } /** * @inheritDoc */ public getAnimationState(animator: AnimatorBase): ParticleOscillatorState { return animator.getAnimationState(this); } /** * @inheritDoc */ public _iGeneratePropertyOfOneParticle(param: ParticleProperties): void { //(Vector3D.x,Vector3D.y,Vector3D.z) is oscillator axis, Vector3D.w is oscillator cycle duration const drift: Vector3D = param[ParticleOscillatorNode.OSCILLATOR_VECTOR3D]; if (!drift) throw (new Error('there is no ' + ParticleOscillatorNode.OSCILLATOR_VECTOR3D + ' in param!')); this._pOneData[0] = drift.x; this._pOneData[1] = drift.y; this._pOneData[2] = drift.z; if (drift.w <= 0) throw (new Error('the cycle duration must greater than zero')); this._pOneData[3] = Math.PI * 2 / drift.w; } }