All files / engine/Source/Scene buildVoxelDrawCommands.js

96.55% Statements 56/58
90% Branches 18/20
100% Functions 2/2
96.55% Lines 56/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                            1x                     15x   15x               15x   15x 1x 1x 1x         1x 1x         1x     15x 2x 2x 2x         2x 2x         2x       15x 15x 15x 15x         15x 15x   15x 15x                           15x 15x 15x                             15x       15x 15x     15x       15x 15x     15x 3x 3x     15x 3x 3x     15x 3x 3x       15x 15x 15x       3x 3x   3x 3x   3x 3x     3x 3x       3x                  
import BlendingState from "./BlendingState.js";
import Cartesian2 from "../Core/Cartesian2.js";
import ClippingPlaneCollection from "./ClippingPlaneCollection.js";
import CullFace from "./CullFace.js";
import defined from "../Core/defined.js";
import DrawCommand from "../Renderer/DrawCommand.js";
import Pass from "../Renderer/Pass.js";
import PrimitiveType from "../Core/PrimitiveType.js";
import processVoxelProperties from "./processVoxelProperties.js";
import RenderState from "../Renderer/RenderState.js";
import ShaderDestination from "../Renderer/ShaderDestination.js";
import VoxelBoundsCollection from "./VoxelBoundsCollection.js";
import VoxelRenderResources from "./VoxelRenderResources.js";
 
const textureResolutionScratch = new Cartesian2();
 
/**
 * @function
 *
 * @param {VoxelPrimitive} primitive
 * @param {Context} context
 *
 * @private
 */
function buildVoxelDrawCommands(primitive, context) {
  const renderResources = new VoxelRenderResources(primitive);
 
  processVoxelProperties(renderResources, primitive);
 
  const {
    shaderBuilder,
    clippingPlanes,
    clippingPlanesLength,
    renderBoundPlanes,
    renderBoundPlanesLength,
  } = renderResources;
 
  if (clippingPlanesLength > 0) {
    const functionId = "getClippingPlane";
    const functionSignature = `vec4 ${functionId}(highp sampler2D packedPlanes, int planeNumber)`;
    const textureResolution = ClippingPlaneCollection.getTextureResolution(
      clippingPlanes,
      context,
      textureResolutionScratch,
    );
    const functionBody = getPlaneFunctionBody(textureResolution);
    shaderBuilder.addFunction(
      functionId,
      functionSignature,
      ShaderDestination.FRAGMENT,
    );
    shaderBuilder.addFunctionLines(functionId, [functionBody]);
  }
 
  if (renderBoundPlanesLength > 0) {
    const functionId = "getBoundPlane";
    const functionSignature = `vec4 ${functionId}(highp sampler2D packedPlanes, int planeNumber)`;
    const textureResolution = VoxelBoundsCollection.getTextureResolution(
      renderBoundPlanes,
      context,
      textureResolutionScratch,
    );
    const functionBody = getPlaneFunctionBody(textureResolution);
    shaderBuilder.addFunction(
      functionId,
      functionSignature,
      ShaderDestination.FRAGMENT,
    );
    shaderBuilder.addFunctionLines(functionId, [functionBody]);
  }
 
  // Compile shaders
  const shaderBuilderPick = shaderBuilder.clone();
  shaderBuilderPick.addDefine("PICKING", undefined, ShaderDestination.FRAGMENT);
  const shaderBuilderPickVoxel = shaderBuilder.clone();
  shaderBuilderPickVoxel.addDefine(
    "PICKING_VOXEL",
    undefined,
    ShaderDestination.FRAGMENT,
  );
  const shaderProgram = shaderBuilder.buildShaderProgram(context);
  const shaderProgramPick = shaderBuilderPick.buildShaderProgram(context);
  const shaderProgramPickVoxel =
    shaderBuilderPickVoxel.buildShaderProgram(context);
  const renderState = RenderState.fromCache({
    cull: {
      enabled: true,
      face: CullFace.BACK,
    },
    depthTest: {
      enabled: false,
    },
    depthMask: false,
    // internally the shader does premultiplied alpha, so it makes sense to blend that way too
    blending: BlendingState.PRE_MULTIPLIED_ALPHA_BLEND,
  });
 
  // Create the draw commands
  const viewportQuadVertexArray = context.getViewportQuadVertexArray();
  const depthTest = primitive._depthTest;
  const drawCommand = new DrawCommand({
    vertexArray: viewportQuadVertexArray,
    primitiveType: PrimitiveType.TRIANGLES,
    renderState: renderState,
    shaderProgram: shaderProgram,
    uniformMap: renderResources.uniformMap,
    modelMatrix: primitive._compoundModelMatrix,
    pass: Pass.VOXELS,
    executeInClosestFrustum: true,
    owner: this,
    cull: depthTest, // don't cull or occlude if depth testing is off
    occlude: depthTest, // don't cull or occlude if depth testing is off
  });
 
  // Create the pick draw command
  const drawCommandPick = DrawCommand.shallowClone(
    drawCommand,
    new DrawCommand(),
  );
  drawCommandPick.shaderProgram = shaderProgramPick;
  drawCommandPick.pickOnly = true;
 
  // Create the pick voxels draw command
  const drawCommandPickVoxel = DrawCommand.shallowClone(
    drawCommand,
    new DrawCommand(),
  );
  drawCommandPickVoxel.shaderProgram = shaderProgramPickVoxel;
  drawCommandPickVoxel.pickOnly = true;
 
  // Delete the old shader programs
  if (defined(primitive._drawCommand)) {
    const command = primitive._drawCommand;
    command.shaderProgram =
      command.shaderProgram && command.shaderProgram.destroy();
  }
  if (defined(primitive._drawCommandPick)) {
    const command = primitive._drawCommandPick;
    command.shaderProgram =
      command.shaderProgram && command.shaderProgram.destroy();
  }
  if (defined(primitive._drawCommandPickVoxel)) {
    const command = primitive._drawCommandPickVoxel;
    command.shaderProgram =
      command.shaderProgram && command.shaderProgram.destroy();
  }
 
  primitive._drawCommand = drawCommand;
  primitive._drawCommandPick = drawCommandPick;
  primitive._drawCommandPickVoxel = drawCommandPickVoxel;
}
 
function getPlaneFunctionBody(textureResolution) {
  const width = textureResolution.x;
  const height = textureResolution.y;
 
  const pixelWidth = 1.0 / width;
  const pixelHeight = 1.0 / height;
 
  let pixelWidthString = `${pixelWidth}`;
  Iif (pixelWidthString.indexOf(".") === -1) {
    pixelWidthString += ".0";
  }
  let pixelHeightString = `${pixelHeight}`;
  Iif (pixelHeightString.indexOf(".") === -1) {
    pixelHeightString += ".0";
  }
 
  return `int pixY = planeNumber / ${width};
    int pixX = planeNumber - (pixY * ${width});
    // Sample from center of pixel
    float u = (float(pixX) + 0.5) * ${pixelWidthString};
    float v = (float(pixY) + 0.5) * ${pixelHeightString};
    return texture(packedPlanes, vec2(u, v));`;
}
 
export default buildVoxelDrawCommands;