webgl_raycast_texture.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js raycast - texture</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. #controls {
  17. position: absolute;
  18. text-align:left;
  19. top: 60px;
  20. left: 5px;
  21. padding: 5px;
  22. }
  23. .control { margin-bottom: 3px; }
  24. input { width: 50px; }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - raycast texture<br>Left to right: buffer geometry - geometry - indexed buffer geometry</div>
  30. <fieldset id="controls">
  31. <legend>Circle</legend>
  32. <div class="control">
  33. WrapS : <select onchange="setwrapS(this)">
  34. <option value="ClampToEdgeWrapping">ClampToEdgeWrapping</option>
  35. <option value="RepeatWrapping" selected>RepeatWrapping</option>
  36. <option value="MirroredRepeatWrapping">MirroredRepeatWrapping</option>
  37. </select>
  38. </div>
  39. <div class="control">
  40. WrapT : <select onchange="setwrapT(this)">
  41. <option value="ClampToEdgeWrapping">ClampToEdgeWrapping</option>
  42. <option value="RepeatWrapping" selected>RepeatWrapping</option>
  43. <option value="MirroredRepeatWrapping">MirroredRepeatWrapping</option>
  44. </select>
  45. </div>
  46. <div class="control">
  47. Offset : X <input type="number" value="0" step="0.05" onchange="setOffsetU(this)" />
  48. Y <input type="number" value="0" step="0.05" onchange="setOffsetV(this)" /><br />
  49. </div>
  50. <div class="control">
  51. Repeat : X <input type="number" value="1" step="0.1" onchange="setRepeatU(this)" />
  52. Y <input type="number" value="1" step="0.1" onchange="setRepeatV(this)" />
  53. </div>
  54. <div class="control">
  55. Rotation : <input type="number" value="0" step="0.1" onchange="setRotation(this)" />
  56. </div>
  57. </fieldset>
  58. <script src="../build/three.js"></script>
  59. <script>
  60. var CanvasTexture = function ( parentTexture ) {
  61. this._canvas = document.createElement( "canvas" );
  62. this._canvas.width = this._canvas.height = 1024;
  63. this._context2D = this._canvas.getContext( "2d" );
  64. if ( parentTexture ) {
  65. this._parentTexture.push( parentTexture );
  66. parentTexture.image = this._canvas;
  67. }
  68. var that = this;
  69. this._background = document.createElement( "img" );
  70. this._background.addEventListener( "load", function () {
  71. that._canvas.width = that._background.naturalWidth;
  72. that._canvas.height = that._background.naturalHeight;
  73. that._crossRadius = Math.ceil( Math.min( that._canvas.width, that._canvas.height / 30 ) );
  74. that._crossMax = Math.ceil( 0.70710678 * that._crossRadius );
  75. that._crossMin = Math.ceil( that._crossMax / 10 );
  76. that._crossThickness = Math.ceil( that._crossMax / 10 );
  77. that._draw();
  78. }, false );
  79. this._background.crossOrigin = '';
  80. this._background.src = "textures/UV_Grid_Sm.jpg";
  81. this._draw();
  82. };
  83. CanvasTexture.prototype = {
  84. constructor: CanvasTexture,
  85. _canvas: null,
  86. _context2D: null,
  87. _xCross: 0,
  88. _yCross: 0,
  89. _crossRadius: 57,
  90. _crossMax: 40,
  91. _crossMin: 4,
  92. _crossThickness: 4,
  93. _parentTexture: [],
  94. addParent: function ( parentTexture ) {
  95. if ( this._parentTexture.indexOf( parentTexture ) === - 1 ) {
  96. this._parentTexture.push( parentTexture );
  97. parentTexture.image = this._canvas;
  98. }
  99. },
  100. setCrossPosition: function ( x, y ) {
  101. this._xCross = x * this._canvas.width;
  102. this._yCross = y * this._canvas.height;
  103. this._draw();
  104. },
  105. _draw: function () {
  106. if ( ! this._context2D ) return;
  107. this._context2D.clearRect( 0, 0, this._canvas.width, this._canvas.height );
  108. // Background.
  109. this._context2D.drawImage( this._background, 0, 0 );
  110. // Yellow cross.
  111. this._context2D.lineWidth = this._crossThickness * 3;
  112. this._context2D.strokeStyle = "#FFFF00";
  113. this._context2D.beginPath();
  114. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross - this._crossMax - 2 );
  115. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross - this._crossMin );
  116. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross + this._crossMin );
  117. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross + this._crossMax + 2 );
  118. this._context2D.moveTo( this._xCross - this._crossMax - 2, this._yCross + this._crossMax + 2 );
  119. this._context2D.lineTo( this._xCross - this._crossMin, this._yCross + this._crossMin );
  120. this._context2D.moveTo( this._xCross + this._crossMin, this._yCross - this._crossMin );
  121. this._context2D.lineTo( this._xCross + this._crossMax + 2, this._yCross - this._crossMax - 2 );
  122. this._context2D.stroke();
  123. for ( var i = 0; i < this._parentTexture.length; i ++ ) {
  124. this._parentTexture[ i ].needsUpdate = true;
  125. }
  126. }
  127. };
  128. var width = window.innerWidth;
  129. var height = window.innerHeight;
  130. var canvas;
  131. var planeTexture, cubeTexture, circleTexture;
  132. var container;
  133. var camera, scene, renderer;
  134. var raycaster = new THREE.Raycaster();
  135. var mouse = new THREE.Vector2();
  136. var onClickPosition = new THREE.Vector2();
  137. init();
  138. render();
  139. function init() {
  140. container = document.getElementById( "container" );
  141. scene = new THREE.Scene();
  142. scene.background = new THREE.Color( 0xeeeeee );
  143. camera = new THREE.PerspectiveCamera( 45, width / height, 1, 1000 );
  144. camera.position.x = - 30;
  145. camera.position.y = 40;
  146. camera.position.z = 50;
  147. camera.lookAt( scene.position );
  148. renderer = new THREE.WebGLRenderer();
  149. renderer.setPixelRatio( window.devicePixelRatio );
  150. renderer.setSize( width, height );
  151. container.appendChild( renderer.domElement );
  152. // A cube, in the middle.
  153. cubeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  154. canvas = new CanvasTexture( cubeTexture );
  155. var cubeMaterial = new THREE.MeshBasicMaterial( { map: cubeTexture } );
  156. var cubeGeometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  157. var uvs = cubeGeometry.attributes.uv.array;
  158. // Set a specific texture mapping.
  159. for ( var i = 0; i < uvs.length; i ++ ) {
  160. uvs[ i ] *= 2;
  161. }
  162. var cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  163. cube.position.x = 4;
  164. cube.position.y = - 5;
  165. cube.position.z = 0;
  166. scene.add( cube );
  167. // A plane on the left.
  168. planeTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.MirroredRepeatWrapping, THREE.MirroredRepeatWrapping );
  169. canvas.addParent( planeTexture );
  170. var planeMaterial = new THREE.MeshBasicMaterial( { map: planeTexture } );
  171. var planeGeometry = new THREE.PlaneBufferGeometry( 25, 25, 1, 1 );
  172. var uvs = planeGeometry.attributes.uv.array;
  173. // Set a specific texture mapping.
  174. for ( var i = 0; i < uvs.length; i ++ ) {
  175. uvs[ i ] *= 2;
  176. }
  177. var plane = new THREE.Mesh( planeGeometry, planeMaterial );
  178. plane.position.x = - 16;
  179. plane.position.y = - 5;
  180. plane.position.z = 0;
  181. scene.add( plane );
  182. // A circle on the right.
  183. circleTexture = new THREE.Texture( undefined, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  184. canvas.addParent( circleTexture );
  185. var circleMaterial = new THREE.MeshBasicMaterial( { map: circleTexture } );
  186. var circleGeometry = new THREE.CircleBufferGeometry( 25, 40, 0, Math.PI * 2 );
  187. var uvs = circleGeometry.attributes.uv.array;
  188. // Set a specific texture mapping.
  189. for ( var i = 0; i < uvs.length; i ++ ) {
  190. uvs[ i ] = ( uvs[ i ] - 0.25 ) * 2;
  191. }
  192. var circle = new THREE.Mesh( circleGeometry, circleMaterial );
  193. circle.position.x = 24;
  194. circle.position.y = - 5;
  195. circle.position.z = 0;
  196. scene.add( circle );
  197. window.addEventListener( 'resize', onWindowResize, false );
  198. container.addEventListener( 'mousemove', onMouseMove, false );
  199. }
  200. function onWindowResize() {
  201. camera.aspect = window.innerWidth / window.innerHeight;
  202. camera.updateProjectionMatrix();
  203. renderer.setSize( window.innerWidth, window.innerHeight );
  204. }
  205. function onMouseMove( evt ) {
  206. evt.preventDefault();
  207. var array = getMousePosition( container, evt.clientX, evt.clientY );
  208. onClickPosition.fromArray( array );
  209. var intersects = getIntersects( onClickPosition, scene.children );
  210. if ( intersects.length > 0 && intersects[ 0 ].uv ) {
  211. var uv = intersects[ 0 ].uv;
  212. intersects[ 0 ].object.material.map.transformUv( uv );
  213. canvas.setCrossPosition( uv.x, uv.y );
  214. }
  215. }
  216. var getMousePosition = function ( dom, x, y ) {
  217. var rect = dom.getBoundingClientRect();
  218. return [ ( x - rect.left ) / rect.width, ( y - rect.top ) / rect.height ];
  219. };
  220. var getIntersects = function ( point, objects ) {
  221. mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
  222. raycaster.setFromCamera( mouse, camera );
  223. return raycaster.intersectObjects( objects );
  224. };
  225. function render() {
  226. requestAnimationFrame( render );
  227. renderer.render( scene, camera );
  228. }
  229. function setwrapS( that ) {
  230. circleTexture.wrapS = THREE[ that.value ];
  231. circleTexture.needsUpdate = true;
  232. }
  233. function setwrapT( that ) {
  234. circleTexture.wrapT = THREE[ that.value ];
  235. circleTexture.needsUpdate = true;
  236. }
  237. function setOffsetU( that ) {
  238. circleTexture.offset.x = parseFloat( that.value );
  239. }
  240. function setOffsetV( that ) {
  241. circleTexture.offset.y = parseFloat( that.value );
  242. }
  243. function setRepeatU( that ) {
  244. circleTexture.repeat.x = parseFloat( that.value );
  245. }
  246. function setRepeatV( that ) {
  247. circleTexture.repeat.y = parseFloat( that.value );
  248. }
  249. function setRotation( that ) {
  250. circleTexture.rotation = parseFloat( that.value );
  251. }
  252. </script>
  253. </body>
  254. </html>