RotateNode.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import TempNode from '../core/TempNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import {
  4. addNodeElement,
  5. nodeProxy,
  6. vec4,
  7. mat2,
  8. mat4,
  9. } from '../shadernode/ShaderNode.js';
  10. import { cos, sin } from '../math/MathNode.js';
  11. class RotateNode extends TempNode {
  12. constructor( positionNode, rotationNode ) {
  13. super();
  14. this.positionNode = positionNode;
  15. this.rotationNode = rotationNode;
  16. }
  17. getNodeType( builder ) {
  18. return this.positionNode.getNodeType( builder );
  19. }
  20. setup( builder ) {
  21. const { rotationNode, positionNode } = this;
  22. const nodeType = this.getNodeType( builder );
  23. if ( nodeType === 'vec2' ) {
  24. const cosAngle = rotationNode.cos();
  25. const sinAngle = rotationNode.sin();
  26. const rotationMatrix = mat2(
  27. cosAngle, sinAngle,
  28. sinAngle.negate(), cosAngle
  29. );
  30. return rotationMatrix.mul( positionNode );
  31. } else {
  32. const rotation = rotationNode;
  33. const rotationXMatrix = mat4( vec4( 1.0, 0.0, 0.0, 0.0 ), vec4( 0.0, cos( rotation.x ), sin( rotation.x ).negate(), 0.0 ), vec4( 0.0, sin( rotation.x ), cos( rotation.x ), 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) );
  34. const rotationYMatrix = mat4( vec4( cos( rotation.y ), 0.0, sin( rotation.y ), 0.0 ), vec4( 0.0, 1.0, 0.0, 0.0 ), vec4( sin( rotation.y ).negate(), 0.0, cos( rotation.y ), 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) );
  35. const rotationZMatrix = mat4( vec4( cos( rotation.z ), sin( rotation.z ).negate(), 0.0, 0.0 ), vec4( sin( rotation.z ), cos( rotation.z ), 0.0, 0.0 ), vec4( 0.0, 0.0, 1.0, 0.0 ), vec4( 0.0, 0.0, 0.0, 1.0 ) );
  36. return rotationXMatrix.mul( rotationYMatrix ).mul( rotationZMatrix ).mul( vec4( positionNode, 1.0 ) ).xyz;
  37. }
  38. }
  39. }
  40. export default RotateNode;
  41. export const rotate = nodeProxy( RotateNode );
  42. addNodeElement( 'rotate', rotate );
  43. addNodeClass( 'RotateNode', RotateNode );