Advertisement
_Fusion_

my first babylonjs project

Mar 25th, 2022 (edited)
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var createScene = function () {
  2.     var scene = new BABYLON.Scene(engine);
  3.     scene.defaultMaterial.dispose()
  4.     scene.defaultMaterial = new BABYLON.CellMaterial("cell", scene)
  5.     scene.defaultMaterial.specularColor = new BABYLON.Color3.Black()
  6.     scene.defaultMaterial.computeHighLevel = true
  7.     scene.ambientColor = BABYLON.Color3.Black()
  8.     scene.fogEnabled = true;
  9.     scene.fogMode = BABYLON.Scene.FOGMODE_EXP;
  10.     scene.fogColor = scene.clearColor
  11.     scene.fogDensity = 0.07
  12.  
  13.     var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
  14.     camera.setTarget(BABYLON.Vector3.Zero());
  15.     camera.attachControl(canvas, true);
  16.  
  17.     var defaultpipeline = new BABYLON.DefaultRenderingPipeline("Default", false, scene, [camera])
  18.     var tonemappingpipeline = new BABYLON.TonemapPostProcess("Tonemapping", BABYLON.TonemappingOperator.HejiDawson, devicePixelRatio, camera)
  19.     var imageprocesspipeline = new BABYLON.ImageProcessingPostProcess("ImageProcessing", devicePixelRatio, camera)
  20.     imageprocesspipeline.exposure = 1.4
  21.  
  22.     BABYLON.Effect.ShadersStore["customVertexShader"]=                
  23.         "precision highp float;\r\n"+
  24.  
  25.         "// Attributes\r\n"+
  26.         "attribute vec3 position;\r\n"+
  27.         "attribute vec2 uv;\r\n"+
  28.  
  29.         "// Uniforms\r\n"+
  30.         "uniform mat4 worldViewProjection;\r\n"+
  31.  
  32.         "// Varying\r\n"+
  33.         "varying vec2 vUV;\r\n"+
  34.  
  35.         "void main(void) {\r\n"+
  36.         "    vec3 f = position;\r\n"+
  37.         "    //f.y = 0.0;\r\n"+
  38.         "    gl_Position = worldViewProjection * vec4(f, 1.0);\r\n"+
  39.  
  40.         "    vUV = uv;\r\n"+
  41.         "}\r\n";
  42.  
  43.         BABYLON.Effect.ShadersStore["customFragmentShader"]=                
  44.         "precision highp float;\r\n"+
  45.  
  46.         "varying vec2 vUV;\r\n"+
  47.  
  48.         "uniform sampler2D textureSampler;\r\n"+
  49.         "uniform float time;\r\n"+
  50.         "uniform vec4 sky1;\r\n"+
  51.         "uniform vec4 sky2;\r\n"+
  52.         "uniform float a;\r\n"+
  53.  
  54.  
  55.  
  56.         "// Sky Gradient by Hazel Quantock\r\n"+
  57.         "#define sphere false\r\n"+
  58.         "#define ground true\r\n"+
  59.  
  60.  
  61.         "// quick and pretty sky colour\r\n"+
  62.         "vec3 SkyColour( vec3 ray )\r\n"+
  63.         "{\r\n"+
  64.         "    return exp2(-ray.y/vec3(.1,.3,.6)); // blue\r\n"+
  65.         "//    return exp2(-ray.y/vec3(.18,.2,.28))*vec3(1,.95,.8); // overcast\r\n"+
  66.         "//    return exp2(-ray.y/vec3(.1,.2,.8))*vec3(1,.75,.5); // dusk\r\n"+
  67.         "//    return exp2(-ray.y/vec3(.03,.2,.9)); // tropical blue\r\n"+
  68.         "//    return exp2(-ray.y/vec3(.4,.06,.01)); // orange-red\r\n"+
  69.         "//    return exp2(-ray.y/vec3(.1,.2,.01)); // green\r\n"+
  70.         "}\r\n"+
  71.  
  72.  
  73.         "vec4 finalImage(vec2 fragCoord, vec2 resolution )\r\n"+
  74.         "{\r\n"+
  75.         "   vec3 ray;\r\n"+
  76.         "    ray.xy = (fragCoord - resolution.xy*.5)/resolution.y;\r\n"+
  77.         "    ray.y -= 0.59;\r\n"+
  78.         "    ray.z = .7;\r\n"+
  79.         "   //ray.z -= dot(ray.xy,ray.xy)*.5; // fisheye lens\r\n"+
  80.         "    ray = normalize(ray);\r\n"+
  81.         "    \r\n"+
  82.         "    // tilt upwards\r\n"+
  83.         "    vec3 k = normalize(vec3(0,sphere?-.5:.8,1));\r\n"+
  84.         "    vec3 i = normalize(cross(vec3(0,1,0),k));\r\n"+
  85.         "    vec3 j = cross(k,i);\r\n"+
  86.         "    ray = ray.x*i+ray.y*j+ray.z*k;\r\n"+
  87.         "    \r\n"+
  88.         "    if ( sphere )\r\n"+
  89.         "    {\r\n"+
  90.         "        // reflect ray off sphere\r\n"+
  91.         "        vec3 c = k*1.8;\r\n"+
  92.         "        float t = dot(c,ray);\r\n"+
  93.         "        float t2 = sqrt(dot(c,c)-t*t);\r\n"+
  94.         "        if ( t2 < 1. )\r\n"+
  95.         "        {\r\n"+
  96.         "            t -= sqrt(1.-t2*t2);\r\n"+
  97.         "            vec3 n = ray*t-c;\r\n"+
  98.         "            ray = reflect(ray,n);\r\n"+
  99.         "        }\r\n"+
  100.         "    }\r\n"+
  101.         "    \r\n"+
  102.         "    vec3 tint = vec3(1);\r\n"+
  103.         "    if ( ground && ray.y < .0 )\r\n"+
  104.         "    {\r\n"+
  105.         "        ray.y = -ray.y;\r\n"+
  106.         "       tint = mix( vec3(.2), tint, pow(1.-ray.y,10.) );\r\n"+
  107.         "    }\r\n"+
  108.         "    \r\n"+
  109.         "    vec4 fragColor;\r\n"+
  110.         "    \r\n"+
  111.         "    vec4 mixS = mix(exp2(-ray.y/sky1), exp2(-ray.y/sky2), a);\r\n"+
  112.         "    fragColor.xyzw = mixS*vec4(tint, 1.0);//SkyColour( ray )*tint;\r\n"+
  113.         "    \r\n"+
  114.  
  115.         "   // signature\r\n"+
  116.         "   #define L(m,n,u,v,l,f) min(f,max(abs(dot(fragCoord-vec2(m,n),vec2(u,v)))-l,abs(dot(fragCoord-vec2(m,n),vec2(-v,u)))-1.))\r\n"+
  117.         "   float sig=L(3,7,0,1,3.5,L(7,7,0,1,3.5,L(5,7,1,0,2.,L(14.5,5,.7071,-.7071,2.5,abs(length(fragCoord-vec2(12.7,7))-3.)-1.))));\r\n"+
  118.         "    fragColor = .6+(clamp(fragColor,0.,1.)-.6)*sig/(.1+abs(sig));\r\n"+
  119.         "    \r\n"+
  120.         "    fragColor.xyz = pow(fragColor.xyz,vec3(1./2.2));\r\n"+
  121.         "    //fragColor.w = 0.5;\r\n"+
  122.         "    \r\n"+
  123.         "    return fragColor;\r\n"+
  124.         "}\r\n"+
  125.  
  126.  
  127.  
  128.         "void main(void) {\r\n"+
  129.         "    //gl_FragColor = texture2D(textureSampler, vUV);\r\n"+
  130.         "    \r\n"+
  131.         "    /*vec3 col;\r\n"+
  132.         "    \r\n"+
  133.         "    vec3 _SkyTint = vec3(1.9,0.55,0);//vec3(1.0, 0.4, 0.1);\r\n"+
  134.         "    vec3 _GroundColor = vec3(0.226,0.000,0.615);//vec3(0.0, 0.0, 0.0);\r\n"+
  135.         "    \r\n"+
  136.         "    float p = vUV.y+cos(time);\r\n"+
  137.  
  138.         "    float p1 = pow(min(1.0, 1.0 - p), 2.0);\r\n"+
  139.  
  140.         "    col.x = smoothstep(_GroundColor.x, _SkyTint.x, p1);\r\n"+
  141.         "    col.y = smoothstep(_GroundColor.y, _SkyTint.y, p1);\r\n"+
  142.         "    col.z = smoothstep(_GroundColor.z, _SkyTint.z, p1);\r\n"+
  143.         "    \r\n"+
  144.         "    gl_FragColor = vec4(1.0-col, 1.0);*/\r\n"+
  145.         "    \r\n"+
  146.         "    gl_FragColor = finalImage(vUV, vec2(1.0, 1.0));\r\n"+
  147.         "    \r\n"+
  148.         "}\r\n";
  149.  
  150.  
  151.  
  152.     var light = new BABYLON.DirectionalLight("Light1", new BABYLON.Vector3(0, -1, 1), scene);
  153.     light.intensity = 1
  154.     light.position = new BABYLON.Vector3(0, 5, -5)
  155.     light.shadowEnabled = true;
  156.  
  157.     var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
  158.     shadowGenerator.useBlurExponentialShadowMap = true;
  159.  
  160.     var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { diameter: 2, segments: 32 }, scene);
  161.     sphere.position.y = 1;
  162.     shadowGenerator.addShadowCaster(sphere);
  163.  
  164.     var ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 50, height: 50, segments: 30 }, scene);
  165.     ground.receiveShadows = true
  166.  
  167.     return scene;
  168. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement