Unity Shaders Intro Part 2: HLSL/CG | Edge Distortion Effects

I recently saw these UI effects in a game called Cult of the Lamb and they were very satisfying to watch. Let’s learn how to create our own types of effects like these.

Prerequisites

  • Unity (I’m using 2022.3.17f)
  • Photo editing software (Aseprite, Photoshop, etc)
  • Seamless perlin noise generator for the noise texture we will need later

Base 2D Shader

Create a basic empty file with the ‘.shader’ extension in your Unity project or Right click > Shader > Standard Surface Shader

Shader "Custom/EdgeShader" 
{
	Properties 
	{
	}
	
	SubShader
	{		
		Pass 
		{
			CGPROGRAM
			ENDCG
		}
	}
}

We want to begin with a base shader to manipulate, so let’s start by displaying a sprite.

Our shader must expose it to the editor in order to set our texture. Add a line under our properties defining a main texture.

_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}

And the variable under SubShader.

sampler2D _MainTex;
float4 _MainTex_ST;

The _ST value will contain the tiling and offset fields for the material texture properties. This information is passed into our shader in the format we specified.

Now define the vertex and fragment functions.

struct vct 
{
	float4 pos : SV_POSITION;
	float2 uv : TEXCOORD0;
};

vct vert_vct (appdata_base v) 
{
	vct o;
	o.pos = UnityObjectToClipPos(v.vertex);
	o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
	return o;
}

fixed4 frag_mult (vct i) : COLOR 
{
	fixed4 col = tex2D(_MainTex, i.uv);
	col.rgb = col.rgb * col.a;
	return col;
}

Simple enough.

…or is it? That doesn’t look like it’s working properly. Let’s fix it.

We can add a Blend under our tags to fix the transparency issue.

Blend SrcAlpha OneMinusSrcAlpha

And we can just add the color property to our shader. At this point, we can display 2D sprites on the screen, yay!

