webgl_raycast_texture.html 10.0 KB

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