webgpu_materials_toon.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - toon material</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. <meta property="og:title" content="three.js webgpu - toon material">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_materials_toon.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_materials_toon.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  17. <div class="title-wrapper">
  18. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Toon Material</span>
  19. </div>
  20. <small>
  21. Toon material and outline post-processing effect.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { toonOutlinePass } from 'three/tsl';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import { FontLoader } from 'three/addons/loaders/FontLoader.js';
  40. import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
  41. let container;
  42. let camera, scene, renderer, renderPipeline;
  43. let particleLight;
  44. const loader = new FontLoader();
  45. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  46. init( font );
  47. } );
  48. function init( font ) {
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2500 );
  52. camera.position.set( 0.0, 400, 400 * 3.5 );
  53. //
  54. scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0x444488 );
  56. //
  57. renderer = new THREE.WebGPURenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setAnimationLoop( render );
  61. renderer.inspector = new Inspector();
  62. container.appendChild( renderer.domElement );
  63. //
  64. renderPipeline = new THREE.RenderPipeline( renderer );
  65. renderPipeline.outputNode = toonOutlinePass( scene, camera );
  66. // Materials
  67. const cubeWidth = 400;
  68. const numberOfSpheresPerSide = 5;
  69. const sphereRadius = ( cubeWidth / numberOfSpheresPerSide ) * 0.8 * 0.5;
  70. const stepSize = 1.0 / numberOfSpheresPerSide;
  71. const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
  72. for ( let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  73. const colors = new Uint8Array( alphaIndex + 2 );
  74. for ( let c = 0; c <= colors.length; c ++ ) {
  75. colors[ c ] = ( c / colors.length ) * 256;
  76. }
  77. const gradientMap = new THREE.DataTexture( colors, colors.length, 1, THREE.RedFormat );
  78. gradientMap.needsUpdate = true;
  79. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  80. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  81. // basic monochromatic energy preservation
  82. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  83. const material = new THREE.MeshToonNodeMaterial( {
  84. color: diffuseColor,
  85. gradientMap: gradientMap
  86. } );
  87. const mesh = new THREE.Mesh( geometry, material );
  88. mesh.position.x = alpha * 400 - 200;
  89. mesh.position.y = beta * 400 - 200;
  90. mesh.position.z = gamma * 400 - 200;
  91. scene.add( mesh );
  92. }
  93. }
  94. }
  95. function addLabel( name, location ) {
  96. const textGeo = new TextGeometry( name, {
  97. font: font,
  98. size: 20,
  99. depth: 1,
  100. curveSegments: 1
  101. } );
  102. const textMaterial = new THREE.MeshBasicNodeMaterial();
  103. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  104. textMesh.position.copy( location );
  105. scene.add( textMesh );
  106. }
  107. addLabel( '-gradientMap', new THREE.Vector3( - 350, 0, 0 ) );
  108. addLabel( '+gradientMap', new THREE.Vector3( 350, 0, 0 ) );
  109. addLabel( '-diffuse', new THREE.Vector3( 0, 0, - 300 ) );
  110. addLabel( '+diffuse', new THREE.Vector3( 0, 0, 300 ) );
  111. particleLight = new THREE.Mesh(
  112. new THREE.SphereGeometry( 4, 8, 8 ),
  113. new THREE.MeshBasicNodeMaterial( { color: 0xffffff } )
  114. );
  115. scene.add( particleLight );
  116. // Lights
  117. scene.add( new THREE.AmbientLight( 0xc1c1c1, 3 ) );
  118. const pointLight = new THREE.PointLight( 0xffffff, 2, 800, 0 );
  119. particleLight.add( pointLight );
  120. //
  121. const controls = new OrbitControls( camera, renderer.domElement );
  122. controls.minDistance = 200;
  123. controls.maxDistance = 2000;
  124. window.addEventListener( 'resize', onWindowResize );
  125. }
  126. function onWindowResize() {
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. //
  132. function render() {
  133. const timer = Date.now() * 0.00025;
  134. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  135. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  136. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  137. renderPipeline.render();
  138. }
  139. </script>
  140. </body>
  141. </html>
粤ICP备19079148号