webgl_decals.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - decal splatter</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. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - decal splatter<br/>
  13. click to shoot
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { GUI } from './jsm/libs/dat.gui.module.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  21. import { DecalGeometry } from './jsm/geometries/DecalGeometry.js';
  22. const container = document.getElementById( 'container' );
  23. let renderer, scene, camera, stats;
  24. let mesh;
  25. let raycaster;
  26. let line;
  27. const intersection = {
  28. intersects: false,
  29. point: new THREE.Vector3(),
  30. normal: new THREE.Vector3()
  31. };
  32. const mouse = new THREE.Vector2();
  33. const intersects = [];
  34. const textureLoader = new THREE.TextureLoader();
  35. const decalDiffuse = textureLoader.load( 'textures/decal/decal-diffuse.png' );
  36. const decalNormal = textureLoader.load( 'textures/decal/decal-normal.jpg' );
  37. const decalMaterial = new THREE.MeshPhongMaterial( {
  38. specular: 0x444444,
  39. map: decalDiffuse,
  40. normalMap: decalNormal,
  41. normalScale: new THREE.Vector2( 1, 1 ),
  42. shininess: 30,
  43. transparent: true,
  44. depthTest: true,
  45. depthWrite: false,
  46. polygonOffset: true,
  47. polygonOffsetFactor: - 4,
  48. wireframe: false
  49. } );
  50. const decals = [];
  51. let mouseHelper;
  52. const position = new THREE.Vector3();
  53. const orientation = new THREE.Euler();
  54. const size = new THREE.Vector3( 10, 10, 10 );
  55. const params = {
  56. minScale: 10,
  57. maxScale: 20,
  58. rotate: true,
  59. clear: function () {
  60. removeDecals();
  61. }
  62. };
  63. window.addEventListener( 'load', init );
  64. function init() {
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. container.appendChild( renderer.domElement );
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. scene = new THREE.Scene();
  72. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  73. camera.position.z = 120;
  74. const controls = new OrbitControls( camera, renderer.domElement );
  75. controls.minDistance = 50;
  76. controls.maxDistance = 200;
  77. scene.add( new THREE.AmbientLight( 0x443333 ) );
  78. const dirLight1 = new THREE.DirectionalLight( 0xffddcc, 1 );
  79. dirLight1.position.set( 1, 0.75, 0.5 );
  80. scene.add( dirLight1 );
  81. const dirLight2 = new THREE.DirectionalLight( 0xccccff, 1 );
  82. dirLight2.position.set( - 1, 0.75, - 0.5 );
  83. scene.add( dirLight2 );
  84. const geometry = new THREE.BufferGeometry();
  85. geometry.setFromPoints( [ new THREE.Vector3(), new THREE.Vector3() ] );
  86. line = new THREE.Line( geometry, new THREE.LineBasicMaterial() );
  87. scene.add( line );
  88. loadLeePerrySmith();
  89. raycaster = new THREE.Raycaster();
  90. mouseHelper = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 10 ), new THREE.MeshNormalMaterial() );
  91. mouseHelper.visible = false;
  92. scene.add( mouseHelper );
  93. window.addEventListener( 'resize', onWindowResize );
  94. let moved = false;
  95. controls.addEventListener( 'change', function () {
  96. moved = true;
  97. } );
  98. window.addEventListener( 'pointerdown', function () {
  99. moved = false;
  100. } );
  101. window.addEventListener( 'pointerup', function ( event ) {
  102. if ( moved === false ) {
  103. checkIntersection( event.clientX, event.clientY );
  104. if ( intersection.intersects ) shoot();
  105. }
  106. } );
  107. window.addEventListener( 'pointermove', onPointerMove );
  108. function onPointerMove( event ) {
  109. if ( event.isPrimary ) {
  110. checkIntersection( event.clientX, event.clientY );
  111. }
  112. }
  113. function checkIntersection( x, y ) {
  114. if ( mesh === undefined ) return;
  115. mouse.x = ( x / window.innerWidth ) * 2 - 1;
  116. mouse.y = - ( y / window.innerHeight ) * 2 + 1;
  117. raycaster.setFromCamera( mouse, camera );
  118. raycaster.intersectObject( mesh, false, intersects );
  119. if ( intersects.length > 0 ) {
  120. const p = intersects[ 0 ].point;
  121. mouseHelper.position.copy( p );
  122. intersection.point.copy( p );
  123. const n = intersects[ 0 ].face.normal.clone();
  124. n.transformDirection( mesh.matrixWorld );
  125. n.multiplyScalar( 10 );
  126. n.add( intersects[ 0 ].point );
  127. intersection.normal.copy( intersects[ 0 ].face.normal );
  128. mouseHelper.lookAt( n );
  129. const positions = line.geometry.attributes.position;
  130. positions.setXYZ( 0, p.x, p.y, p.z );
  131. positions.setXYZ( 1, n.x, n.y, n.z );
  132. positions.needsUpdate = true;
  133. intersection.intersects = true;
  134. intersects.length = 0;
  135. } else {
  136. intersection.intersects = false;
  137. }
  138. }
  139. const gui = new GUI();
  140. gui.add( params, 'minScale', 1, 30 );
  141. gui.add( params, 'maxScale', 1, 30 );
  142. gui.add( params, 'rotate' );
  143. gui.add( params, 'clear' );
  144. gui.open();
  145. onWindowResize();
  146. animate();
  147. }
  148. function loadLeePerrySmith() {
  149. const loader = new GLTFLoader();
  150. loader.load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
  151. mesh = gltf.scene.children[ 0 ];
  152. mesh.material = new THREE.MeshPhongMaterial( {
  153. specular: 0x111111,
  154. map: textureLoader.load( 'models/gltf/LeePerrySmith/Map-COL.jpg' ),
  155. specularMap: textureLoader.load( 'models/gltf/LeePerrySmith/Map-SPEC.jpg' ),
  156. normalMap: textureLoader.load( 'models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg' ),
  157. shininess: 25
  158. } );
  159. scene.add( mesh );
  160. mesh.scale.set( 10, 10, 10 );
  161. } );
  162. }
  163. function shoot() {
  164. position.copy( intersection.point );
  165. orientation.copy( mouseHelper.rotation );
  166. if ( params.rotate ) orientation.z = Math.random() * 2 * Math.PI;
  167. const scale = params.minScale + Math.random() * ( params.maxScale - params.minScale );
  168. size.set( scale, scale, scale );
  169. const material = decalMaterial.clone();
  170. material.color.setHex( Math.random() * 0xffffff );
  171. const m = new THREE.Mesh( new DecalGeometry( mesh, position, orientation, size ), material );
  172. decals.push( m );
  173. scene.add( m );
  174. }
  175. function removeDecals() {
  176. decals.forEach( function ( d ) {
  177. scene.remove( d );
  178. } );
  179. decals.length = 0;
  180. }
  181. function onWindowResize() {
  182. camera.aspect = window.innerWidth / window.innerHeight;
  183. camera.updateProjectionMatrix();
  184. renderer.setSize( window.innerWidth, window.innerHeight );
  185. }
  186. function animate() {
  187. requestAnimationFrame( animate );
  188. renderer.render( scene, camera );
  189. stats.update();
  190. }
  191. </script>
  192. </body>
  193. </html>