PaintViveController.js 4.2 KB

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