RotateNode.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import TempNode from '../core/TempNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import {
  4. addNodeElement,
  5. nodeProxy,
  6. vec4,
  7. mat4,
  8. } from '../shadernode/ShaderNode.js';
  9. import { cos, sin } from '../math/MathNode.js';
  10. class RotateNode extends TempNode {
  11. constructor( positionNode, rotationNode ) {
  12. super( 'vec3' );
  13. this.positionNode = positionNode;
  14. this.rotationNode = rotationNode;
  15. }
  16. setup() {
  17. const { rotationNode, positionNode } = this;
  18. const rotation = rotationNode;
  19. 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 ) );
  20. 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 ) );
  21. 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 ) );
  22. return rotationXMatrix.mul( rotationYMatrix ).mul( rotationZMatrix ).mul( vec4( positionNode, 1.0 ) ).xyz;
  23. }
  24. }
  25. export default RotateNode;
  26. export const rotate = nodeProxy( RotateNode );
  27. addNodeElement( 'rotate', rotate );
  28. addNodeClass( 'RotateNode', RotateNode );