diff --git a/src/shaders/DefaultCubemapNormalMaterial.hx b/src/shaders/DefaultCubemapNormalMaterial.hx new file mode 100644 index 00000000..1b00aae1 --- /dev/null +++ b/src/shaders/DefaultCubemapNormalMaterial.hx @@ -0,0 +1,98 @@ +package shaders; + +class DefaultCubemapNormalMaterial extends hxsl.Shader { + static var SRC = { + @param var diffuseMap:Sampler2D; + @param var specularColor:Vec4; + @param var shininess:Float; + @param var secondaryMapUvFactor:Float; + @param var cubeMap:SamplerCube; + @global var camera:{ + var position:Vec3; + @var var dir:Vec3; + }; + @global var global:{ + @perObject var modelView:Mat4; + @perObject var modelViewInverse:Mat4; + }; + @input var input:{ + var position:Vec3; + var normal:Vec3; + var uv:Vec2; + var t:Vec3; + var b:Vec3; + var n:Vec3; + }; + var calculatedUV:Vec2; + var pixelColor:Vec4; + var specColor:Vec3; + var specPower:Float; + @const var doGammaRamp:Bool; + @var var outShading:Vec4; + @var var outLightVec:Vec4; + @var var outEyePos:Vec3; + @var var outReflectVec:Vec3; + function lambert(normal:Vec3, lightPosition:Vec3):Float { + var result = dot(normal, lightPosition); + return saturate(result); + } + function vertex() { + calculatedUV = input.uv; + outLightVec = vec4(0); + var inLightVec = vec3(-0.5732, 0.27536, -0.77176) * mat3(global.modelViewInverse); + var eyePos = camera.position * mat3x4(global.modelViewInverse); + // eyePos /= vec3(global.modelViewInverse[0].x, global.modelViewInverse[1].y, global.modelViewInverse[2].z); + outLightVec.xyz = -inLightVec; + outLightVec.w = step(-0.5, dot(input.normal, -inLightVec)); + outEyePos = eyePos; + outShading = vec4(saturate(dot(-inLightVec, input.normal))); + outShading.w = 1; + outShading *= vec4(1.08, 1.03, 0.90, 1); + + var cubeTrans = mat3(global.modelView); + var cubeEyePos = camera.position - global.modelView[3].xyz; + + var cubeVertPos = input.position * cubeTrans; + var cubeNormal = (input.normal * cubeTrans).normalize(); + var eyeToVert = cubeVertPos - cubeEyePos; + outReflectVec = reflect(eyeToVert, cubeNormal); + } + function fragment() { + // Diffuse part + var diffuse = diffuseMap.get(calculatedUV); + var ambient = vec4(0.472, 0.424, 0.475, 1.00); + + var outCol = (outShading + ambient) * diffuse; + outCol += diffuse.a * cubeMap.get(outReflectVec); + + var eyeVec = (outEyePos - input.position).normalize(); + var halfAng = (eyeVec + outLightVec.xyz).normalize(); + var specValue = saturate(input.normal.dot(halfAng)) * outLightVec.w; + var specular = specularColor * pow(specValue, shininess); + + outCol.a = 1; + outCol += specular * diffuse.a; + + // Gamma correction using our regression model + if (doGammaRamp) { + var a = 1.00759; + var b = 1.18764; + outCol.x = a * pow(outCol.x, b); + outCol.y = a * pow(outCol.y, b); + outCol.z = a * pow(outCol.z, b); + } + + pixelColor = outCol; + } + } + + public function new(diffuse, cubeMap, shininess, specularColor, secondaryMapUvFactor) { + super(); + this.diffuseMap = diffuse; + this.cubeMap = cubeMap; + this.shininess = shininess; + this.specularColor = specularColor; + this.secondaryMapUvFactor = secondaryMapUvFactor; + this.doGammaRamp = true; + } +} diff --git a/src/shapes/EasterEgg.hx b/src/shapes/EasterEgg.hx index 0aaa83cb..612af703 100644 --- a/src/shapes/EasterEgg.hx +++ b/src/shapes/EasterEgg.hx @@ -41,4 +41,30 @@ class EasterEgg extends PowerUp { } public function use(timeState:src.TimeState) {} + + override function postProcessMaterial(matName:String, material:h3d.mat.Material) { + if (matName == "egg_skin") { + var diffuseTex = ResourceLoader.getTexture("data/shapes/items/egg_skin.png").resource; + diffuseTex.wrap = Repeat; + diffuseTex.mipMap = Nearest; + + var cubemapTex = new h3d.mat.Texture(64, 64, [Cube]); + var cubemapFace = ResourceLoader.getImage('data/skies/gemCubemapUp.png').resource; + for (i in 0...6) { + cubemapTex.uploadPixels(cubemapFace.getPixels(), 0, i); + } + + var shader = new shaders.DefaultCubemapNormalMaterial(diffuseTex, cubemapTex, 32, new h3d.Vector(1, 1, 1, 1), 1); + shader.doGammaRamp = false; + var dtsTex = material.mainPass.getShader(shaders.DtsTexture); + dtsTex.passThrough = true; + material.mainPass.removeShader(material.textureShader); + material.mainPass.addShader(shader); + var thisprops:Dynamic = material.getDefaultProps(); + thisprops.light = false; // We will calculate our own lighting + material.props = thisprops; + material.shadows = false; + material.receiveShadows = true; + } + } }