|
@@ -4,20 +4,15 @@
|
|
|
* @author Alexander Rose <alexander.rose@weirdbyte.de>
|
|
|
*/
|
|
|
|
|
|
-import REGL = require('regl');
|
|
|
import { ValueCell } from 'mol-util/value-cell'
|
|
|
import { ColorData } from 'mol-geo/util/color-data';
|
|
|
import { SizeData } from 'mol-geo/util/size-data';
|
|
|
|
|
|
-import { Attributes, AttributesData, AttributesBuffers } from '../renderable'
|
|
|
-import Attribute from '../attribute'
|
|
|
-import { ShaderDefines } from '../shaders';
|
|
|
+import { ShaderDefines } from '../shader-code';
|
|
|
import { UniformDefs, UniformValues } from '../webgl/uniform';
|
|
|
-import { AttributeDefs } from '../webgl/buffer';
|
|
|
-
|
|
|
-
|
|
|
-export type ReglUniforms = { [k: string]: REGL.Uniform | REGL.Texture }
|
|
|
-export type ReglAttributes = { [k: string]: REGL.AttributeConfig }
|
|
|
+import { AttributeDefs, AttributeValues } from '../webgl/buffer';
|
|
|
+import { Vec3, Vec2 } from 'mol-math/linear-algebra';
|
|
|
+import { TextureDefs, TextureValues } from '../webgl/texture';
|
|
|
|
|
|
export function calculateTextureInfo (n: number, itemSize: number) {
|
|
|
const sqN = Math.sqrt(n * itemSize)
|
|
@@ -27,47 +22,15 @@ export function calculateTextureInfo (n: number, itemSize: number) {
|
|
|
return { width, height, length: width * height * itemSize }
|
|
|
}
|
|
|
|
|
|
-export interface Texture extends Uint8Array {
|
|
|
- width: number,
|
|
|
+export interface TextureImage {
|
|
|
+ array: Uint8Array
|
|
|
+ width: number
|
|
|
height: number
|
|
|
}
|
|
|
|
|
|
-export function createColorTexture (n: number): Texture {
|
|
|
- const colorTexInfo = calculateTextureInfo(n, 3)
|
|
|
- const colorTexture = new Uint8Array(colorTexInfo.length)
|
|
|
- return Object.assign(colorTexture, {
|
|
|
- width: colorTexInfo.width,
|
|
|
- height: colorTexInfo.height
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
-export function createTransformAttributes (regl: REGL.Regl, transform: ValueCell<Float32Array>, count: number) {
|
|
|
- const size = 4
|
|
|
- const divisor = 1
|
|
|
- const bpe = transform.ref.value.BYTES_PER_ELEMENT
|
|
|
- const stride = 16 * bpe
|
|
|
- return {
|
|
|
- transformColumn0: Attribute.create(regl, transform, count, { size, divisor, offset: 0, stride }),
|
|
|
- transformColumn1: Attribute.create(regl, transform, count, { size, divisor, offset: 4 * bpe, stride }),
|
|
|
- transformColumn2: Attribute.create(regl, transform, count, { size, divisor, offset: 8 * bpe, stride }),
|
|
|
- transformColumn3: Attribute.create(regl, transform, count, { size, divisor, offset: 12 * bpe, stride })
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export function createColorUniforms (regl: REGL.Regl, color: ValueCell<Texture>) {
|
|
|
- const colorTex = regl.texture({
|
|
|
- width: color.ref.value.width,
|
|
|
- height: color.ref.value.height,
|
|
|
- format: 'rgb',
|
|
|
- type: 'uint8',
|
|
|
- wrapS: 'clamp',
|
|
|
- wrapT: 'clamp',
|
|
|
- data: color.ref.value
|
|
|
- })
|
|
|
- return {
|
|
|
- colorTex,
|
|
|
- colorTexSize: [ color.ref.value.width, color.ref.value.height ]
|
|
|
- }
|
|
|
+export function createColorTexture (n: number): TextureImage {
|
|
|
+ const { length, width, height } = calculateTextureInfo(n, 3)
|
|
|
+ return { array: new Uint8Array(length), width, height }
|
|
|
}
|
|
|
|
|
|
export function getColorDefines(color: ValueCell<ColorData>) {
|
|
@@ -91,14 +54,6 @@ export function getSizeDefines(size: ValueCell<SizeData>) {
|
|
|
return defines
|
|
|
}
|
|
|
|
|
|
-export function getBuffers<T extends AttributesData>(attributes: Attributes<T>): AttributesBuffers<T> {
|
|
|
- const buffers: AttributesBuffers<any> = {}
|
|
|
- for (const k of Object.keys(attributes)) {
|
|
|
- buffers[k] = attributes[k].buffer
|
|
|
- }
|
|
|
- return buffers as AttributesBuffers<T>
|
|
|
-}
|
|
|
-
|
|
|
export function fillSerial<T extends Helpers.NumberArray> (array: T) {
|
|
|
const n = array.length
|
|
|
for (let i = 0; i < n; ++i) array[ i ] = i
|
|
@@ -146,16 +101,16 @@ export function getBaseUniformDefs(props: BaseProps) {
|
|
|
|
|
|
export function getBaseUniformValues(props: BaseProps) {
|
|
|
const { objectId, instanceCount, elementCount } = props
|
|
|
- const uniformValues: UniformValues<any> = {
|
|
|
+ const uniformValues: UniformValues = {
|
|
|
objectId, instanceCount, elementCount
|
|
|
}
|
|
|
const color = props.color.ref.value
|
|
|
if (color.type === 'instance' || color.type === 'element' || color.type === 'element-instance') {
|
|
|
const { width, height } = color.value.ref.value
|
|
|
- uniformValues.colorTex = new ImageData(new Uint8ClampedArray(color.value.ref.value), width, height)
|
|
|
- uniformValues.colorTexSize = [ width, height ]
|
|
|
+ uniformValues.colorTex = new ImageData(new Uint8ClampedArray(color.value.ref.value.array), width, height)
|
|
|
+ uniformValues.colorTexSize = Vec2.create(width, height)
|
|
|
} else if (color.type === 'uniform') {
|
|
|
- uniformValues.color = color.value
|
|
|
+ uniformValues.color = color.value as Vec3
|
|
|
}
|
|
|
const size = props.size ? props.size.ref.value : undefined
|
|
|
if (size && size.type === 'uniform') {
|
|
@@ -169,69 +124,82 @@ export function getBaseAttributeDefs(props: BaseProps) {
|
|
|
instanceId: { kind: 'float32', itemSize: 1, divisor: 1 },
|
|
|
position: { kind: 'float32', itemSize: 1, divisor: 0 },
|
|
|
elementId: { kind: 'float32', itemSize: 1, divisor: 0 },
|
|
|
- transformColumn0: { kind: 'float32', itemSize: 4, divisor: 1 },
|
|
|
- transformColumn1: { kind: 'float32', itemSize: 4, divisor: 1 },
|
|
|
- transformColumn2: { kind: 'float32', itemSize: 4, divisor: 1 },
|
|
|
- transformColumn3: { kind: 'float32', itemSize: 4, divisor: 1 },
|
|
|
+ transform: { kind: 'float32', itemSize: 16, divisor: 1 },
|
|
|
+ }
|
|
|
+ if (props.normal) {
|
|
|
+ attributeDefs.normal = { kind: 'float32', itemSize: 3, divisor:0 }
|
|
|
}
|
|
|
const color = props.color.ref.value
|
|
|
- if (color.type === 'instance' || color.type === 'element' || color.type === 'element-instance') {
|
|
|
- uniformDefs.colorTexSize = 'v2'
|
|
|
- uniformDefs.colorTex = 't2'
|
|
|
- } else if (color.type === 'uniform') {
|
|
|
- uniformDefs.color = 'v3'
|
|
|
+ if (color.type === 'attribute') {
|
|
|
+ attributeDefs.color = { kind: 'float32', itemSize: 3, divisor:0 }
|
|
|
}
|
|
|
const size = props.size ? props.size.ref.value : undefined
|
|
|
- if (size && size.type === 'uniform') {
|
|
|
- uniformDefs.size = 'f'
|
|
|
+ if (size && size.type === 'attribute') {
|
|
|
+ attributeDefs.size = { kind: 'float32', itemSize: 1, divisor:0 }
|
|
|
}
|
|
|
return attributeDefs
|
|
|
}
|
|
|
|
|
|
-export function createBaseAttributes(regl: REGL.Regl, props: BaseProps): ReglAttributes {
|
|
|
- const { instanceCount, positionCount, position, id, normal, transform } = props
|
|
|
+export function getBaseAttributeValues(props: BaseProps) {
|
|
|
+ const { instanceCount, position, id, normal, transform } = props
|
|
|
const instanceId = ValueCell.create(fillSerial(new Float32Array(instanceCount)))
|
|
|
- const attributes = getBuffers({
|
|
|
- instanceId: Attribute.create(regl, instanceId, instanceCount, { size: 1, divisor: 1 }),
|
|
|
- position: Attribute.create(regl, position, positionCount, { size: 3 }),
|
|
|
- elementId: Attribute.create(regl, id, positionCount, { size: 1 }),
|
|
|
- ...createTransformAttributes(regl, transform, instanceCount)
|
|
|
- })
|
|
|
+ const attributeValues: AttributeValues = {
|
|
|
+ instanceId: instanceId.ref.value,
|
|
|
+ position: position.ref.value,
|
|
|
+ elementId: id.ref.value,
|
|
|
+ transform: transform.ref.value
|
|
|
+ }
|
|
|
if (normal) {
|
|
|
- attributes.normal = Attribute.create(regl, normal as any, positionCount, { size: 3 }).buffer
|
|
|
+ attributeValues.normal = normal.ref.value
|
|
|
}
|
|
|
const color = props.color.ref.value
|
|
|
if (color.type === 'attribute') {
|
|
|
- attributes.color = Attribute.create(regl, color.value, positionCount, { size: 3 }).buffer
|
|
|
+ attributeValues.color = color.value.ref.value
|
|
|
}
|
|
|
const size = props.size ? props.size.ref.value : undefined
|
|
|
if (size && size.type === 'attribute') {
|
|
|
- attributes.size = Attribute.create(regl, size.value, positionCount, { size: 1 }).buffer
|
|
|
+ attributeValues.size = size.value.ref.value
|
|
|
+ }
|
|
|
+ return attributeValues
|
|
|
+}
|
|
|
+
|
|
|
+export function getBaseTextureDefs(props: BaseProps) {
|
|
|
+ const textureDefs: TextureDefs = {}
|
|
|
+ const color = props.color.ref.value
|
|
|
+ if (color.type === 'instance' || color.type === 'element' || color.type === 'element-instance') {
|
|
|
+ textureDefs.colorTex = true
|
|
|
}
|
|
|
- return attributes
|
|
|
+ return textureDefs
|
|
|
}
|
|
|
|
|
|
-export function createBaseDefines(regl: REGL.Regl, props: BaseProps): ShaderDefines {
|
|
|
+export function getBaseTextureValues(props: BaseProps) {
|
|
|
+ const textureValues: TextureValues = {}
|
|
|
+ const color = props.color.ref.value
|
|
|
+ if (color.type === 'instance' || color.type === 'element' || color.type === 'element-instance') {
|
|
|
+ textureValues.colorTex = color.value.ref.value
|
|
|
+ }
|
|
|
+ return textureValues
|
|
|
+}
|
|
|
+
|
|
|
+export function getBaseDefines(props: BaseProps): ShaderDefines {
|
|
|
return {
|
|
|
...getColorDefines(props.color),
|
|
|
...(props.size ? getSizeDefines(props.size) : undefined)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function destroyAttributes(attributes: ReglAttributes) {
|
|
|
- for (const k in attributes) {
|
|
|
- const buffer = attributes[k].buffer
|
|
|
- if (buffer) {
|
|
|
- buffer.destroy()
|
|
|
- }
|
|
|
+export function getBaseDefs(props: BaseProps) {
|
|
|
+ return {
|
|
|
+ uniformDefs: getBaseUniformDefs(props),
|
|
|
+ attributeDefs: getBaseAttributeDefs(props),
|
|
|
+ textureDefs: getBaseTextureDefs(props),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function destroyUniforms(uniforms: ReglUniforms) {
|
|
|
- for (const k in uniforms) {
|
|
|
- const uniform = uniforms[k]
|
|
|
- if ((uniform as any).destroy) {
|
|
|
- (uniform as any).destroy()
|
|
|
- }
|
|
|
+export function getBaseValues(props: BaseProps) {
|
|
|
+ return {
|
|
|
+ uniformValues: getBaseUniformValues(props),
|
|
|
+ attributeValues: getBaseAttributeValues(props),
|
|
|
+ textureValues: getBaseTextureValues(props),
|
|
|
}
|
|
|
-}
|
|
|
+}
|