Shader "Custom/EdgeShaderB" 
{
    Properties 
    {
        _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }
    
    SubShader
    {		
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        Blend SrcAlpha OneMinusSrcAlpha
        
        Pass 
        {
            CGPROGRAM
            #pragma vertex vert_vct
            #pragma fragment frag_mult 
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            
            struct vct 
            {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            vct vert_vct(vct v)
            {
                vct o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                return o;
            }

            fixed4 frag_mult (vct i) : COLOR
            {
                fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
                return col;
            }

            ENDCG
        }
    }
}

Now we can start messing with things.

Edge Distortion Shader

We want to add some movement and distortion to our sprite. Begin with movement.

How can we manipulate our shader pixels? Let’s show an example by modifying our main texture. We’ll simply change the position. To do so, we can do something simple like shifting the texture coordinate down and to the left.

fixed4 frag_mult (vct i) : COLOR
{
	float2 shift = i.texcoord + float2(0.15, 0.25);
	fixed4 col = tex2D(_MainTex, shift) * i.color;

	return col;
}

Okay, now how about some movement?

fixed4 frag_mult (vct i) : COLOR
{
	float2 shift = i.texcoord + float2(cos(_Time.x * 2.0) * 0.2, sin(_Time.x * 2.0) * 0.2);
	fixed4 col = tex2D(_MainTex, shift) * i.color;

	return col;
}

If you examine your sprite at this point, you may notice some odd distortion as it moves.

Set your sprite’s import settings correctly!
Mesh Type: Full Rect
Wrap Mode: Repeat

Once you ensure your sprite has the correct import settings, it’s time to introduce our final 2d sprite we want to manipulate with the shader to achieve our effect.

This image will greatly change the shader appearance, and you should try different gradients and patterns. Here’s my image scaled up:

But I recommend using the smallest resolution that looks good for your project due to memory and performance.

yes it’s that small (12×12)

We also need a seamless noise texture, for the distortion.

Let’s add another variable for it.

_NoiseTex ("Base (RGB) Trans (A)", 2D) = "white" {}

Once we’ve assigned our noise texture, it’s time to start moving it.

fixed4 frag_mult (vct i) : COLOR
{
	float2 shim = i.texcoord + float2(
		tex2D(_NoiseTex, i.vertex.xy/500 - float2(_Time.w/60, 0)).x,
		tex2D(_NoiseTex, i.vertex.xy/500 - float2(0, _Time.w/60)).y
	);
	fixed4 col = tex2D(_MainTex, shim) * i.color;
	return col;
}

Now, add the static sprite to its left in the same color and connect it vertically.

Adjusting the transparency will function as expected, so we could overlay this.

Shader "Custom/EdgeShader" 
{
    Properties 
    {
        _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
        _NoiseTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }
    
    SubShader
    {		
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        Blend SrcAlpha OneMinusSrcAlpha 
        
        Pass 
        {
            CGPROGRAM
            #pragma vertex vert_vct
            #pragma fragment frag_mult 
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            sampler2D _NoiseTex;
            float4 _MainTex_ST;
            float4 _NoiseTex_ST;
            
            struct vct 
            {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            vct vert_vct(vct v)
            {
                vct o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                return o;
            }

            fixed4 frag_mult (vct i) : COLOR
            {
                    float2 shim = i.texcoord + 
                float2(tex2D(_NoiseTex, i.vertex.xy/500 - float2(_Time.w/60, 0)).x,
                tex2D(_NoiseTex, i.vertex.xy/500 - float2(0, _Time.w/60)).y);
                fixed4 col = tex2D(_MainTex, shim) * i.color;
                return col;
            }

            ENDCG
        }
    }
}

Crown Shader

Here’s my quick little crown sprite.

Let’s make it evil.

We can repurpose the wall shader we just created and scale down the distortion as well as smoothing it

fixed4 frag_mult(v2f_vct i) : COLOR
{
    float2 shim = i.texcoord + float2(
        tex2D(_NoiseTex, i.vertex.xy/250 - float2(_Time.w/7.2, 0)).x,
        tex2D(_NoiseTex, i.vertex.xy/250 - float2(0, _Time.w/7.2)).y
    )/ 20;

    fixed4 col = tex2D(_MainTex, col) * i.color;

    return col;
}

Then we can add another pass to handle the normal sprite display.

Shader "Custom/CrownShader" 
{
    Properties 
    {
        _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
        _NoiseTex ("Base (RGB) Trans (A)", 2D) = "white" {}
        _SpriteColor ("Color Tint Mult", Color) = (1,1,1,1)
    }
    
    SubShader
    {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        Blend SrcAlpha OneMinusSrcAlpha
        
        Pass 
        {
            CGPROGRAM
            #pragma vertex vert_vct
            #pragma fragment frag_mult 
            #pragma fragmentoption ARB_precision_hint_fastest
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            sampler2D _NoiseTex;
            float4 _MainTex_ST;
            float4 _NoiseTex_ST;

            struct vct
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            vct vert_vct(vct v)
            {
                vct o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                return o;
            }

            fixed4 frag_mult(vct i) : COLOR
            {
                float2 shim = i.texcoord + float2(
                    tex2D(_NoiseTex, i.vertex.xy/250 - float2(_Time.w/7.2, 0)).x,
                    tex2D(_NoiseTex, i.vertex.xy/250 - float2(0, _Time.w/7.2)).y
                )/ 20;

                shim *= float2(0.97, 0.91);
                shim -= float2(0.01, 0);

                fixed4 col = tex2D(_MainTex, shim) * i.color;
                return col;
            }
            
            ENDCG
        } 
        Pass 
        {
            CGPROGRAM
            #pragma vertex vert_vct
            #pragma fragment frag_mult 
            #pragma fragmentoption ARB_precision_hint_fastest
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            sampler2D _NoiseTex;
            float4 _MainTex_ST;
            float4 _NoiseTex_ST;

            float4 _SpriteColor;

            struct vct 
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            vct vert_vct(vct v)
            {
                vct o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                return o;
            }

            fixed4 frag_mult(vct i) : COLOR
            {
                float2 uv = i.texcoord;
                uv -= 0.5;
                uv *= 1.1;
                uv += 0.5;

                fixed4 col = tex2D(_MainTex, uv);
                col.rgb = _SpriteColor.rgb;

                return col;
            }
            
            ENDCG
        } 
    }
}

Source


It helps me if you share this post

Published 2024-01-26 06:00:00

AI Music Generation: MusicGen

Researchers have recently released a new paper and subsequent model, “Simple and Controllable Music Generation”, where they highlight it “is comprised of a single-stage transformer LM together with efficient token interleaving patterns, which eliminates the need for cascading several models”. What this essentially means in practice is the music generation can now be completed in less steps, and is getting more efficient as we make progress on various different types of models.

I expect AI to hit every industry in an increasingly rapid pace as more and more research becomes available and progress starts leapfrogging based on other models. MUSICGEN was trained with about 20K hours of unlicensed music, and the results are impressive.

Here are some interesting generations I thought sounded nice. As more models from massively trained datasets hit the public, we will see more community efforts and models as well just like with art.

Medium Model

I used the less performant medium model (1.5B parameters and approx 3.7 GB) to demonstrate how even on relatively poor hardware you could achieve reasonable results. Here is some lofi generated from the medium model.

Large Model

A step up is the 6.5 GB model. This produce slightly better sounding results.

What is that melody?

There is also a ‘Melody’ model that is a refined 1.5B parameter version.

Limitations

There are a few limitations on this model, namely the lack of vocals.

Limitations:

  • The model is not able to generate realistic vocals.
  • The model has been trained with English descriptions and will not perform as well in other languages.
  • The model does not perform equally well for all music styles and cultures.
  • The model sometimes generates end of songs, collapsing to silence.

However, future models and efforts will remedy these points. It’s only a matter of time before a trained vocal model is released with how fast machine learning advancements are accelerating.


It helps me if you share this post

Published 2023-06-10 18:36:40

AI

AI will help developer efficiency, not replace it.

One of the most significant use cases I’ve found for AI in my development work is its ability to automate repetitive tasks, such as using a bunch of similarly named, grouped variables. I recently was creating a ‘Human’ class, and needed all body parts for variables. That was suggested and picked up almost immediately by Copilot after a couple lines, and the whole class was done in mere seconds vs a few minutes. This adds up and means that I can focus on other creative tasks, such as developing new features, creating new UI ideas or focusing on user feedback. The result is increased productivity and faster software development.

I imagine a future where one can describe the architecture of my Android app in as much detail as possible and then go in and clean up the resulting code manually to a specific vision. Developers will be fast tracked to a more active management role.


It helps me if you share this post

Published 2023-05-17 01:05:41

Bad Bot Traffic

The internet has changed the way we live, work, and communicate. It’s opened up new possibilities for innovation and creativity, allowing individuals and businesses to connect with each other and share their ideas with the world. But with this rise in connectivity and accessibility, there has slowly been another, darker side rising, in the form of bot traffic and spam.

Bots have become a major problem on the internet, with their traffic accounting for over 50% of all web traffic and spam in recent years (this number fluctuates) making up the majority of all email traffic. This rise in bot traffic and spam has had a devastating impact on small websites and businesses, choking their ability to innovate and grow.

Small websites and businesses rely on organic traffic to grow and thrive. However, with so much spam on the internet, it can be difficult for these small players to compete. Bots can scrape content from websites, stealing their intellectual property and driving down their search engine rankings. And, can also be used to flood websites with irrelevant comments and messages, making it difficult for genuine users to engage with the content. Any website or content not protected with some sort of bot protection is immediately bombarded, DDoSed, hacked, or simply probed for information. I have almost 1,000 spam comments sitting in my queue for this website from the last time I checked. spam comments number: 886

This can have a particularly devastating impact on small websites and businesses. As the internet has become more monetized, with companies looking to profit from every aspect of online life, small websites and businesses have found it increasingly difficult to compete. They are often forced to rely on advertising revenue to survive, but with so much bot traffic and spam, they find it harder to generate real revenue, along with the growing popularity of ad blockers which are essentially a must in today’s internet landscape.

Another problem with the rise of bot traffic and spam is that it has made the internet more rigid and lifeless. Where once the internet was a place for hobbyists and enthusiasts to share their ideas and connect with like-minded individuals, it has now become a place where everything is monetized and controlled by large corporations.

In recent years, the rise of increasing malicious traffic on the internet has led many businesses to turn to big players like Cloudflare for protection. While these services can provide effective protection against bots and other malicious activity, they can also come at a cost for smaller businesses and startups.

One of the biggest problems with relying on big players for bot protection is that it can stifle innovation. Smaller businesses and startups are often the most innovative, as they are not burdened by the same bureaucracy and red tape as larger companies.

This is because these big players often provide a one-size-fits-all solution that may not be suitable for smaller businesses with different needs and requirements. Smaller businesses may be forced to adapt their operations and workflows to fit the requirements of these big players, rather than being able to innovate and develop their own unique solutions. Less competition, less innovation, and leads to things like Cloudflare taking down the entire internet when it has problems.

Another problem with relying on big players for bot protection is that it can be expensive. These services can come at a significant cost, which can be prohibitive for smaller businesses and startups that may not have the same financial resources as larger companies. This can create a barrier to entry for these smaller players and may limit their ability to compete with larger companies. Bots drive these costs up significantly, and basically make a gated entry for participation.

The rise of bot traffic and spam on the internet is a major problem that is choking innovation and creativity, overall making it difficult for small websites and businesses to compete. The web, nowadays, has become a strictly regulated and experiment discouraged place. Accounts are needed for every service. Every file could be a virus, and it’s often recommended not to click on ANY strange link or file, or venture off the beaten path. There’s no adventure, no random communities and sites you can discover. The internet, at its roots, is a place created for innovation and creativity. In recent years, it looks like just another tool for corporate profit.


It helps me if you share this post

Published 2023-03-18 21:00:00

Help! I’ve been hacked! What do I do?! My PC Has a Virus or is Infected Recovery Guide

This is a guide for virus removal for Windows PCs. If you have a computer/computers that you believe have a virus or have been hacked, here are the steps you must take to protect yourself.

Isolate from the internet

This is the most important step. A lot of functionality is limited if they don’t have a connection.

Make sure the device you believe has been compromised is disconnected from all forms of connectivity. Bluetooth should be off, airplane mode should be on, Ethernet should be unplugged. WiFi should be turned off, and device should be powered down until ready to perform other necessary recovery steps. This will prevent any malware from getting worse, ransomware from progressing, or hackers from sending remote instructions to your computer.

Additionally, immediately boot your computer into Safe Mode (as fast as possible), to prevent malware processes like ransomware from progressing further.

Booting into safe mode (with networking)

Safe Mode is a diagnostic operating mode, used mainly to troubleshoot problems affecting the normal operation of Windows. Such problems range from conflicting drivers to viruses preventing Windows from starting normally. In Safe Mode, only a few applications work and Windows loads just the basic drivers and a minimum of operating system components. This is why most viruses are inactive when using Windows in Safe Mode, and they can be easily removed.

bitdefender.com

From Settings app

  1. Press the Windows logo key windows key + I on your keyboard to open Settings. If that doesn’t work, click the Start windows key button in the lower-left corner of your screen, then select Settings Settings icon.
  2. Select Update & security Update and security icon, then click on Recovery Recovery icon.
  3. Under Advanced startup, select Restart now.
  4. After your PC restarts to the Choose an option screen, go to Troubleshoot > Advanced options > Startup Settings > Restart.
  5. After your PC restarts, you’ll see a list of options. Press 4 or F4 to start your PC in Safe Mode. Or if you’ll need to use the Internet, select 5 or F5 for Safe Mode with Networking.

From sign in screen

1. Restart your PC. When you get to the Windows sign-in (login) screen, hold the Shift key down while you click the Power  icon in the lower-right corner of the screen then select Restart.
2. After your PC restarts to the Choose an option screen, go to Troubleshoot > Advanced options > Startup Settings > Restart.
3. After your PC restarts, you’ll see a list of options. Press 4 or F4 to start your PC in Safe Mode. Or if you’ll need to use the Internet, select 5 or F5 for Safe Mode with Networking.

From system configuration

1. Launch System Configuration in Windows by simultaneously pressing the Windows windows key + keys on your keyboard. Then write msconfig in the text field and press OK.
2. Switch to Boot tab and, in the Boot options section, select the Safe Boot with Network. Then click OK.


If you have an Ethernet cable, plug the computer in directly.

NOTE: After you finished your work in Safe Mode, please open System Configuration again (step 1) and uncheck the Safe Boot option (step 2). Click OK and restart your machine. Your computer will now boot normally.
safe mode checkbox system configuration

3. Windows will tell you that you need to reboot your computer in order for the new setting to take effect. After the reboot, your computer will automatically boot into Safe Mode.

IMPORTANT: You may not have internet because of drivers and Safe Mode

Safe Mode doesn’t load most third party drivers as a precaution. This could lead to the scenario where you can’t access the internet. In this instance, you can use another computer to download the .exe setup file and transfer it with a USB drive. You could even use your phone to download and transfer from your phone with a hard wire.

Use Virus removal tools

AFTER YOU HAVE REBOOTED INTO SAFE MODE I recommend:

  1. Download Malwarebytes FREE, install and run
    (they will push you to buy the premium version, it is unneeded for our usage)
  2. Download AdwCleaner, install and run
  3. Download Sophos on demand Scan & Clean. If you want a faster download I’ve mirrored it, but this may be an out of date (3/9/2022) version. This is a ‘second opinion’ scanner that should be run after Malwarebytes.
32 BIT64 BIT
DOWNLOADDOWNLOAD

If you prefer, you can use your own antivirus removal tools.


If you are sure the virus is removed off the device, you can start recovery steps

After removing all traces of Malware

Okay, you’ve restarted your machine. You’ve run Malwarebytes. You’ve run Adwcleaner. You’ve turned off safe mode and now you’re back on the desktop. What now?

Run another virus scan

Seriously, you want to be 100% sure your device is at ground 0 again, especially after a breach. It’s better to be safe than sorry. Now that your device is at a “normal” state, it’s best to be sure some sneaky process isn’t running in the background again somehow.

Change your passwords

Depending on the type of virus, it may be prudent to update the passwords you use for online sites that are important to you. Especially any financial accounts or important email passwords. Trojans frequently exfiltrate passwords as one of the first actions taken upon an infected system.

Check your files

Double check that none of your important files were affected. If they were, this is a great reminder to do a backup! Or at least backup the files that are important to you.

Check antivirus settings

Make sure everything is functioning again and there aren’t any settings turned off from the attack.

Monitor site logins

Watch for site logins (via email or sms) over the next few weeks. If you’ve changed your passwords this shouldn’t be an issue but you can never be too careful.


It helps me if you share this post

Published 2022-12-10 07:00:00

Press SHIFT to disable caps lock

When typing, it’s always disconcerting to realize THAT CAPS LOCK IS ON. Caps Lock is useful (sometimes), but more often than not I find myself accidentally engaging it. However, you can change things around in your preferred OS (this guide is for Windows) to allow disabling Caps Lock with Shift. This simple setting changes things for the better, and makes more logical sense.

We’ve Been Doing It Wrong

The logical argument for disabling Caps Lock with Shift boils down to states, and being aware of the key’s current state with the least amount of information possible.

If Caps Lock is a toggle, it’s possible to accidentally hit the key an unknown number of times, or lose track of whether it’s on or off. In order to discover the ‘state’ of the key, you must begin typing. The other way to discover the ‘state’ would be to glance down at your keys, or have some other sort of ‘indicator’ like a keyboard implements visually or graphically. Both of these are wasted efforts and time.

When typing, you shouldn’t look at the keys as much as possible. The cleaner way to handle our problem then is to make Shift disable Caps Lock. When you start typing your sentence, if caps lock is on, it’s naturally disabled. It works naturally with how you type and I no longer encountered any errors with Caps Lock at all upon integrating this. When you need to use it, turn it on. Then, go back to typing as before. It’s no longer a separate mechanism to keep track of, but integrated into the typing experience and bows out quickly after usage without any extra key press. As an added bonus, you don’t have to wonder if Caps Lock is ON either. You simply click it, and type. If it was on, no effect!

To learn how to enable this glorious setting, just read on. Or, if you’re using Linux, this will get that Google search (or DuckDuckGo) started for you. 🙂

Windows 10

  1. Visit Settings > Typing > Advanced keyboard settings
  2. Then find Input language hot keys
  3. From there you will see the very last image’s menu

Windows 11

  1. Navigate to Time and Language > Typing > Advanced keyboard settings
  2. Find Input language hot keys
  3. You will see the very last image’s menu

You will then want to change this option:

Now I can’t go back, and I never wonder or think about caps lock accidentally being on. Been using this as default for around five years now. It surprises me this isn’t the de facto setting.


It helps me if you share this post

Published 2022-08-19 07:05:33

Carbon Coding Language: Google’s Experimental Successor to C++

On July 19th 2022, Google introduced Carbon Language. What exactly is Carbon, and what does it aim to achieve? Note that the Carbon coding language is experimental.

To understand Carbon, we first need to take a look at the language it’s attempting to augment. That is, C++. It remains the dominant programming language for performance critical software, and has been a stable foundation for massive codebases. However, improving C++ is extremely difficult. This is due to a few reasons:

  • Decades of technical debt
  • Prioritizing backwards compatibility over new features
  • C++, ideally, is about standardization rather than design

Carbon, as Google puts it, is okay with “exploring significant backwards incompatible changes”. This has pros for those wanting to work with a language developing with the mindset of “move fast and break things”.

Carbon promises a few things in their readme:

Carbon is fundamentally a successor language approach, rather than an attempt to incrementally evolve C++. It is designed around interoperability with C++ as well as large-scale adoption and migration for existing C++ codebases and developers. A successor language for C++ requires:

  • Performance matching C++, an essential property for our developers.
  • Seamless, bidirectional interoperability with C++, such that a library anywhere in an existing C++ stack can adopt Carbon without porting the rest.
  • A gentle learning curve with reasonable familiarity for C++ developers.
  • Comparable expressivity and support for existing software’s design and architecture.
  • Scalable migration, with some level of source-to-source translation for idiomatic C++ code.

Google wants Carbon to fill an analogous role for C++ in the future, much like TypeScript or Kotlin does for their respective languages.

JavaScript → TypeScript
Java → Kotlin
C++ → Carbon?

Talk is cheap, show me the code

Okay, so what does Carbon look like then?

First, let’s see how to calculate the area of a circle in C++.

// C++ Code
#include <math.h>
#include <iostream>
#include <span>
#include <vector>

struct Circle {
  float r;
};

void PrintTotalArea(std::span<Circle> circles){
  float area = 0;
  for (const Circle& c : circles) {
    area += M_PI * c.r * c.r;
  }
}

auto main(int argc, char** argv) -> {
  std::vector<Circle> circles = {{1.0}, {2.0}};
  // Converts 'vector' to 'span' implicitly
  PrintTotalArea(circles);
  return 0;
}
C++ coding example

Compared to Carbon:

// Carbon Code
package Geometry api;
import Math;

class Circle {
  var r: f32;
};

fn PrintTotalArea(circles: Slice(Circle)) {
  var area: f32 = 0;
  for (c: Circle in circles) {
    area += Math.Pi * c.r * c.r;
  }
}

fn Main() -> i32 {
  // Array like vector
  var circles: Array(Circle) = ({.r = 1.0}, {.r = 2.0});
  
  // Array to slice implicitly 
  PrintTotalArea(circles);
  return 0;
}
Carbon coding example

My initial thoughts are that the syntax looks mixed between C#, JavaScript, and C++. Prepending “var” before each variable seems redundant. Why not a type name followed by a declaration? One might argue that it leads to easy variable identification without memorization of variable types but that makes little sense as you put the type anyway. The way variables are initialized with “:” instead of =, reminds me of Javascript. Not sure if that’s a good thing, it looks less like C++ than I expected. Oddly, they chose “import” for the system packages it seems which is also shared with Python. I do like the shortening of function to fn. You could argue shorthand is the point because it’s cleaner and smaller, but again why is it defined as a function and then an ‘i32’? Seems redundant. unless they decided fn FunctionName() -> i32 is shorter than int FunctionName(). It could be their goal is simply to separate the syntax from other known languages enough to recognize at a glance. Maybe I’m missing something.

One neat feature they’ve shown is the interoperability between Carbon and C++. You can call C++ from Carbon and vice versa. You can rewrite or replace as little or as much of your libraries as you want without fear of breaking anything. Well, at least without breaking anything more than normal when dealing with C++.

// C++ code used in both Carbon and C++;
struct Circle ( float r; ); 
// Carbon exposing a function for C++:
package Geometry api; 
import Cpp library "circle.h";
import Math; 
fn PrintTotalArea(circles: Slice(Cpp.Circ/e)) {
    var area: f32 = 0;
    for (c: Cpp.Circle in circles) { 
        area += Math.Pi * c.r * c.r;
    } 
    Print("Total area: {0}", area); 
}
// C++ calling Carbon:
#include <vector>
auto main(int argc, char** argv) -> int { 
    std::vector<Circle> circles = {{1.0), (2.8)}}; 
    Geometry::PrintTotalArea(circles);
    return 0; 
}
C++ code used in both Carbon and C++

And better memory safety is also promised

Safety, and especially memory safety, remains a key challenge for C++ and something a successor language needs to address. Our initial priority and focus is on immediately addressing important, low-hanging fruit in the safety space:

  • Tracking uninitialized states better, increased enforcement of initialization, and systematically providing hardening against initialization bugs when desired.
  • Designing fundamental APIs and idioms to support dynamic bounds checks in debug and hardened builds.
  • Having a default debug build mode that is both cheaper and more comprehensive than existing C++ build modes even when combined with Address Sanitizer.

Time will tell if the language develops into a developer favorite or fades into obscurity like Dlang. What, you haven’t heard of D?


It helps me if you share this post

Published 2022-08-03 00:19:33