import { assert } from "@here/harp-utils" ; import * as THREE from "three" ; /**
 * Values for boolean shader defines
 */ export const DEFINE_BOOL_TRUE = "" ; export const DEFINE_BOOL_FALSE = undefined ; param shaderContent param shaderName param insertedShaderName param addTab export function insertShaderInclude ( shaderContent , shaderName , insertedShaderName , addTab ? ) { const tabChar = addTab === true ? "\t" : "" ; const result = shaderContent . replace ( `#include <${ shaderName }>` , `#include <${ shaderName }>
${ tabChar }#include <${ insertedShaderName }>` ) ; return result ; } export interface ForcedBlending { /**
     * This material has `blending` always enabled regardless of `opacity` setting.s
     */ forcedBlending ? : true ; } param material note export function enforceBlending ( material ) { if ( material . transparent ) { return ; } enableBlending ( material ) ; material . forcedBlending = true ; } note see THREE . Material . needsUpdate . param material export function enableBlending ( material ) { if ( material . transparent === true || material . forcedBlending === true ) { return ; } material . blending = THREE . CustomBlending ; if ( material . premultipliedAlpha === true ) { material . blendSrc = THREE . OneFactor ; material . blendDst = THREE . OneMinusSrcAlphaFactor ; material . blendSrcAlpha = THREE . OneFactor ; material . blendDstAlpha = THREE . OneMinusSrcAlphaFactor ; } else { material . blendSrc = THREE . SrcAlphaFactor ; material . blendDst = THREE . OneMinusSrcAlphaFactor ; material . blendSrcAlpha = THREE . OneFactor ; material . blendDstAlpha = THREE . OneMinusSrcAlphaFactor ; } } note see THREE . Material . needsUpdate . see enableBlending . param material export function disableBlending ( material ) { if ( material . transparent === true || material . forcedBlending === true ) { return ; } material . blending = THREE . NormalBlending ; } note param material param key param value returns export function setShaderMaterialDefine ( material , key , value ) { assert ( material . defines !== undefined , "Do not use this function in ShaderMaterial derived c-tor." ) ; const semanticValue = getShaderMaterialDefine ( material , key ) ; const needsUpdate = value !== semanticValue ; if ( ! needsUpdate ) { return false ; } setShaderDefine ( material . defines , key , value ) ; material . needsUpdate = needsUpdate ; return true ; } note see setShaderMaterialDefine . param material param key param fallbackValue export function getShaderMaterialDefine ( material , key , fallbackValue = false ) { if ( material . defines === undefined ) { return fallbackValue ; } return getShaderDefine ( material . defines , key ) ; } param defines param key param value returns see setShaderMaterialDefine . export function setShaderDefine ( defines , key , value ) { let updated = false ; if ( typeof value === "number" ) { updated = defines [ key ] !== value ; defines [ key ] = value ; } else if ( value === true ) { updated = defines [ key ] !== DEFINE_BOOL_TRUE ; defines [ key ] = DEFINE_BOOL_TRUE ; } else if ( value === false && defines [ key ] !== undefined ) { delete defines [ key ] ; updated = true ; } return updated ; } param defines param key export function getShaderDefine ( defines , key ) { const currentValue = defines [ key ] ; const semanticValue = currentValue === DEFINE_BOOL_FALSE ? false : currentValue === DEFINE_BOOL_TRUE ? true : currentValue ; return semanticValue ; }