@@ -43,6 +43,10 @@ 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+ SegmentColorHash ,
48+ SegmentColorShaderManager ,
49+ } from "#src/segment_color.js" ;
4650import {
4751 forEachVisibleSegment ,
4852 getObjectKey ,
@@ -56,7 +60,7 @@ import {
5660import type { WatchableValueInterface } from "#src/trackable_value.js" ;
5761import { makeCachedDerivedWatchableValue } from "#src/trackable_value.js" ;
5862import type { Borrowed , RefCounted } from "#src/util/disposable.js" ;
59- import type { vec4 } from "#src/util/geom.js" ;
63+ import { vec4 } from "#src/util/geom.js" ;
6064import {
6165 getFrustrumPlanes ,
6266 mat3 ,
@@ -68,14 +72,27 @@ import {
6872import * as matrix from "#src/util/matrix.js" ;
6973import { GLBuffer } from "#src/webgl/buffer.js" ;
7074import type { GL } from "#src/webgl/context.js" ;
71- import { parameterizedEmitterDependentShaderGetter } from "#src/webgl/dynamic_shader.js" ;
75+ import {
76+ parameterizedEmitterDependentShaderGetter ,
77+ shaderCodeWithLineDirective ,
78+ WatchableShaderError ,
79+ } from "#src/webgl/dynamic_shader.js" ;
7280import type { ShaderBuilder , ShaderProgram } from "#src/webgl/shader.js" ;
81+ import {
82+ addControlsToBuilder ,
83+ setControlsInShader ,
84+ ShaderControlState ,
85+ } from "#src/webgl/shader_ui_controls.js" ;
7386import type { RPC } from "#src/worker_rpc.js" ;
7487import { registerSharedObjectOwner } from "#src/worker_rpc.js" ;
7588
7689const tempMat4 = mat4 . create ( ) ;
7790const tempMat3 = mat3 . create ( ) ;
7891
92+ const tempVec3 = vec3 . create ( ) ;
93+
94+ const colorHash = SegmentColorHash . getDefault ( ) ;
95+
7996// To validate the octrees and to determine the multiscale fragment responsible for each framebuffer
8097// location, set `DEBUG_MULTISCALE_FRAGMENTS=true` and also set `DEBUG_PICKING=true` in
8198// `src/object_picking.ts`.
@@ -269,6 +286,14 @@ export class MeshShaderManager {
269286 gl . uniform4fv ( shader . uniform ( "uColor" ) , color ) ;
270287 }
271288
289+ setID ( gl : GL , shader : ShaderProgram , id : bigint ) {
290+ gl . uniform2ui (
291+ shader . uniform ( `uID` ) ,
292+ Number ( id & 0xffffffffn ) ,
293+ Number ( id >> 32n ) ,
294+ ) ;
295+ }
296+
272297 setPickID ( gl : GL , shader : ShaderProgram , pickID : number ) {
273298 gl . uniform1ui ( shader . uniform ( "uPickID" ) , pickID ) ;
274299 }
@@ -303,6 +328,7 @@ export class MeshShaderManager {
303328 indexBegin : number ,
304329 indexEnd : number ,
305330 ) {
331+ // drawFragmentHelper
306332 this . vertexPositionHandler . bind ( gl , shader , fragmentChunk ) ;
307333 const { meshData } = fragmentChunk ;
308334 fragmentChunk . normalBuffer . bindToVertexAttrib (
@@ -328,6 +354,18 @@ export class MeshShaderManager {
328354 drawFragment ( gl : GL , shader : ShaderProgram , fragmentChunk : FragmentChunk ) {
329355 const { meshData } = fragmentChunk ;
330356 const { indices } = meshData ;
357+ const id = ( meshData as any ) . id ;
358+ const bigId = BigInt ( id ) ;
359+ tempVec3 ;
360+ colorHash ;
361+ // const color = colorHash.compute(tempVec3, bigId);
362+ // console.log('color', id, color);
363+ // this.setColor(
364+ // gl,
365+ // shader,
366+ // vec4.fromValues(color[0], color[1], color[2], 1.0),
367+ // );
368+ this . setID ( gl , shader , bigId ) ;
331369 this . drawFragmentHelper ( gl , shader , fragmentChunk , 0 , indices . length ) ;
332370 }
333371
@@ -349,27 +387,33 @@ export class MeshShaderManager {
349387 }
350388
351389 makeGetter ( layer : RefCounted & { gl : GL ; displayState : MeshDisplayState } ) {
352- const silhouetteRenderingEnabled = layer . registerDisposer (
353- makeCachedDerivedWatchableValue (
354- ( x ) => x > 0 ,
355- [ layer . displayState . silhouetteRendering ] ,
356- ) ,
357- ) ;
390+ makeCachedDerivedWatchableValue ;
391+ // const silhouetteRenderingEnabled = layer.registerDisposer(
392+ // makeCachedDerivedWatchableValue(
393+ // (x) => x > 0,
394+ // [layer.displayState.silhouetteRendering],
395+ // ),
396+ // );
397+
358398 return parameterizedEmitterDependentShaderGetter ( layer , layer . gl , {
359399 memoizeKey : `mesh/MeshShaderManager/${ this . fragmentRelativeVertices } /${ this . vertexPositionFormat } ` ,
360- parameters : silhouetteRenderingEnabled ,
361- defineShader : ( builder , silhouetteRenderingEnabled ) => {
400+ parameters : layer . displayState . shaderControlState . builderState ,
401+ shaderError : layer . displayState . shaderError ,
402+ defineShader : ( builder , shaderBuilderState ) => {
403+ addControlsToBuilder ( shaderBuilderState , builder ) ;
362404 this . vertexPositionHandler . defineShader ( builder ) ;
363405 builder . addAttribute ( "highp vec2" , "aVertexNormal" ) ;
364406 builder . addVarying ( "highp vec4" , "vColor" ) ;
407+ builder . addVarying ( "highp float" , "vLightingFactor" ) ;
365408 builder . addUniform ( "highp vec4" , "uLightDirection" ) ;
366409 builder . addUniform ( "highp vec4" , "uColor" ) ;
367410 builder . addUniform ( "highp mat3" , "uNormalMatrix" ) ;
368411 builder . addUniform ( "highp mat4" , "uModelViewProjection" ) ;
369412 builder . addUniform ( "highp uint" , "uPickID" ) ;
370- if ( silhouetteRenderingEnabled ) {
371- builder . addUniform ( "highp float" , "uSilhouettePower" ) ;
372- }
413+ builder . addUniform ( "highp uvec2" , "uID" ) ;
414+ // if (silhouetteRenderingEnabled) {
415+ // builder.addUniform("highp float", "uSilhouettePower");
416+ // }
373417 if ( this . fragmentRelativeVertices ) {
374418 builder . addUniform ( "highp vec3" , "uFragmentOrigin" ) ;
375419 builder . addUniform ( "highp vec3" , "uFragmentShape" ) ;
@@ -394,21 +438,30 @@ vec3 normal = normalize(uNormalMatrix * (normalMultiplier * origNormal));
394438float absCosAngle = abs(dot(normal, uLightDirection.xyz));
395439float lightingFactor = absCosAngle + uLightDirection.w;
396440vColor = vec4(lightingFactor * uColor.rgb, uColor.a);
441+ vLightingFactor = lightingFactor;
397442` ;
398- if ( silhouetteRenderingEnabled ) {
399- vertexMain += `
400- vColor *= pow(1.0 - absCosAngle, uSilhouettePower);
401- ` ;
402- }
443+ // if (silhouetteRenderingEnabled) {
444+ // vertexMain += `
445+ // vColor *= pow(1.0 - absCosAngle, uSilhouettePower);
446+ // `;
447+ // }
403448 builder . setVertexMain ( vertexMain ) ;
404- builder . setFragmentMain ( "emit(vColor, uPickID);" ) ;
449+
450+ const shaderManager = new SegmentColorShaderManager ( "getColor" ) ;
451+ shaderManager . defineShader ( builder ) ;
452+ builder . setFragmentMainFunction (
453+ shaderCodeWithLineDirective ( shaderBuilderState . parseResult . code ) ,
454+ ) ;
405455 } ,
406456 } ) ;
407457 }
408458}
409459
410460export interface MeshDisplayState extends SegmentationDisplayState3D {
411461 silhouetteRendering : WatchableValueInterface < number > ;
462+
463+ shaderControlState : ShaderControlState ;
464+ shaderError : WatchableShaderError ;
412465}
413466
414467export class MeshLayer extends PerspectiveViewRenderLayer < ThreeDimensionalRenderLayerAttachmentState > {
@@ -432,6 +485,22 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
432485 this . registerDisposer (
433486 displayState . silhouetteRendering . changed . add ( this . redrawNeeded . dispatch ) ,
434487 ) ;
488+ this . registerDisposer (
489+ displayState . shaderControlState . changed . add ( this . redrawNeeded . dispatch ) ,
490+ ) ;
491+ this . registerDisposer (
492+ displayState . shaderControlState . parseResult . changed . add (
493+ this . redrawNeeded . dispatch ,
494+ ) ,
495+ ) ;
496+
497+ console . log ( "or are we not multiscale?" ) ;
498+
499+ this . registerDisposer (
500+ displayState . shaderControlState . changed . add ( ( ) => {
501+ console . log ( "shader control state changed!" ) ;
502+ } ) ,
503+ ) ;
435504
436505 const sharedObject = ( this . backend = this . registerDisposer (
437506 new SegmentationLayerSharedObject (
@@ -490,11 +559,20 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
490559 if ( modelMatrix === undefined ) {
491560 return ;
492561 }
493- const { shader } = this . getShader ( renderContext . emitter ) ;
562+ // console.log("get shader!", this.displayState);
563+ const { shader, parameters } = this . getShader ( renderContext . emitter ) ;
494564 if ( shader === null ) return ;
495565 shader . bind ( ) ;
496566 meshShaderManager . beginLayer ( gl , shader , renderContext , this . displayState ) ;
497567 meshShaderManager . beginModel ( gl , shader , renderContext , modelMatrix ) ;
568+ setControlsInShader ;
569+ this . displayState . silhouetteRendering ;
570+ setControlsInShader (
571+ gl ,
572+ shader ,
573+ this . displayState . shaderControlState ,
574+ parameters . parseResult . controls ,
575+ ) ;
498576
499577 const manifestChunks = this . source . chunks ;
500578
@@ -774,6 +852,8 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
774852 displayState . silhouetteRendering . changed . add ( this . redrawNeeded . dispatch ) ,
775853 ) ;
776854
855+ console . log ( "are we multiscale?" ) ;
856+
777857 const sharedObject = ( this . backend = this . registerDisposer (
778858 new SegmentationLayerSharedObject (
779859 chunkManager ,
@@ -829,7 +909,8 @@ export class MultiscaleMeshLayer extends PerspectiveViewRenderLayer<ThreeDimensi
829909 attachment ,
830910 ) ;
831911 if ( modelMatrix === undefined ) return ;
832- const { shader } = this . getShader ( renderContext . emitter ) ;
912+ const { shader, parameters } = this . getShader ( renderContext . emitter ) ;
913+ parameters ;
833914 if ( shader === null ) return ;
834915 shader . bind ( ) ;
835916 meshShaderManager . beginLayer ( gl , shader , renderContext , this . displayState ) ;
0 commit comments