All files / common/rage_modules material.mjs

80.26% Statements 122/152
68.42% Branches 13/19
63.15% Functions 12/19
80.26% Lines 122/152

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 1521x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x     1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x 1x 1x         1x 1x         1x 1x         1x 1x         1x 1x             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
 
import { hexToRgb } from './utils.mjs'
 
 
 
export class Material {
    constructor(){
 
        this.id = false
 
        this.baseColor = new Uint8ClampedArray(4)
        this.ambientColor = new Uint8ClampedArray(4)
        this.diffuseColor = new Uint8ClampedArray(4)
        this.specularColor = new Uint8ClampedArray(4)
        this.emissionColor = new Uint8ClampedArray(4)
 
        this.useColor = true
        this.useADSE = false
        this.useTexture = false
        this.hasTexture = false
        this.texture = false
 
    }
 
    addToResourceManager(rm){
        
    }
 
    setBaseColor(red, green, blue, alpha){
        this.baseColor[0] = red
        this.baseColor[1] = green
        this.baseColor[2] = blue
        this.baseColor[3] = alpha || 255
        return this
    }
 
    setAmbientColor(red, green, blue, alpha){
        this.ambientColor[0] = red
        this.ambientColor[1] = green
        this.ambientColor[2] = blue
        this.ambientColor[3] = alpha || 255
        return this
    }
 
    setDiffuseColor(red, green, blue, alpha){
        this.diffuseColor[0] = red
        this.diffuseColor[1] = green
        this.diffuseColor[2] = blue
        this.diffuseColor[3] = alpha || 255
        return this
    }
 
    setSpecularColor(red, green, blue, alpha){
        this.specularColor[0] = red
        this.specularColor[1] = green
        this.specularColor[2] = blue
        this.specularColor[3] = alpha || 255
        return this
    }
 
    setEmissionColor(red, green, blue, alpha){
        this.emissionColor[0] = red
        this.emissionColor[1] = green
        this.emissionColor[2] = blue
        this.emissionColor[3] = alpha || 255
        return this
    }
 
    setADSE(ambient, diffuse, specular, emission){
        this.setAmbientColor(ambient[0], ambient[1], ambient[2], ambient[3])
        this.setDiffuseColor(diffuse[0], diffuse[1], diffuse[2], diffuse[3])
        this.setSpecularColor(specular[0], specular[1], specular[2], specular[3])
        this.setEmissionColor(emission[0], emission[1], emission[2], emission[3])
        return this
    }
 
    setBaseColorFromHex(hex){
        let color = hexToRgb(hex)
        this.setBaseColor(color.r, color.g, color.b, color.a || 255)
        return this
    }
 
    setAmbientColorFromHex(hex){
        let color = hexToRgb(hex)
        this.setAmbientColor(color.r, color.g, color.b, color.a || 255)
        return this
    }
 
    setDiffuseColorFromHex(hex){
        let color = hexToRgb(hex)
        this.setDiffuseColor(color.r, color.g, color.b, color.a || 255)
        return this
    }
 
    setSpecularColorFromHex(hex){
        let color = hexToRgb(hex)
        this.setSpecularColor(color.r, color.g, color.b, color.a || 255)
        return this
    }
 
    setEmissionColorFromHex(hex){
        let color = hexToRgb(hex)
        this.setEmissionColor(color.r, color.g, color.b, color.a || 255)
        return this
    }
 
    setADSEFromHex(ambient, diffuse, specular, emission){
        this.setAmbientColorFromHex(ambient)
        this.setDiffuseColorFromHex(diffuse)
        this.setSpecularColorFromHex(specular)
        this.setEmissionColorFromHex(emission)
        return this
    }
 
 
    createTexture(src){
        // CODE: COMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        this.texture = new Texture(this, src)
        return this
    }
 
    textureLoaded(){
 
        this.hasTexture = true
        this.useTexture = true
    
    }
}
 
export class Texture {
    constructor(parent, src){
        this.parent = parent
        this.src = src
        this.img = new Image()
        this.img.src = this.src
        this.img.onload = (event)=>{
            this.processImage(event)
        }
 
        this.processed = false
 
    }
 
    processImage(event){
 
 
        this.parent.textureLoaded()
 
    }
}