gaussian-surface-mesh.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { UnitsMeshParams, UnitsTextureMeshParams, UnitsVisual, UnitsMeshVisual, UnitsTextureMeshVisual } from '../units-visual';
  8. import { GaussianDensityParams, computeUnitGaussianDensity, computeUnitGaussianDensityTexture2d, GaussianDensityProps, computeStructureGaussianDensity, computeStructureGaussianDensityTexture2d } from './util/gaussian';
  9. import { VisualContext } from '../../visual';
  10. import { Unit, Structure } from '../../../mol-model/structure';
  11. import { Theme } from '../../../mol-theme/theme';
  12. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  13. import { computeMarchingCubesMesh } from '../../../mol-geo/util/marching-cubes/algorithm';
  14. import { ElementIterator, getElementLoci, eachElement, getSerialElementLoci, eachSerialElement } from './util/element';
  15. import { VisualUpdateState } from '../../util';
  16. import { TextureMesh } from '../../../mol-geo/geometry/texture-mesh/texture-mesh';
  17. import { extractIsosurface } from '../../../mol-gl/compute/marching-cubes/isosurface';
  18. import { Sphere3D } from '../../../mol-math/geometry';
  19. import { ComplexVisual, ComplexMeshParams, ComplexMeshVisual, ComplexTextureMeshVisual, ComplexTextureMeshParams } from '../complex-visual';
  20. import { getVolumeSliceInfo, StructureGroup } from './util/common';
  21. import { WebGLContext } from '../../../mol-gl/webgl/context';
  22. import { MeshValues } from '../../../mol-gl/renderable/mesh';
  23. import { TextureMeshValues } from '../../../mol-gl/renderable/texture-mesh';
  24. import { Texture } from '../../../mol-gl/webgl/texture';
  25. import { applyMeshColorSmoothing } from '../../../mol-geo/geometry/mesh/color-smoothing';
  26. import { applyTextureMeshColorSmoothing } from '../../../mol-geo/geometry/texture-mesh/color-smoothing';
  27. import { ColorSmoothingParams, getColorSmoothingProps } from '../../../mol-geo/geometry/base';
  28. import { Vec3 } from '../../../mol-math/linear-algebra';
  29. import { isTimingMode } from '../../../mol-util/debug';
  30. import { ValueCell } from '../../../mol-util/value-cell';
  31. const SharedParams = {
  32. ...GaussianDensityParams,
  33. ...ColorSmoothingParams,
  34. ignoreHydrogens: PD.Boolean(false),
  35. onlyPolarHydrogens: PD.Boolean(false),
  36. tryUseGpu: PD.Boolean(true),
  37. includeParent: PD.Boolean(false, { isHidden: true }),
  38. };
  39. type SharedParams = typeof SharedParams
  40. export const GaussianSurfaceMeshParams = {
  41. ...UnitsMeshParams,
  42. ...UnitsTextureMeshParams,
  43. ...SharedParams,
  44. };
  45. export type GaussianSurfaceMeshParams = typeof GaussianSurfaceMeshParams
  46. export const StructureGaussianSurfaceMeshParams = {
  47. ...ComplexMeshParams,
  48. ...ComplexTextureMeshParams,
  49. ...SharedParams,
  50. };
  51. export type StructureGaussianSurfaceMeshParams = typeof StructureGaussianSurfaceMeshParams
  52. function gpuSupport(webgl: WebGLContext) {
  53. return webgl.extensions.colorBufferFloat && webgl.extensions.textureFloat && webgl.extensions.blendMinMax && webgl.extensions.drawBuffers;
  54. }
  55. function suitableForGpu(structure: Structure, props: PD.Values<SharedParams>, webgl: WebGLContext) {
  56. // lower resolutions are about as fast on CPU vs integrated GPU,
  57. // very low resolutions have artifacts when calculated on GPU
  58. if (props.resolution > 1) return false;
  59. // the GPU is much more memory contraint, especially true for integrated GPUs,
  60. // being conservative here still allows for small and medium sized assemblies
  61. const d = webgl.maxTextureSize / 3;
  62. const { areaCells, maxAreaCells } = getVolumeSliceInfo(structure.boundary.box, props.resolution, d * d);
  63. return areaCells < maxAreaCells;
  64. }
  65. export function GaussianSurfaceVisual(materialId: number, structure: Structure, props: PD.Values<GaussianSurfaceMeshParams>, webgl?: WebGLContext) {
  66. if (props.tryUseGpu && webgl && gpuSupport(webgl) && suitableForGpu(structure, props, webgl)) {
  67. return GaussianSurfaceTextureMeshVisual(materialId);
  68. }
  69. return GaussianSurfaceMeshVisual(materialId);
  70. }
  71. export function StructureGaussianSurfaceVisual(materialId: number, structure: Structure, props: PD.Values<StructureGaussianSurfaceMeshParams>, webgl?: WebGLContext) {
  72. if (props.tryUseGpu && webgl && gpuSupport(webgl) && suitableForGpu(structure, props, webgl)) {
  73. return StructureGaussianSurfaceTextureMeshVisual(materialId);
  74. }
  75. return StructureGaussianSurfaceMeshVisual(materialId);
  76. }
  77. type GaussianSurfaceMeta = {
  78. resolution?: number
  79. colorTexture?: Texture
  80. }
  81. //
  82. async function createGaussianSurfaceMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: GaussianDensityProps, mesh?: Mesh): Promise<Mesh> {
  83. const { smoothness } = props;
  84. const { transform, field, idField, radiusFactor, resolution, maxRadius } = await computeUnitGaussianDensity(structure, unit, theme.size, props).runInContext(ctx.runtime);
  85. const params = {
  86. isoLevel: Math.exp(-smoothness) / radiusFactor,
  87. scalarField: field,
  88. idField
  89. };
  90. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime);
  91. (surface.meta.resolution as GaussianSurfaceMeta['resolution']) = resolution;
  92. Mesh.transform(surface, transform);
  93. if (ctx.webgl && !ctx.webgl.isWebGL2) {
  94. Mesh.uniformTriangleGroup(surface);
  95. ValueCell.updateIfChanged(surface.varyingGroup, false);
  96. } else {
  97. ValueCell.updateIfChanged(surface.varyingGroup, true);
  98. }
  99. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, maxRadius);
  100. surface.setBoundingSphere(sphere);
  101. return surface;
  102. }
  103. export function GaussianSurfaceMeshVisual(materialId: number): UnitsVisual<GaussianSurfaceMeshParams> {
  104. return UnitsMeshVisual<GaussianSurfaceMeshParams>({
  105. defaultProps: PD.getDefaultValues(GaussianSurfaceMeshParams),
  106. createGeometry: createGaussianSurfaceMesh,
  107. createLocationIterator: ElementIterator.fromGroup,
  108. getLoci: getElementLoci,
  109. eachLocation: eachElement,
  110. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<GaussianSurfaceMeshParams>, currentProps: PD.Values<GaussianSurfaceMeshParams>) => {
  111. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  112. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true;
  113. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true;
  114. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  115. if (newProps.onlyPolarHydrogens !== currentProps.onlyPolarHydrogens) state.createGeometry = true;
  116. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  117. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  118. if (newProps.smoothColors.name !== currentProps.smoothColors.name) {
  119. state.updateColor = true;
  120. } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') {
  121. if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true;
  122. if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true;
  123. }
  124. },
  125. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<GaussianSurfaceMeshParams>, webgl?: WebGLContext) => {
  126. return props.tryUseGpu && !!webgl && suitableForGpu(structureGroup.structure, props, webgl);
  127. },
  128. processValues: (values: MeshValues, geometry: Mesh, props: PD.Values<GaussianSurfaceMeshParams>, theme: Theme, webgl?: WebGLContext) => {
  129. const { resolution, colorTexture } = geometry.meta as GaussianSurfaceMeta;
  130. const csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution);
  131. if (csp) {
  132. applyMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture);
  133. (geometry.meta.colorTexture as GaussianSurfaceMeta['colorTexture']) = values.tColorGrid.ref.value;
  134. }
  135. },
  136. dispose: (geometry: Mesh) => {
  137. (geometry.meta as GaussianSurfaceMeta).colorTexture?.destroy();
  138. }
  139. }, materialId);
  140. }
  141. //
  142. async function createStructureGaussianSurfaceMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: GaussianDensityProps, mesh?: Mesh): Promise<Mesh> {
  143. const { smoothness } = props;
  144. const { transform, field, idField, radiusFactor, resolution, maxRadius } = await computeStructureGaussianDensity(structure, theme.size, props).runInContext(ctx.runtime);
  145. const params = {
  146. isoLevel: Math.exp(-smoothness) / radiusFactor,
  147. scalarField: field,
  148. idField
  149. };
  150. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime);
  151. (surface.meta.resolution as GaussianSurfaceMeta['resolution']) = resolution;
  152. Mesh.transform(surface, transform);
  153. if (ctx.webgl && !ctx.webgl.isWebGL2) {
  154. Mesh.uniformTriangleGroup(surface);
  155. ValueCell.updateIfChanged(surface.varyingGroup, false);
  156. } else {
  157. ValueCell.updateIfChanged(surface.varyingGroup, true);
  158. }
  159. const sphere = Sphere3D.expand(Sphere3D(), structure.boundary.sphere, maxRadius);
  160. surface.setBoundingSphere(sphere);
  161. return surface;
  162. }
  163. export function StructureGaussianSurfaceMeshVisual(materialId: number): ComplexVisual<StructureGaussianSurfaceMeshParams> {
  164. return ComplexMeshVisual<StructureGaussianSurfaceMeshParams>({
  165. defaultProps: PD.getDefaultValues(StructureGaussianSurfaceMeshParams),
  166. createGeometry: createStructureGaussianSurfaceMesh,
  167. createLocationIterator: ElementIterator.fromStructure,
  168. getLoci: getSerialElementLoci,
  169. eachLocation: eachSerialElement,
  170. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<GaussianSurfaceMeshParams>, currentProps: PD.Values<GaussianSurfaceMeshParams>) => {
  171. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  172. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true;
  173. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true;
  174. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  175. if (newProps.onlyPolarHydrogens !== currentProps.onlyPolarHydrogens) state.createGeometry = true;
  176. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  177. if (newProps.smoothColors.name !== currentProps.smoothColors.name) {
  178. state.updateColor = true;
  179. } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') {
  180. if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true;
  181. if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true;
  182. }
  183. },
  184. mustRecreate: (structure: Structure, props: PD.Values<StructureGaussianSurfaceMeshParams>, webgl?: WebGLContext) => {
  185. return props.tryUseGpu && !!webgl && suitableForGpu(structure, props, webgl);
  186. },
  187. processValues: (values: MeshValues, geometry: Mesh, props: PD.Values<GaussianSurfaceMeshParams>, theme: Theme, webgl?: WebGLContext) => {
  188. const { resolution, colorTexture } = geometry.meta as GaussianSurfaceMeta;
  189. const csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution);
  190. if (csp) {
  191. applyMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture);
  192. (geometry.meta.colorTexture as GaussianSurfaceMeta['colorTexture']) = values.tColorGrid.ref.value;
  193. }
  194. },
  195. dispose: (geometry: Mesh) => {
  196. (geometry.meta as GaussianSurfaceMeta).colorTexture?.destroy();
  197. }
  198. }, materialId);
  199. }
  200. //
  201. const GaussianSurfaceName = 'gaussian-surface';
  202. async function createGaussianSurfaceTextureMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: GaussianDensityProps, textureMesh?: TextureMesh): Promise<TextureMesh> {
  203. if (!ctx.webgl) throw new Error('webgl context required to create gaussian surface texture-mesh');
  204. if (isTimingMode) ctx.webgl.timer.mark('createGaussianSurfaceTextureMesh');
  205. const { namedTextures, resources, extensions: { colorBufferFloat, textureFloat, colorBufferHalfFloat, textureHalfFloat } } = ctx.webgl;
  206. if (!namedTextures[GaussianSurfaceName]) {
  207. namedTextures[GaussianSurfaceName] = colorBufferHalfFloat && textureHalfFloat
  208. ? resources.texture('image-float16', 'rgba', 'fp16', 'linear')
  209. : colorBufferFloat && textureFloat
  210. ? resources.texture('image-float32', 'rgba', 'float', 'linear')
  211. : resources.texture('image-uint8', 'rgba', 'ubyte', 'linear');
  212. }
  213. const densityTextureData = await computeUnitGaussianDensityTexture2d(structure, unit, theme.size, true, props, ctx.webgl, namedTextures[GaussianSurfaceName]).runInContext(ctx.runtime);
  214. const isoLevel = Math.exp(-props.smoothness) / densityTextureData.radiusFactor;
  215. const axisOrder = Vec3.create(0, 1, 2);
  216. const buffer = textureMesh?.doubleBuffer.get();
  217. const gv = extractIsosurface(ctx.webgl, densityTextureData.texture, densityTextureData.gridDim, densityTextureData.gridTexDim, densityTextureData.gridTexScale, densityTextureData.transform, isoLevel, false, true, axisOrder, true, buffer?.vertex, buffer?.group, buffer?.normal);
  218. if (isTimingMode) ctx.webgl.timer.markEnd('createGaussianSurfaceTextureMesh');
  219. const groupCount = unit.elements.length;
  220. const boundingSphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, densityTextureData.maxRadius);
  221. const surface = TextureMesh.create(gv.vertexCount, groupCount, gv.vertexTexture, gv.groupTexture, gv.normalTexture, boundingSphere, textureMesh);
  222. (surface.meta as GaussianSurfaceMeta).resolution = densityTextureData.resolution;
  223. surface.meta.webgl = ctx.webgl;
  224. return surface;
  225. }
  226. export function GaussianSurfaceTextureMeshVisual(materialId: number): UnitsVisual<GaussianSurfaceMeshParams> {
  227. return UnitsTextureMeshVisual<GaussianSurfaceMeshParams>({
  228. defaultProps: PD.getDefaultValues(GaussianSurfaceMeshParams),
  229. createGeometry: createGaussianSurfaceTextureMesh,
  230. createLocationIterator: ElementIterator.fromGroup,
  231. getLoci: getElementLoci,
  232. eachLocation: eachElement,
  233. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<GaussianSurfaceMeshParams>, currentProps: PD.Values<GaussianSurfaceMeshParams>) => {
  234. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  235. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true;
  236. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true;
  237. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  238. if (newProps.onlyPolarHydrogens !== currentProps.onlyPolarHydrogens) state.createGeometry = true;
  239. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  240. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  241. if (newProps.smoothColors.name !== currentProps.smoothColors.name) {
  242. state.updateColor = true;
  243. } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') {
  244. if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true;
  245. if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true;
  246. }
  247. },
  248. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<GaussianSurfaceMeshParams>, webgl?: WebGLContext) => {
  249. return !props.tryUseGpu || !webgl || !suitableForGpu(structureGroup.structure, props, webgl);
  250. },
  251. processValues: (values: TextureMeshValues, geometry: TextureMesh, props: PD.Values<GaussianSurfaceMeshParams>, theme: Theme, webgl?: WebGLContext) => {
  252. const { resolution, colorTexture } = geometry.meta as GaussianSurfaceMeta;
  253. const csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution);
  254. if (csp && webgl) {
  255. applyTextureMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture);
  256. (geometry.meta as GaussianSurfaceMeta).colorTexture = values.tColorGrid.ref.value;
  257. }
  258. },
  259. dispose: (geometry: TextureMesh) => {
  260. geometry.vertexTexture.ref.value.destroy();
  261. geometry.groupTexture.ref.value.destroy();
  262. geometry.normalTexture.ref.value.destroy();
  263. geometry.doubleBuffer.destroy();
  264. (geometry.meta as GaussianSurfaceMeta).colorTexture?.destroy();
  265. }
  266. }, materialId);
  267. }
  268. //
  269. async function createStructureGaussianSurfaceTextureMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: GaussianDensityProps, textureMesh?: TextureMesh): Promise<TextureMesh> {
  270. if (!ctx.webgl) throw new Error('webgl context required to create structure gaussian surface texture-mesh');
  271. if (isTimingMode) ctx.webgl.timer.mark('createStructureGaussianSurfaceTextureMesh');
  272. const { namedTextures, resources, extensions: { colorBufferFloat, textureFloat, colorBufferHalfFloat, textureHalfFloat } } = ctx.webgl;
  273. if (!namedTextures[GaussianSurfaceName]) {
  274. namedTextures[GaussianSurfaceName] = colorBufferHalfFloat && textureHalfFloat
  275. ? resources.texture('image-float16', 'rgba', 'fp16', 'linear')
  276. : colorBufferFloat && textureFloat
  277. ? resources.texture('image-float32', 'rgba', 'float', 'linear')
  278. : resources.texture('image-uint8', 'rgba', 'ubyte', 'linear');
  279. }
  280. const densityTextureData = await computeStructureGaussianDensityTexture2d(structure, theme.size, true, props, ctx.webgl, namedTextures[GaussianSurfaceName]).runInContext(ctx.runtime);
  281. const isoLevel = Math.exp(-props.smoothness) / densityTextureData.radiusFactor;
  282. const axisOrder = Vec3.create(0, 1, 2);
  283. const buffer = textureMesh?.doubleBuffer.get();
  284. const gv = extractIsosurface(ctx.webgl, densityTextureData.texture, densityTextureData.gridDim, densityTextureData.gridTexDim, densityTextureData.gridTexScale, densityTextureData.transform, isoLevel, false, true, axisOrder, true, buffer?.vertex, buffer?.group, buffer?.normal);
  285. if (isTimingMode) ctx.webgl.timer.markEnd('createStructureGaussianSurfaceTextureMesh');
  286. const groupCount = structure.elementCount;
  287. const boundingSphere = Sphere3D.expand(Sphere3D(), structure.boundary.sphere, densityTextureData.maxRadius);
  288. const surface = TextureMesh.create(gv.vertexCount, groupCount, gv.vertexTexture, gv.groupTexture, gv.normalTexture, boundingSphere, textureMesh);
  289. (surface.meta as GaussianSurfaceMeta).resolution = densityTextureData.resolution;
  290. surface.meta.webgl = ctx.webgl;
  291. return surface;
  292. }
  293. export function StructureGaussianSurfaceTextureMeshVisual(materialId: number): ComplexVisual<StructureGaussianSurfaceMeshParams> {
  294. return ComplexTextureMeshVisual<StructureGaussianSurfaceMeshParams>({
  295. defaultProps: PD.getDefaultValues(StructureGaussianSurfaceMeshParams),
  296. createGeometry: createStructureGaussianSurfaceTextureMesh,
  297. createLocationIterator: ElementIterator.fromStructure,
  298. getLoci: getSerialElementLoci,
  299. eachLocation: eachSerialElement,
  300. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<StructureGaussianSurfaceMeshParams>, currentProps: PD.Values<StructureGaussianSurfaceMeshParams>) => {
  301. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  302. if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true;
  303. if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true;
  304. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  305. if (newProps.onlyPolarHydrogens !== currentProps.onlyPolarHydrogens) state.createGeometry = true;
  306. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  307. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  308. if (newProps.smoothColors.name !== currentProps.smoothColors.name) {
  309. state.updateColor = true;
  310. } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') {
  311. if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true;
  312. if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true;
  313. }
  314. },
  315. mustRecreate: (structure: Structure, props: PD.Values<StructureGaussianSurfaceMeshParams>, webgl?: WebGLContext) => {
  316. return !props.tryUseGpu || !webgl || !suitableForGpu(structure, props, webgl);
  317. },
  318. processValues: (values: TextureMeshValues, geometry: TextureMesh, props: PD.Values<GaussianSurfaceMeshParams>, theme: Theme, webgl?: WebGLContext) => {
  319. const { resolution, colorTexture } = geometry.meta as GaussianSurfaceMeta;
  320. const csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution);
  321. if (csp && webgl) {
  322. applyTextureMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture);
  323. (geometry.meta as GaussianSurfaceMeta).colorTexture = values.tColorGrid.ref.value;
  324. }
  325. },
  326. dispose: (geometry: TextureMesh) => {
  327. geometry.vertexTexture.ref.value.destroy();
  328. geometry.groupTexture.ref.value.destroy();
  329. geometry.normalTexture.ref.value.destroy();
  330. geometry.doubleBuffer.destroy();
  331. (geometry.meta as GaussianSurfaceMeta).colorTexture?.destroy();
  332. }
  333. }, materialId);
  334. }