customMaterial.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import { TilesRenderer } from '../src/index.js';
  2. import {
  3. Scene,
  4. DirectionalLight,
  5. AmbientLight,
  6. WebGLRenderer,
  7. PerspectiveCamera,
  8. Box3,
  9. OrthographicCamera,
  10. sRGBEncoding,
  11. Group,
  12. ShaderMaterial,
  13. MeshStandardMaterial,
  14. PCFSoftShadowMap,
  15. } from 'three';
  16. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  17. import * as dat from 'three/examples/jsm/libs/dat.gui.module.js';
  18. import Stats from 'three/examples/jsm/libs/stats.module.js';
  19. let camera, controls, scene, renderer, tiles, orthoCamera;
  20. let offsetParent, box, dirLight;
  21. let stats;
  22. const DEFAULT = 0;
  23. const GRADIENT = 1;
  24. const TOPOGRAPHIC_LINES = 2;
  25. const LIGHTING = 3;
  26. const params = {
  27. 'material': DEFAULT,
  28. 'orthographic': false,
  29. };
  30. const gradientShader = {
  31. vertexShader: /* glsl */`
  32. varying vec3 wPosition;
  33. void main() {
  34. #include <begin_vertex>
  35. #include <project_vertex>
  36. wPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;
  37. }
  38. `,
  39. fragmentShader: /* glsl */`
  40. varying vec3 wPosition;
  41. void main() {
  42. float minVal = - 30.0;
  43. float maxVal = 30.0;
  44. float val = ( wPosition.y - minVal ) / ( maxVal - minVal );
  45. vec4 color1 = vec4( 0.149, 0.196, 0.219, 1.0 ) * 0.5;
  46. vec4 color2 = vec4( 1.0 );
  47. gl_FragColor = mix( color1, color2, val );
  48. }
  49. `,
  50. };
  51. const topoShader = {
  52. extensions: {
  53. derivatives: true,
  54. },
  55. vertexShader: /* glsl */`
  56. varying vec3 wPosition;
  57. varying vec3 vViewPosition;
  58. void main() {
  59. #include <begin_vertex>
  60. #include <project_vertex>
  61. wPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;
  62. vViewPosition = - mvPosition.xyz;
  63. }
  64. `,
  65. fragmentShader: /* glsl */`
  66. varying vec3 wPosition;
  67. varying vec3 vViewPosition;
  68. void main() {
  69. // lighting
  70. vec3 fdx = vec3( dFdx( wPosition.x ), dFdx( wPosition.y ), dFdx( wPosition.z ) );
  71. vec3 fdy = vec3( dFdy( wPosition.x ), dFdy( wPosition.y ), dFdy( wPosition.z ) );
  72. vec3 worldNormal = normalize( cross( fdx, fdy ) );
  73. float lighting =
  74. 0.4 +
  75. clamp( dot( worldNormal, vec3( 1.0, 1.0, 1.0 ) ), 0.0, 1.0 ) * 0.5 +
  76. clamp( dot( worldNormal, vec3( - 1.0, 1.0, - 1.0 ) ), 0.0, 1.0 ) * 0.3;
  77. // thickness scale
  78. float upwardness = dot( worldNormal, vec3( 0.0, 1.0, 0.0 ) );
  79. float yInv = saturate( 1.0 - abs( upwardness ) );
  80. float thicknessScale = pow( yInv, 0.4 );
  81. thicknessScale *= 0.25 + 0.5 * ( vViewPosition.z + 1.0 ) / 2.0;
  82. // thickness
  83. float thickness = 0.01 * thicknessScale;
  84. float thickness2 = thickness / 2.0;
  85. float m = mod( wPosition.y, 3.0 );
  86. // soften edge
  87. float center = thickness2;
  88. float dist = clamp( abs( m - thickness2 ) / thickness2, 0.0, 1.0 );
  89. vec4 topoColor = vec4( 0.149, 0.196, 0.219, 1.0 ) * 0.5;
  90. gl_FragColor = mix( topoColor * lighting, vec4( lighting ), dist );
  91. }
  92. `,
  93. };
  94. init();
  95. animate();
  96. function updateMaterial( scene ) {
  97. const materialIndex = parseFloat( params.material );
  98. scene.traverse( c => {
  99. if ( c.isMesh ) {
  100. c.material.dispose();
  101. switch( materialIndex ) {
  102. case DEFAULT:
  103. c.material = c.originalMaterial;
  104. c.material.side = 2;
  105. c.receiveShadow = false;
  106. c.castShadow = false;
  107. break;
  108. case GRADIENT:
  109. c.material = new ShaderMaterial( gradientShader );
  110. c.material.side = 2;
  111. c.receiveShadow = false;
  112. c.castShadow = false;
  113. break;
  114. case TOPOGRAPHIC_LINES:
  115. c.material = new ShaderMaterial( topoShader );
  116. c.material.side = 2;
  117. c.material.flatShading = true;
  118. c.receiveShadow = false;
  119. c.castShadow = false;
  120. break;
  121. case LIGHTING:
  122. c.material = new MeshStandardMaterial();
  123. c.material.side = 2;
  124. c.receiveShadow = true;
  125. c.castShadow = true;
  126. }
  127. }
  128. } );
  129. }
  130. function onLoadModel( scene ) {
  131. scene.traverse( c => {
  132. if ( c.isMesh ) {
  133. c.originalMaterial = c.material;
  134. }
  135. } );
  136. updateMaterial( scene );
  137. }
  138. function init() {
  139. scene = new Scene();
  140. // primary camera view
  141. renderer = new WebGLRenderer( { antialias: true } );
  142. renderer.setPixelRatio( window.devicePixelRatio );
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. renderer.setClearColor( 0x151c1f );
  145. renderer.shadowMap.enabled = true;
  146. renderer.shadowMap.type = PCFSoftShadowMap;
  147. renderer.outputEncoding = sRGBEncoding;
  148. document.body.appendChild( renderer.domElement );
  149. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  150. camera.position.set( 400, 400, 400 );
  151. orthoCamera = new OrthographicCamera();
  152. // controls
  153. controls = new OrbitControls( camera, renderer.domElement );
  154. controls.screenSpacePanning = false;
  155. controls.minDistance = 1;
  156. controls.maxDistance = 2000;
  157. // lights
  158. dirLight = new DirectionalLight( 0xffffff, 1.25 );
  159. dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
  160. dirLight.castShadow = true;
  161. dirLight.shadow.bias = - 0.01;
  162. dirLight.shadow.mapSize.setScalar( 2048 );
  163. const shadowCam = dirLight.shadow.camera;
  164. shadowCam.left = - 200;
  165. shadowCam.bottom = - 200;
  166. shadowCam.right = 200;
  167. shadowCam.top = 200;
  168. shadowCam.updateProjectionMatrix();
  169. scene.add( dirLight );
  170. const ambLight = new AmbientLight( 0xffffff, 0.05 );
  171. scene.add( ambLight );
  172. box = new Box3();
  173. offsetParent = new Group();
  174. scene.add( offsetParent );
  175. // tiles
  176. const url = window.location.hash.replace( /^#/, '' ) || '../data/tileset.json';
  177. tiles = new TilesRenderer( url );
  178. tiles.errorTarget = 2;
  179. tiles.onLoadModel = onLoadModel;
  180. offsetParent.add( tiles.group );
  181. onWindowResize();
  182. window.addEventListener( 'resize', onWindowResize, false );
  183. // GUI
  184. const gui = new dat.GUI();
  185. gui.width = 300;
  186. gui.add( params, 'orthographic' );
  187. gui.add( params, 'material', { DEFAULT, GRADIENT, TOPOGRAPHIC_LINES, LIGHTING } )
  188. .onChange( () => {
  189. tiles.forEachLoadedModel( updateMaterial )
  190. } );
  191. gui.open();
  192. // Stats
  193. stats = new Stats();
  194. stats.showPanel( 0 );
  195. document.body.appendChild( stats.dom );
  196. }
  197. function onWindowResize() {
  198. camera.aspect = window.innerWidth / window.innerHeight;
  199. renderer.setPixelRatio( window.devicePixelRatio );
  200. renderer.setSize( window.innerWidth, window.innerHeight );
  201. camera.updateProjectionMatrix();
  202. updateOrthoCamera();
  203. }
  204. function updateOrthoCamera() {
  205. orthoCamera.position.copy( camera.position );
  206. orthoCamera.rotation.copy( camera.rotation );
  207. const scale = camera.position.distanceTo( controls.target ) / 2.0;
  208. let aspect = window.innerWidth / window.innerHeight;
  209. orthoCamera.left = - aspect * scale;
  210. orthoCamera.right = aspect * scale;
  211. orthoCamera.bottom = - scale;
  212. orthoCamera.top = scale;
  213. orthoCamera.near = camera.near;
  214. orthoCamera.far = camera.far;
  215. orthoCamera.updateProjectionMatrix();
  216. }
  217. function animate() {
  218. requestAnimationFrame( animate );
  219. if ( params.orthographic ) {
  220. tiles.deleteCamera( camera );
  221. tiles.setCamera( orthoCamera );
  222. tiles.setResolutionFromRenderer( orthoCamera, renderer );
  223. } else {
  224. tiles.deleteCamera( orthoCamera );
  225. tiles.setCamera( camera );
  226. tiles.setResolutionFromRenderer( camera, renderer );
  227. }
  228. offsetParent.rotation.set( 0, 0, 0 );
  229. if ( params.up === '-Z' ) {
  230. offsetParent.rotation.x = Math.PI / 2;
  231. }
  232. offsetParent.updateMatrixWorld( true );
  233. // update tiles center
  234. if ( tiles.getBounds( box ) ) {
  235. box.getCenter( tiles.group.position );
  236. tiles.group.position.multiplyScalar( - 1 );
  237. }
  238. // update tiles
  239. window.tiles = tiles;
  240. camera.updateMatrixWorld();
  241. orthoCamera.updateMatrixWorld();
  242. tiles.update();
  243. render();
  244. stats.update();
  245. }
  246. function render() {
  247. updateOrthoCamera();
  248. renderer.render( scene, params.orthographic ? orthoCamera : camera );
  249. }