PaintViveController.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. */
  4. import {
  5. CanvasTexture,
  6. CircleBufferGeometry,
  7. Color,
  8. DoubleSide,
  9. Group,
  10. IcosahedronBufferGeometry,
  11. Mesh,
  12. MeshBasicMaterial,
  13. Shape,
  14. ShapeBufferGeometry
  15. } from "../../../build/three.module.js";
  16. import { ViveController } from "../vr/ViveController.js";
  17. var PaintViveController = function ( id ) {
  18. ViveController.call( this, id );
  19. var PI2 = Math.PI * 2;
  20. var MODES = { COLOR: 0, SIZE: 1 };
  21. var mode = MODES.COLOR;
  22. var color = new Color( 1, 1, 1 );
  23. var size = 1.0;
  24. //
  25. function generateHueTexture() {
  26. var canvas = document.createElement( 'canvas' );
  27. canvas.width = 256;
  28. canvas.height = 256;
  29. var context = canvas.getContext( '2d' );
  30. var imageData = context.getImageData( 0, 0, 256, 256 );
  31. var data = imageData.data;
  32. var swatchColor = new Color();
  33. for ( var i = 0, j = 0; i < data.length; i += 4, j ++ ) {
  34. var x = ( ( j % 256 ) / 256 ) - 0.5;
  35. var y = ( Math.floor( j / 256 ) / 256 ) - 0.5;
  36. swatchColor.setHSL( Math.atan2( y, x ) / PI2, 1, ( 0.5 - Math.sqrt( x * x + y * y ) ) * 2.0 );
  37. data[ i + 0 ] = swatchColor.r * 256;
  38. data[ i + 1 ] = swatchColor.g * 256;
  39. data[ i + 2 ] = swatchColor.b * 256;
  40. data[ i + 3 ] = 256;
  41. }
  42. context.putImageData( imageData, 0, 0 );
  43. return new CanvasTexture( canvas );
  44. }
  45. // COLOR UI
  46. var geometry = new CircleBufferGeometry( 1, 32 );
  47. var material = new MeshBasicMaterial( { map: generateHueTexture() } );
  48. var colorUI = new Mesh( geometry, material );
  49. colorUI.position.set( 0, 0.005, 0.0495 );
  50. colorUI.rotation.x = - 1.45;
  51. colorUI.scale.setScalar( 0.02 );
  52. this.add( colorUI );
  53. var geometry = new IcosahedronBufferGeometry( 0.1, 2 );
  54. var material = new MeshBasicMaterial();
  55. material.color = color;
  56. var ball = new Mesh( geometry, material );
  57. colorUI.add( ball );
  58. // SIZE UI
  59. var sizeUI = new Group();
  60. sizeUI.position.set( 0, 0.005, 0.0495 );
  61. sizeUI.rotation.x = - 1.45;
  62. sizeUI.scale.setScalar( 0.02 );
  63. this.add( sizeUI );
  64. var triangleShape = new Shape();
  65. triangleShape.moveTo( 0, - 1 );
  66. triangleShape.lineTo( 1, 1 );
  67. triangleShape.lineTo( - 1, 1 );
  68. var geometry = new ShapeBufferGeometry( triangleShape );
  69. var material = new MeshBasicMaterial( { color: 0x222222, wireframe: true } );
  70. var sizeUIOutline = new Mesh( geometry, material );
  71. sizeUIOutline.position.z = 0.001;
  72. resizeTriangleGeometry( sizeUIOutline.geometry, 1.0 );
  73. sizeUI.add( sizeUIOutline );
  74. var geometry = new ShapeBufferGeometry( triangleShape );
  75. var material = new MeshBasicMaterial( { side: DoubleSide } );
  76. material.color = color;
  77. var sizeUIFill = new Mesh( geometry, material );
  78. sizeUIFill.position.z = 0.0011;
  79. resizeTriangleGeometry( sizeUIFill.geometry, 0.5 );
  80. sizeUI.add( sizeUIFill );
  81. sizeUI.visible = false;
  82. function onAxisChanged( event ) {
  83. if ( this.getButtonState( 'thumbpad' ) === false ) return;
  84. var x = event.axes[ 0 ] / 2.0;
  85. var y = - event.axes[ 1 ] / 2.0;
  86. if ( mode === MODES.COLOR ) {
  87. color.setHSL( Math.atan2( y, x ) / PI2, 1, ( 0.5 - Math.sqrt( x * x + y * y ) ) * 2.0 );
  88. ball.position.set( event.axes[ 0 ], event.axes[ 1 ], 0 );
  89. }
  90. if ( mode === MODES.SIZE ) {
  91. var ratio = 0.5 - y;
  92. size = ratio * 2;
  93. resizeTriangleGeometry( sizeUIFill.geometry, ratio );
  94. }
  95. }
  96. function resizeTriangleGeometry( geometry, ratio ) {
  97. var x = 0, y = 0;
  98. var fullWidth = 0.75, fullHeight = 1.5;
  99. var angle = Math.atan( ( fullWidth / 2 ) / fullHeight );
  100. var bottomY = y - fullHeight / 2;
  101. var height = fullHeight * ratio;
  102. var width = ( Math.tan( angle ) * height ) * 2;
  103. var position = geometry.attributes.position;
  104. position.setXYZ( 0, x, bottomY, 0 );
  105. position.setXYZ( 1, x + width / 2, bottomY + height, 0 );
  106. position.setXYZ( 2, x - width / 2, bottomY + height, 0 );
  107. position.needsUpdate = true;
  108. }
  109. function onGripsDown() {
  110. if ( mode === MODES.COLOR ) {
  111. mode = MODES.SIZE;
  112. colorUI.visible = false;
  113. sizeUI.visible = true;
  114. return;
  115. }
  116. if ( mode === MODES.SIZE ) {
  117. mode = MODES.COLOR;
  118. colorUI.visible = true;
  119. sizeUI.visible = false;
  120. return;
  121. }
  122. }
  123. this.getColor = function () {
  124. return color;
  125. };
  126. this.getSize = function () {
  127. return size;
  128. };
  129. this.addEventListener( 'axischanged', onAxisChanged );
  130. this.addEventListener( 'gripsdown', onGripsDown );
  131. };
  132. PaintViveController.prototype = Object.create( ViveController.prototype );
  133. PaintViveController.prototype.constructor = PaintViveController;
  134. export { PaintViveController };