@@ -43,6 +43,9 @@ import type {
4343import { PerspectiveViewRenderLayer } from "#src/perspective_view/render_layer.js" ;
4444import type { ThreeDimensionalRenderLayerAttachmentState } from "#src/renderlayer.js" ;
4545import { update3dRenderLayerAttachment } from "#src/renderlayer.js" ;
46+ import {
47+ SegmentColorShaderManager ,
48+ } from "#src/segment_color.js" ;
4649import {
4750 forEachVisibleSegment ,
4851 getObjectKey ,
@@ -56,7 +59,7 @@ import {
5659import type { WatchableValueInterface } from "#src/trackable_value.js" ;
5760import { makeCachedDerivedWatchableValue } from "#src/trackable_value.js" ;
5861import type { Borrowed , RefCounted } from "#src/util/disposable.js" ;
59- import type { vec4 } from "#src/util/geom.js" ;
62+ import { vec4 } from "#src/util/geom.js" ;
6063import {
6164 getFrustrumPlanes ,
6265 mat3 ,
@@ -68,8 +71,17 @@ import {
6871import * as matrix from "#src/util/matrix.js" ;
6972import { GLBuffer } from "#src/webgl/buffer.js" ;
7073import type { GL } from "#src/webgl/context.js" ;
71- import { parameterizedEmitterDependentShaderGetter } from "#src/webgl/dynamic_shader.js" ;
74+ import {
75+ parameterizedEmitterDependentShaderGetter ,
76+ shaderCodeWithLineDirective ,
77+ WatchableShaderError ,
78+ } from "#src/webgl/dynamic_shader.js" ;
7279import type { ShaderBuilder , ShaderProgram } from "#src/webgl/shader.js" ;
80+ import {
81+ addControlsToBuilder ,
82+ setControlsInShader ,
83+ ShaderControlState ,
84+ } from "#src/webgl/shader_ui_controls.js" ;
7385import type { RPC } from "#src/worker_rpc.js" ;
7486import { registerSharedObjectOwner } from "#src/worker_rpc.js" ;
7587
@@ -349,24 +361,44 @@ export class MeshShaderManager {
349361 }
350362
351363 makeGetter ( layer : RefCounted & { gl : GL ; displayState : MeshDisplayState } ) {
352- const silhouetteRenderingEnabled = layer . registerDisposer (
364+ const parameters = layer . registerDisposer (
353365 makeCachedDerivedWatchableValue (
354- ( x ) => x > 0 ,
355- [ layer . displayState . silhouetteRendering ] ,
366+ ( silhouetteRendering , shaderBuilderState ) => {
367+ return {
368+ silhouetteRenderingEnabled : silhouetteRendering > 0 ,
369+ shaderBuilderState,
370+ } ;
371+ } ,
372+ [
373+ layer . displayState . silhouetteRendering ,
374+ layer . displayState . shaderControlState . builderState ,
375+ ] ,
356376 ) ,
357377 ) ;
378+
358379 return parameterizedEmitterDependentShaderGetter ( layer , layer . gl , {
359380 memoizeKey : `mesh/MeshShaderManager/${ this . fragmentRelativeVertices } /${ this . vertexPositionFormat } ` ,
360- parameters : silhouetteRenderingEnabled ,
361- defineShader : ( builder , silhouetteRenderingEnabled ) => {
381+ parameters,
382+ encodeParameters : ( p ) => {
383+ return `${ p . silhouetteRenderingEnabled } /${ p . shaderBuilderState . parseResult . code } ` ;
384+ } ,
385+ shaderError : layer . displayState . shaderError ,
386+ defineShader : (
387+ builder ,
388+ { silhouetteRenderingEnabled, shaderBuilderState } ,
389+ ) => {
390+ addControlsToBuilder ( shaderBuilderState , builder ) ;
362391 this . vertexPositionHandler . defineShader ( builder ) ;
363392 builder . addAttribute ( "highp vec2" , "aVertexNormal" ) ;
364393 builder . addVarying ( "highp vec4" , "vColor" ) ;
394+ builder . addVarying ( "highp float" , "vLightingFactor" ) ; // TODO do I keep this here?
365395 builder . addUniform ( "highp vec4" , "uLightDirection" ) ;
366396 builder . addUniform ( "highp vec4" , "uColor" ) ;
367397 builder . addUniform ( "highp mat3" , "uNormalMatrix" ) ;
368398 builder . addUniform ( "highp mat4" , "uModelViewProjection" ) ;
369399 builder . addUniform ( "highp uint" , "uPickID" ) ;
400+ builder . addUniform ( "highp uvec2" , "uID" ) ;
401+
370402 if ( silhouetteRenderingEnabled ) {
371403 builder . addUniform ( "highp float" , "uSilhouettePower" ) ;
372404 }
@@ -375,6 +407,12 @@ export class MeshShaderManager {
375407 builder . addUniform ( "highp vec3" , "uFragmentShape" ) ;
376408 }
377409 builder . addVertexCode ( glsl_decodeNormalOctahedronSnorm8 ) ;
410+
411+ builder . addVertexCode ( `
412+ vec4 defaultColor() { return uColor; }
413+ void setColor(vec4 color) {
414+ vColor = color;
415+ }` ) ;
378416 let vertexMain = "" ;
379417 if ( this . fragmentRelativeVertices ) {
380418 vertexMain += `
@@ -393,22 +431,33 @@ vec3 origNormal = decodeNormalOctahedronSnorm8(aVertexNormal);
393431vec3 normal = normalize(uNormalMatrix * (normalMultiplier * origNormal));
394432float absCosAngle = abs(dot(normal, uLightDirection.xyz));
395433float lightingFactor = absCosAngle + uLightDirection.w;
396- vColor = vec4(lightingFactor * uColor.rgb, uColor.a);
434+ vColor = uColor;
435+ vColor = segmentColor(vColor);
436+ vColor = vec4(lightingFactor * vColor.rgb, vColor.a);
437+ vLightingFactor = lightingFactor;
397438` ;
398439 if ( silhouetteRenderingEnabled ) {
399440 vertexMain += `
400441vColor *= pow(1.0 - absCosAngle, uSilhouettePower);
401442` ;
402443 }
403444 builder . setVertexMain ( vertexMain ) ;
445+ const shaderManager = new SegmentColorShaderManager ( "getColor" ) ;
446+ shaderManager . defineShader ( builder ) ;
404447 builder . setFragmentMain ( "emit(vColor, uPickID);" ) ;
448+ const segmentColor = shaderCodeWithLineDirective (
449+ shaderBuilderState . parseResult . code ,
450+ ) ;
451+ builder . addVertexCode ( segmentColor + "\n" ) ;
405452 } ,
406453 } ) ;
407454 }
408455}
409456
410457export interface MeshDisplayState extends SegmentationDisplayState3D {
411458 silhouetteRendering : WatchableValueInterface < number > ;
459+ shaderControlState : ShaderControlState ;
460+ shaderError : WatchableShaderError ;
412461}
413462
414463export class MeshLayer extends PerspectiveViewRenderLayer < ThreeDimensionalRenderLayerAttachmentState > {
@@ -432,6 +481,14 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
432481 this . registerDisposer (
433482 displayState . silhouetteRendering . changed . add ( this . redrawNeeded . dispatch ) ,
434483 ) ;
484+ this . registerDisposer (
485+ displayState . shaderControlState . changed . add ( this . redrawNeeded . dispatch ) ,
486+ ) ;
487+ this . registerDisposer (
488+ displayState . shaderControlState . parseResult . changed . add (
489+ this . redrawNeeded . dispatch ,
490+ ) ,
491+ ) ;
435492
436493 const sharedObject = ( this . backend = this . registerDisposer (
437494 new SegmentationLayerSharedObject (
@@ -490,11 +547,19 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
490547 if ( modelMatrix === undefined ) {
491548 return ;
492549 }
493- const { shader } = this . getShader ( renderContext . emitter ) ;
550+ const { shader, parameters } = this . getShader ( renderContext . emitter ) ;
494551 if ( shader === null ) return ;
495552 shader . bind ( ) ;
496553 meshShaderManager . beginLayer ( gl , shader , renderContext , this . displayState ) ;
497554 meshShaderManager . beginModel ( gl , shader , renderContext , modelMatrix ) ;
555+ setControlsInShader ;
556+ this . displayState . silhouetteRendering ;
557+ setControlsInShader (
558+ gl ,
559+ shader ,
560+ this . displayState . shaderControlState ,
561+ parameters . shaderBuilderState . parseResult . controls ,
562+ ) ;
498563
499564 const manifestChunks = this . source . chunks ;
500565
@@ -774,6 +839,8 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
774839 displayState . silhouetteRendering . changed . add ( this . redrawNeeded . dispatch ) ,
775840 ) ;
776841
842+ console . log ( "are we multiscale?" ) ;
843+
777844 const sharedObject = ( this . backend = this . registerDisposer (
778845 new SegmentationLayerSharedObject (
779846 chunkManager ,
@@ -829,7 +896,9 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
829896 attachment ,
830897 ) ;
831898 if ( modelMatrix === undefined ) return ;
832- const { shader } = this . getShader ( renderContext . emitter ) ;
899+ console . log ( "get shader multiscale!" ) ;
900+ const { shader, parameters } = this . getShader ( renderContext . emitter ) ;
901+ parameters ;
833902 if ( shader === null ) return ;
834903 shader . bind ( ) ;
835904 meshShaderManager . beginLayer ( gl , shader , renderContext , this . displayState ) ;
@@ -900,6 +969,7 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
900969 if ( renderContext . emitPickID ) {
901970 meshShaderManager . setPickID ( gl , shader , pickIndex ! ) ;
902971 }
972+
903973 getMultiscaleChunksToDraw (
904974 manifest ,
905975 modelViewProjection ,
0 commit comments