renderer.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Viewport } from '../mol-canvas3d/camera/util';
  7. import { ICamera } from '../mol-canvas3d/camera';
  8. import { Scene } from './scene';
  9. import { WebGLContext } from './webgl/context';
  10. import { Mat4, Vec3, Vec4, Vec2, Quat } from '../mol-math/linear-algebra';
  11. import { GraphicsRenderable } from './renderable';
  12. import { Color } from '../mol-util/color';
  13. import { ValueCell, deepEqual } from '../mol-util';
  14. import { GlobalUniformValues } from './renderable/schema';
  15. import { GraphicsRenderVariant } from './webgl/render-item';
  16. import { ParamDefinition as PD } from '../mol-util/param-definition';
  17. import { Clipping } from '../mol-theme/clipping';
  18. import { stringToWords } from '../mol-util/string';
  19. import { degToRad } from '../mol-math/misc';
  20. import { createNullTexture, Texture, Textures } from './webgl/texture';
  21. import { arrayMapUpsert } from '../mol-util/array';
  22. import { clamp } from '../mol-math/interpolate';
  23. export interface RendererStats {
  24. programCount: number
  25. shaderCount: number
  26. attributeCount: number
  27. elementsCount: number
  28. framebufferCount: number
  29. renderbufferCount: number
  30. textureCount: number
  31. vertexArrayCount: number
  32. drawCount: number
  33. instanceCount: number
  34. instancedDrawCount: number
  35. }
  36. interface Renderer {
  37. readonly stats: RendererStats
  38. readonly props: Readonly<RendererProps>
  39. clear: (toBackgroundColor: boolean) => void
  40. clearDepth: () => void
  41. update: (camera: ICamera) => void
  42. renderPick: (group: Scene.Group, camera: ICamera, variant: GraphicsRenderVariant, depthTexture: Texture | null) => void
  43. renderDepth: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  44. renderMarkingDepth: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  45. renderMarkingMask: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  46. renderBlended: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  47. renderBlendedOpaque: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  48. renderBlendedTransparent: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  49. renderBlendedVolumeOpaque: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  50. renderBlendedVolumeTransparent: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  51. renderWboitOpaque: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  52. renderWboitTransparent: (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => void
  53. setProps: (props: Partial<RendererProps>) => void
  54. setViewport: (x: number, y: number, width: number, height: number) => void
  55. setTransparentBackground: (value: boolean) => void
  56. setDrawingBufferSize: (width: number, height: number) => void
  57. setPixelRatio: (value: number) => void
  58. dispose: () => void
  59. }
  60. export const RendererParams = {
  61. backgroundColor: PD.Color(Color(0x000000), { description: 'Background color of the 3D canvas' }),
  62. // the following are general 'material' parameters
  63. pickingAlphaThreshold: PD.Numeric(0.5, { min: 0.0, max: 1.0, step: 0.01 }, { description: 'The minimum opacity value needed for an object to be pickable.' }),
  64. interiorDarkening: PD.Numeric(0.5, { min: 0.0, max: 1.0, step: 0.01 }),
  65. interiorColorFlag: PD.Boolean(true, { label: 'Use Interior Color' }),
  66. interiorColor: PD.Color(Color.fromNormalizedRgb(0.3, 0.3, 0.3)),
  67. highlightColor: PD.Color(Color.fromNormalizedRgb(1.0, 0.4, 0.6)),
  68. selectColor: PD.Color(Color.fromNormalizedRgb(0.2, 1.0, 0.1)),
  69. highlightStrength: PD.Numeric(0.7, { min: 0.0, max: 1.0, step: 0.1 }),
  70. selectStrength: PD.Numeric(0.7, { min: 0.0, max: 1.0, step: 0.1 }),
  71. markerPriority: PD.Select(1, [[1, 'Highlight'], [2, 'Select']]),
  72. xrayEdgeFalloff: PD.Numeric(1, { min: 0.0, max: 3.0, step: 0.1 }),
  73. lightInclination: PD.Numeric(180, { min: 0, max: 180, step: 1 }),
  74. lightAzimuth: PD.Numeric(0, { min: 0, max: 360, step: 1 }),
  75. lightColor: PD.Color(Color.fromNormalizedRgb(1.0, 1.0, 1.0)),
  76. ambientColor: PD.Color(Color.fromNormalizedRgb(1.0, 1.0, 1.0)),
  77. style: PD.MappedStatic('matte', {
  78. custom: PD.Group({
  79. lightIntensity: PD.Numeric(0.6, { min: 0.0, max: 1.0, step: 0.01 }),
  80. ambientIntensity: PD.Numeric(0.4, { min: 0.0, max: 1.0, step: 0.01 }),
  81. metalness: PD.Numeric(0.0, { min: 0.0, max: 1.0, step: 0.01 }),
  82. roughness: PD.Numeric(1.0, { min: 0.0, max: 1.0, step: 0.01 }),
  83. reflectivity: PD.Numeric(0.5, { min: 0.0, max: 1.0, step: 0.01 }),
  84. }, { isExpanded: true }),
  85. flat: PD.Group({}),
  86. matte: PD.Group({}),
  87. glossy: PD.Group({}),
  88. metallic: PD.Group({}),
  89. plastic: PD.Group({}),
  90. }, { label: 'Lighting', description: 'Style in which the 3D scene is rendered/lighted' }),
  91. clip: PD.Group({
  92. variant: PD.Select('instance', PD.arrayToOptions<Clipping.Variant>(['instance', 'pixel'])),
  93. objects: PD.ObjectList({
  94. type: PD.Select('plane', PD.objectToOptions(Clipping.Type, t => stringToWords(t))),
  95. invert: PD.Boolean(false),
  96. position: PD.Vec3(Vec3()),
  97. rotation: PD.Group({
  98. axis: PD.Vec3(Vec3.create(1, 0, 0)),
  99. angle: PD.Numeric(0, { min: -180, max: 180, step: 1 }, { description: 'Angle in Degrees' }),
  100. }, { isExpanded: true }),
  101. scale: PD.Vec3(Vec3.create(1, 1, 1)),
  102. }, o => stringToWords(o.type))
  103. })
  104. };
  105. export type RendererProps = PD.Values<typeof RendererParams>
  106. export type Style = {
  107. lightIntensity: number
  108. ambientIntensity: number
  109. metalness: number
  110. roughness: number
  111. reflectivity: number
  112. }
  113. export function getStyle(props: RendererProps['style']): Style {
  114. switch (props.name) {
  115. case 'custom':
  116. return props.params as Style;
  117. case 'flat':
  118. return {
  119. lightIntensity: 0, ambientIntensity: 1,
  120. metalness: 0, roughness: 0.4, reflectivity: 0.5
  121. };
  122. case 'matte':
  123. return {
  124. lightIntensity: 0.7, ambientIntensity: 0.3,
  125. metalness: 0, roughness: 1, reflectivity: 0.5
  126. };
  127. case 'glossy':
  128. return {
  129. lightIntensity: 0.7, ambientIntensity: 0.3,
  130. metalness: 0, roughness: 0.4, reflectivity: 0.5
  131. };
  132. case 'metallic':
  133. return {
  134. lightIntensity: 0.7, ambientIntensity: 0.7,
  135. metalness: 0.6, roughness: 0.6, reflectivity: 0.5
  136. };
  137. case 'plastic':
  138. return {
  139. lightIntensity: 0.7, ambientIntensity: 0.3,
  140. metalness: 0, roughness: 0.2, reflectivity: 0.5
  141. };
  142. }
  143. }
  144. type Clip = {
  145. variant: Clipping.Variant
  146. objects: {
  147. count: number
  148. type: number[]
  149. invert: boolean[]
  150. position: number[]
  151. rotation: number[]
  152. scale: number[]
  153. }
  154. }
  155. const tmpQuat = Quat();
  156. function getClip(props: RendererProps['clip'], clip?: Clip): Clip {
  157. const { type, invert, position, rotation, scale } = clip?.objects || {
  158. type: (new Array(5)).fill(1),
  159. invert: (new Array(5)).fill(false),
  160. position: (new Array(5 * 3)).fill(0),
  161. rotation: (new Array(5 * 4)).fill(0),
  162. scale: (new Array(5 * 3)).fill(1),
  163. };
  164. for (let i = 0, il = props.objects.length; i < il; ++i) {
  165. const p = props.objects[i];
  166. type[i] = Clipping.Type[p.type];
  167. invert[i] = p.invert;
  168. Vec3.toArray(p.position, position, i * 3);
  169. Quat.toArray(Quat.setAxisAngle(tmpQuat, p.rotation.axis, degToRad(p.rotation.angle)), rotation, i * 4);
  170. Vec3.toArray(p.scale, scale, i * 3);
  171. }
  172. return {
  173. variant: props.variant,
  174. objects: { count: props.objects.length, type, invert, position, rotation, scale }
  175. };
  176. }
  177. namespace Renderer {
  178. export function create(ctx: WebGLContext, props: Partial<RendererProps> = {}): Renderer {
  179. const { gl, state, stats, extensions: { fragDepth } } = ctx;
  180. const p = PD.merge(RendererParams, PD.getDefaultValues(RendererParams), props);
  181. const style = getStyle(p.style);
  182. const clip = getClip(p.clip);
  183. const viewport = Viewport();
  184. const drawingBufferSize = Vec2.create(gl.drawingBufferWidth, gl.drawingBufferHeight);
  185. const bgColor = Color.toVec3Normalized(Vec3(), p.backgroundColor);
  186. let transparentBackground = false;
  187. const nullDepthTexture = createNullTexture(gl);
  188. const sharedTexturesList: Textures = [
  189. ['tDepth', nullDepthTexture]
  190. ];
  191. const view = Mat4();
  192. const invView = Mat4();
  193. const modelView = Mat4();
  194. const invModelView = Mat4();
  195. const invProjection = Mat4();
  196. const modelViewProjection = Mat4();
  197. const invModelViewProjection = Mat4();
  198. const cameraDir = Vec3();
  199. const viewOffset = Vec2();
  200. const lightDirection = Vec3();
  201. Vec3.directionFromSpherical(lightDirection, degToRad(p.lightInclination), degToRad(p.lightAzimuth), 1);
  202. const lightColor = Color.toVec3Normalized(Vec3(), p.lightColor);
  203. Vec3.scale(lightColor, lightColor, style.lightIntensity);
  204. const ambientColor = Color.toVec3Normalized(Vec3(), p.ambientColor);
  205. Vec3.scale(ambientColor, ambientColor, style.ambientIntensity);
  206. const globalUniforms: GlobalUniformValues = {
  207. uModel: ValueCell.create(Mat4.identity()),
  208. uView: ValueCell.create(view),
  209. uInvView: ValueCell.create(invView),
  210. uModelView: ValueCell.create(modelView),
  211. uInvModelView: ValueCell.create(invModelView),
  212. uInvProjection: ValueCell.create(invProjection),
  213. uProjection: ValueCell.create(Mat4()),
  214. uModelViewProjection: ValueCell.create(modelViewProjection),
  215. uInvModelViewProjection: ValueCell.create(invModelViewProjection),
  216. uIsOrtho: ValueCell.create(1),
  217. uViewOffset: ValueCell.create(viewOffset),
  218. uPixelRatio: ValueCell.create(ctx.pixelRatio),
  219. uViewport: ValueCell.create(Viewport.toVec4(Vec4(), viewport)),
  220. uDrawingBufferSize: ValueCell.create(drawingBufferSize),
  221. uCameraPosition: ValueCell.create(Vec3()),
  222. uCameraDir: ValueCell.create(cameraDir),
  223. uNear: ValueCell.create(1),
  224. uFar: ValueCell.create(10000),
  225. uFogNear: ValueCell.create(1),
  226. uFogFar: ValueCell.create(10000),
  227. uFogColor: ValueCell.create(bgColor),
  228. uRenderWboit: ValueCell.create(false),
  229. uMarkingDepthTest: ValueCell.create(false),
  230. uTransparentBackground: ValueCell.create(false),
  231. uClipObjectType: ValueCell.create(clip.objects.type),
  232. uClipObjectInvert: ValueCell.create(clip.objects.invert),
  233. uClipObjectPosition: ValueCell.create(clip.objects.position),
  234. uClipObjectRotation: ValueCell.create(clip.objects.rotation),
  235. uClipObjectScale: ValueCell.create(clip.objects.scale),
  236. uLightDirection: ValueCell.create(lightDirection),
  237. uLightColor: ValueCell.create(lightColor),
  238. uAmbientColor: ValueCell.create(ambientColor),
  239. // the following 3 are general 'material' uniforms
  240. uMetalness: ValueCell.create(style.metalness),
  241. uRoughness: ValueCell.create(style.roughness),
  242. uReflectivity: ValueCell.create(style.reflectivity),
  243. uPickingAlphaThreshold: ValueCell.create(p.pickingAlphaThreshold),
  244. uInteriorDarkening: ValueCell.create(p.interiorDarkening),
  245. uInteriorColorFlag: ValueCell.create(p.interiorColorFlag),
  246. uInteriorColor: ValueCell.create(Color.toVec3Normalized(Vec3(), p.interiorColor)),
  247. uHighlightColor: ValueCell.create(Color.toVec3Normalized(Vec3(), p.highlightColor)),
  248. uSelectColor: ValueCell.create(Color.toVec3Normalized(Vec3(), p.selectColor)),
  249. uHighlightStrength: ValueCell.create(p.highlightStrength),
  250. uSelectStrength: ValueCell.create(p.selectStrength),
  251. uMarkerPriority: ValueCell.create(p.markerPriority),
  252. uXrayEdgeFalloff: ValueCell.create(p.xrayEdgeFalloff),
  253. };
  254. const globalUniformList = Object.entries(globalUniforms);
  255. let globalUniformsNeedUpdate = true;
  256. const renderObject = (r: GraphicsRenderable, variant: GraphicsRenderVariant) => {
  257. if (r.state.disposed || !r.state.visible || (!r.state.pickable && variant[0] === 'p')) {
  258. return;
  259. }
  260. let definesNeedUpdate = false;
  261. if (r.state.noClip) {
  262. if (r.values.dClipObjectCount.ref.value !== 0) {
  263. ValueCell.update(r.values.dClipObjectCount, 0);
  264. definesNeedUpdate = true;
  265. }
  266. } else {
  267. if (r.values.dClipObjectCount.ref.value !== clip.objects.count) {
  268. ValueCell.update(r.values.dClipObjectCount, clip.objects.count);
  269. definesNeedUpdate = true;
  270. }
  271. if (r.values.dClipVariant.ref.value !== clip.variant) {
  272. ValueCell.update(r.values.dClipVariant, clip.variant);
  273. definesNeedUpdate = true;
  274. }
  275. }
  276. if (definesNeedUpdate) r.update();
  277. const program = r.getProgram(variant);
  278. if (state.currentProgramId !== program.id) {
  279. // console.log('new program')
  280. globalUniformsNeedUpdate = true;
  281. program.use();
  282. }
  283. if (globalUniformsNeedUpdate) {
  284. // console.log('globalUniformsNeedUpdate')
  285. program.setUniforms(globalUniformList);
  286. globalUniformsNeedUpdate = false;
  287. }
  288. if (r.values.dRenderMode) { // indicates direct-volume
  289. // culling done in fragment shader
  290. state.disable(gl.CULL_FACE);
  291. state.frontFace(gl.CCW);
  292. if (variant === 'colorBlended') {
  293. // depth test done manually in shader against `depthTexture`
  294. // still need to enable when fragDepth can be used to write depth
  295. if (r.values.dRenderMode.ref.value === 'volume' || !fragDepth) {
  296. state.disable(gl.DEPTH_TEST);
  297. state.depthMask(false);
  298. } else {
  299. state.enable(gl.DEPTH_TEST);
  300. state.depthMask(r.values.uAlpha.ref.value === 1.0);
  301. }
  302. }
  303. } else {
  304. if (r.values.dDoubleSided) {
  305. if (r.values.dDoubleSided.ref.value || r.values.hasReflection.ref.value) {
  306. state.disable(gl.CULL_FACE);
  307. } else {
  308. state.enable(gl.CULL_FACE);
  309. }
  310. } else {
  311. // webgl default
  312. state.disable(gl.CULL_FACE);
  313. }
  314. if (r.values.dFlipSided) {
  315. if (r.values.dFlipSided.ref.value) {
  316. state.frontFace(gl.CW);
  317. state.cullFace(gl.FRONT);
  318. } else {
  319. state.frontFace(gl.CCW);
  320. state.cullFace(gl.BACK);
  321. }
  322. } else {
  323. // webgl default
  324. state.frontFace(gl.CCW);
  325. state.cullFace(gl.BACK);
  326. }
  327. }
  328. r.render(variant, sharedTexturesList);
  329. };
  330. const update = (camera: ICamera) => {
  331. ValueCell.update(globalUniforms.uView, camera.view);
  332. ValueCell.update(globalUniforms.uInvView, Mat4.invert(invView, camera.view));
  333. ValueCell.update(globalUniforms.uProjection, camera.projection);
  334. ValueCell.update(globalUniforms.uInvProjection, Mat4.invert(invProjection, camera.projection));
  335. ValueCell.updateIfChanged(globalUniforms.uIsOrtho, camera.state.mode === 'orthographic' ? 1 : 0);
  336. ValueCell.update(globalUniforms.uViewOffset, camera.viewOffset.enabled ? Vec2.set(viewOffset, camera.viewOffset.offsetX * 16, camera.viewOffset.offsetY * 16) : Vec2.set(viewOffset, 0, 0));
  337. ValueCell.update(globalUniforms.uCameraPosition, camera.state.position);
  338. ValueCell.update(globalUniforms.uCameraDir, Vec3.normalize(cameraDir, Vec3.sub(cameraDir, camera.state.target, camera.state.position)));
  339. ValueCell.updateIfChanged(globalUniforms.uFar, camera.far);
  340. ValueCell.updateIfChanged(globalUniforms.uNear, camera.near);
  341. ValueCell.updateIfChanged(globalUniforms.uFogFar, camera.fogFar);
  342. ValueCell.updateIfChanged(globalUniforms.uFogNear, camera.fogNear);
  343. ValueCell.updateIfChanged(globalUniforms.uTransparentBackground, transparentBackground);
  344. };
  345. const updateInternal = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null, renderWboit: boolean, markingDepthTest: boolean) => {
  346. arrayMapUpsert(sharedTexturesList, 'tDepth', depthTexture || nullDepthTexture);
  347. ValueCell.update(globalUniforms.uModel, group.view);
  348. ValueCell.update(globalUniforms.uModelView, Mat4.mul(modelView, group.view, camera.view));
  349. ValueCell.update(globalUniforms.uInvModelView, Mat4.invert(invModelView, modelView));
  350. ValueCell.update(globalUniforms.uModelViewProjection, Mat4.mul(modelViewProjection, modelView, camera.projection));
  351. ValueCell.update(globalUniforms.uInvModelViewProjection, Mat4.invert(invModelViewProjection, modelViewProjection));
  352. ValueCell.updateIfChanged(globalUniforms.uRenderWboit, renderWboit);
  353. ValueCell.updateIfChanged(globalUniforms.uMarkingDepthTest, markingDepthTest);
  354. state.enable(gl.SCISSOR_TEST);
  355. state.colorMask(true, true, true, true);
  356. const { x, y, width, height } = viewport;
  357. gl.viewport(x, y, width, height);
  358. gl.scissor(x, y, width, height);
  359. globalUniformsNeedUpdate = true;
  360. state.currentRenderItemId = -1;
  361. };
  362. const renderPick = (group: Scene.Group, camera: ICamera, variant: GraphicsRenderVariant, depthTexture: Texture | null) => {
  363. state.disable(gl.BLEND);
  364. state.enable(gl.DEPTH_TEST);
  365. state.depthMask(true);
  366. updateInternal(group, camera, depthTexture, false, false);
  367. const { renderables } = group;
  368. for (let i = 0, il = renderables.length; i < il; ++i) {
  369. if (!renderables[i].state.colorOnly) {
  370. renderObject(renderables[i], variant);
  371. }
  372. }
  373. };
  374. const renderDepth = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  375. state.disable(gl.BLEND);
  376. state.enable(gl.DEPTH_TEST);
  377. state.depthMask(true);
  378. updateInternal(group, camera, depthTexture, false, false);
  379. const { renderables } = group;
  380. for (let i = 0, il = renderables.length; i < il; ++i) {
  381. renderObject(renderables[i], 'depth');
  382. }
  383. };
  384. const renderMarkingDepth = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  385. state.disable(gl.BLEND);
  386. state.enable(gl.DEPTH_TEST);
  387. state.depthMask(true);
  388. updateInternal(group, camera, depthTexture, false, false);
  389. const { renderables } = group;
  390. for (let i = 0, il = renderables.length; i < il; ++i) {
  391. const r = renderables[i];
  392. if (r.values.markerAverage.ref.value !== 1) {
  393. renderObject(renderables[i], 'markingDepth');
  394. }
  395. }
  396. };
  397. const renderMarkingMask = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  398. state.disable(gl.BLEND);
  399. state.enable(gl.DEPTH_TEST);
  400. state.depthMask(true);
  401. updateInternal(group, camera, depthTexture, false, !!depthTexture);
  402. const { renderables } = group;
  403. for (let i = 0, il = renderables.length; i < il; ++i) {
  404. const r = renderables[i];
  405. if (r.values.markerAverage.ref.value > 0) {
  406. renderObject(renderables[i], 'markingMask');
  407. }
  408. }
  409. };
  410. const renderBlended = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  411. renderBlendedOpaque(group, camera, depthTexture);
  412. renderBlendedTransparent(group, camera, depthTexture);
  413. };
  414. const renderBlendedOpaque = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  415. state.disable(gl.BLEND);
  416. state.enable(gl.DEPTH_TEST);
  417. state.depthMask(true);
  418. updateInternal(group, camera, depthTexture, false, false);
  419. const { renderables } = group;
  420. for (let i = 0, il = renderables.length; i < il; ++i) {
  421. const r = renderables[i];
  422. if (r.state.opaque) {
  423. renderObject(r, 'colorBlended');
  424. }
  425. }
  426. };
  427. const renderBlendedTransparent = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  428. state.enable(gl.DEPTH_TEST);
  429. updateInternal(group, camera, depthTexture, false, false);
  430. const { renderables } = group;
  431. if (transparentBackground) {
  432. state.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  433. } else {
  434. state.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  435. }
  436. state.enable(gl.BLEND);
  437. state.depthMask(true);
  438. for (let i = 0, il = renderables.length; i < il; ++i) {
  439. const r = renderables[i];
  440. if (!r.state.opaque && r.state.writeDepth) {
  441. renderObject(r, 'colorBlended');
  442. }
  443. }
  444. state.depthMask(false);
  445. for (let i = 0, il = renderables.length; i < il; ++i) {
  446. const r = renderables[i];
  447. if (!r.state.opaque && !r.state.writeDepth) {
  448. renderObject(r, 'colorBlended');
  449. }
  450. }
  451. };
  452. const renderBlendedVolumeOpaque = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  453. state.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  454. state.enable(gl.BLEND);
  455. updateInternal(group, camera, depthTexture, false, false);
  456. const { renderables } = group;
  457. for (let i = 0, il = renderables.length; i < il; ++i) {
  458. const r = renderables[i];
  459. // TODO: simplify, handle in renderable.state???
  460. // uAlpha is updated in "render" so we need to recompute it here
  461. const alpha = clamp(r.values.alpha.ref.value * r.state.alphaFactor, 0, 1);
  462. if (alpha === 1 && r.values.transparencyAverage.ref.value !== 1 && !r.values.dXrayShaded?.ref.value) {
  463. renderObject(r, 'colorBlended');
  464. }
  465. }
  466. };
  467. const renderBlendedVolumeTransparent = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  468. state.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  469. state.enable(gl.BLEND);
  470. updateInternal(group, camera, depthTexture, false, false);
  471. const { renderables } = group;
  472. for (let i = 0, il = renderables.length; i < il; ++i) {
  473. const r = renderables[i];
  474. // TODO: simplify, handle in renderable.state???
  475. // uAlpha is updated in "render" so we need to recompute it here
  476. const alpha = clamp(r.values.alpha.ref.value * r.state.alphaFactor, 0, 1);
  477. if (alpha < 1 || r.values.transparencyAverage.ref.value > 0 || r.values.dXrayShaded?.ref.value) {
  478. renderObject(r, 'colorBlended');
  479. }
  480. }
  481. };
  482. const renderWboitOpaque = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  483. state.disable(gl.BLEND);
  484. state.enable(gl.DEPTH_TEST);
  485. state.depthMask(true);
  486. updateInternal(group, camera, depthTexture, false, false);
  487. const { renderables } = group;
  488. for (let i = 0, il = renderables.length; i < il; ++i) {
  489. const r = renderables[i];
  490. // TODO: simplify, handle in renderable.state???
  491. // uAlpha is updated in "render" so we need to recompute it here
  492. const alpha = clamp(r.values.alpha.ref.value * r.state.alphaFactor, 0, 1);
  493. if (alpha === 1 && r.values.transparencyAverage.ref.value !== 1 && r.values.dRenderMode?.ref.value !== 'volume' && r.values.dPointStyle?.ref.value !== 'fuzzy' && !r.values.dXrayShaded?.ref.value) {
  494. renderObject(r, 'colorWboit');
  495. }
  496. }
  497. };
  498. const renderWboitTransparent = (group: Scene.Group, camera: ICamera, depthTexture: Texture | null) => {
  499. updateInternal(group, camera, depthTexture, true, false);
  500. const { renderables } = group;
  501. for (let i = 0, il = renderables.length; i < il; ++i) {
  502. const r = renderables[i];
  503. // TODO: simplify, handle in renderable.state???
  504. // uAlpha is updated in "render" so we need to recompute it here
  505. const alpha = clamp(r.values.alpha.ref.value * r.state.alphaFactor, 0, 1);
  506. if (alpha < 1 || r.values.transparencyAverage.ref.value > 0 || r.values.dRenderMode?.ref.value === 'volume' || r.values.dPointStyle?.ref.value === 'fuzzy' || !!r.values.uBackgroundColor || r.values.dXrayShaded?.ref.value) {
  507. renderObject(r, 'colorWboit');
  508. }
  509. }
  510. };
  511. return {
  512. clear: (toBackgroundColor: boolean) => {
  513. state.enable(gl.SCISSOR_TEST);
  514. state.enable(gl.DEPTH_TEST);
  515. state.colorMask(true, true, true, true);
  516. state.depthMask(true);
  517. if (transparentBackground) {
  518. state.clearColor(0, 0, 0, 0);
  519. } else if (toBackgroundColor) {
  520. state.clearColor(bgColor[0], bgColor[1], bgColor[2], 1);
  521. } else {
  522. state.clearColor(1, 1, 1, 1);
  523. }
  524. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  525. },
  526. clearDepth: () => {
  527. state.enable(gl.SCISSOR_TEST);
  528. state.enable(gl.DEPTH_TEST);
  529. state.depthMask(true);
  530. gl.clear(gl.DEPTH_BUFFER_BIT);
  531. },
  532. update,
  533. renderPick,
  534. renderDepth,
  535. renderMarkingDepth,
  536. renderMarkingMask,
  537. renderBlended,
  538. renderBlendedOpaque,
  539. renderBlendedTransparent,
  540. renderBlendedVolumeOpaque,
  541. renderBlendedVolumeTransparent,
  542. renderWboitOpaque,
  543. renderWboitTransparent,
  544. setProps: (props: Partial<RendererProps>) => {
  545. if (props.backgroundColor !== undefined && props.backgroundColor !== p.backgroundColor) {
  546. p.backgroundColor = props.backgroundColor;
  547. Color.toVec3Normalized(bgColor, p.backgroundColor);
  548. ValueCell.update(globalUniforms.uFogColor, Vec3.copy(globalUniforms.uFogColor.ref.value, bgColor));
  549. }
  550. if (props.pickingAlphaThreshold !== undefined && props.pickingAlphaThreshold !== p.pickingAlphaThreshold) {
  551. p.pickingAlphaThreshold = props.pickingAlphaThreshold;
  552. ValueCell.update(globalUniforms.uPickingAlphaThreshold, p.pickingAlphaThreshold);
  553. }
  554. if (props.interiorDarkening !== undefined && props.interiorDarkening !== p.interiorDarkening) {
  555. p.interiorDarkening = props.interiorDarkening;
  556. ValueCell.update(globalUniforms.uInteriorDarkening, p.interiorDarkening);
  557. }
  558. if (props.interiorColorFlag !== undefined && props.interiorColorFlag !== p.interiorColorFlag) {
  559. p.interiorColorFlag = props.interiorColorFlag;
  560. ValueCell.update(globalUniforms.uInteriorColorFlag, p.interiorColorFlag);
  561. }
  562. if (props.interiorColor !== undefined && props.interiorColor !== p.interiorColor) {
  563. p.interiorColor = props.interiorColor;
  564. ValueCell.update(globalUniforms.uInteriorColor, Color.toVec3Normalized(globalUniforms.uInteriorColor.ref.value, p.interiorColor));
  565. }
  566. if (props.highlightColor !== undefined && props.highlightColor !== p.highlightColor) {
  567. p.highlightColor = props.highlightColor;
  568. ValueCell.update(globalUniforms.uHighlightColor, Color.toVec3Normalized(globalUniforms.uHighlightColor.ref.value, p.highlightColor));
  569. }
  570. if (props.selectColor !== undefined && props.selectColor !== p.selectColor) {
  571. p.selectColor = props.selectColor;
  572. ValueCell.update(globalUniforms.uSelectColor, Color.toVec3Normalized(globalUniforms.uSelectColor.ref.value, p.selectColor));
  573. }
  574. if (props.highlightStrength !== undefined && props.highlightStrength !== p.highlightStrength) {
  575. p.highlightStrength = props.highlightStrength;
  576. ValueCell.update(globalUniforms.uHighlightStrength, p.highlightStrength);
  577. }
  578. if (props.selectStrength !== undefined && props.selectStrength !== p.selectStrength) {
  579. p.selectStrength = props.selectStrength;
  580. ValueCell.update(globalUniforms.uSelectStrength, p.selectStrength);
  581. }
  582. if (props.markerPriority !== undefined && props.markerPriority !== p.markerPriority) {
  583. p.markerPriority = props.markerPriority;
  584. ValueCell.update(globalUniforms.uMarkerPriority, p.markerPriority);
  585. }
  586. if (props.xrayEdgeFalloff !== undefined && props.xrayEdgeFalloff !== p.xrayEdgeFalloff) {
  587. p.xrayEdgeFalloff = props.xrayEdgeFalloff;
  588. ValueCell.update(globalUniforms.uXrayEdgeFalloff, p.xrayEdgeFalloff);
  589. }
  590. if (props.lightInclination !== undefined && props.lightInclination !== p.lightInclination) {
  591. p.lightInclination = props.lightInclination;
  592. Vec3.directionFromSpherical(lightDirection, degToRad(p.lightInclination), degToRad(p.lightAzimuth), 1);
  593. ValueCell.update(globalUniforms.uLightDirection, lightDirection);
  594. }
  595. if (props.lightAzimuth !== undefined && props.lightAzimuth !== p.lightAzimuth) {
  596. p.lightAzimuth = props.lightAzimuth;
  597. Vec3.directionFromSpherical(lightDirection, degToRad(p.lightInclination), degToRad(p.lightAzimuth), 1);
  598. ValueCell.update(globalUniforms.uLightDirection, lightDirection);
  599. }
  600. if (props.lightColor !== undefined && props.lightColor !== p.lightColor) {
  601. p.lightColor = props.lightColor;
  602. Color.toVec3Normalized(lightColor, p.lightColor);
  603. Vec3.scale(lightColor, lightColor, style.lightIntensity);
  604. ValueCell.update(globalUniforms.uLightColor, lightColor);
  605. }
  606. if (props.ambientColor !== undefined && props.ambientColor !== p.ambientColor) {
  607. p.ambientColor = props.ambientColor;
  608. Color.toVec3Normalized(ambientColor, p.ambientColor);
  609. Vec3.scale(ambientColor, ambientColor, style.ambientIntensity);
  610. ValueCell.update(globalUniforms.uAmbientColor, ambientColor);
  611. }
  612. if (props.style !== undefined) {
  613. p.style = props.style;
  614. Object.assign(style, getStyle(props.style));
  615. Color.toVec3Normalized(lightColor, p.lightColor);
  616. Vec3.scale(lightColor, lightColor, style.lightIntensity);
  617. ValueCell.update(globalUniforms.uLightColor, lightColor);
  618. Color.toVec3Normalized(ambientColor, p.ambientColor);
  619. Vec3.scale(ambientColor, ambientColor, style.ambientIntensity);
  620. ValueCell.update(globalUniforms.uAmbientColor, ambientColor);
  621. ValueCell.updateIfChanged(globalUniforms.uMetalness, style.metalness);
  622. ValueCell.updateIfChanged(globalUniforms.uRoughness, style.roughness);
  623. ValueCell.updateIfChanged(globalUniforms.uReflectivity, style.reflectivity);
  624. }
  625. if (props.clip !== undefined && !deepEqual(props.clip, p.clip)) {
  626. p.clip = props.clip;
  627. Object.assign(clip, getClip(props.clip, clip));
  628. ValueCell.update(globalUniforms.uClipObjectPosition, clip.objects.position);
  629. ValueCell.update(globalUniforms.uClipObjectRotation, clip.objects.rotation);
  630. ValueCell.update(globalUniforms.uClipObjectScale, clip.objects.scale);
  631. ValueCell.update(globalUniforms.uClipObjectType, clip.objects.type);
  632. }
  633. },
  634. setViewport: (x: number, y: number, width: number, height: number) => {
  635. gl.viewport(x, y, width, height);
  636. gl.scissor(x, y, width, height);
  637. if (x !== viewport.x || y !== viewport.y || width !== viewport.width || height !== viewport.height) {
  638. Viewport.set(viewport, x, y, width, height);
  639. ValueCell.update(globalUniforms.uViewport, Vec4.set(globalUniforms.uViewport.ref.value, x, y, width, height));
  640. }
  641. },
  642. setTransparentBackground: (value: boolean) => {
  643. transparentBackground = value;
  644. },
  645. setDrawingBufferSize: (width: number, height: number) => {
  646. if (width !== drawingBufferSize[0] || height !== drawingBufferSize[1]) {
  647. ValueCell.update(globalUniforms.uDrawingBufferSize, Vec2.set(drawingBufferSize, width, height));
  648. }
  649. },
  650. setPixelRatio: (value: number) => {
  651. ValueCell.update(globalUniforms.uPixelRatio, value);
  652. },
  653. props: p,
  654. get stats(): RendererStats {
  655. return {
  656. programCount: ctx.stats.resourceCounts.program,
  657. shaderCount: ctx.stats.resourceCounts.shader,
  658. attributeCount: ctx.stats.resourceCounts.attribute,
  659. elementsCount: ctx.stats.resourceCounts.elements,
  660. framebufferCount: ctx.stats.resourceCounts.framebuffer,
  661. renderbufferCount: ctx.stats.resourceCounts.renderbuffer,
  662. textureCount: ctx.stats.resourceCounts.texture,
  663. vertexArrayCount: ctx.stats.resourceCounts.vertexArray,
  664. drawCount: stats.drawCount,
  665. instanceCount: stats.instanceCount,
  666. instancedDrawCount: stats.instancedDrawCount,
  667. };
  668. },
  669. dispose: () => {
  670. // TODO
  671. }
  672. };
  673. }
  674. }
  675. export { Renderer };